opcode.rs: Use Parsable trait for opcode parsing

This commit is contained in:
John 2023-08-23 00:23:46 -05:00
parent 2f867a67ad
commit 938b7d2af2
2 changed files with 9 additions and 3 deletions

View File

@ -34,8 +34,7 @@ impl Parsable for Instruction {
T: crate::TokenStream<'text>, T: crate::TokenStream<'text>,
{ {
// parse an opcode // parse an opcode
let insn = stream.expect(Type::Insn)?; let opcode: Opcode = Opcode::parse(p, stream)?;
let opcode: Opcode = insn.parse()?;
// resolve the opcode to a final opcode and an encoding // resolve the opcode to a final opcode and an encoding
let (opcode, encoding) = opcode.resolve(); let (opcode, encoding) = opcode.resolve();
// parse the encoding // parse the encoding

View File

@ -124,11 +124,18 @@ impl Opcode {
} }
} }
impl Parsable for Opcode {
fn parse<'text, T>(_: &Parser, stream: &mut T) -> Result<Self, Error>
where T: TokenStream<'text> {
stream.expect(Type::Insn)?.parse().map_err(|e: Error| e.context(stream.context()))
}
}
impl FromStr for Opcode { impl FromStr for Opcode {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
use Opcode::*; use Opcode::*;
//TODO: Reduce allocations here //TODO: Reduce allocations here?
let s = s.to_ascii_lowercase(); let s = s.to_ascii_lowercase();
Ok(match s.as_str() { Ok(match s.as_str() {
"rrc" => Rrc, "rrc" => Rrc,