File: grammar.peg

package info (click to toggle)
golang-github-hashicorp-go-bexpr 0.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 356 kB
  • sloc: makefile: 50
file content (162 lines) | stat: -rw-r--r-- 4,481 bytes parent folder | download | duplicates (2)
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
{
package bexpr

import (
   "strconv"
   "strings"
)
}

Input <- _? "(" _? expr:OrExpression _? ")" _? EOF {
   return expr, nil
} / _? expr:OrExpression _? EOF {
   return expr, nil
}

OrExpression <- left:AndExpression _ "or" _ right:OrExpression {
   return &BinaryExpression{
      Operator: BinaryOpOr,
      Left: left.(Expression),
      Right: right.(Expression),
   }, nil
} / expr:AndExpression {
   return expr, nil
}

AndExpression <- left:NotExpression _ "and" _ right:AndExpression {
   return &BinaryExpression{
      Operator: BinaryOpAnd,
      Left: left.(Expression),
      Right: right.(Expression),
   }, nil
} / expr:NotExpression {
   return expr, nil
}

NotExpression <- "not" _ expr:NotExpression {
   if unary, ok := expr.(*UnaryExpression); ok && unary.Operator == UnaryOpNot {
      // small optimization to get rid unnecessary levels of AST nodes
      // for things like:  not not foo == 3  which is equivalent to foo == 3
      return unary.Operand, nil
   }

   return &UnaryExpression{
      Operator: UnaryOpNot,
      Operand: expr.(Expression),
   }, nil
} / expr:ParenthesizedExpression {
   return expr, nil
}

ParenthesizedExpression "grouping" <- "(" _? expr:OrExpression _? ")" {
   return expr, nil
} / expr:MatchExpression {
   return expr, nil
} / "(" _? OrExpression _? !")" &{
   return false, errors.New("Unmatched parentheses")
}

MatchExpression "match" <- MatchSelectorOpValue / MatchSelectorOp / MatchValueOpSelector

MatchSelectorOpValue "match" <- selector:Selector operator:(MatchEqual / MatchNotEqual / MatchContains / MatchNotContains / MatchMatches / MatchNotMatches) value:Value {
   return &MatchExpression{Selector: selector.(Selector), Operator: operator.(MatchOperator), Value: value.(*MatchValue)}, nil
}

MatchSelectorOp "match" <- selector:Selector operator:(MatchIsEmpty / MatchIsNotEmpty) {
   return &MatchExpression{Selector: selector.(Selector), Operator: operator.(MatchOperator), Value: nil}, nil
}

MatchValueOpSelector "match" <- value:Value operator:(MatchIn / MatchNotIn) selector:Selector {
   return &MatchExpression{Selector: selector.(Selector), Operator: operator.(MatchOperator), Value: value.(*MatchValue)}, nil
} / Value operator:(MatchIn / MatchNotIn) !Selector &{
   return false, errors.New("Invalid selector")
}

MatchEqual <- _? "==" _? {
   return MatchEqual, nil
}
MatchNotEqual <- _? "!=" _? {
   return MatchNotEqual, nil
}
MatchIsEmpty <- _ "is" _ "empty" {
   return MatchIsEmpty, nil
}
MatchIsNotEmpty <- _"is" _ "not" _ "empty" {
   return MatchIsNotEmpty, nil
}
MatchIn <- _ "in" _ {
   return MatchIn, nil
}
MatchNotIn <- _ "not" _ "in" _ {
   return MatchNotIn, nil
}
MatchContains <- _ "contains" _ {
   return MatchIn, nil
}
MatchNotContains <- _ "not" _ "contains" _ {
   return MatchNotIn, nil
}
MatchMatches <- _ "matches" _ {
   return MatchMatches, nil
}
MatchNotMatches <- _ "not" _ "matches" _ {
   return MatchNotMatches, nil
}

Selector "selector" <- first:Identifier rest:SelectorOrIndex* {
   sel := Selector{
      first.(string),
   }

   if rest != nil {
      for _, v := range rest.([]interface{}) {
        sel = append(sel, v.(string))
      }
   }
   return sel, nil
}

Identifier <- [a-zA-Z] [a-zA-Z0-9_]* {
   return string(c.text), nil
}

SelectorOrIndex <- "." ident:Identifier {
   return ident, nil
} / expr:IndexExpression {
   return expr, nil
}

IndexExpression "index" <- "[" _? lit:StringLiteral _? "]" {
   return lit, nil
} / "[" _? !StringLiteral &{
   return false, errors.New("Invalid index")
} / "[" _? StringLiteral _? !"]" &{
   return false, errors.New("Unclosed index expression")
}

Value "value" <- selector:Selector { return &MatchValue{Raw:strings.Join(selector.(Selector), ".")}, nil }
       / n:NumberLiteral { return &MatchValue{Raw: n.(string)}, nil }
       / s:StringLiteral { return &MatchValue{Raw: s.(string)}, nil}

NumberLiteral "number" <- "-"? IntegerOrFloat &AfterNumbers {
   return string(c.text), nil
} / "-"? IntegerOrFloat !AfterNumbers &{
   return false, errors.New("Invalid number literal")
}

AfterNumbers <- &(_ / EOF / ")")

IntegerOrFloat <- ("0" / [1-9][0-9]*) ("." [0-9]+)?

StringLiteral "string" <- ('`' RawStringChar* '`' / '"' DoubleStringChar* '"') {
  return strconv.Unquote(string(c.text))
} / ('`' RawStringChar* / '"' DoubleStringChar*) EOF &{
  return false, errors.New("Unterminated string literal")
}

RawStringChar <- !'`' .
DoubleStringChar <- !'"' .

_ "whitespace" <- [ \t\r\n]+

EOF <- !.