From 938b7d2af21bfc3a87273f60f3357ffb193cb6fa Mon Sep 17 00:00:00 2001 From: John Breaux Date: Wed, 23 Aug 2023 00:23:46 -0500 Subject: [PATCH] opcode.rs: Use `Parsable` trait for opcode parsing --- src/parser/instruction.rs | 3 +-- src/parser/instruction/opcode.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/parser/instruction.rs b/src/parser/instruction.rs index 57e2e9b..a19bcf1 100644 --- a/src/parser/instruction.rs +++ b/src/parser/instruction.rs @@ -34,8 +34,7 @@ impl Parsable for Instruction { T: crate::TokenStream<'text>, { // parse an opcode - let insn = stream.expect(Type::Insn)?; - let opcode: Opcode = insn.parse()?; + let opcode: Opcode = Opcode::parse(p, stream)?; // resolve the opcode to a final opcode and an encoding let (opcode, encoding) = opcode.resolve(); // parse the encoding diff --git a/src/parser/instruction/opcode.rs b/src/parser/instruction/opcode.rs index 1384cc8..bd38c5f 100644 --- a/src/parser/instruction/opcode.rs +++ b/src/parser/instruction/opcode.rs @@ -124,11 +124,18 @@ impl Opcode { } } +impl Parsable for Opcode { + fn parse<'text, T>(_: &Parser, stream: &mut T) -> Result + where T: TokenStream<'text> { + stream.expect(Type::Insn)?.parse().map_err(|e: Error| e.context(stream.context())) + } +} + impl FromStr for Opcode { type Err = Error; fn from_str(s: &str) -> Result { use Opcode::*; - //TODO: Reduce allocations here + //TODO: Reduce allocations here? let s = s.to_ascii_lowercase(); Ok(match s.as_str() { "rrc" => Rrc,