*_operand.rs: Improve clarity of expectation-failure errors
This commit is contained in:
parent
8ea41b46f8
commit
7901c24d80
@ -68,11 +68,11 @@ impl Parsable for PrimaryOperand {
|
|||||||
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: crate::TokenStream<'text> {
|
where T: crate::TokenStream<'text> {
|
||||||
use PrimaryOperand::*;
|
use PrimaryOperand::*;
|
||||||
// Try parsing as Register Direct
|
// Try parsing as Register (Direct)
|
||||||
if let Some(r) = Register::try_parse(p, stream)? {
|
if let Some(r) = Register::try_parse(p, stream)? {
|
||||||
return Ok(Self::Direct(r));
|
return Ok(Self::Direct(r));
|
||||||
}
|
}
|
||||||
// Try parsing as Number Indexed
|
// Try parsing as Number (Indexed)
|
||||||
if let Some(idx) = Number::try_parse(p, stream)? {
|
if let Some(idx) = Number::try_parse(p, stream)? {
|
||||||
stream.expect(Type::LParen)?;
|
stream.expect(Type::LParen)?;
|
||||||
let reg = Register::parse(p, stream)?;
|
let reg = Register::parse(p, stream)?;
|
||||||
@ -80,10 +80,14 @@ impl Parsable for PrimaryOperand {
|
|||||||
return Ok(Self::Indexed(reg, idx));
|
return Ok(Self::Indexed(reg, idx));
|
||||||
}
|
}
|
||||||
// Or directly match any of the valid prefix markers
|
// Or directly match any of the valid prefix markers
|
||||||
let token = stream.expect_any_of([Type::Indirect, Type::Absolute, Type::Immediate])?;
|
// Type::Register and Type::Number are included here to make error messages clearer.
|
||||||
|
// their inclusion will cause a negligible slowdown when the next token is not a prefix marker
|
||||||
|
// (a failure condition)
|
||||||
|
let token =
|
||||||
|
stream.expect_any_of([Type::Indirect, Type::Absolute, Type::Immediate, Type::Register, Type::Number])?;
|
||||||
Ok(match token.variant() {
|
Ok(match token.variant() {
|
||||||
Type::Indirect => {
|
Type::Indirect => {
|
||||||
let reg = stream.expect(Type::Register)?.parse()?;
|
let reg = Register::parse(p, stream)?;
|
||||||
match stream.expect(Type::Plus) {
|
match stream.expect(Type::Plus) {
|
||||||
Ok(_) => PostInc(reg),
|
Ok(_) => PostInc(reg),
|
||||||
Err(_) => Indirect(reg),
|
Err(_) => Indirect(reg),
|
||||||
@ -93,7 +97,7 @@ impl Parsable for PrimaryOperand {
|
|||||||
Type::Immediate => {
|
Type::Immediate => {
|
||||||
let number = Number::parse(p, stream)?;
|
let number = Number::parse(p, stream)?;
|
||||||
match number.into() {
|
match number.into() {
|
||||||
// There are two representations for the all-ones constant, since Number preserves absolute
|
// There are two representations for the all-ones constant, since Number preserves
|
||||||
// signedness.
|
// signedness.
|
||||||
-1 | 0xffff => MinusOne,
|
-1 | 0xffff => MinusOne,
|
||||||
0 => Zero,
|
0 => Zero,
|
||||||
|
@ -55,28 +55,28 @@ impl Parsable for SecondaryOperand {
|
|||||||
where T: crate::TokenStream<'text> {
|
where T: crate::TokenStream<'text> {
|
||||||
use SecondaryOperand::*;
|
use SecondaryOperand::*;
|
||||||
stream.allow(Type::Separator);
|
stream.allow(Type::Separator);
|
||||||
// Try parsing as Register Direct
|
// Try parsing as Register (Direct)
|
||||||
if let Some(r) = Register::try_parse(p, stream)? {
|
if let Some(r) = Register::try_parse(p, stream)? {
|
||||||
return Ok(Self::Direct(r));
|
return Ok(Self::Direct(r));
|
||||||
}
|
}
|
||||||
// Try parsing as Number Indexed
|
// Try parsing as Number (Indexed)
|
||||||
if let Some(idx) = Number::try_parse(p, stream)? {
|
if let Some(idx) = Number::try_parse(p, stream)? {
|
||||||
stream.expect(Type::LParen)?;
|
stream.expect(Type::LParen)?;
|
||||||
let reg = Register::parse(p, stream)?;
|
let reg = Register::parse(p, stream)?;
|
||||||
stream.expect(Type::RParen)?;
|
stream.expect(Type::RParen)?;
|
||||||
return Ok(Self::Indexed(reg, idx));
|
return Ok(Self::Indexed(reg, idx));
|
||||||
}
|
}
|
||||||
let token = stream.expect_any_of([Type::Absolute, Type::Immediate])?;
|
// Type::Register and Type::Number are included here to make error messages clearer.
|
||||||
|
// their inclusion will cause a negligible slowdown when the next token is not a prefix marker
|
||||||
|
// (a failure condition) but should not match a token
|
||||||
|
let token = stream.expect_any_of([Type::Absolute, Type::Immediate, Type::Register, Type::Number])?;
|
||||||
Ok(match token.variant() {
|
Ok(match token.variant() {
|
||||||
Type::Absolute => Absolute(Number::parse(p, stream)?),
|
Type::Absolute => Absolute(Number::parse(p, stream)?),
|
||||||
Type::Immediate => {
|
Type::Immediate => match Number::parse(p, stream)?.into() {
|
||||||
let number = Number::parse(p, stream)?;
|
0 => Zero,
|
||||||
match number.into() {
|
1 => One,
|
||||||
0 => Zero,
|
n => Err(Error::FatSecondaryImmediate(n as isize).context(stream.context()))?,
|
||||||
1 => One,
|
},
|
||||||
n => Err(Error::FatSecondaryImmediate(n as isize).context(stream.context()))?,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => unreachable!("Token {token:?} passed expectation but failed match!"),
|
_ => unreachable!("Token {token:?} passed expectation but failed match!"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user