snail-lang 0.3.9__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {snail_lang-0.3.9 → snail_lang-0.4.0}/Cargo.lock +7 -7
- {snail_lang-0.3.9 → snail_lang-0.4.0}/PKG-INFO +6 -13
- {snail_lang-0.3.9 → snail_lang-0.4.0}/README.md +4 -12
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-ast/Cargo.toml +1 -1
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-core/Cargo.toml +1 -1
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-error/Cargo.toml +1 -1
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-lower/Cargo.toml +1 -1
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-lower/README.md +2 -2
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-lower/src/awk.rs +2 -2
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-lower/src/constants.rs +0 -4
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/Cargo.toml +1 -1
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/src/lib.rs +1 -1
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/src/snail.pest +1 -1
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/tests/errors.rs +2 -2
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-python/Cargo.toml +1 -1
- {snail_lang-0.3.9 → snail_lang-0.4.0}/pyproject.toml +4 -1
- {snail_lang-0.3.9 → snail_lang-0.4.0}/python/snail/runtime/structured_accessor.py +1 -1
- snail_lang-0.3.9/python/snail/vendor/__init__.py +0 -0
- snail_lang-0.3.9/python/snail/vendor/jmespath/LICENSE +0 -21
- snail_lang-0.3.9/python/snail/vendor/jmespath/__init__.py +0 -12
- snail_lang-0.3.9/python/snail/vendor/jmespath/ast.py +0 -90
- snail_lang-0.3.9/python/snail/vendor/jmespath/compat.py +0 -19
- snail_lang-0.3.9/python/snail/vendor/jmespath/exceptions.py +0 -137
- snail_lang-0.3.9/python/snail/vendor/jmespath/functions.py +0 -366
- snail_lang-0.3.9/python/snail/vendor/jmespath/lexer.py +0 -258
- snail_lang-0.3.9/python/snail/vendor/jmespath/parser.py +0 -526
- snail_lang-0.3.9/python/snail/vendor/jmespath/visitor.py +0 -329
- {snail_lang-0.3.9 → snail_lang-0.4.0}/Cargo.toml +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/LICENSE +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-ast/README.md +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-ast/src/ast.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-ast/src/awk.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-ast/src/lib.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-core/README.md +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-core/src/lib.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-error/README.md +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-error/src/lib.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-lower/src/expr.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-lower/src/helpers.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-lower/src/lib.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-lower/src/operators.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-lower/src/program.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-lower/src/py_ast.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-lower/src/stmt.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/README.md +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/src/awk.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/src/expr.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/src/literal.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/src/stmt.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/src/string.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/src/util.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/tests/common.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/tests/parser.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/tests/statements.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/tests/syntax_expressions.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-parser/tests/syntax_strings.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/crates/snail-python/src/lib.rs +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/python/snail/__init__.py +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/python/snail/cli.py +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/python/snail/runtime/__init__.py +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/python/snail/runtime/compact_try.py +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/python/snail/runtime/regex.py +0 -0
- {snail_lang-0.3.9 → snail_lang-0.4.0}/python/snail/runtime/subprocess.py +0 -0
|
@@ -485,11 +485,11 @@ checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
|
|
|
485
485
|
|
|
486
486
|
[[package]]
|
|
487
487
|
name = "snail-ast"
|
|
488
|
-
version = "0.
|
|
488
|
+
version = "0.4.0"
|
|
489
489
|
|
|
490
490
|
[[package]]
|
|
491
491
|
name = "snail-core"
|
|
492
|
-
version = "0.
|
|
492
|
+
version = "0.4.0"
|
|
493
493
|
dependencies = [
|
|
494
494
|
"pyo3",
|
|
495
495
|
"snail-ast",
|
|
@@ -500,14 +500,14 @@ dependencies = [
|
|
|
500
500
|
|
|
501
501
|
[[package]]
|
|
502
502
|
name = "snail-error"
|
|
503
|
-
version = "0.
|
|
503
|
+
version = "0.4.0"
|
|
504
504
|
dependencies = [
|
|
505
505
|
"snail-ast",
|
|
506
506
|
]
|
|
507
507
|
|
|
508
508
|
[[package]]
|
|
509
509
|
name = "snail-lower"
|
|
510
|
-
version = "0.
|
|
510
|
+
version = "0.4.0"
|
|
511
511
|
dependencies = [
|
|
512
512
|
"pyo3",
|
|
513
513
|
"snail-ast",
|
|
@@ -516,7 +516,7 @@ dependencies = [
|
|
|
516
516
|
|
|
517
517
|
[[package]]
|
|
518
518
|
name = "snail-parser"
|
|
519
|
-
version = "0.
|
|
519
|
+
version = "0.4.0"
|
|
520
520
|
dependencies = [
|
|
521
521
|
"pest",
|
|
522
522
|
"pest_derive",
|
|
@@ -526,7 +526,7 @@ dependencies = [
|
|
|
526
526
|
|
|
527
527
|
[[package]]
|
|
528
528
|
name = "snail-proptest"
|
|
529
|
-
version = "0.
|
|
529
|
+
version = "0.4.0"
|
|
530
530
|
dependencies = [
|
|
531
531
|
"proptest",
|
|
532
532
|
"pyo3",
|
|
@@ -540,7 +540,7 @@ dependencies = [
|
|
|
540
540
|
|
|
541
541
|
[[package]]
|
|
542
542
|
name = "snail-python"
|
|
543
|
-
version = "0.
|
|
543
|
+
version = "0.4.0"
|
|
544
544
|
dependencies = [
|
|
545
545
|
"pyo3",
|
|
546
546
|
"snail-core",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: snail-lang
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Requires-Dist: jmespath>=1.0.1
|
|
4
5
|
Requires-Dist: maturin>=1.5 ; extra == 'dev'
|
|
5
6
|
Requires-Dist: pytest ; extra == 'dev'
|
|
6
7
|
Provides-Extra: dev
|
|
@@ -12,17 +13,11 @@ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
|
12
13
|
<p align="center">
|
|
13
14
|
<img src="logo.png" alt="Snail logo" width="200">
|
|
14
15
|
</p>
|
|
15
|
-
|
|
16
|
-
<h1 align="center">Snail</h1>
|
|
17
16
|
<p align="center"><em>What do you get when you shove a snake in a shell?</em></p>
|
|
18
17
|
|
|
19
|
-
<h1>Snail
|
|
20
|
-
improving my knowledge of AI code developement. Things are probably broken
|
|
21
|
-
in interesting and horrible ways.</h1>
|
|
22
|
-
|
|
23
|
-
---
|
|
18
|
+
<h1 align="center">Snail</h1>
|
|
24
19
|
|
|
25
|
-
**Snail** is a programming language that compiles to Python, combining Python's
|
|
20
|
+
**Snail** is a programming language that compiles to Python, combining Python's familiarity and extensive libraries with Perl/awk-inspired syntax for quick scripts and one-liners.
|
|
26
21
|
|
|
27
22
|
## Installing Snail
|
|
28
23
|
|
|
@@ -62,7 +57,7 @@ BEGIN { total = 0 }
|
|
|
62
57
|
END { print("Sum:", total); assert total == 15}
|
|
63
58
|
```
|
|
64
59
|
|
|
65
|
-
Built-in variables: `$
|
|
60
|
+
Built-in variables: `$0` (line), `$1`, `$2` etc (access fields), `$n` (line number), `$fn` (per-file line number), `$p` (file path), `$m` (last match).
|
|
66
61
|
|
|
67
62
|
|
|
68
63
|
### Compact Error Handling
|
|
@@ -192,7 +187,7 @@ snail 'result = int("oops"):"bad int {$e}"?; print(result)'
|
|
|
192
187
|
snail 'm = "user@example.com" in /^[\\w.]+@([\\w.]+)$/; if m { print(m[1]) }'
|
|
193
188
|
|
|
194
189
|
# Awk mode: print line numbers for matches
|
|
195
|
-
rg -n "TODO" README.md | snail --awk '/TODO/ { print("{$n}: {$
|
|
190
|
+
rg -n "TODO" README.md | snail --awk '/TODO/ { print("{$n}: {$0}") }'
|
|
196
191
|
```
|
|
197
192
|
|
|
198
193
|
## 🏗️ Architecture
|
|
@@ -239,8 +234,6 @@ Installation per platform:
|
|
|
239
234
|
- **macOS**: `brew install python@3.12` (or use the system Python 3)
|
|
240
235
|
- **Windows**: Download from [python.org](https://www.python.org/downloads/)
|
|
241
236
|
|
|
242
|
-
**No Python packages required**: Snail vendors jmespath under `snail.vendor`.
|
|
243
|
-
|
|
244
237
|
### Build, Test, and Install
|
|
245
238
|
|
|
246
239
|
```bash
|
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<img src="logo.png" alt="Snail logo" width="200">
|
|
3
3
|
</p>
|
|
4
|
-
|
|
5
|
-
<h1 align="center">Snail</h1>
|
|
6
4
|
<p align="center"><em>What do you get when you shove a snake in a shell?</em></p>
|
|
7
5
|
|
|
8
|
-
<h1>Snail
|
|
9
|
-
improving my knowledge of AI code developement. Things are probably broken
|
|
10
|
-
in interesting and horrible ways.</h1>
|
|
11
|
-
|
|
12
|
-
---
|
|
6
|
+
<h1 align="center">Snail</h1>
|
|
13
7
|
|
|
14
|
-
**Snail** is a programming language that compiles to Python, combining Python's
|
|
8
|
+
**Snail** is a programming language that compiles to Python, combining Python's familiarity and extensive libraries with Perl/awk-inspired syntax for quick scripts and one-liners.
|
|
15
9
|
|
|
16
10
|
## Installing Snail
|
|
17
11
|
|
|
@@ -51,7 +45,7 @@ BEGIN { total = 0 }
|
|
|
51
45
|
END { print("Sum:", total); assert total == 15}
|
|
52
46
|
```
|
|
53
47
|
|
|
54
|
-
Built-in variables: `$
|
|
48
|
+
Built-in variables: `$0` (line), `$1`, `$2` etc (access fields), `$n` (line number), `$fn` (per-file line number), `$p` (file path), `$m` (last match).
|
|
55
49
|
|
|
56
50
|
|
|
57
51
|
### Compact Error Handling
|
|
@@ -181,7 +175,7 @@ snail 'result = int("oops"):"bad int {$e}"?; print(result)'
|
|
|
181
175
|
snail 'm = "user@example.com" in /^[\\w.]+@([\\w.]+)$/; if m { print(m[1]) }'
|
|
182
176
|
|
|
183
177
|
# Awk mode: print line numbers for matches
|
|
184
|
-
rg -n "TODO" README.md | snail --awk '/TODO/ { print("{$n}: {$
|
|
178
|
+
rg -n "TODO" README.md | snail --awk '/TODO/ { print("{$n}: {$0}") }'
|
|
185
179
|
```
|
|
186
180
|
|
|
187
181
|
## 🏗️ Architecture
|
|
@@ -228,8 +222,6 @@ Installation per platform:
|
|
|
228
222
|
- **macOS**: `brew install python@3.12` (or use the system Python 3)
|
|
229
223
|
- **Windows**: Download from [python.org](https://www.python.org/downloads/)
|
|
230
224
|
|
|
231
|
-
**No Python packages required**: Snail vendors jmespath under `snail.vendor`.
|
|
232
|
-
|
|
233
225
|
### Build, Test, and Install
|
|
234
226
|
|
|
235
227
|
```bash
|
|
@@ -27,7 +27,7 @@ This crate is the semantic transformation core of the Snail compiler. It takes S
|
|
|
27
27
|
- **Regex expressions** (`/pattern/`): Transformed into `__snail_regex_compile(pattern)` call
|
|
28
28
|
- **Regex matching** (`string in /pattern/`): Transformed into `__snail_regex_search(string, pattern)` call
|
|
29
29
|
- **Structured accessors** (`$[query]`): Transformed into `__SnailStructuredAccessor(query)` instance
|
|
30
|
-
- **Awk variables**: `$
|
|
30
|
+
- **Awk variables**: `$0`, `$<num>`, `$n`, `$fn`, `$p`, `$m` mapped to Python variable names
|
|
31
31
|
|
|
32
32
|
## Awk Mode Lowering
|
|
33
33
|
|
|
@@ -35,7 +35,7 @@ When lowering awk programs, generates a complete Python AST that:
|
|
|
35
35
|
1. Imports `sys` for accessing command-line arguments and stdin
|
|
36
36
|
2. Executes BEGIN blocks before processing input
|
|
37
37
|
3. Creates a main loop that reads lines from files or stdin
|
|
38
|
-
4. Updates awk variables (`$
|
|
38
|
+
4. Updates awk variables (`$0`, `$<num>`, `$n`, etc.) for each line
|
|
39
39
|
5. Evaluates patterns and executes actions for matching lines
|
|
40
40
|
6. Executes END blocks after all input is processed
|
|
41
41
|
|
|
@@ -427,8 +427,8 @@ fn regex_pattern_components(pattern: &Expr) -> Option<(Expr, RegexPattern, Sourc
|
|
|
427
427
|
span,
|
|
428
428
|
} => Some((*value.clone(), pattern.clone(), span.clone())),
|
|
429
429
|
Expr::Regex { pattern, span } => Some((
|
|
430
|
-
Expr::
|
|
431
|
-
|
|
430
|
+
Expr::FieldIndex {
|
|
431
|
+
index: "0".to_string(),
|
|
432
432
|
span: span.clone(),
|
|
433
433
|
},
|
|
434
434
|
pattern.clone(),
|
|
@@ -9,8 +9,6 @@ pub const SNAIL_JMESPATH_QUERY: &str = "__snail_jmespath_query";
|
|
|
9
9
|
pub const SNAIL_PARTIAL_HELPER: &str = "__snail_partial";
|
|
10
10
|
|
|
11
11
|
// Awk-related constants (public within crate)
|
|
12
|
-
pub(crate) const SNAIL_AWK_LINE: &str = "$l";
|
|
13
|
-
pub(crate) const SNAIL_AWK_FIELDS: &str = "$f";
|
|
14
12
|
pub(crate) const SNAIL_AWK_NR: &str = "$n";
|
|
15
13
|
pub(crate) const SNAIL_AWK_FNR: &str = "$fn";
|
|
16
14
|
pub(crate) const SNAIL_AWK_PATH: &str = "$p";
|
|
@@ -24,8 +22,6 @@ pub(crate) const SNAIL_AWK_MATCH_PYVAR: &str = "__snail_match";
|
|
|
24
22
|
|
|
25
23
|
pub(crate) fn injected_py_name(name: &str) -> Option<&'static str> {
|
|
26
24
|
match name {
|
|
27
|
-
SNAIL_AWK_LINE => Some(SNAIL_AWK_LINE_PYVAR),
|
|
28
|
-
SNAIL_AWK_FIELDS => Some(SNAIL_AWK_FIELDS_PYVAR),
|
|
29
25
|
SNAIL_AWK_NR => Some(SNAIL_AWK_NR_PYVAR),
|
|
30
26
|
SNAIL_AWK_FNR => Some(SNAIL_AWK_FNR_PYVAR),
|
|
31
27
|
SNAIL_AWK_PATH => Some(SNAIL_AWK_PATH_PYVAR),
|
|
@@ -86,7 +86,7 @@ pub fn parse_awk_program(source: &str) -> Result<AwkProgram, ParseError> {
|
|
|
86
86
|
})
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
const AWK_ONLY_NAMES: [&str;
|
|
89
|
+
const AWK_ONLY_NAMES: [&str; 4] = ["$n", "$fn", "$p", "$m"];
|
|
90
90
|
const AWK_ONLY_MESSAGE: &str = "awk variables are only valid in awk mode; use --awk";
|
|
91
91
|
|
|
92
92
|
fn validate_no_awk_syntax(program: &Program, source: &str) -> Result<(), ParseError> {
|
|
@@ -206,7 +206,7 @@ none = { "None" }
|
|
|
206
206
|
// Special variables: exception, AWK fields, injected vars
|
|
207
207
|
exception_var = { "$e" }
|
|
208
208
|
field_index_var = @{ "$" ~ ASCII_DIGIT+ }
|
|
209
|
-
injected_var = { "$fn" | "$
|
|
209
|
+
injected_var = { "$fn" | "$n" | "$p" | "$m" }
|
|
210
210
|
|
|
211
211
|
// Number, string, and regex literals
|
|
212
212
|
number = @{ ASCII_DIGIT+ ~ ("." ~ ASCII_DIGIT+)? }
|
|
@@ -22,9 +22,9 @@ fn rejects_user_defined_dollar_identifiers() {
|
|
|
22
22
|
|
|
23
23
|
#[test]
|
|
24
24
|
fn rejects_awk_only_variables_in_regular_mode() {
|
|
25
|
-
let err = parse_err("value = $
|
|
25
|
+
let err = parse_err("value = $n");
|
|
26
26
|
let message = err.to_string();
|
|
27
|
-
assert!(message.contains("$
|
|
27
|
+
assert!(message.contains("$n"));
|
|
28
28
|
assert!(message.contains("--awk"));
|
|
29
29
|
expect_err_span(&err, 1, 9);
|
|
30
30
|
}
|
|
@@ -4,11 +4,14 @@ build-backend = "maturin"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "snail-lang"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.4.0"
|
|
8
8
|
description = "Snail programming language interpreter"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
11
11
|
license = { file = "LICENSE" }
|
|
12
|
+
dependencies = [
|
|
13
|
+
"jmespath>=1.0.1",
|
|
14
|
+
]
|
|
12
15
|
|
|
13
16
|
[project.optional-dependencies]
|
|
14
17
|
dev = [
|
|
File without changes
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
from . import parser
|
|
2
|
-
from .visitor import Options
|
|
3
|
-
|
|
4
|
-
__version__ = "1.0.1"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def compile(expression):
|
|
8
|
-
return parser.Parser().parse(expression)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def search(expression, data, options=None):
|
|
12
|
-
return parser.Parser().parse(expression).search(data, options=options)
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
# AST nodes have this structure:
|
|
2
|
-
# {"type": <node type>", children: [], "value": ""}
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
def comparator(name, first, second):
|
|
6
|
-
return {'type': 'comparator', 'children': [first, second], 'value': name}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def current_node():
|
|
10
|
-
return {'type': 'current', 'children': []}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def expref(expression):
|
|
14
|
-
return {'type': 'expref', 'children': [expression]}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def function_expression(name, args):
|
|
18
|
-
return {'type': 'function_expression', 'children': args, 'value': name}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def field(name):
|
|
22
|
-
return {"type": "field", "children": [], "value": name}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def filter_projection(left, right, comparator):
|
|
26
|
-
return {'type': 'filter_projection', 'children': [left, right, comparator]}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def flatten(node):
|
|
30
|
-
return {'type': 'flatten', 'children': [node]}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
def identity():
|
|
34
|
-
return {"type": "identity", 'children': []}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
def index(index):
|
|
38
|
-
return {"type": "index", "value": index, "children": []}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
def index_expression(children):
|
|
42
|
-
return {"type": "index_expression", 'children': children}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
def key_val_pair(key_name, node):
|
|
46
|
-
return {"type": "key_val_pair", 'children': [node], "value": key_name}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
def literal(literal_value):
|
|
50
|
-
return {'type': 'literal', 'value': literal_value, 'children': []}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def multi_select_dict(nodes):
|
|
54
|
-
return {"type": "multi_select_dict", "children": nodes}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
def multi_select_list(nodes):
|
|
58
|
-
return {"type": "multi_select_list", "children": nodes}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
def or_expression(left, right):
|
|
62
|
-
return {"type": "or_expression", "children": [left, right]}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
def and_expression(left, right):
|
|
66
|
-
return {"type": "and_expression", "children": [left, right]}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
def not_expression(expr):
|
|
70
|
-
return {"type": "not_expression", "children": [expr]}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
def pipe(left, right):
|
|
74
|
-
return {'type': 'pipe', 'children': [left, right]}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
def projection(left, right):
|
|
78
|
-
return {'type': 'projection', 'children': [left, right]}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def subexpression(children):
|
|
82
|
-
return {"type": "subexpression", 'children': children}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
def slice(start, end, step):
|
|
86
|
-
return {"type": "slice", "children": [start, end, step]}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
def value_projection(left, right):
|
|
90
|
-
return {'type': 'value_projection', 'children': [left, right]}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import sys
|
|
2
|
-
import inspect
|
|
3
|
-
from itertools import zip_longest
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
text_type = str
|
|
7
|
-
string_type = str
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def with_str_method(cls):
|
|
11
|
-
# In python3, we don't need to do anything, we return a str type.
|
|
12
|
-
return cls
|
|
13
|
-
|
|
14
|
-
def with_repr_method(cls):
|
|
15
|
-
return cls
|
|
16
|
-
|
|
17
|
-
def get_methods(cls):
|
|
18
|
-
for name, method in inspect.getmembers(cls, predicate=inspect.isfunction):
|
|
19
|
-
yield name, method
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
from .compat import with_str_method
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
class JMESPathError(ValueError):
|
|
5
|
-
pass
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
@with_str_method
|
|
9
|
-
class ParseError(JMESPathError):
|
|
10
|
-
_ERROR_MESSAGE = "Invalid jmespath expression"
|
|
11
|
-
|
|
12
|
-
def __init__(self, lex_position, token_value, token_type, msg=_ERROR_MESSAGE):
|
|
13
|
-
super(ParseError, self).__init__(lex_position, token_value, token_type)
|
|
14
|
-
self.lex_position = lex_position
|
|
15
|
-
self.token_value = token_value
|
|
16
|
-
self.token_type = token_type.upper()
|
|
17
|
-
self.msg = msg
|
|
18
|
-
# Whatever catches the ParseError can fill in the full expression
|
|
19
|
-
self.expression = None
|
|
20
|
-
|
|
21
|
-
def __str__(self):
|
|
22
|
-
# self.lex_position +1 to account for the starting double quote char.
|
|
23
|
-
underline = " " * (self.lex_position + 1) + "^"
|
|
24
|
-
return (
|
|
25
|
-
"%s: Parse error at column %s, "
|
|
26
|
-
'token "%s" (%s), for expression:\n"%s"\n%s'
|
|
27
|
-
% (
|
|
28
|
-
self.msg,
|
|
29
|
-
self.lex_position,
|
|
30
|
-
self.token_value,
|
|
31
|
-
self.token_type,
|
|
32
|
-
self.expression,
|
|
33
|
-
underline,
|
|
34
|
-
)
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
@with_str_method
|
|
39
|
-
class IncompleteExpressionError(ParseError):
|
|
40
|
-
def set_expression(self, expression):
|
|
41
|
-
self.expression = expression
|
|
42
|
-
self.lex_position = len(expression)
|
|
43
|
-
self.token_type = None
|
|
44
|
-
self.token_value = None
|
|
45
|
-
|
|
46
|
-
def __str__(self):
|
|
47
|
-
# self.lex_position +1 to account for the starting double quote char.
|
|
48
|
-
underline = " " * (self.lex_position + 1) + "^"
|
|
49
|
-
return 'Invalid jmespath expression: Incomplete expression:\n"%s"\n%s' % (
|
|
50
|
-
self.expression,
|
|
51
|
-
underline,
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
@with_str_method
|
|
56
|
-
class LexerError(ParseError):
|
|
57
|
-
def __init__(self, lexer_position, lexer_value, message, expression=None):
|
|
58
|
-
self.lexer_position = lexer_position
|
|
59
|
-
self.lexer_value = lexer_value
|
|
60
|
-
self.message = message
|
|
61
|
-
super(LexerError, self).__init__(lexer_position, lexer_value, message)
|
|
62
|
-
# Whatever catches LexerError can set this.
|
|
63
|
-
self.expression = expression
|
|
64
|
-
|
|
65
|
-
def __str__(self):
|
|
66
|
-
underline = " " * self.lexer_position + "^"
|
|
67
|
-
return "Bad jmespath expression: %s:\n%s\n%s" % (
|
|
68
|
-
self.message,
|
|
69
|
-
self.expression,
|
|
70
|
-
underline,
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
@with_str_method
|
|
75
|
-
class ArityError(ParseError):
|
|
76
|
-
def __init__(self, expected, actual, name):
|
|
77
|
-
self.expected_arity = expected
|
|
78
|
-
self.actual_arity = actual
|
|
79
|
-
self.function_name = name
|
|
80
|
-
self.expression = None
|
|
81
|
-
|
|
82
|
-
def __str__(self):
|
|
83
|
-
return "Expected %s %s for function %s(), received %s" % (
|
|
84
|
-
self.expected_arity,
|
|
85
|
-
self._pluralize("argument", self.expected_arity),
|
|
86
|
-
self.function_name,
|
|
87
|
-
self.actual_arity,
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
def _pluralize(self, word, count):
|
|
91
|
-
if count == 1:
|
|
92
|
-
return word
|
|
93
|
-
else:
|
|
94
|
-
return word + "s"
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
@with_str_method
|
|
98
|
-
class VariadictArityError(ArityError):
|
|
99
|
-
def __str__(self):
|
|
100
|
-
return "Expected at least %s %s for function %s(), received %s" % (
|
|
101
|
-
self.expected_arity,
|
|
102
|
-
self._pluralize("argument", self.expected_arity),
|
|
103
|
-
self.function_name,
|
|
104
|
-
self.actual_arity,
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
@with_str_method
|
|
109
|
-
class JMESPathTypeError(JMESPathError):
|
|
110
|
-
def __init__(self, function_name, current_value, actual_type, expected_types):
|
|
111
|
-
self.function_name = function_name
|
|
112
|
-
self.current_value = current_value
|
|
113
|
-
self.actual_type = actual_type
|
|
114
|
-
self.expected_types = expected_types
|
|
115
|
-
|
|
116
|
-
def __str__(self):
|
|
117
|
-
return (
|
|
118
|
-
"In function %s(), invalid type for value: %s, "
|
|
119
|
-
'expected one of: %s, received: "%s"'
|
|
120
|
-
% (
|
|
121
|
-
self.function_name,
|
|
122
|
-
self.current_value,
|
|
123
|
-
self.expected_types,
|
|
124
|
-
self.actual_type,
|
|
125
|
-
)
|
|
126
|
-
)
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
class EmptyExpressionError(JMESPathError):
|
|
130
|
-
def __init__(self):
|
|
131
|
-
super(EmptyExpressionError, self).__init__(
|
|
132
|
-
"Invalid JMESPath expression: cannot be empty."
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
class UnknownFunctionError(JMESPathError):
|
|
137
|
-
pass
|