1
1
# Language Definition
2
2
3
- ** Expr** package uses a specific syntax. In this document, you can find all supported
4
- syntaxes.
3
+ ** Expr** is an expression evaluation language for Go.
5
4
6
5
## Supported Literals
7
6
@@ -24,26 +23,24 @@ Example:
24
23
10_000_000_000
25
24
```
26
25
27
- ## Accessing Public Properties
26
+ ## Fields
28
27
29
- Public properties on structs can be accessed by using the ` . ` syntax.
30
- If you pass an array into an expression, use the ` [] ` syntax to access array keys.
28
+ Struct fields and map elements can be accessed by using the ` . ` or the ` [] ` syntax.
31
29
32
- ``` js
33
- foo .Array [0 ].Value
30
+ ```
31
+ foo.Field
32
+ bar["some-key"]
34
33
```
35
34
36
- ## Functions and Methods
35
+ ## Functions
37
36
38
- Functions may be called using ` () ` syntax. The ` . ` syntax can also be used to call methods on an struct .
37
+ Functions may be called using the ` () ` syntax.
39
38
40
- ``` js
41
- price . String ()
39
+ ```
40
+ foo.Method ()
42
41
```
43
42
44
- ## Supported Operators
45
-
46
- The package comes with a lot of operators:
43
+ ## Operators
47
44
48
45
### Arithmetic Operators
49
46
@@ -56,8 +53,8 @@ The package comes with a lot of operators:
56
53
57
54
Example:
58
55
59
- ``` js
60
- life + universe + everything
56
+ ```
57
+ x**2 + y
61
58
```
62
59
63
60
### Comparison Operators
@@ -89,21 +86,11 @@ life < universe || life < everything
89
86
* ` startsWith ` (has prefix)
90
87
* ` endsWith ` (has suffix)
91
88
92
- To test if a string does * not* match a regex, use the logical ` not ` operator in combination with the ` matches ` operator:
93
-
94
- ``` js
95
- not (" foo" matches " ^b.+" )
96
- ```
97
-
98
- You must use parenthesis because the unary operator ` not ` has precedence over the binary operator ` matches ` .
99
-
100
89
Example:
101
90
102
- ``` js
103
- ' Arthur' + ' ' + ' Dent'
104
91
```
105
-
106
- Result will be set to ` Arthur Dent ` .
92
+ "hello" matches "h.*"
93
+ ```
107
94
108
95
### Membership Operators
109
96
@@ -112,11 +99,11 @@ Result will be set to `Arthur Dent`.
112
99
113
100
Example:
114
101
115
- ``` js
102
+ ```
116
103
user.Group in ["human_resources", "marketing"]
117
104
```
118
105
119
- ``` js
106
+ ```
120
107
"foo" in {foo: 1, bar: 2}
121
108
```
122
109
@@ -126,13 +113,13 @@ user.Group in ["human_resources", "marketing"]
126
113
127
114
Example:
128
115
129
- ``` js
116
+ ```
130
117
user.Age in 18..45
131
118
```
132
119
133
120
The range is inclusive:
134
121
135
- ``` js
122
+ ```
136
123
1..3 == [1, 2, 3]
137
124
```
138
125
@@ -142,15 +129,15 @@ The range is inclusive:
142
129
143
130
Example:
144
131
145
- ``` js
132
+ ```
146
133
user.Age > 30 ? "mature" : "immature"
147
134
```
148
135
149
136
## Builtin functions
150
137
151
138
* ` len ` (length of array, map or string)
152
139
* ` all ` (will return ` true ` if all element satisfies the predicate)
153
- * ` none ` (will return ` true ` if all element does NOT satisfies the predicate)
140
+ * ` none ` (will return ` true ` if all element does NOT satisfy the predicate)
154
141
* ` any ` (will return ` true ` if any element satisfies the predicate)
155
142
* ` one ` (will return ` true ` if exactly ONE element satisfies the predicate)
156
143
* ` filter ` (filter array by the predicate)
@@ -161,29 +148,29 @@ Examples:
161
148
162
149
Ensure all tweets are less than 280 chars.
163
150
164
- ``` js
151
+ ```
165
152
all(Tweets, {.Size < 280})
166
153
```
167
154
168
155
Ensure there is exactly one winner.
169
156
170
- ``` js
157
+ ```
171
158
one(Participants, {.Winner})
172
159
```
173
160
174
161
## Closures
175
162
176
- * ` {...} ` (closure)
177
-
178
- Closures allowed only with builtin functions. To access current item use ` # ` symbol.
163
+ The closure is an expression that accepts a single argument. To access
164
+ the argument use the ` # ` symbol.
179
165
180
- ``` js
166
+ ```
181
167
map(0..9, {# / 2})
182
168
```
183
169
184
- If the item of array is struct, it's possible to access fields of struct with omitted ` # ` symbol (` #.Value ` becomes ` .Value ` ).
170
+ If the item of array is struct, it is possible to access fields of struct with
171
+ omitted ` # ` symbol (` #.Value ` becomes ` .Value ` ).
185
172
186
- ``` js
173
+ ```
187
174
filter(Tweets, {len(.Value) > 280})
188
175
```
189
176
@@ -197,7 +184,7 @@ Example:
197
184
198
185
Variable ` array ` is ` [1,2,3,4,5] ` .
199
186
200
- ``` js
187
+ ```
201
188
array[1:5] == [2,3,4]
202
189
array[3:] == [4,5]
203
190
array[:4] == [1,2,3]
0 commit comments