Skip to content

Commit

Permalink
Allow expected!() to accept a &'static str expr, not just a literal
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmehall committed Oct 7, 2023
1 parent c870876 commit a85e71b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion peg-macros/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub enum Expr {
MatchStrExpr(Box<SpannedExpr>),
PositionExpr,
QuietExpr(Box<SpannedExpr>),
FailExpr(Literal),
FailExpr(Group),
PrecedenceExpr {
levels: Vec<PrecedenceLevel>,
},
Expand Down
2 changes: 1 addition & 1 deletion peg-macros/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2778,7 +2778,7 @@ pub mod peg {
let __choice_res = {
let __seq_res =
__parse_sp(__input, __state, __err_state, __pos);
match __seq_res { :: peg :: RuleResult :: Matched (__pos , sp) => { match :: peg :: ParseLiteral :: parse_string_literal (__input , __pos , "expected") { :: peg :: RuleResult :: Matched (__pos , __val) => { match :: peg :: ParseLiteral :: parse_string_literal (__input , __pos , "!") { :: peg :: RuleResult :: Matched (__pos , __val) => { match :: peg :: ParseLiteral :: parse_string_literal (__input , __pos , "(") { :: peg :: RuleResult :: Matched (__pos , __val) => { { let __seq_res = __parse_LITERAL (__input , __state , __err_state , __pos) ; match __seq_res { :: peg :: RuleResult :: Matched (__pos , s) => { match :: peg :: ParseLiteral :: parse_string_literal (__input , __pos , ")") { :: peg :: RuleResult :: Matched (__pos , __val) => { :: peg :: RuleResult :: Matched (__pos , (|| { FailExpr (s) . at (sp) }) ()) } :: peg :: RuleResult :: Failed => { __err_state . mark_failure (__pos , "\")\"") ; :: peg :: RuleResult :: Failed } } } :: peg :: RuleResult :: Failed => :: peg :: RuleResult :: Failed , } } } :: peg :: RuleResult :: Failed => { __err_state . mark_failure (__pos , "\"(\"") ; :: peg :: RuleResult :: Failed } } } :: peg :: RuleResult :: Failed => { __err_state . mark_failure (__pos , "\"!\"") ; :: peg :: RuleResult :: Failed } } } :: peg :: RuleResult :: Failed => { __err_state . mark_failure (__pos , "\"expected\"") ; :: peg :: RuleResult :: Failed } } } :: peg :: RuleResult :: Failed => :: peg :: RuleResult :: Failed , }
match __seq_res { :: peg :: RuleResult :: Matched (__pos , sp) => { match :: peg :: ParseLiteral :: parse_string_literal (__input , __pos , "expected") { :: peg :: RuleResult :: Matched (__pos , __val) => { match :: peg :: ParseLiteral :: parse_string_literal (__input , __pos , "!") { :: peg :: RuleResult :: Matched (__pos , __val) => { { let __seq_res = __parse_PAREN_GROUP (__input , __state , __err_state , __pos) ; match __seq_res { :: peg :: RuleResult :: Matched (__pos , s) => { :: peg :: RuleResult :: Matched (__pos , (|| { FailExpr (s) . at (sp) }) ()) } :: peg :: RuleResult :: Failed => :: peg :: RuleResult :: Failed , } } } :: peg :: RuleResult :: Failed => { __err_state . mark_failure (__pos , "\"!\"") ; :: peg :: RuleResult :: Failed } } } :: peg :: RuleResult :: Failed => { __err_state . mark_failure (__pos , "\"expected\"") ; :: peg :: RuleResult :: Failed } } } :: peg :: RuleResult :: Failed => :: peg :: RuleResult :: Failed , }
};
match __choice_res {
::peg::RuleResult::Matched(__pos, __value) => {
Expand Down
2 changes: 1 addition & 1 deletion peg-macros/grammar.rustpeg
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ rule primary() -> SpannedExpr
= sp:sp() "precedence" "!" "{" levels:precedence_level()**"--" "}" { PrecedenceExpr{ levels:levels }.at(sp) }
/ sp:sp() "position" "!" "(" ")" { PositionExpr.at(sp) }
/ sp:sp() "quiet" "!" "{" e:expression() "}" { QuietExpr(Box::new(e)).at(sp) }
/ sp:sp() "expected" "!" "(" s:LITERAL() ")" { FailExpr(s).at(sp) }
/ sp:sp() "expected" "!" s:PAREN_GROUP() { FailExpr(s).at(sp) }
/ &("_" / "__" / "___") sp:sp() name:IDENT() { RuleExpr(name, Vec::new()).at(sp) }
/ sp:sp() name:IDENT() "(" args:(rule_arg() ** ",") ")" { RuleExpr(name, args).at(sp) }
/ sp:sp() l:LITERAL() { LiteralExpr(l).at(sp) }
Expand Down
6 changes: 6 additions & 0 deletions tests/run-pass/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ peg::parser!{ grammar parser() for str {
pub rule q() = (quiet!{
("a" / "b" / "c") ("1" / "2")
} / expected!("letter followed by number"))+

pub rule var(s: &'static str) = expected!(s)
}}

fn main() {
Expand Down Expand Up @@ -59,4 +61,8 @@ aaaabaaaa
let err = parser::q("a1bb").unwrap_err();
assert_eq!(err.location.offset, 2);
assert_eq!(err.expected.to_string(), "one of EOF, letter followed by number");

let err = parser::var("", "asdf").unwrap_err();
assert_eq!(err.expected.to_string(), "asdf");

}

0 comments on commit a85e71b

Please sign in to comment.