From f0235832c36d9e9582034b32af30af4ef1276a51 Mon Sep 17 00:00:00 2001 From: Alain Zscheile Date: Mon, 22 May 2023 21:17:30 +0200 Subject: [PATCH] parse 'tag' expressions --- crates/wafl-parser/src/lex.rs | 5 ----- crates/wafl-parser/src/parser.rs | 31 +++++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/crates/wafl-parser/src/lex.rs b/crates/wafl-parser/src/lex.rs index 6b51fea..828758e 100644 --- a/crates/wafl-parser/src/lex.rs +++ b/crates/wafl-parser/src/lex.rs @@ -32,11 +32,6 @@ impl Token<'_> { pub fn is_def_attr(&self) -> bool { matches!(self, Token::Final) } - - #[inline] - pub fn is_cobj_attr(&self) -> bool { - matches!(self, Token::CtrlFlowEdit) - } } #[derive(Clone, PartialEq, Eq)] diff --git a/crates/wafl-parser/src/parser.rs b/crates/wafl-parser/src/parser.rs index 637b54d..0dc698f 100644 --- a/crates/wafl-parser/src/parser.rs +++ b/crates/wafl-parser/src/parser.rs @@ -50,10 +50,7 @@ pub enum Expression { obj: Box, args: BTreeMap, (Location, Expression)>, }, - Tag { - obj: Box, - args: BTreeMap, (Location, Expression)>, - }, + Tag(Path), } #[derive(Clone, Debug)] @@ -79,6 +76,7 @@ pub enum ErrorKind { UnexpectedTrail, InvalidIdentifier, DuplicateIdentifier, + InvalidCombination(&'static str), Unexpected(&'static str), Unknown, } @@ -183,16 +181,33 @@ impl FullIdentifier { impl Expression { fn parse_high(ctx: &mut ParserContext<'_>) -> Result { - let mut cfe = false; - for (_, i) in ctx.pklx.peeking_take_while(|(_, t)| t.is_cobj_attr()) { + let (mut cfe, mut tag) = (false, false); + let preloc = ctx.peek_loc(); + for (_, i) in ctx + .pklx + .peeking_take_while(|(_, t)| matches!(Token::CtrlFlowEdit | Token::Tag)) + { match i { Token::CtrlFlowEdit => cfe = true, + Token::Tag => tag = true, _ => unimplemented!(), } } + if cfe && tag { + return Err(Error { + loc: preloc, + kind: ErrorKind::InvalidCombination( + "you can't specify both cfe and tag at the same time", + ), + }); + } + let obj = match ctx.pklx.peek() { None => Err(ctx.make_eof()), + Some((_, Token::Identifier(ident))) if tag => { + Path::parse_high(ctx).map(Expression::Tag) + } Some((_, Token::OpenBrace)) => { ctx.pklx.next(); let mut codata = Vec::new(); @@ -220,6 +235,10 @@ impl Expression { } }) } + Some(&(loc, _)) if tag => Err(Error { + loc, + kind: ErrorKind::Unexpected("identifier"), + }), Some(&(loc, _)) if cfe => Err(Error { loc, kind: ErrorKind::Unexpected("braced statement set"),