| 1 | use proc_macro2::{Group, Ident, Literal, Span, TokenStream}; |
| 2 | |
| 3 | #[derive(Debug)] |
| 4 | pub struct Grammar { |
| 5 | pub doc: Option<TokenStream>, |
| 6 | pub visibility: Option<TokenStream>, |
| 7 | pub name: Ident, |
| 8 | pub lifetime_params: Option<Vec<TokenStream>>, |
| 9 | pub args: Vec<(Ident, TokenStream)>, |
| 10 | pub items: Vec<Item>, |
| 11 | pub input_type: TokenStream, |
| 12 | } |
| 13 | |
| 14 | impl Grammar { |
| 15 | pub fn iter_rules(&self) -> impl Iterator<Item = &Rule> { |
| 16 | self.items.iter().filter_map(|item| match item { |
| 17 | Item::Rule(r) => Some(r), |
| 18 | _ => None, |
| 19 | }) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | #[derive(Debug)] |
| 24 | pub enum Item { |
| 25 | Use(TokenStream), |
| 26 | Rule(Rule), |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug)] |
| 30 | pub enum Cache { |
| 31 | Simple, |
| 32 | Recursive |
| 33 | } |
| 34 | |
| 35 | #[derive(Debug)] |
| 36 | pub struct Rule { |
| 37 | pub span: Span, |
| 38 | pub name: Ident, |
| 39 | pub ty_params: Option<Vec<TokenStream>>, |
| 40 | pub params: Vec<RuleParam>, |
| 41 | pub expr: SpannedExpr, |
| 42 | pub ret_type: Option<TokenStream>, |
| 43 | pub where_clause: Option<TokenStream>, |
| 44 | pub doc: Option<TokenStream>, |
| 45 | pub visibility: Option<TokenStream>, |
| 46 | pub cache: Option<Cache>, |
| 47 | pub no_eof: bool, |
| 48 | } |
| 49 | |
| 50 | #[derive(Debug)] |
| 51 | pub struct RuleParam { |
| 52 | pub name: Ident, |
| 53 | pub ty: RuleParamTy, |
| 54 | } |
| 55 | |
| 56 | #[derive(Debug)] |
| 57 | pub enum RuleParamTy { |
| 58 | Rust(TokenStream), |
| 59 | Rule(TokenStream), |
| 60 | } |
| 61 | |
| 62 | #[derive(Debug, Clone)] |
| 63 | pub struct TaggedExpr { |
| 64 | pub name: Option<Ident>, |
| 65 | pub expr: SpannedExpr, |
| 66 | } |
| 67 | #[derive(Debug, Clone)] |
| 68 | pub struct SpannedExpr { |
| 69 | pub span: Span, |
| 70 | pub expr: Expr, |
| 71 | } |
| 72 | |
| 73 | #[derive(Debug, Clone)] |
| 74 | pub enum Expr { |
| 75 | LiteralExpr(Literal), |
| 76 | PatternExpr(Group), |
| 77 | RuleExpr(Ident, Option<TokenStream>, Vec<RuleArg>), |
| 78 | MethodExpr(Ident, TokenStream), |
| 79 | CustomExpr(Group), |
| 80 | ChoiceExpr(Vec<SpannedExpr>), |
| 81 | OptionalExpr(Box<SpannedExpr>), |
| 82 | Repeat { inner: Box<SpannedExpr>, bound: BoundedRepeat, sep: Option<Box<SpannedExpr>> }, |
| 83 | PosAssertExpr(Box<SpannedExpr>), |
| 84 | NegAssertExpr(Box<SpannedExpr>), |
| 85 | ActionExpr(Vec<TaggedExpr>, Option<Group>), |
| 86 | MatchStrExpr(Box<SpannedExpr>), |
| 87 | PositionExpr, |
| 88 | QuietExpr(Box<SpannedExpr>), |
| 89 | FailExpr(Group), |
| 90 | PrecedenceExpr { |
| 91 | levels: Vec<PrecedenceLevel>, |
| 92 | }, |
| 93 | MarkerExpr(bool), |
| 94 | } |
| 95 | |
| 96 | impl Expr { |
| 97 | pub fn at(self, sp: Span) -> SpannedExpr { |
| 98 | SpannedExpr { expr: self, span:sp } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | #[derive(Debug, Clone)] |
| 103 | pub enum RuleArg { |
| 104 | Rust(TokenStream), |
| 105 | Peg(SpannedExpr), |
| 106 | } |
| 107 | |
| 108 | #[derive(Debug, Clone)] |
| 109 | pub struct PrecedenceLevel { |
| 110 | pub operators: Vec<PrecedenceOperator>, |
| 111 | } |
| 112 | |
| 113 | #[derive(Debug, Clone)] |
| 114 | pub struct PrecedenceOperator { |
| 115 | pub span: Span, |
| 116 | pub elements: Vec<TaggedExpr>, |
| 117 | pub action: Group, |
| 118 | } |
| 119 | |
| 120 | #[derive(Debug, Clone)] |
| 121 | pub enum BoundedRepeat { |
| 122 | None, |
| 123 | Plus, |
| 124 | Exact(TokenStream), |
| 125 | Both(Option<TokenStream>, Option<TokenStream>), |
| 126 | } |
| 127 | |
| 128 | impl BoundedRepeat { |
| 129 | pub fn has_lower_bound(&self) -> bool { |
| 130 | match self { |
| 131 | BoundedRepeat::None | BoundedRepeat::Both(None, _) => false, |
| 132 | BoundedRepeat::Plus | BoundedRepeat::Exact(_) | BoundedRepeat::Both(Some(_), _) => true |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | pub fn has_upper_bound(&self) -> bool { |
| 137 | match self { |
| 138 | BoundedRepeat::None | BoundedRepeat::Plus | BoundedRepeat::Both(_, None) => false, |
| 139 | BoundedRepeat::Exact(_) | BoundedRepeat::Both(_, Some(_)) => true |
| 140 | } |
| 141 | } |
| 142 | } |