| 1 |
pub grammar peg() for FlatTokenStream { |
| 2 |
|
| 3 |
use crate::ast::*; |
| 4 |
use crate::ast::Expr::*; |
| 5 |
use crate::tokens::FlatTokenStream; |
| 6 |
use proc_macro2::{ TokenStream, Ident, Group, Literal, Delimiter, Span }; |
| 7 |
|
| 8 |
pub rule peg_grammar() -> Grammar |
| 9 |
= doc:rust_doc_comment() visibility:rust_visibility() "grammar" name:IDENT() lifetime_params:rust_lifetime_params()? args:grammar_args() "for" input_type:$(rust_type()) "{" items:item()* "}" |
| 10 |
{ Grammar { doc, visibility, name, lifetime_params, args, input_type, items } } |
| 11 |
|
| 12 |
rule rust_lifetime_params() -> Vec<TokenStream> |
| 13 |
= "<" p:(($(LIFETIME())) ++ ",") ","? ">" { p } |
| 14 |
|
| 15 |
rule grammar_args() -> Vec<(Ident, TokenStream)> |
| 16 |
= "(" args:((i:IDENT() ":" t:$(rust_type()) { (i, t) })**",") ","? ")" { args } |
| 17 |
|
| 18 |
rule peg_rule() -> Rule |
| 19 |
= doc:rust_doc_comment() cache:cacheflag() no_eof:no_eof_flag() visibility:rust_visibility() |
| 20 |
span:sp() "rule" |
| 21 |
header:( |
| 22 |
&("_" / "__" / "___") name:IDENT() ("(" ")")? { (name, None, Vec::new()) } |
| 23 |
/ name:IDENT() ty_params:rust_ty_params()? params:rule_params() { (name, ty_params, params) } |
| 24 |
) |
| 25 |
ret_type:("->" t:$(rust_type()) {t})? |
| 26 |
where_clause:$(rust_where_clause())? |
| 27 |
"=" expr:expression() ";"? |
| 28 |
{ Rule { span, doc, name:header.0, ty_params:header.1, params:header.2, expr, ret_type, where_clause, visibility, no_eof, cache } } |
| 29 |
|
| 30 |
rule cacheflag() -> Option<Cache> = "#" "[" "cache" "]" {Some(Cache::Simple)} / "#" "[" "cache_left_rec" "]" {Some(Cache::Recursive)} / {None} |
| 31 |
|
| 32 |
rule no_eof_flag() -> bool = "#" "[" "no_eof" "]" {true} / {false} |
| 33 |
|
| 34 |
rule rule_param_ty() -> RuleParamTy |
| 35 |
= "rule" "<" r:$(rust_type()) ">" { RuleParamTy::Rule(r) } |
| 36 |
/ t:$(rust_type()) { RuleParamTy::Rust(t) } |
| 37 |
|
| 38 |
rule rule_params() -> Vec<RuleParam> |
| 39 |
= "(" params:(x:(name:IDENT() ":" ty:rule_param_ty() { RuleParam { name, ty} }) ++ "," ","? {x})? ")" { params.unwrap_or_default() } |
| 40 |
|
| 41 |
rule item() -> Item |
| 42 |
= u:rust_use() { Item::Use(u) } |
| 43 |
/ r:peg_rule() { Item::Rule(r) } |
| 44 |
|
| 45 |
rule rust_doc_comment() -> Option<TokenStream> = $(("#" "[" "doc" "=" LITERAL() "]")*)? |
| 46 |
|
| 47 |
rule rust_visibility() -> Option<TokenStream> = $("pub" PAREN_GROUP()?)? |
| 48 |
|
| 49 |
rule rust_use() -> TokenStream |
| 50 |
= v:$("use" rust_path() ( |
| 51 |
"::" "*" |
| 52 |
/ "::" "{" ((IDENT() ("as" IDENT())?) ++ "," ","?) "}" |
| 53 |
/ ("as" IDENT())? |
| 54 |
) ";") { v.to_owned() } |
| 55 |
|
| 56 |
rule rust_path() |
| 57 |
= ("crate" "::")? IDENT() ++ "::" |
| 58 |
|
| 59 |
rule rust_type() |
| 60 |
= BRACKET_GROUP() |
| 61 |
/ "&" LIFETIME()? "mut"? rust_type() |
| 62 |
/ "dyn" rust_ty_param_bound() ++ "+" |
| 63 |
/ "impl" rust_ty_param_bound() ++ "+" |
| 64 |
/ "(" (rust_type() ++ "," ","?)? ")" |
| 65 |
/ ("<" rust_type() ("as" rust_ty_path())? ">")? rust_ty_path() |
| 66 |
|
| 67 |
rule rust_ty_path() |
| 68 |
= "::"? (IDENT() ("::"? (rust_generic_args() / PAREN_GROUP() ("->" rust_type())?))?) ++ "::" |
| 69 |
|
| 70 |
rule rust_ty_params() -> Vec<TokenStream> |
| 71 |
= "<" p:($(rust_generic_param()) ++ ",") ","? ">" { p } |
| 72 |
|
| 73 |
rule rust_where_clause() |
| 74 |
= "where" ( |
| 75 |
LIFETIME() (":" LIFETIME() ++ "+")? |
| 76 |
/ rust_for_lifetimes()? rust_type() ":" rust_ty_param_bound() ++ "+" |
| 77 |
) ** "," ","? |
| 78 |
|
| 79 |
rule rust_generic_param() |
| 80 |
= LIFETIME() (":" LIFETIME() ++ "+")? |
| 81 |
/ IDENT() (":" rust_ty_param_bound() ++ "+")? |
| 82 |
|
| 83 |
rule rust_for_lifetimes() |
| 84 |
= "for" rust_ty_params() |
| 85 |
|
| 86 |
rule rust_ty_param_bound() |
| 87 |
= LIFETIME() |
| 88 |
/ "?"? rust_for_lifetimes()? rust_ty_path() |
| 89 |
/ "(" "?"? rust_for_lifetimes()? rust_ty_path() ")" |
| 90 |
|
| 91 |
rule rust_generic_args() |
| 92 |
= "<" (LIFETIME() / rust_type() / BRACE_GROUP() / LITERAL()) ++ "," ","? ">" |
| 93 |
|
| 94 |
rule expression() -> SpannedExpr = choice() |
| 95 |
|
| 96 |
rule choice() -> SpannedExpr = sp:sp() s:sequence() ++ "/" { |
| 97 |
if s.len() == 1 { |
| 98 |
s.into_iter().next().unwrap() |
| 99 |
} else { |
| 100 |
ChoiceExpr(s).at(sp) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
rule sequence() -> SpannedExpr |
| 105 |
= sp:sp() elements:labeled()* code:BRACE_GROUP()? { |
| 106 |
if let Some(code) = code { |
| 107 |
ActionExpr(elements, Some(code)).at(sp) |
| 108 |
} else if elements.len() != 1 { |
| 109 |
ActionExpr(elements, None).at(sp) |
| 110 |
} else { |
| 111 |
elements.into_iter().next().unwrap().expr |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
rule labeled() -> TaggedExpr |
| 116 |
= label:(l:IDENT() ":" {l})? expression:suffixed() |
| 117 |
{ TaggedExpr{ name: label, expr: expression } } |
| 118 |
|
| 119 |
rule suffixed() -> SpannedExpr |
| 120 |
= e:prefixed() sp:sp() "?" { OptionalExpr(Box::new(e)).at(sp) } |
| 121 |
/ e:prefixed() sp:sp() "**" count:repeatcount() sep:primary() { Repeat { inner: Box::new(e), bound: count, sep: Some(Box::new(sep)) }.at(sp) } |
| 122 |
/ e:prefixed() sp:sp() "++" sep:primary() { Repeat { inner: Box::new(e), bound: BoundedRepeat::Plus, sep: Some(Box::new(sep)) }.at(sp )} |
| 123 |
/ e:prefixed() sp:sp() "*" count:repeatcount() { Repeat { inner: Box::new(e), bound: count, sep: None }.at(sp) } |
| 124 |
/ e:prefixed() sp:sp() "+" { Repeat { inner: Box::new(e), bound: BoundedRepeat::Plus, sep: None }.at(sp) } |
| 125 |
/ prefixed() |
| 126 |
|
| 127 |
rule repeatcount() -> BoundedRepeat |
| 128 |
= "<" n:repeatnum() ">" { BoundedRepeat::Exact(n) } |
| 129 |
/ "<" min:repeatnum()? "," max:repeatnum()? ">" { BoundedRepeat::Both(min, max) } |
| 130 |
/ { BoundedRepeat::None } |
| 131 |
|
| 132 |
rule repeatnum() -> TokenStream = $(INTEGER() / BRACE_GROUP()) |
| 133 |
|
| 134 |
rule prefixed() -> SpannedExpr |
| 135 |
= sp:sp() "$" expression:primary() { MatchStrExpr(Box::new(expression)).at(sp) } |
| 136 |
/ sp:sp() "&" expression:primary() { PosAssertExpr(Box::new(expression)).at(sp) } |
| 137 |
/ sp:sp() "!" expression:primary() { NegAssertExpr(Box::new(expression)).at(sp) } |
| 138 |
/ primary() |
| 139 |
|
| 140 |
#[cache] |
| 141 |
rule primary() -> SpannedExpr |
| 142 |
= sp:sp() "precedence" "!" "{" levels:precedence_level()**"--" "}" { PrecedenceExpr{ levels:levels }.at(sp) } |
| 143 |
/ sp:sp() "position" "!" "(" ")" { PositionExpr.at(sp) } |
| 144 |
/ sp:sp() "quiet" "!" "{" e:expression() "}" { QuietExpr(Box::new(e)).at(sp) } |
| 145 |
/ sp:sp() "expected" "!" s:PAREN_GROUP() { FailExpr(s).at(sp) } |
| 146 |
/ &("_" / "__" / "___") sp:sp() name:IDENT() { RuleExpr(name, None, Vec::new()).at(sp) } |
| 147 |
/ sp:sp() name:IDENT() generics:$("::" rust_generic_args())? "(" args:(rule_arg() ** ",") ")" { RuleExpr(name, generics, args).at(sp) } |
| 148 |
/ sp:sp() l:LITERAL() { LiteralExpr(l).at(sp) } |
| 149 |
/ sp:sp() p:BRACKET_GROUP() { PatternExpr(p).at(sp) } |
| 150 |
/ "(" sp:sp() "@" ")" { MarkerExpr(true).at(sp) } |
| 151 |
/ sp:sp() "@" { MarkerExpr(false).at(sp) } |
| 152 |
/ sp:sp() "##" method:IDENT() args:PAREN_GROUP() { MethodExpr(method, args.stream()).at(sp) } |
| 153 |
/ sp:sp() "#" code:BRACE_GROUP() { CustomExpr(code).at(sp) } |
| 154 |
/ "(" expression:expression() ")" { expression } |
| 155 |
|
| 156 |
rule rule_arg() -> RuleArg |
| 157 |
= "<" e:expression() ">" { RuleArg::Peg(e) } |
| 158 |
/ tt:$( #{|input, pos| input.eat_until(pos, ',')}+ ) { RuleArg::Rust(tt) } |
| 159 |
|
| 160 |
rule precedence_level() -> PrecedenceLevel |
| 161 |
= operators:precedence_op()+ |
| 162 |
{ PrecedenceLevel{ operators: operators } } |
| 163 |
|
| 164 |
rule precedence_op() -> PrecedenceOperator |
| 165 |
= span:sp() elements:labeled()* action:BRACE_GROUP() |
| 166 |
{ PrecedenceOperator{ span, elements, action } } |
| 167 |
|
| 168 |
rule sp() -> Span = #{|input, pos| input.next_span(pos)} |
| 169 |
rule KEYWORD() = "pub" / "rule" / "use" / "type" / "where" |
| 170 |
rule IDENT() -> Ident = !KEYWORD() i:#{|input, pos| input.ident(pos)} {i} |
| 171 |
rule LITERAL() -> Literal = #{|input, pos| input.literal(pos)} |
| 172 |
rule PAREN_GROUP() -> Group = #{|input, pos| input.group(pos, Delimiter::Parenthesis)} |
| 173 |
rule BRACE_GROUP() -> Group = #{|input, pos| input.group(pos, Delimiter::Brace)} |
| 174 |
rule BRACKET_GROUP() -> Group = #{|input, pos| input.group(pos, Delimiter::Bracket)} |
| 175 |
rule LIFETIME() = "'" IDENT() |
| 176 |
rule INTEGER() = LITERAL() |
| 177 |
|
| 178 |
} |