Skip to content

Commit

Permalink
template parameters are Vec instead of ExprList
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcoph committed Oct 7, 2023
1 parent b14cc83 commit 70ce6f4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 51 deletions.
44 changes: 28 additions & 16 deletions hexparser/src/m_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub enum Statement {
Struct {
name: Spanned<String>,
body: Box<Spanned<Expr>>,
template_parameters: Option<Box<Spanned<Expr>>>
template_parameters: Vec<Spanned<Expr>>
},
Namespace {
name: Box<Spanned<Expr>>,
Expand All @@ -103,14 +103,14 @@ pub enum Statement {
},
Using {
new_name: Spanned<String>,
template_parameters: Option<Box<Spanned<Expr>>>,
template_parameters: Vec<Spanned<Expr>>,
old_name: Spanned<HexTypeDef>
},
Error,
Union {
name: Spanned<String>,
body: Box<Spanned<Expr>>,
template_parameters: Option<Box<Spanned<Expr>>>
template_parameters: Vec<Spanned<Expr>>
},
ArrayDefinition {
value_type: Spanned<HexTypeDef>,
Expand Down Expand Up @@ -293,7 +293,7 @@ fn function_call_expr<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanne
expression_recovery(map(
then(
namespace_resolution,
parameters
parameters
),
|(func_name, arguments)| {
let args_span = if arguments.len() > 0 {
Expand Down Expand Up @@ -321,7 +321,7 @@ fn function_call_statement<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, S
statement_recovery(map(
then(
namespace_resolution,
parameters
parameters
),
|(func_name, arguments)| {
let args_span = if arguments.len() > 0 {
Expand Down Expand Up @@ -741,7 +741,7 @@ fn non_dolar_assignment_statement<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a
operator: assignment,
roperand: Box::new(roperand),
};

(expr, span)
}
))(input)
Expand Down Expand Up @@ -784,7 +784,7 @@ fn assignment_expr<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<S
operator: assignment,
roperand: Box::new(roperand),
};

(expr, span)
}
))(input)
Expand Down Expand Up @@ -827,7 +827,7 @@ fn assignment_statement<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Span
operator: assignment,
roperand: Box::new(roperand),
};

(expr, span)
}
))(input)
Expand Down Expand Up @@ -1164,7 +1164,7 @@ pub(crate) fn member<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned
)))(input)
}

pub(crate) fn template_list<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<Expr>> {
pub(crate) fn template_list<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Vec<Spanned<Expr>>> {
map_with_span(
delimited(
just(Token::Op("<")),
Expand All @@ -1179,7 +1179,7 @@ pub(crate) fn template_list<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b,
),
|list, span| {
let list = list.into_iter().map(|(_, a)| a).collect(); // ignore "auto" for now // TODO: don't ignore it
(Expr::ExprList { list }, span)
list
}
)(input)
}
Expand All @@ -1206,11 +1206,15 @@ pub(crate) fn parse_struct<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, S
))
),
|(name, template_parameters, inheritance, (body, body_span)), span| { // TODO: Take into account the inheritance.
let template_parameters = match template_parameters {
Some(t) => t,
None => Vec::new()
};
(
Statement::Struct {
name,
body: Box::new((Expr::StatementList { list: body }, body_span)),
template_parameters: template_parameters.boxed()
template_parameters
},
span
)
Expand All @@ -1233,11 +1237,15 @@ fn parse_union<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<State
))
),
|(name, template_parameters, (body, body_span)), span| {
let template_parameters = match template_parameters {
Some(t) => t,
None => Vec::new()
};
(
Statement::Union {
name,
body: Box::new((Expr::StatementList { list: body }, body_span)),
template_parameters: template_parameters.boxed(),
template_parameters,
},
span
)
Expand Down Expand Up @@ -1439,7 +1447,7 @@ fn parse_bitfield<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<St
0 => span.clone(),
_ => body.get(0).unwrap().1.start..body.get(body.len()-1).unwrap().1.end
};

(
Expr::StatementList { list: body },
span
Expand Down Expand Up @@ -1605,7 +1613,7 @@ fn placement<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<Stateme
},
None => (Expr::Value { val: Value::Null }, fake_span.clone()),
};

((name, fake_span), None, body)
}
),
Expand Down Expand Up @@ -1699,10 +1707,14 @@ fn using<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<Statement>>
Some(old_name) => old_name,
None => (HexTypeDef{ endianness: Endianness::Unkown, name: (HexType::Null, span.clone()) }, span.clone()),
};
let template_parameters = match template_parameters {
Some(t) => t,
None => Vec::new()
};
(
Statement::Using {
new_name,
template_parameters: template_parameters.boxed(),
template_parameters,
old_name
},
span
Expand Down Expand Up @@ -1855,7 +1867,7 @@ fn parser<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<Expr>> {
let input = recover_err(&e);
let (rest, input) = input.take_split(1);
let span = input.span();
let state = input.tokens[0].extra;
let state = input.tokens[0].extra;
state.0.report_error(RecoveredError(span.clone(), "Unexpected token".to_string()));
Ok((rest, (Statement::Error, span)))
},
Expand Down
52 changes: 26 additions & 26 deletions hexparser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ fn stmnt_comparer(stmnt1: &Statement, stmnt2: &Statement) -> Result<(), CompErr>
false => {println!("Different bitfield names");Err(CompErr)},
},
(
Statement::Using { new_name: (n1, _), template_parameters: None, old_name: (
Statement::Using { new_name: (n1, _), template_parameters: t_p1, old_name: (
HexTypeDef { endianness: e1, name: (hn1, _) }, _
) },
Statement::Using { new_name: (n2, _), template_parameters: None, old_name: (
Statement::Using { new_name: (n2, _), template_parameters: t_p2, old_name: (
HexTypeDef { endianness: e2, name: (hn2, _) }, _
) }
) => match n1 == n2 {
Expand Down Expand Up @@ -227,8 +227,8 @@ fn stmnt_comparer(stmnt1: &Statement, stmnt2: &Statement) -> Result<(), CompErr>
},
Err(e) => Err(e),
},
(Statement::Union { name: (n1, _), body: b1, template_parameters: None },
Statement::Union { name: (n2, _), body: b2, template_parameters: None }
(Statement::Union { name: (n1, _), body: b1, template_parameters: t_p1 },
Statement::Union { name: (n2, _), body: b2, template_parameters: t_p2 }
) => match n1 == n2 {
true => match expr_comparer(&b1.0, &b2.0) {
Ok(_) => Ok(()),
Expand Down Expand Up @@ -276,8 +276,8 @@ fn stmnt_comparer(stmnt1: &Statement, stmnt2: &Statement) -> Result<(), CompErr>
Ok(_) => Ok(()),
Err(e) => Err(e),
},
(Statement::Struct { name: (n1, _), body: b1, template_parameters: None },
Statement::Struct { name: (n2, _), body: b2, template_parameters: None }
(Statement::Struct { name: (n1, _), body: b1, template_parameters: t_p1 },
Statement::Struct { name: (n2, _), body: b2, template_parameters: t_p2 }
) => match n1 == n2 {
true => match expr_comparer(&b1.0, &b2.0) {
Ok(_) => Ok(()),
Expand Down Expand Up @@ -583,7 +583,7 @@ fn test_pattern_arrays() {
}, 0..0)
]
}),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Definition(Definition {
value_type: (HexTypeDef {
Expand Down Expand Up @@ -663,7 +663,7 @@ fn test_pattern_attributes() {
body: bnull!()
}), 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Struct {
name: (String::from("SealedTest"), 0..0),
Expand All @@ -677,7 +677,7 @@ fn test_pattern_attributes() {
body: bnull!()
}), 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Struct {
name: (String::from("HiddenTest"), 0..0),
Expand All @@ -691,7 +691,7 @@ fn test_pattern_attributes() {
body: bnull!()
}), 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Struct {
name: (String::from("ColorTest"), 0..0),
Expand All @@ -706,7 +706,7 @@ fn test_pattern_attributes() {
body: bnull!()
}, 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Struct {
name: (String::from("NoUniqueAddressTest"), 0..0),
Expand All @@ -728,7 +728,7 @@ fn test_pattern_attributes() {
body: bnull!()
}), 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Func {
name: (String::from("format_test"), 0..0),
Expand Down Expand Up @@ -994,7 +994,7 @@ fn test_pattern_extra_semicolon() {
body: bnull!()
}), 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Struct {
name: (String::from("Test2"), 0..0),
Expand All @@ -1016,7 +1016,7 @@ fn test_pattern_extra_semicolon() {
body: bnull!()
}), 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Definition(Definition {
value_type: (HexTypeDef {
Expand Down Expand Up @@ -1083,7 +1083,7 @@ fn test_pattern_namespaces() {
body: bnull!()
}), 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0)
] })
}, 0..0),
Expand All @@ -1100,13 +1100,13 @@ fn test_pattern_namespaces() {
body: bnull!()
}), 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0)
] })
}, 0..0),
(Statement::Using {
new_name: (String::from("ATest"), 0..0),
template_parameters: None,
template_parameters: vec![],
old_name: (HexTypeDef {
endianness: Endianness::Unkown,
name: (HexType::Path(vec![
Expand Down Expand Up @@ -1257,7 +1257,7 @@ fn test_pattern_nested_structs() {
body: bnull!()
}), 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Struct {
name: (String::from("Body"), 0..0),
Expand All @@ -1281,7 +1281,7 @@ fn test_pattern_nested_structs() {
body: bnull!()
}, 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Struct {
name: (String::from("Data"), 0..0),
Expand All @@ -1303,7 +1303,7 @@ fn test_pattern_nested_structs() {
body: bnull!()
}), 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Definition(Definition {
value_type: (HexTypeDef {
Expand Down Expand Up @@ -1363,7 +1363,7 @@ fn test_pattern_padding() {
}, 0..0)
]
}),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Definition(Definition {
value_type: (HexTypeDef {
Expand Down Expand Up @@ -1531,7 +1531,7 @@ fn test_pattern_rvalues() {
body: bnull!()
}, 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Struct {
name: (String::from("B"), 0..0),
Expand All @@ -1547,7 +1547,7 @@ fn test_pattern_rvalues() {
}), 0..0)
]
}),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Struct {
name: (String::from("A"), 0..0),
Expand All @@ -1571,7 +1571,7 @@ fn test_pattern_rvalues() {
}), 0..0)
]
}),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Definition(Definition {
value_type: (HexTypeDef {
Expand Down Expand Up @@ -1629,7 +1629,7 @@ fn test_pattern_structs() {
}, 0..0)
]
}),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Definition(Definition {
value_type: (HexTypeDef {
Expand Down Expand Up @@ -1685,7 +1685,7 @@ fn test_pattern_unions() {
body: bnull!()
}), 0..0)
] }),
template_parameters: None
template_parameters: vec![]
}, 0..0),
(Statement::Definition(Definition {
value_type: (HexTypeDef {
Expand Down
Loading

0 comments on commit 70ce6f4

Please sign in to comment.