1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::num::NonZeroI64;
use std::ops;

mod parse;
mod eval;
mod token;

// Aliases

type Input = char;
type Error = chumsky::error::Simple<char>;

// Items

pub struct Span {
    _source: ops::Range<usize>,
}

impl From<ops::Range<usize>> for Span {
    fn from(span: ops::Range<usize>) -> Self {
        Span { _source: span }
    }
}

pub struct Ident {
    #[allow(dead_code)]
    span: Span,
    val: String,
}

impl Ident {
    pub fn as_str(&self) -> &str {
        &self.val
    }
}

pub struct IntLit {
    span: Span,
    val: i64,
}

pub struct NonZeroIntLit {
    _span: Span,
    val: NonZeroI64,
}

pub struct StringContent {
    _span: Span,
    val: String,
}

pub struct SingleStringLit {
    _start: token::SingleQuote,
    content: StringContent,
    _end: token::SingleQuote,
}

pub struct DoubleStringLit {
    _start: token::DoubleQuote,
    content: StringContent,
    _end: token::DoubleQuote,
}

pub enum StringLit {
    Single(SingleStringLit),
    Double(DoubleStringLit),
}

impl StringLit {
    fn as_str(&self) -> &str {
        match self {
            StringLit::Single(s) => &s.content.val,
            StringLit::Double(s) => &s.content.val,
        }
    }
}

pub struct BoolLit {
    _span: Span,
    val: bool,
}

pub struct NullLit {
    _span: Span
}

/// A compiled JSON path. Can be used to match against items any number of times, preventing
/// recompilation of the same pattern many times.
pub struct Path {
    _dollar: token::Dollar,
    children: Vec<Operator>,
    tilde: Option<token::Tilde>,
}

pub struct SubPath {
    kind: PathKind,
    children: Vec<Operator>,
    tilde: Option<token::Tilde>,
}

pub enum PathKind {
    Root(token::Dollar),
    Relative(token::At),
}

pub enum Operator {
    Dot(token::Dot, DotIdent),
    Bracket(token::Bracket, BracketInner),
    Recursive(token::DotDot, Option<RecursiveOp>),
}

pub enum RecursiveOp {
    Raw(DotIdent),
    Bracket(token::Bracket, BracketInner),
}

pub enum DotIdent {
    Wildcard(token::Star),
    Parent(token::Caret),
    Name(Ident),
}

pub struct StepRange {
    start: Option<IntLit>,
    _colon1: token::Colon,
    end: Option<IntLit>,
    _colon2: token::Colon,
    step: Option<NonZeroIntLit>,
}

pub struct Range {
    start: Option<IntLit>,
    _colon: token::Colon,
    end: Option<IntLit>,
}

pub enum UnionComponent {
    StepRange(StepRange),
    Range(Range),
    Parent(token::Caret),
    Path(SubPath),
    Filter(Filter),
    Literal(BracketLit),
}

pub enum BracketInner {
    Union(Vec<UnionComponent>),
    StepRange(StepRange),
    Range(Range),
    Wildcard(token::Star),
    Parent(token::Caret),
    Path(SubPath),
    Filter(Filter),
    Literal(BracketLit),
}

pub enum BracketLit {
    Int(IntLit),
    String(StringLit),
}

pub struct Filter {
    _question: token::Question,
    _paren: token::Paren,
    inner: FilterExpr,
}

pub enum ExprLit {
    Int(IntLit),
    Str(StringLit),
    Bool(BoolLit),
    Null(NullLit),
}

pub enum FilterExpr {
    Unary(UnOp, Box<FilterExpr>),
    Binary(Box<FilterExpr>, BinOp, Box<FilterExpr>),
    Path(SubPath),
    Lit(ExprLit),
    Parens(token::Paren, Box<FilterExpr>),
}

pub enum UnOp {
    Neg(token::Dash),
    Not(token::Bang),
}

pub enum BinOp {
    And(token::DoubleAnd),
    Or(token::DoublePipe),

    Eq(token::EqEq),
    Le(token::LessEq),
    Lt(token::LessThan),
    Gt(token::GreaterThan),
    Ge(token::GreaterEq),

    Add(token::Plus),
    Sub(token::Dash),
    Mul(token::Star),
    Div(token::RightSlash),
    Rem(token::Percent),
}