command_parser: unnest the grammar definition

master
Astro 2019-09-13 01:10:31 +02:00
parent 3fd1b2265d
commit f2dcb8b08d
1 changed files with 28 additions and 39 deletions

View File

@ -84,8 +84,8 @@ impl Command {
$block $block
} }
)* )*
Token::End => Err(Error::UnexpectedEnd), Token::End => return Err(Error::UnexpectedEnd),
_ => Err(Error::UnexpectedToken(lexer.token)) _ => return Err(Error::UnexpectedToken(lexer.token))
} }
}; };
} }
@ -94,7 +94,7 @@ impl Command {
($result: expr) => { ($result: expr) => {
match lexer.token { match lexer.token {
Token::End => Ok($result), Token::End => Ok($result),
_ => Err(Error::UnexpectedToken(lexer.token)), _ => return Err(Error::UnexpectedToken(lexer.token)),
} }
}; };
} }
@ -111,42 +111,31 @@ impl Command {
], ],
End => Ok(Command::Report(ReportMode::Once)), End => Ok(Command::Report(ReportMode::Once)),
], ],
Channel => choice![ Channel => {
let channel = choice![
Number => { Number => {
let channel = CHANNEL_IDS.iter() CHANNEL_IDS.iter()
.position(|id| *id == lexer.slice()); .position(|id| *id == lexer.slice())
match channel { .ok_or(Error::NoSuchChannel)
Some(channel) => { },
]? as u8;
choice![ choice![
Enable => Ok(Command::Channel( Enable =>
channel as u8, Ok(Command::Channel(channel, ChannelCommand::Enable)),
ChannelCommand::Enable Disable =>
)), Ok(Command::Channel(channel, ChannelCommand::Enable)),
Disable => Ok(Command::Channel( Setup => {
channel as u8, let setup = choice![
ChannelCommand::Enable
)),
Setup => choice![
Number => { Number => {
let setup = SETUP_IDS.iter() SETUP_IDS.iter()
.position(|id| *id == lexer.slice()); .position(|id| *id == lexer.slice())
match setup { .ok_or(Error::NoSuchSetup)
Some(setup) => },
end!(Command::Channel( ]? as u8;
channel as u8, end!(Command::Channel(channel, ChannelCommand::Setup(setup)))
ChannelCommand::Setup(setup as u8)
)),
None =>
Err(Error::NoSuchSetup)
}
}, },
],
] ]
}
None => Err(Error::NoSuchChannel)
}
}, },
],
] ]
} }
} }