Added lifetime to Parsable trait.

This commit is contained in:
Filipe Rodrigues 2021-04-26 16:05:07 +01:00
parent f54325bafe
commit 9b79f3b5c9
18 changed files with 36 additions and 36 deletions

View File

@ -107,8 +107,8 @@ impl TryEncode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
#[rustfmt::skip]
let parsers: &[&dyn Fn() -> Result<Self, ParseError>] = &[
&|| alu ::Inst::parse(mnemonic, args, ctx).map(Self::Alu ),

View File

@ -39,8 +39,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
match imm::Inst::parse(mnemonic, args, ctx) {
Ok(inst) => Ok(Self::Imm(inst)),
Err(ParseError::UnknownMnemonic) => reg::Inst::parse(mnemonic, args, ctx).map(Self::Reg),

View File

@ -118,8 +118,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
#[rustfmt::skip]
let to_kind = match mnemonic {
"addi" => |value: i64| value.try_into().map(Kind::Add ),

View File

@ -132,8 +132,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
#[rustfmt::skip]
let kind = match mnemonic {
"add" => Kind::Add ,

View File

@ -168,8 +168,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
let inst = match mnemonic {
"cop0" | "cop1" | "cop2" | "cop3" => {
let n = mnemonic[3..].parse().expect("Unable to parse 0..=3");

View File

@ -120,8 +120,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
// Note: Literals are absolute, not relative
// Calculates the offset between a position and the current one

View File

@ -39,8 +39,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
match imm::Inst::parse(mnemonic, args, ctx) {
Ok(inst) => Ok(Self::Imm(inst)),
Err(ParseError::UnknownMnemonic) => reg::Inst::parse(mnemonic, args, ctx).map(Self::Reg),

View File

@ -88,8 +88,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
let (pos, kind) = match mnemonic {
"j" => match args {
[arg] => (ctx.arg_pos(arg)?, Kind::Jump),

View File

@ -73,8 +73,8 @@ impl Encode for Inst {
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
let (target, kind) = match mnemonic {
"jr" => match *args {
[LineArg::Register(target)] => (target, Kind::Jump),

View File

@ -114,8 +114,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
let kind = match mnemonic {
"lb" => Kind::Byte,
"lh" => Kind::HalfWord,

View File

@ -46,8 +46,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
if mnemonic != "lui" {
return Err(ParseError::UnknownMnemonic);
}

View File

@ -154,8 +154,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
let inst = match mnemonic {
"mflo" | "mfhi" | "mtlo" | "mthi" => {
let reg = match *args {

View File

@ -48,8 +48,8 @@ impl TryEncode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
match imm::Inst::parse(mnemonic, args, ctx) {
Ok(inst) => Ok(Self::Imm(inst)),
Err(ParseError::UnknownMnemonic) => reg::Inst::parse(mnemonic, args, ctx).map(Self::Reg),

View File

@ -105,8 +105,8 @@ impl TryEncode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
let kind = match mnemonic {
"sll" => Kind::LeftLogical,
"srl" => Kind::RightLogical,

View File

@ -91,8 +91,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
let kind = match mnemonic {
"sllv" => Kind::LeftLogical,
"srlv" => Kind::RightLogical,

View File

@ -104,8 +104,8 @@ impl Encode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
let kind = match mnemonic {
"sb" => Kind::Byte,
"sh" => Kind::HalfWord,

View File

@ -86,8 +86,8 @@ impl TryEncode for Inst {
}
}
impl Parsable for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
impl<'a> Parsable<'a> for Inst {
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
let kind = match mnemonic {
"sys" => Kind::Sys,
"break" => Kind::Break,

View File

@ -13,9 +13,9 @@ use crate::Pos;
use std::convert::TryInto;
/// Instruction parsing
pub trait Parsable: Sized {
pub trait Parsable<'a>: Sized + 'a {
/// Parses this instruction
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError>;
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError>;
}
/// Parsing context