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>,
{
// 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

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 {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use Opcode::*;
//TODO: Reduce allocations here
//TODO: Reduce allocations here?
let s = s.to_ascii_lowercase();
Ok(match s.as_str() {
"rrc" => Rrc,