parser.rs: Give priority to instruction parsing

This commit is contained in:
John 2023-08-23 00:15:47 -05:00
parent b5fd49b0b4
commit 8c2f53f950

View File

@ -47,27 +47,26 @@ pub(crate) mod line {
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Line { pub enum Line {
Empty, Empty,
Label(Label), // TODO: Label resolution
Insn(Instruction), Insn(Instruction),
Directive(Directive),
Comment(Comment), Comment(Comment),
Directive(Directive),
Label(Label), // TODO: Label resolution
EndOfFile, // Expected end of file EndOfFile, // Expected end of file
} }
impl Parsable for Line { impl Parsable for Line {
fn parse<'text, T>(p: &Parser, stream: &mut T) -> Result<Self, Error> fn parse<'text, T>(p: &Parser, stream: &mut T) -> Result<Self, Error>
where T: TokenStream<'text> { where T: TokenStream<'text> {
if let Ok(token) = stream.peek_expect_any_of([Type::Comment, Type::Directive, Type::Insn, Type::Identifier]) if let Ok(token) = stream.peek_expect_any_of([Type::Insn, Type::Comment, Type::Directive, Type::Identifier])
{ {
return Ok(match token.variant() { return Ok(match token.variant() {
Type::Insn => Self::Insn(Instruction::parse(p, stream)?),
Type::Comment => Self::Comment(Comment::parse(p, stream)?), Type::Comment => Self::Comment(Comment::parse(p, stream)?),
Type::Directive => Self::Directive(Directive::parse(p, stream)?), Type::Directive => Self::Directive(Directive::parse(p, stream)?),
Type::Identifier => Self::Label(Label::parse(p, stream)?), Type::Identifier => Self::Label(Label::parse(p, stream)?),
Type::Insn => Self::Insn(Instruction::parse(p, stream)?),
_ => unreachable!(), _ => unreachable!(),
}); });
} }
// TODO: preserve comments
let token = stream.expect_any_of([Type::EndOfFile])?; let token = stream.expect_any_of([Type::EndOfFile])?;
Ok(match token.variant() { Ok(match token.variant() {
Type::EndOfFile => Self::EndOfFile, Type::EndOfFile => Self::EndOfFile,