snail-lang 0.5.2__tar.gz → 0.6.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.5.2 → snail_lang-0.6.0}/Cargo.lock +7 -7
- {snail_lang-0.5.2 → snail_lang-0.6.0}/PKG-INFO +33 -6
- {snail_lang-0.5.2 → snail_lang-0.6.0}/README.md +32 -5
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-ast/Cargo.toml +1 -1
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-ast/src/ast.rs +2 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-ast/src/awk.rs +1 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-core/Cargo.toml +1 -1
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-core/src/lib.rs +19 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-error/Cargo.toml +1 -1
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-lower/Cargo.toml +1 -1
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-lower/README.md +1 -1
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-lower/src/constants.rs +16 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-lower/src/expr.rs +42 -6
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-lower/src/helpers.rs +31 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-lower/src/lib.rs +2 -0
- snail_lang-0.6.0/crates/snail-lower/src/map.rs +235 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/Cargo.toml +1 -1
- snail_lang-0.6.0/crates/snail-parser/src/lib.rs +837 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/src/snail.pest +9 -7
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/src/string.rs +68 -15
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/tests/common.rs +27 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/tests/errors.rs +82 -1
- snail_lang-0.6.0/crates/snail-parser/tests/syntax_strings.rs +190 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-python/Cargo.toml +1 -1
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-python/src/lib.rs +58 -11
- {snail_lang-0.5.2 → snail_lang-0.6.0}/pyproject.toml +1 -1
- {snail_lang-0.5.2 → snail_lang-0.6.0}/python/snail/cli.py +101 -4
- {snail_lang-0.5.2 → snail_lang-0.6.0}/python/snail/runtime/__init__.py +11 -0
- snail_lang-0.6.0/python/snail/runtime/lazy_text.py +50 -0
- snail_lang-0.5.2/crates/snail-parser/src/lib.rs +0 -444
- snail_lang-0.5.2/crates/snail-parser/tests/syntax_strings.rs +0 -92
- {snail_lang-0.5.2 → snail_lang-0.6.0}/Cargo.toml +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/LICENSE +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-ast/README.md +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-ast/src/lib.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-core/README.md +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-error/README.md +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-error/src/lib.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-lower/src/awk.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-lower/src/operators.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-lower/src/program.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-lower/src/py_ast.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-lower/src/stmt.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/README.md +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/src/awk.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/src/expr.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/src/literal.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/src/stmt.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/src/util.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/tests/parser.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/tests/statements.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-parser/tests/syntax_expressions.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/crates/snail-python/build.rs +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/python/snail/__init__.py +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/python/snail/runtime/compact_try.py +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/python/snail/runtime/regex.py +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.0}/python/snail/runtime/structured_accessor.py +0 -0
- {snail_lang-0.5.2 → snail_lang-0.6.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.6.0"
|
|
489
489
|
|
|
490
490
|
[[package]]
|
|
491
491
|
name = "snail-core"
|
|
492
|
-
version = "0.
|
|
492
|
+
version = "0.6.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.6.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.6.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.6.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.6.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.6.0"
|
|
544
544
|
dependencies = [
|
|
545
545
|
"pyo3",
|
|
546
546
|
"snail-core",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: snail-lang
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Requires-Dist: jmespath>=1.0.1
|
|
5
5
|
Requires-Dist: maturin>=1.5 ; extra == 'dev'
|
|
6
6
|
Requires-Dist: pytest ; extra == 'dev'
|
|
@@ -19,6 +19,12 @@ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
|
19
19
|
|
|
20
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.
|
|
21
21
|
|
|
22
|
+
## AI Slop!
|
|
23
|
+
|
|
24
|
+
Snail is me learning how to devlop code using LLMs. I think its neat, and
|
|
25
|
+
maybe useful. I don't think this is high quality. I am going to try and LLM my
|
|
26
|
+
way into something good, but its certainly not there yet.
|
|
27
|
+
|
|
22
28
|
## Installing Snail
|
|
23
29
|
|
|
24
30
|
Install [uv](https://docs.astral.sh/uv/getting-started/installation/) and then run:
|
|
@@ -51,13 +57,27 @@ semicolons are optional. You can separate statements with newlines.
|
|
|
51
57
|
|
|
52
58
|
Process files line-by-line with familiar awk semantics:
|
|
53
59
|
|
|
54
|
-
```snail-awk("
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
END { print("Sum:", total); assert total == 15}
|
|
60
|
+
```snail-awk("hello world\nfoo bar\n")
|
|
61
|
+
/hello/ { print("matched:", $0) }
|
|
62
|
+
{ print($1, "->", $2) }
|
|
58
63
|
```
|
|
59
64
|
|
|
60
|
-
Built-in variables
|
|
65
|
+
**Built-in variables:**
|
|
66
|
+
|
|
67
|
+
| Variable | Description |
|
|
68
|
+
|----------|-------------|
|
|
69
|
+
| `$0` | Current line (with newline stripped) |
|
|
70
|
+
| `$1`, `$2`, ... | Individual fields (whitespace-split) |
|
|
71
|
+
| `$f` | All fields as a list |
|
|
72
|
+
| `$n` | Global line number (across all files) |
|
|
73
|
+
| `$fn` | Per-file line number |
|
|
74
|
+
| `$p` | Current file path |
|
|
75
|
+
| `$m` | Last regex match object |
|
|
76
|
+
|
|
77
|
+
Begin/end blocks use CLI flags (`-b`/`--begin`, `-e`/`--end`) for setup and teardown:
|
|
78
|
+
```bash
|
|
79
|
+
echo -e "5\n4\n3\n2\n1" | snail --awk --begin 'total = 0' --end 'print("Sum:", total)' '/^[0-9]+/ { total = total + int($1) }'
|
|
80
|
+
```
|
|
61
81
|
|
|
62
82
|
### Compact Error Handling
|
|
63
83
|
|
|
@@ -229,6 +249,13 @@ Plug 'sudonym1/snail', { 'rtp': 'extras/vim' }
|
|
|
229
249
|
|
|
230
250
|
See [extras/vim/README.md](extras/vim/README.md) for details. Tree-sitter grammar available in `extras/tree-sitter-snail/`.
|
|
231
251
|
|
|
252
|
+
## Performance
|
|
253
|
+
|
|
254
|
+
Section is WIP
|
|
255
|
+
|
|
256
|
+
Startup performance is benchmarked with `./benchmarks/startup.py`. On my
|
|
257
|
+
machine snail adds 5 ms of overhead above the regular python3 interpreter.
|
|
258
|
+
|
|
232
259
|
## 🛠️ Building from Source
|
|
233
260
|
|
|
234
261
|
### Prerequisites
|
|
@@ -7,6 +7,12 @@
|
|
|
7
7
|
|
|
8
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.
|
|
9
9
|
|
|
10
|
+
## AI Slop!
|
|
11
|
+
|
|
12
|
+
Snail is me learning how to devlop code using LLMs. I think its neat, and
|
|
13
|
+
maybe useful. I don't think this is high quality. I am going to try and LLM my
|
|
14
|
+
way into something good, but its certainly not there yet.
|
|
15
|
+
|
|
10
16
|
## Installing Snail
|
|
11
17
|
|
|
12
18
|
Install [uv](https://docs.astral.sh/uv/getting-started/installation/) and then run:
|
|
@@ -39,13 +45,27 @@ semicolons are optional. You can separate statements with newlines.
|
|
|
39
45
|
|
|
40
46
|
Process files line-by-line with familiar awk semantics:
|
|
41
47
|
|
|
42
|
-
```snail-awk("
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
END { print("Sum:", total); assert total == 15}
|
|
48
|
+
```snail-awk("hello world\nfoo bar\n")
|
|
49
|
+
/hello/ { print("matched:", $0) }
|
|
50
|
+
{ print($1, "->", $2) }
|
|
46
51
|
```
|
|
47
52
|
|
|
48
|
-
Built-in variables
|
|
53
|
+
**Built-in variables:**
|
|
54
|
+
|
|
55
|
+
| Variable | Description |
|
|
56
|
+
|----------|-------------|
|
|
57
|
+
| `$0` | Current line (with newline stripped) |
|
|
58
|
+
| `$1`, `$2`, ... | Individual fields (whitespace-split) |
|
|
59
|
+
| `$f` | All fields as a list |
|
|
60
|
+
| `$n` | Global line number (across all files) |
|
|
61
|
+
| `$fn` | Per-file line number |
|
|
62
|
+
| `$p` | Current file path |
|
|
63
|
+
| `$m` | Last regex match object |
|
|
64
|
+
|
|
65
|
+
Begin/end blocks use CLI flags (`-b`/`--begin`, `-e`/`--end`) for setup and teardown:
|
|
66
|
+
```bash
|
|
67
|
+
echo -e "5\n4\n3\n2\n1" | snail --awk --begin 'total = 0' --end 'print("Sum:", total)' '/^[0-9]+/ { total = total + int($1) }'
|
|
68
|
+
```
|
|
49
69
|
|
|
50
70
|
### Compact Error Handling
|
|
51
71
|
|
|
@@ -217,6 +237,13 @@ Plug 'sudonym1/snail', { 'rtp': 'extras/vim' }
|
|
|
217
237
|
|
|
218
238
|
See [extras/vim/README.md](extras/vim/README.md) for details. Tree-sitter grammar available in `extras/tree-sitter-snail/`.
|
|
219
239
|
|
|
240
|
+
## Performance
|
|
241
|
+
|
|
242
|
+
Section is WIP
|
|
243
|
+
|
|
244
|
+
Startup performance is benchmarked with `./benchmarks/startup.py`. On my
|
|
245
|
+
machine snail adds 5 ms of overhead above the regular python3 interpreter.
|
|
246
|
+
|
|
220
247
|
## 🛠️ Building from Source
|
|
221
248
|
|
|
222
249
|
### Prerequisites
|
|
@@ -189,11 +189,13 @@ pub enum Expr {
|
|
|
189
189
|
String {
|
|
190
190
|
value: String,
|
|
191
191
|
raw: bool,
|
|
192
|
+
bytes: bool,
|
|
192
193
|
delimiter: StringDelimiter,
|
|
193
194
|
span: SourceSpan,
|
|
194
195
|
},
|
|
195
196
|
FString {
|
|
196
197
|
parts: Vec<FStringPart>,
|
|
198
|
+
bytes: bool,
|
|
197
199
|
span: SourceSpan,
|
|
198
200
|
},
|
|
199
201
|
Bool {
|
|
@@ -32,5 +32,24 @@ pub fn compile_snail_source_with_auto_print(
|
|
|
32
32
|
let module = lower_awk_program_with_auto_print(py, &program, auto_print_last)?;
|
|
33
33
|
Ok(module)
|
|
34
34
|
}
|
|
35
|
+
CompileMode::Map => {
|
|
36
|
+
let program = parse_map_program(source)?;
|
|
37
|
+
let module = lower_map_program_with_auto_print(py, &program, auto_print_last)?;
|
|
38
|
+
Ok(module)
|
|
39
|
+
}
|
|
35
40
|
}
|
|
36
41
|
}
|
|
42
|
+
|
|
43
|
+
/// Compile an awk program with separate begin and end code blocks.
|
|
44
|
+
/// Each begin/end source is parsed as a regular Snail program.
|
|
45
|
+
pub fn compile_awk_source_with_begin_end(
|
|
46
|
+
py: Python<'_>,
|
|
47
|
+
main_source: &str,
|
|
48
|
+
begin_sources: &[&str],
|
|
49
|
+
end_sources: &[&str],
|
|
50
|
+
auto_print_last: bool,
|
|
51
|
+
) -> Result<PyObject, SnailError> {
|
|
52
|
+
let program = parse_awk_program_with_begin_end(main_source, begin_sources, end_sources)?;
|
|
53
|
+
let module = lower_awk_program_with_auto_print(py, &program, auto_print_last)?;
|
|
54
|
+
Ok(module)
|
|
55
|
+
}
|
|
@@ -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**: `$0`, `$<num>`, `$n`, `$fn`, `$p`, `$m` mapped to Python variable names
|
|
30
|
+
- **Awk variables**: `$0`, `$<num>`, `$n`, `$fn`, `$f`, `$p`, `$m` mapped to Python variable names
|
|
31
31
|
|
|
32
32
|
## Awk Mode Lowering
|
|
33
33
|
|
|
@@ -20,6 +20,7 @@ pub(crate) const SNAIL_AWK_NR: &str = "$n";
|
|
|
20
20
|
pub(crate) const SNAIL_AWK_FNR: &str = "$fn";
|
|
21
21
|
pub(crate) const SNAIL_AWK_PATH: &str = "$p";
|
|
22
22
|
pub(crate) const SNAIL_AWK_MATCH: &str = "$m";
|
|
23
|
+
pub(crate) const SNAIL_AWK_FIELDS: &str = "$f";
|
|
23
24
|
pub(crate) const SNAIL_AWK_LINE_PYVAR: &str = "__snail_line";
|
|
24
25
|
pub(crate) const SNAIL_AWK_FIELDS_PYVAR: &str = "__snail_fields";
|
|
25
26
|
pub(crate) const SNAIL_AWK_NR_PYVAR: &str = "__snail_nr_user";
|
|
@@ -27,12 +28,27 @@ pub(crate) const SNAIL_AWK_FNR_PYVAR: &str = "__snail_fnr_user";
|
|
|
27
28
|
pub(crate) const SNAIL_AWK_PATH_PYVAR: &str = "__snail_path_user";
|
|
28
29
|
pub(crate) const SNAIL_AWK_MATCH_PYVAR: &str = "__snail_match";
|
|
29
30
|
|
|
31
|
+
// Map-related constants (public within crate)
|
|
32
|
+
pub(crate) const SNAIL_MAP_SRC: &str = "$src";
|
|
33
|
+
pub(crate) const SNAIL_MAP_FD: &str = "$fd";
|
|
34
|
+
pub(crate) const SNAIL_MAP_TEXT: &str = "$text";
|
|
35
|
+
pub(crate) const SNAIL_MAP_SRC_PYVAR: &str = "__snail_src";
|
|
36
|
+
pub(crate) const SNAIL_MAP_FD_PYVAR: &str = "__snail_fd";
|
|
37
|
+
pub(crate) const SNAIL_MAP_TEXT_PYVAR: &str = "__snail_text";
|
|
38
|
+
pub const SNAIL_LAZY_TEXT_CLASS: &str = "__SnailLazyText";
|
|
39
|
+
|
|
30
40
|
pub(crate) fn injected_py_name(name: &str) -> Option<&'static str> {
|
|
31
41
|
match name {
|
|
42
|
+
// Awk variables
|
|
32
43
|
SNAIL_AWK_NR => Some(SNAIL_AWK_NR_PYVAR),
|
|
33
44
|
SNAIL_AWK_FNR => Some(SNAIL_AWK_FNR_PYVAR),
|
|
34
45
|
SNAIL_AWK_PATH => Some(SNAIL_AWK_PATH_PYVAR),
|
|
35
46
|
SNAIL_AWK_MATCH => Some(SNAIL_AWK_MATCH_PYVAR),
|
|
47
|
+
SNAIL_AWK_FIELDS => Some(SNAIL_AWK_FIELDS_PYVAR),
|
|
48
|
+
// Map variables
|
|
49
|
+
SNAIL_MAP_SRC => Some(SNAIL_MAP_SRC_PYVAR),
|
|
50
|
+
SNAIL_MAP_FD => Some(SNAIL_MAP_FD_PYVAR),
|
|
51
|
+
SNAIL_MAP_TEXT => Some(SNAIL_MAP_TEXT_PYVAR),
|
|
36
52
|
_ => None,
|
|
37
53
|
}
|
|
38
54
|
}
|
|
@@ -4,7 +4,7 @@ use snail_ast::*;
|
|
|
4
4
|
use snail_error::LowerError;
|
|
5
5
|
|
|
6
6
|
use crate::constants::*;
|
|
7
|
-
use crate::helpers::{name_expr, number_expr, regex_pattern_expr, string_expr};
|
|
7
|
+
use crate::helpers::{byte_string_expr, name_expr, number_expr, regex_pattern_expr, string_expr};
|
|
8
8
|
use crate::operators::{lower_binary_op, lower_bool_op, lower_compare_op, lower_unary_op};
|
|
9
9
|
use crate::py_ast::{AstBuilder, py_err_to_lower};
|
|
10
10
|
|
|
@@ -441,18 +441,53 @@ pub(crate) fn lower_expr_with_exception(
|
|
|
441
441
|
Expr::String {
|
|
442
442
|
value,
|
|
443
443
|
raw,
|
|
444
|
+
bytes,
|
|
444
445
|
delimiter,
|
|
445
446
|
span,
|
|
446
|
-
} =>
|
|
447
|
-
|
|
447
|
+
} => {
|
|
448
|
+
if *bytes {
|
|
449
|
+
byte_string_expr(builder, value, *raw, *delimiter, span)
|
|
450
|
+
} else {
|
|
451
|
+
string_expr(builder, value, *raw, *delimiter, span)
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
Expr::FString { parts, bytes, span } => {
|
|
448
455
|
let values = lower_fstring_parts(builder, parts, exception_name)?;
|
|
449
|
-
builder
|
|
456
|
+
let joined = builder
|
|
450
457
|
.call_node(
|
|
451
458
|
"JoinedStr",
|
|
452
459
|
vec![PyList::new_bound(builder.py(), values).into_py(builder.py())],
|
|
453
460
|
span,
|
|
454
461
|
)
|
|
455
|
-
.map_err(py_err_to_lower)
|
|
462
|
+
.map_err(py_err_to_lower)?;
|
|
463
|
+
|
|
464
|
+
if *bytes {
|
|
465
|
+
// Wrap in .encode() call: f"...".encode()
|
|
466
|
+
let encode_attr = builder
|
|
467
|
+
.call_node(
|
|
468
|
+
"Attribute",
|
|
469
|
+
vec![
|
|
470
|
+
joined,
|
|
471
|
+
"encode".to_string().into_py(builder.py()),
|
|
472
|
+
builder.load_ctx().map_err(py_err_to_lower)?,
|
|
473
|
+
],
|
|
474
|
+
span,
|
|
475
|
+
)
|
|
476
|
+
.map_err(py_err_to_lower)?;
|
|
477
|
+
builder
|
|
478
|
+
.call_node(
|
|
479
|
+
"Call",
|
|
480
|
+
vec![
|
|
481
|
+
encode_attr,
|
|
482
|
+
PyList::empty_bound(builder.py()).into_py(builder.py()),
|
|
483
|
+
PyList::empty_bound(builder.py()).into_py(builder.py()),
|
|
484
|
+
],
|
|
485
|
+
span,
|
|
486
|
+
)
|
|
487
|
+
.map_err(py_err_to_lower)
|
|
488
|
+
} else {
|
|
489
|
+
Ok(joined)
|
|
490
|
+
}
|
|
456
491
|
}
|
|
457
492
|
Expr::Bool { value, span } => builder
|
|
458
493
|
.call_node("Constant", vec![value.into_py(builder.py())], span)
|
|
@@ -1324,7 +1359,7 @@ fn substitute_placeholder(expr: &Expr, replacement: &Expr) -> Expr {
|
|
|
1324
1359
|
| Expr::None { .. }
|
|
1325
1360
|
| Expr::StructuredAccessor { .. }
|
|
1326
1361
|
| Expr::FieldIndex { .. } => expr.clone(),
|
|
1327
|
-
Expr::FString { parts, span } => Expr::FString {
|
|
1362
|
+
Expr::FString { parts, bytes, span } => Expr::FString {
|
|
1328
1363
|
parts: parts
|
|
1329
1364
|
.iter()
|
|
1330
1365
|
.map(|part| match part {
|
|
@@ -1334,6 +1369,7 @@ fn substitute_placeholder(expr: &Expr, replacement: &Expr) -> Expr {
|
|
|
1334
1369
|
}
|
|
1335
1370
|
})
|
|
1336
1371
|
.collect(),
|
|
1372
|
+
bytes: *bytes,
|
|
1337
1373
|
span: span.clone(),
|
|
1338
1374
|
},
|
|
1339
1375
|
Expr::Unary { op, expr, span } => Expr::Unary {
|
|
@@ -75,6 +75,37 @@ pub(crate) fn string_expr(
|
|
|
75
75
|
Ok(expr.into_py(builder.py()))
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
pub(crate) fn byte_string_expr(
|
|
79
|
+
builder: &AstBuilder<'_>,
|
|
80
|
+
value: &str,
|
|
81
|
+
raw: bool,
|
|
82
|
+
delimiter: StringDelimiter,
|
|
83
|
+
span: &SourceSpan,
|
|
84
|
+
) -> Result<PyObject, LowerError> {
|
|
85
|
+
let rendered = match (raw, delimiter) {
|
|
86
|
+
(true, StringDelimiter::Single) => format!("rb'{}'", value),
|
|
87
|
+
(true, StringDelimiter::Double) => format!("rb\"{}\"", value),
|
|
88
|
+
(true, StringDelimiter::TripleSingle) => format!("rb'''{}'''", value),
|
|
89
|
+
(true, StringDelimiter::TripleDouble) => format!("rb\"\"\"{}\"\"\"", value),
|
|
90
|
+
(false, StringDelimiter::Single) => format!("b'{}'", value),
|
|
91
|
+
(false, StringDelimiter::Double) => format!("b\"{}\"", value),
|
|
92
|
+
(false, StringDelimiter::TripleSingle) => format!("b'''{}'''", value),
|
|
93
|
+
(false, StringDelimiter::TripleDouble) => format!("b\"\"\"{}\"\"\"", value),
|
|
94
|
+
};
|
|
95
|
+
let expr = builder
|
|
96
|
+
.py()
|
|
97
|
+
.import_bound("ast")
|
|
98
|
+
.and_then(|ast| ast.getattr("parse"))
|
|
99
|
+
.and_then(|parse| parse.call1((rendered,)))
|
|
100
|
+
.and_then(|module| module.getattr("body"))
|
|
101
|
+
.and_then(|body| body.get_item(0))
|
|
102
|
+
.and_then(|expr_stmt| expr_stmt.getattr("value"));
|
|
103
|
+
|
|
104
|
+
let expr = expr.map_err(py_err_to_lower)?;
|
|
105
|
+
set_location(&expr, span).map_err(py_err_to_lower)?;
|
|
106
|
+
Ok(expr.into_py(builder.py()))
|
|
107
|
+
}
|
|
108
|
+
|
|
78
109
|
pub(crate) fn number_expr(
|
|
79
110
|
builder: &AstBuilder<'_>,
|
|
80
111
|
value: &str,
|
|
@@ -3,6 +3,7 @@ mod awk;
|
|
|
3
3
|
mod constants;
|
|
4
4
|
mod expr;
|
|
5
5
|
mod helpers;
|
|
6
|
+
mod map;
|
|
6
7
|
mod operators;
|
|
7
8
|
mod program;
|
|
8
9
|
mod py_ast;
|
|
@@ -10,6 +11,7 @@ mod stmt;
|
|
|
10
11
|
|
|
11
12
|
// Re-export public API
|
|
12
13
|
pub use constants::*;
|
|
14
|
+
pub use map::{lower_map_program, lower_map_program_with_auto_print};
|
|
13
15
|
pub use program::{
|
|
14
16
|
lower_awk_program, lower_awk_program_with_auto_print, lower_program,
|
|
15
17
|
lower_program_with_auto_print,
|