papagaio 0.5.2 → 0.6.1
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.
- package/README.md +242 -85
- package/bin/cli.qjs +1 -1
- package/examples/simple.html +2 -2
- package/examples/wasm.papagaio +70 -0
- package/index.html +12 -3
- package/package.json +7 -3
- package/src/papagaio-bootstrap.mjs +3 -5
- package/src/papagaio.js +64 -83
- package/tests/tests.json +35 -35
package/README.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Papagaio
|
|
2
|
-
Minimal yet powerful text preprocessor
|
|
2
|
+
Minimal yet powerful text preprocessor.
|
|
3
|
+
|
|
4
|
+
- **It's portable!** papagaio requires only ES6 and nothing else.
|
|
5
|
+
- **It's small!** papagaio is around ~250 lines and ~10kb.
|
|
6
|
+
- **It's easy!** papagaio doesnt have any complicated stuff, 1 class and 1 method for doing everything!
|
|
7
|
+
- **It's flexible!** do papagaio sigil and delimiters conflict with whatever you want to process? then simply change it! papagaio allow us to modify ANY of its keywords and symbols.
|
|
8
|
+
- **It's powerful!!** aside been inspired by the m4 preprocessor and meant to be a preprocessor, papagaio still a fully-featured programming language because it can evaluate any valid javascript code using $eval;
|
|
3
9
|
|
|
4
10
|
## Installation
|
|
5
11
|
```javascript
|
|
@@ -19,7 +25,6 @@ papagaio.symbols = {
|
|
|
19
25
|
block: "block", // block keyword
|
|
20
26
|
regex: "regex" // regex keyword
|
|
21
27
|
};
|
|
22
|
-
papagaio.recursion_limit = 512;
|
|
23
28
|
```
|
|
24
29
|
|
|
25
30
|
---
|
|
@@ -28,14 +33,14 @@ papagaio.recursion_limit = 512;
|
|
|
28
33
|
|
|
29
34
|
### 1. Simple Variables
|
|
30
35
|
```
|
|
31
|
-
pattern {$x} {$x}
|
|
36
|
+
$pattern {$x} {$x}
|
|
32
37
|
hello
|
|
33
38
|
```
|
|
34
39
|
Output: `hello`
|
|
35
40
|
|
|
36
41
|
### 2. Multiple Variables
|
|
37
42
|
```
|
|
38
|
-
pattern {$x $y $z} {$z, $y, $x}
|
|
43
|
+
$pattern {$x $y $z} {$z, $y, $x}
|
|
39
44
|
apple banana cherry
|
|
40
45
|
```
|
|
41
46
|
Output: `cherry, banana, apple`
|
|
@@ -53,19 +58,19 @@ Automatically adapts based on context:
|
|
|
53
58
|
- **Otherwise**: Captures a single word (non-whitespace token)
|
|
54
59
|
|
|
55
60
|
```
|
|
56
|
-
pattern {$x} {[$x]}
|
|
61
|
+
$pattern {$x} {[$x]}
|
|
57
62
|
hello world
|
|
58
63
|
```
|
|
59
|
-
Output: `[hello]`
|
|
64
|
+
Output: `[hello] [world]`
|
|
60
65
|
|
|
61
66
|
```
|
|
62
|
-
pattern {$name $block content {(}{)}} {$name: $content}
|
|
67
|
+
$pattern {$name $block content {(}{)}} {$name: $content}
|
|
63
68
|
greeting (hello world)
|
|
64
69
|
```
|
|
65
70
|
Output: `greeting: hello world`
|
|
66
71
|
|
|
67
72
|
```
|
|
68
|
-
pattern {$prefix:$suffix} {$suffix-$prefix}
|
|
73
|
+
$pattern {$prefix:$suffix} {$suffix-$prefix}
|
|
69
74
|
key:value
|
|
70
75
|
```
|
|
71
76
|
Output: `value-key`
|
|
@@ -74,13 +79,13 @@ Output: `value-key`
|
|
|
74
79
|
Same behavior as `$x`, but won't fail if empty or not found.
|
|
75
80
|
|
|
76
81
|
```
|
|
77
|
-
pattern {$x? world} {<$x>}
|
|
82
|
+
$pattern {$x? world} {<$x>}
|
|
78
83
|
world
|
|
79
84
|
```
|
|
80
85
|
Output: `<>`
|
|
81
86
|
|
|
82
87
|
```
|
|
83
|
-
pattern {$greeting? $name} {Hello $name$greeting}
|
|
88
|
+
$pattern {$greeting? $name} {Hello $name$greeting}
|
|
84
89
|
Hi John
|
|
85
90
|
```
|
|
86
91
|
Output: `Hello JohnHi`
|
|
@@ -98,30 +103,25 @@ $regex varName {pattern}
|
|
|
98
103
|
|
|
99
104
|
### Basic Example
|
|
100
105
|
```
|
|
101
|
-
pattern {$regex num {[0-9]+}} {Number: $num}
|
|
106
|
+
$pattern {$regex num {[0-9]+}} {Number: $num}
|
|
102
107
|
The answer is 42
|
|
103
108
|
```
|
|
104
109
|
Output: `Number: 42`
|
|
105
110
|
|
|
106
111
|
### Complex Patterns
|
|
107
112
|
```
|
|
108
|
-
pattern {$regex email {\w+@\w+\.\w+}} {Email found: $email}
|
|
113
|
+
$pattern {$regex email {\w+@\w+\.\w+}} {Email found: $email}
|
|
109
114
|
Contact: user@example.com
|
|
110
115
|
```
|
|
111
116
|
Output: `Email found: user@example.com`
|
|
112
117
|
|
|
113
118
|
### Multiple Regex Variables
|
|
114
119
|
```
|
|
115
|
-
pattern {$regex year {[0-9]{4}}-$regex month {[0-9]{2}}} {Month $month in $year}
|
|
120
|
+
$pattern {$regex year {[0-9]{4}}-$regex month {[0-9]{2}}} {Month $month in $year}
|
|
116
121
|
2024-03
|
|
117
122
|
```
|
|
118
123
|
Output: `Month 03 in 2024`
|
|
119
124
|
|
|
120
|
-
### Notes
|
|
121
|
-
- Regex patterns are cached for performance
|
|
122
|
-
- Matches are anchored at the current position (no searching ahead)
|
|
123
|
-
- Invalid regex patterns will cause the match to fail gracefully
|
|
124
|
-
|
|
125
125
|
---
|
|
126
126
|
|
|
127
127
|
## Blocks
|
|
@@ -135,21 +135,21 @@ $block varName {open}{close}
|
|
|
135
135
|
|
|
136
136
|
### Basic Example
|
|
137
137
|
```
|
|
138
|
-
pattern {$name $block content {(}{)}} {[$content]}
|
|
138
|
+
$pattern {$name $block content {(}{)}} {[$content]}
|
|
139
139
|
data (hello world)
|
|
140
140
|
```
|
|
141
141
|
Output: `[hello world]`
|
|
142
142
|
|
|
143
143
|
### Custom Delimiters
|
|
144
144
|
```
|
|
145
|
-
pattern {$block data {<<}{>>}} {DATA: $data}
|
|
145
|
+
$pattern {$block data {<<}{>>}} {DATA: $data}
|
|
146
146
|
<<json stuff>>
|
|
147
147
|
```
|
|
148
148
|
Output: `DATA: json stuff`
|
|
149
149
|
|
|
150
150
|
### Multi-Character Delimiters
|
|
151
151
|
```
|
|
152
|
-
pattern {$block code {```}{```}} {<pre>$code</pre>}
|
|
152
|
+
$pattern {$block code {```}{```}} {<pre>$code</pre>}
|
|
153
153
|
```markdown
|
|
154
154
|
# Title
|
|
155
155
|
```
|
|
@@ -157,91 +157,108 @@ Output: `<pre># Title</pre>`
|
|
|
157
157
|
|
|
158
158
|
### Multiple Blocks
|
|
159
159
|
```
|
|
160
|
-
pattern {$block a {(}{)}, $block b {[}{]}} {$a|$b}
|
|
160
|
+
$pattern {$block a {(}{)}, $block b {[}{]}} {$a|$b}
|
|
161
161
|
(first), [second]
|
|
162
162
|
```
|
|
163
163
|
Output: `first|second`
|
|
164
164
|
|
|
165
165
|
### Nested Blocks
|
|
166
166
|
```
|
|
167
|
-
pattern {$block outer {(}{)}} {[$outer]}
|
|
167
|
+
$pattern {$block outer {(}{)}} {[$outer]}
|
|
168
168
|
(outer (inner))
|
|
169
169
|
```
|
|
170
170
|
Output: `[outer (inner)]`
|
|
171
171
|
|
|
172
172
|
---
|
|
173
173
|
|
|
174
|
-
##
|
|
174
|
+
## Pattern Scopes
|
|
175
|
+
|
|
176
|
+
Patterns defined within a replacement body create nested scopes with hierarchical inheritance.
|
|
175
177
|
|
|
176
178
|
### Basic Pattern
|
|
177
179
|
```
|
|
178
|
-
pattern {
|
|
180
|
+
$pattern {hello} {world}
|
|
181
|
+
hello
|
|
179
182
|
```
|
|
183
|
+
Output: `world`
|
|
180
184
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
Output: `<h1>Welcome</h1>`
|
|
185
|
+
**Key Properties:**
|
|
186
|
+
* Patterns are scoped to their context
|
|
187
|
+
* Child patterns inherit parent patterns
|
|
188
|
+
* Patterns do not persist between `process()` calls
|
|
189
|
+
* Perfect for hierarchical transformations
|
|
187
190
|
|
|
188
|
-
###
|
|
191
|
+
### Nested Patterns with Inheritance
|
|
189
192
|
```
|
|
190
|
-
pattern {
|
|
191
|
-
pattern {
|
|
192
|
-
|
|
193
|
-
|
|
193
|
+
$pattern {outer $x} {
|
|
194
|
+
$pattern {inner $y} {[$y from $x]}
|
|
195
|
+
inner $x
|
|
196
|
+
}
|
|
197
|
+
outer hello
|
|
194
198
|
```
|
|
195
|
-
Output: `
|
|
196
|
-
|
|
197
|
-
---
|
|
198
|
-
|
|
199
|
-
## Subpatterns
|
|
199
|
+
Output: `[hello from hello]`
|
|
200
200
|
|
|
201
|
-
|
|
201
|
+
The inner pattern has access to `$x` from the outer pattern's capture.
|
|
202
202
|
|
|
203
|
-
###
|
|
203
|
+
### Deep Nesting
|
|
204
204
|
```
|
|
205
|
-
$pattern {
|
|
205
|
+
$pattern {level1 $a} {
|
|
206
|
+
$pattern {level2 $b} {
|
|
207
|
+
$pattern {level3 $c} {$a > $b > $c}
|
|
208
|
+
level3 $b
|
|
209
|
+
}
|
|
210
|
+
level2 $a
|
|
211
|
+
}
|
|
212
|
+
level1 ROOT
|
|
206
213
|
```
|
|
214
|
+
Output: `ROOT > ROOT > ROOT`
|
|
207
215
|
|
|
208
|
-
|
|
216
|
+
Each nested level inherits all patterns from parent scopes.
|
|
217
|
+
|
|
218
|
+
### Sibling Scopes Don't Share
|
|
209
219
|
```
|
|
210
|
-
pattern {
|
|
211
|
-
$
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
220
|
+
$pattern {branch1} {
|
|
221
|
+
$pattern {x} {A}
|
|
222
|
+
x
|
|
223
|
+
}
|
|
224
|
+
$pattern {branch2} {
|
|
225
|
+
x
|
|
216
226
|
}
|
|
217
|
-
|
|
227
|
+
branch1
|
|
228
|
+
branch2
|
|
218
229
|
```
|
|
219
230
|
Output:
|
|
220
231
|
```
|
|
221
|
-
|
|
232
|
+
A
|
|
233
|
+
x
|
|
222
234
|
```
|
|
223
235
|
|
|
224
|
-
|
|
225
|
-
* Subpatterns exist only within the running pattern.
|
|
226
|
-
* They do not leak into the global pattern list.
|
|
227
|
-
* They can recursively modify inner content before `$eval` or other processors.
|
|
228
|
-
* Multiple subpatterns can coexist in the same replacement.
|
|
236
|
+
Patterns in `branch1` are not available in `branch2` (they are siblings, not parent-child).
|
|
229
237
|
|
|
230
238
|
---
|
|
231
239
|
|
|
232
240
|
## Special Keywords
|
|
233
241
|
|
|
234
242
|
### $eval
|
|
235
|
-
Executes JavaScript code.
|
|
243
|
+
Executes JavaScript code with access to the Papagaio instance.
|
|
244
|
+
|
|
236
245
|
```
|
|
237
|
-
pattern {$x} {$eval{return parseInt($x)*2;}}
|
|
246
|
+
$pattern {$x} {$eval{return parseInt($x)*2;}}
|
|
238
247
|
5
|
|
239
248
|
```
|
|
240
249
|
Output: `10`
|
|
241
250
|
|
|
242
|
-
|
|
251
|
+
**Accessing Papagaio Instance:**
|
|
252
|
+
```
|
|
253
|
+
$pattern {info} {$eval{
|
|
254
|
+
return `Content length: ${papagaio.content.length}`;
|
|
255
|
+
}}
|
|
256
|
+
info
|
|
243
257
|
```
|
|
244
|
-
|
|
258
|
+
|
|
259
|
+
**Multi-character delimiters:**
|
|
260
|
+
```
|
|
261
|
+
$pattern {$x} {$eval<<parseInt($x)*2>>}
|
|
245
262
|
5
|
|
246
263
|
```
|
|
247
264
|
Output: `10`
|
|
@@ -254,41 +271,141 @@ Output: `10`
|
|
|
254
271
|
* `$x` = smart capture (context-aware: word, until literal, or until block)
|
|
255
272
|
* `$x?` = optional version of `$x` (won't fail if empty)
|
|
256
273
|
* `$regex name {pattern}` = regex-based capture
|
|
257
|
-
*
|
|
258
|
-
*
|
|
274
|
+
* Variables automatically skip leading whitespace
|
|
275
|
+
* Trailing whitespace is trimmed when variables appear before literals
|
|
276
|
+
|
|
277
|
+
### Pattern Matching
|
|
278
|
+
* `$pattern {match} {replace}` = pattern scoped to current context
|
|
279
|
+
* Patterns inherit from parent scopes hierarchically
|
|
280
|
+
* Each `process()` call starts with a clean slate (no persistence)
|
|
259
281
|
|
|
260
282
|
### Block Matching
|
|
261
283
|
* `$block name {open}{close}` captures delimited regions
|
|
262
284
|
* Supports nested delimiters of any length
|
|
263
285
|
* Multi-character delimiters fully supported (e.g., `{>>>}{<<<}`)
|
|
264
|
-
|
|
265
|
-
### Whitespace Handling
|
|
266
|
-
* Variables automatically skip leading whitespace when needed
|
|
267
|
-
* Trailing whitespace is trimmed when variables appear before literals
|
|
286
|
+
* Blocks support arbitrary nesting depth
|
|
268
287
|
|
|
269
288
|
---
|
|
270
289
|
|
|
271
290
|
## Multi-Character Delimiter Support
|
|
272
291
|
|
|
273
|
-
|
|
292
|
+
Papagaio fully supports multi-character delimiters throughout all features.
|
|
274
293
|
|
|
275
|
-
###
|
|
294
|
+
### Configuration
|
|
276
295
|
```javascript
|
|
277
296
|
const p = new Papagaio('$', '<<<', '>>>');
|
|
278
297
|
```
|
|
279
298
|
|
|
299
|
+
### In Patterns
|
|
300
|
+
```
|
|
301
|
+
$pattern<<<$x>>> <<<[$x]>>>
|
|
302
|
+
hello
|
|
303
|
+
```
|
|
304
|
+
Output: `[hello]`
|
|
305
|
+
|
|
280
306
|
### In Blocks
|
|
281
307
|
```
|
|
282
|
-
pattern
|
|
308
|
+
$pattern<<<$block data {<<}{>>}>>> <<<$data>>>
|
|
283
309
|
<<content>>
|
|
284
310
|
```
|
|
311
|
+
Output: `content`
|
|
285
312
|
|
|
286
313
|
### In Eval
|
|
287
314
|
```
|
|
288
|
-
|
|
289
|
-
pattern <<<$x>>> <<<$eval<<<return $x + 1>>>>>>
|
|
315
|
+
$pattern<<<$x>>> <<<$eval<<<return $x + 1>>>>>>
|
|
290
316
|
5
|
|
291
317
|
```
|
|
318
|
+
Output: `6`
|
|
319
|
+
|
|
320
|
+
---
|
|
321
|
+
|
|
322
|
+
## Advanced Examples
|
|
323
|
+
|
|
324
|
+
### Markdown-like Processor
|
|
325
|
+
```javascript
|
|
326
|
+
const p = new Papagaio();
|
|
327
|
+
const template = `
|
|
328
|
+
$pattern {# $title} {<h1>$title</h1>}
|
|
329
|
+
$pattern {## $title} {<h2>$title</h2>}
|
|
330
|
+
$pattern {**$text**} {<strong>$text</strong>}
|
|
331
|
+
|
|
332
|
+
# Hello World
|
|
333
|
+
## Subtitle
|
|
334
|
+
**bold text**
|
|
335
|
+
`;
|
|
336
|
+
|
|
337
|
+
p.process(template);
|
|
338
|
+
// Output:
|
|
339
|
+
// <h1>Hello World</h1>
|
|
340
|
+
// <h2>Subtitle</h2>
|
|
341
|
+
// <strong>bold text</strong>
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### Template System with State
|
|
345
|
+
```javascript
|
|
346
|
+
const p = new Papagaio();
|
|
347
|
+
p.vars = {}; // Custom property for storing variables
|
|
348
|
+
|
|
349
|
+
const template = `
|
|
350
|
+
$pattern {var $name = $value} {$eval{
|
|
351
|
+
papagaio.vars['$name'] = '$value';
|
|
352
|
+
return '';
|
|
353
|
+
}}
|
|
354
|
+
$pattern {get $name} {$eval{
|
|
355
|
+
return papagaio.vars['$name'] || 'undefined';
|
|
356
|
+
}}
|
|
357
|
+
|
|
358
|
+
var title = My Page
|
|
359
|
+
var author = John Doe
|
|
360
|
+
Title: get title
|
|
361
|
+
Author: get author
|
|
362
|
+
`;
|
|
363
|
+
|
|
364
|
+
p.process(template);
|
|
365
|
+
// Output:
|
|
366
|
+
// Title: My Page
|
|
367
|
+
// Author: John Doe
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
### Conditional Processing
|
|
371
|
+
```javascript
|
|
372
|
+
const p = new Papagaio();
|
|
373
|
+
const template = `
|
|
374
|
+
$pattern {if $block cond {(}{)} then $block yes {[}{]} else $block no {<}{>}} {
|
|
375
|
+
$eval{
|
|
376
|
+
const condition = ($cond).trim();
|
|
377
|
+
return condition === 'true' ? '$yes' : '$no';
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (true) then [yes branch] else <no branch>
|
|
382
|
+
if (false) then [yes branch] else <no branch>
|
|
383
|
+
`;
|
|
384
|
+
|
|
385
|
+
p.process(template);
|
|
386
|
+
// Output:
|
|
387
|
+
// yes branch
|
|
388
|
+
// no branch
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
### Function-like Patterns
|
|
392
|
+
```javascript
|
|
393
|
+
const p = new Papagaio();
|
|
394
|
+
const template = `
|
|
395
|
+
$pattern {double $x} {$eval{return parseInt('$x') * 2}}
|
|
396
|
+
$pattern {add $x $y} {$eval{return parseInt('$x') + parseInt('$y')}}
|
|
397
|
+
|
|
398
|
+
double 5
|
|
399
|
+
add 3 7
|
|
400
|
+
add (double 4) 10
|
|
401
|
+
`;
|
|
402
|
+
|
|
403
|
+
p.process(template);
|
|
404
|
+
// Output:
|
|
405
|
+
// 10
|
|
406
|
+
// 10
|
|
407
|
+
// 18
|
|
408
|
+
```
|
|
292
409
|
|
|
293
410
|
---
|
|
294
411
|
|
|
@@ -298,8 +415,9 @@ pattern <<<$x>>> <<<$eval<<<return $x + 1>>>>>>
|
|
|
298
415
|
|---------|----------|
|
|
299
416
|
| Variable not captured | Check context: use `$x?` for optional, or verify literals/blocks exist |
|
|
300
417
|
| Block mismatch | Verify opening and closing delimiters match the declaration |
|
|
301
|
-
| Infinite recursion |
|
|
418
|
+
| Infinite recursion | Pattern creates circular transformation; redesign pattern logic |
|
|
302
419
|
| Pattern not matching | Verify whitespace between tokens, check if variable should be optional |
|
|
420
|
+
| Pattern not available | Check scope hierarchy; patterns only inherit from parents, not siblings |
|
|
303
421
|
| Nested blocks fail | Ensure delimiters are properly balanced |
|
|
304
422
|
| Multi-char delimiters broken | Check delimiters don't conflict; use escaping if needed |
|
|
305
423
|
| Regex not matching | Test regex pattern separately; ensure it matches at the exact position |
|
|
@@ -309,20 +427,59 @@ pattern <<<$x>>> <<<$eval<<<return $x + 1>>>>>>
|
|
|
309
427
|
## Syntax Reference
|
|
310
428
|
|
|
311
429
|
```
|
|
312
|
-
pattern {$x $y} {$y, $x}
|
|
313
|
-
pattern {$x? $y} {$y, $x}
|
|
314
|
-
pattern {$regex n {[0-9]+}} {$n}
|
|
315
|
-
pattern {$block n {o}{c}} {$n}
|
|
316
|
-
$
|
|
317
|
-
|
|
430
|
+
$pattern {$x $y} {$y, $x} # pattern with variables
|
|
431
|
+
$pattern {$x? $y} {$y, $x} # optional variable
|
|
432
|
+
$pattern {$regex n {[0-9]+}} {$n} # regex capture
|
|
433
|
+
$pattern {$block n {o}{c}} {$n} # block capture with custom delimiters
|
|
434
|
+
$eval{code} # JavaScript evaluation
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
---
|
|
438
|
+
|
|
439
|
+
## API Reference
|
|
440
|
+
|
|
441
|
+
### Constructor
|
|
442
|
+
```javascript
|
|
443
|
+
new Papagaio(sigil, open, close, pattern, evalKw, blockKw, regexKw)
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
**Parameters:**
|
|
447
|
+
- `sigil` (default: `'$'`) - Variable prefix
|
|
448
|
+
- `open` (default: `'{'`) - Opening delimiter
|
|
449
|
+
- `close` (default: `'}'`) - Closing delimiter
|
|
450
|
+
- `pattern` (default: `'pattern'`) - Pattern keyword
|
|
451
|
+
- `evalKw` (default: `'eval'`) - Eval keyword
|
|
452
|
+
- `blockKw` (default: `'block'`) - Block keyword
|
|
453
|
+
- `regexKw` (default: `'regex'`) - Regex keyword
|
|
454
|
+
|
|
455
|
+
### Properties
|
|
456
|
+
- `papagaio.content` - Last processed output
|
|
457
|
+
- `papagaio.match` - Last matched substring (available in replacements)
|
|
458
|
+
- `papagaio.symbols` - Configuration object
|
|
459
|
+
- `papagaio.exit` - Optional hook function called after processing
|
|
460
|
+
|
|
461
|
+
### Methods
|
|
462
|
+
- `papagaio.process(input)` - Process input text and return transformed output
|
|
463
|
+
|
|
464
|
+
### Exit Hook
|
|
465
|
+
```javascript
|
|
466
|
+
const p = new Papagaio();
|
|
467
|
+
p.exit = function() {
|
|
468
|
+
console.log('Processing complete:', this.content);
|
|
469
|
+
};
|
|
470
|
+
p.process('$pattern {x} {y}\nx');
|
|
318
471
|
```
|
|
319
472
|
|
|
320
473
|
---
|
|
321
474
|
|
|
322
475
|
## Performance Notes
|
|
323
476
|
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
477
|
+
* Multi-character delimiter matching is optimized with substring operations
|
|
478
|
+
* Nested patterns inherit parent patterns through recursive application
|
|
479
|
+
* Nested blocks and patterns have no theoretical depth limit
|
|
480
|
+
* Large recursion limits can impact performance on complex inputs
|
|
481
|
+
* Each `process()` call is independent with no persistent state between calls
|
|
482
|
+
|
|
483
|
+
---
|
|
484
|
+
|
|
485
|
+
***PAPAGAIO IS CURRENTLY IN HEAVY DEVELOPMENT AND EXPERIMENTATION PHASE***
|
package/bin/cli.qjs
CHANGED
|
@@ -7,7 +7,7 @@ import * as os from "os";
|
|
|
7
7
|
import { Papagaio } from "../src/papagaio.js";
|
|
8
8
|
|
|
9
9
|
// Version (você pode hardcoded ou ler de um arquivo JSON se necessário)
|
|
10
|
-
const VERSION = "
|
|
10
|
+
const VERSION = "0.6.0";
|
|
11
11
|
|
|
12
12
|
// Parse command line arguments
|
|
13
13
|
const args = scriptArgs.slice(1); // QuickJS usa scriptArgs ao invés de process.argv
|
package/examples/simple.html
CHANGED
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
6
|
<title>papagaio test</title>
|
|
7
7
|
</head>
|
|
8
|
-
<script src="src/papagaio-bootstrap.mjs" type="module"></script>
|
|
8
|
+
<script src="../src/papagaio-bootstrap.mjs" type="module"></script>
|
|
9
9
|
<script type="papagaio">
|
|
10
|
-
pattern {abc} {$eval{console.log(papagaio)}}
|
|
10
|
+
$pattern {abc} {$eval{console.log(papagaio)} aaaaaaaaaaaaaaaaaaaaaaaaaaaa}
|
|
11
11
|
abc
|
|
12
12
|
</script>
|
|
13
13
|
<body>
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// generic ts-like to wasm compiler
|
|
2
|
+
|
|
3
|
+
// util patterns
|
|
4
|
+
$pattern {// $comment $regex newline{[^\n]*}} {}
|
|
5
|
+
$pattern {$regex spaces{\s\s}}{ }
|
|
6
|
+
|
|
7
|
+
$eval{
|
|
8
|
+
papagaio.exit = function()
|
|
9
|
+
{
|
|
10
|
+
papagaio.content = "(module\n" + papagaio.content + "\n)";
|
|
11
|
+
papagaio.exit = null;
|
|
12
|
+
};
|
|
13
|
+
return ""
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
$pattern {export function $name $block params{(}{)}:$rets $block content{}{}} {
|
|
17
|
+
$pattern {parametrize}
|
|
18
|
+
{
|
|
19
|
+
$eval {
|
|
20
|
+
let str = "$params".replace("(", "").replace(")", "");
|
|
21
|
+
let params = str.split(",").map(p => p.trim()).filter(p => p);
|
|
22
|
+
let new_stuff = "";
|
|
23
|
+
for (const param of params) {
|
|
24
|
+
if (!param.includes(":")) continue; // Pula se não tem ':'
|
|
25
|
+
const [name, type] = param.split(":");
|
|
26
|
+
if (name && type) { // Verifica se ambos existem
|
|
27
|
+
new_stuff += ` (param $${name.trim()} ${type.trim()}) `;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return new_stuff;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
(func (export "$name") parametrize (result $rets)
|
|
35
|
+
$content
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
$pattern {function $name $block params{(}{)}:$rets $block content{}{}} {
|
|
40
|
+
$pattern {parametrize}
|
|
41
|
+
{
|
|
42
|
+
$eval {
|
|
43
|
+
let str = "$params".replace("(", "").replace(")", "");
|
|
44
|
+
let params = str.split(",").map(p => p.trim()).filter(p => p);
|
|
45
|
+
let new_stuff = "";
|
|
46
|
+
for (const param of params) {
|
|
47
|
+
if (!param.includes(":")) continue; // Pula se não tem ':'
|
|
48
|
+
const [name, type] = param.split(":");
|
|
49
|
+
if (name && type) { // Verifica se ambos existem
|
|
50
|
+
new_stuff += ` (param $${name.trim()} ${type.trim()}) `;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return new_stuff;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
(func $$name parametrize (result $rets)
|
|
58
|
+
$content
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function name(a:i32, b:i32):i64 i64
|
|
63
|
+
{
|
|
64
|
+
contentnans
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function funcao_exportada(a:f32, b:f32, c:f32):i64 i64 i64 i64
|
|
68
|
+
{
|
|
69
|
+
fução expoortada1
|
|
70
|
+
}
|
package/index.html
CHANGED
|
@@ -7,13 +7,16 @@
|
|
|
7
7
|
</head>
|
|
8
8
|
<body>
|
|
9
9
|
|
|
10
|
-
<h1>🦜 papagaio</h1>
|
|
10
|
+
<h1>🦜 papagaio </h1>
|
|
11
11
|
<p>
|
|
12
12
|
easy yet powerful text preprocessor.
|
|
13
13
|
</p>
|
|
14
14
|
<hr>
|
|
15
15
|
<div>
|
|
16
|
-
<
|
|
16
|
+
<strong>
|
|
17
|
+
<label for="sketchSelect">Current Sketch:</label>
|
|
18
|
+
</strong>
|
|
19
|
+
|
|
17
20
|
<select id="sketchSelect" onchange="switchSketch()">
|
|
18
21
|
<option value="">-- Select Sketch --</option>
|
|
19
22
|
</select>
|
|
@@ -24,7 +27,9 @@
|
|
|
24
27
|
</div>
|
|
25
28
|
<hr>
|
|
26
29
|
<div>
|
|
30
|
+
<strong>
|
|
27
31
|
<label for="sketchSelect">Sketches:</label>
|
|
32
|
+
</strong>
|
|
28
33
|
<button onclick="exportSketches()">Export</button>
|
|
29
34
|
<input type="file" id="importFileInput" onchange="importSketches(this.files[0])" accept=".json">
|
|
30
35
|
</div>
|
|
@@ -270,7 +275,8 @@
|
|
|
270
275
|
function exportSketches() {
|
|
271
276
|
const data = {
|
|
272
277
|
papagaio_sketches: localStorage.getItem('papagaio_sketches'),
|
|
273
|
-
papagaio_current_sketch: localStorage.getItem('papagaio_current_sketch')
|
|
278
|
+
papagaio_current_sketch: localStorage.getItem('papagaio_current_sketch'),
|
|
279
|
+
papagaio_config: localStorage.getItem('papagaio_config')
|
|
274
280
|
};
|
|
275
281
|
|
|
276
282
|
const blob = new Blob([JSON.stringify(data, null, 2)], {
|
|
@@ -297,6 +303,9 @@
|
|
|
297
303
|
if (data.papagaio_current_sketch) {
|
|
298
304
|
localStorage.setItem('papagaio_current_sketch', data.papagaio_current_sketch);
|
|
299
305
|
}
|
|
306
|
+
if (data.papagaio_config) {
|
|
307
|
+
localStorage.setItem('papagaio_config', data.papagaio_config);
|
|
308
|
+
}
|
|
300
309
|
location.reload();
|
|
301
310
|
} catch (err) {
|
|
302
311
|
alert('Error importing file: ' + err.message);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "papagaio",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "easy yet powerful preprocessor",
|
|
5
5
|
"main": "src/papagaio.js",
|
|
6
6
|
"type": "module",
|
|
@@ -16,7 +16,11 @@
|
|
|
16
16
|
"macro",
|
|
17
17
|
"pattern",
|
|
18
18
|
"eval",
|
|
19
|
-
"parser"
|
|
19
|
+
"parser",
|
|
20
|
+
"codegen",
|
|
21
|
+
"regex",
|
|
22
|
+
"syntax",
|
|
23
|
+
"easy"
|
|
20
24
|
],
|
|
21
25
|
"author": "jardimdanificado",
|
|
22
26
|
"bugs": {
|
|
@@ -24,6 +28,6 @@
|
|
|
24
28
|
},
|
|
25
29
|
"homepage": "https://github.com/jardimdanificado/papagaio#readme",
|
|
26
30
|
"bin": {
|
|
27
|
-
"papagaio": "
|
|
31
|
+
"papagaio": "bin/cli.js"
|
|
28
32
|
}
|
|
29
33
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// papagaio-bootstrap.js
|
|
2
|
+
// only needed if using <script type="papagaio"> in browser
|
|
2
3
|
import { Papagaio } from "./papagaio.js";
|
|
3
4
|
|
|
4
5
|
(async () => {
|
|
@@ -15,11 +16,8 @@ import { Papagaio } from "./papagaio.js";
|
|
|
15
16
|
|
|
16
17
|
const out = p.process(src);
|
|
17
18
|
|
|
18
|
-
const s = document.createElement("
|
|
19
|
-
s.type = "module";
|
|
19
|
+
const s = document.createElement("div");
|
|
20
20
|
s.textContent = out;
|
|
21
|
-
|
|
22
|
-
// executa no mesmo ponto onde script estava
|
|
23
|
-
el.replaceWith(s);
|
|
21
|
+
window.document.body.appendChild(s);
|
|
24
22
|
}
|
|
25
23
|
})();
|
package/src/papagaio.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
// papagaio - https://github.com/jardimdanificado/papagaio
|
|
1
2
|
function parsePattern(p, pat) {
|
|
2
3
|
const t = [], S = p.symbols.sigil, O = p.symbols.open;
|
|
3
4
|
let i = 0;
|
|
4
|
-
|
|
5
5
|
while (i < pat.length) {
|
|
6
6
|
if (pat.startsWith(S + p.symbols.regex, i)) {
|
|
7
7
|
let j = i + S.length + p.symbols.regex.length;
|
|
@@ -46,8 +46,7 @@ function parsePattern(p, pat) {
|
|
|
46
46
|
const optional = pat[j] === '?';
|
|
47
47
|
if (optional) j++;
|
|
48
48
|
t.push({ type: 'var', varName: v, optional });
|
|
49
|
-
i = j;
|
|
50
|
-
continue;
|
|
49
|
+
i = j; continue;
|
|
51
50
|
}
|
|
52
51
|
t.push({ type: 'lit', value: S }); i += S.length; continue;
|
|
53
52
|
}
|
|
@@ -62,47 +61,42 @@ function parsePattern(p, pat) {
|
|
|
62
61
|
return t;
|
|
63
62
|
}
|
|
64
63
|
|
|
65
|
-
function matchPattern(p, src,
|
|
64
|
+
function matchPattern(p, src, tok, pos = 0) {
|
|
66
65
|
let cap = {};
|
|
67
|
-
for (let ti = 0; ti <
|
|
68
|
-
const
|
|
69
|
-
if (
|
|
70
|
-
if (
|
|
71
|
-
if (
|
|
66
|
+
for (let ti = 0; ti < tok.length; ti++) {
|
|
67
|
+
const t = tok[ti];
|
|
68
|
+
if (t.type === 'ws') { while (pos < src.length && /\s/.test(src[pos])) pos++; continue; }
|
|
69
|
+
if (t.type === 'lit') { if (!src.startsWith(t.value, pos)) return null; pos += t.value.length; continue; }
|
|
70
|
+
if (t.type === 'regex') {
|
|
72
71
|
try {
|
|
73
|
-
|
|
74
|
-
if (!regex) {
|
|
75
|
-
regex = new RegExp(tok.regex);
|
|
76
|
-
p._regexCache.set(tok.regex, regex);
|
|
77
|
-
}
|
|
78
|
-
const m = src.slice(pos).match(regex);
|
|
72
|
+
const rx = new RegExp(t.regex), m = src.slice(pos).match(rx);
|
|
79
73
|
if (!m || m.index !== 0) return null;
|
|
80
|
-
cap[p.symbols.sigil +
|
|
74
|
+
cap[p.symbols.sigil + t.varName] = m[0];
|
|
81
75
|
pos += m[0].length;
|
|
82
|
-
} catch { return null; }
|
|
76
|
+
} catch (e) { return null; }
|
|
83
77
|
continue;
|
|
84
78
|
}
|
|
85
|
-
if (
|
|
79
|
+
if (t.type === 'var') {
|
|
86
80
|
while (pos < src.length && /\s/.test(src[pos])) pos++;
|
|
87
|
-
const nx = findNext(
|
|
81
|
+
const nx = findNext(tok, ti);
|
|
88
82
|
let v = '';
|
|
89
|
-
if (nx
|
|
83
|
+
if (nx && nx.type === 'block') {
|
|
90
84
|
while (pos < src.length && !src.startsWith(nx.open, pos) && src[pos] !== '\n') v += src[pos++];
|
|
91
85
|
v = v.trimEnd();
|
|
92
|
-
} else if (nx
|
|
86
|
+
} else if (nx && nx.type === 'lit') {
|
|
93
87
|
while (pos < src.length && !src.startsWith(nx.value, pos) && src[pos] !== '\n') v += src[pos++];
|
|
94
88
|
v = v.trimEnd();
|
|
95
89
|
} else {
|
|
96
90
|
while (pos < src.length && !/\s/.test(src[pos])) v += src[pos++];
|
|
97
91
|
}
|
|
98
|
-
if (!v && !
|
|
99
|
-
cap[p.symbols.sigil +
|
|
92
|
+
if (!v && !t.optional) return null;
|
|
93
|
+
cap[p.symbols.sigil + t.varName] = v;
|
|
100
94
|
continue;
|
|
101
95
|
}
|
|
102
|
-
if (
|
|
103
|
-
if (!src.startsWith(
|
|
104
|
-
const [c, e] = extractBlock(p, src, pos,
|
|
105
|
-
cap[p.symbols.sigil +
|
|
96
|
+
if (t.type === 'block') {
|
|
97
|
+
if (!src.startsWith(t.open, pos)) return null;
|
|
98
|
+
const [c, e] = extractBlock(p, src, pos, t.open, t.close);
|
|
99
|
+
cap[p.symbols.sigil + t.varName] = c; pos = e; continue;
|
|
106
100
|
}
|
|
107
101
|
}
|
|
108
102
|
return { captures: cap, endPos: pos };
|
|
@@ -134,58 +128,41 @@ function extractBlock(p, src, i, od = p.symbols.open, cd = p.symbols.close) {
|
|
|
134
128
|
return ['', i];
|
|
135
129
|
}
|
|
136
130
|
|
|
137
|
-
function collectPats(p, src) {
|
|
138
|
-
const arr = [];
|
|
139
|
-
const rx = new RegExp(`(?:^|\\b)${esc(p.symbols.pattern)}\\s*${esc(p.symbols.open)}`, "g");
|
|
140
|
-
let out = src;
|
|
141
|
-
while (1) {
|
|
142
|
-
rx.lastIndex = 0; const m = rx.exec(out); if (!m) break;
|
|
143
|
-
const s = m.index, o = m.index + m[0].length - p.symbols.open.length;
|
|
144
|
-
const [mp, em] = extractBlock(p, out, o); let k = em;
|
|
145
|
-
while (k < out.length && /\s/.test(out[k])) k++;
|
|
146
|
-
if (k < out.length && out.substring(k, k + p.symbols.open.length) === p.symbols.open) {
|
|
147
|
-
const [rp, er] = extractBlock(p, out, k);
|
|
148
|
-
arr.push({ m: mp.trim(), r: rp.trim() });
|
|
149
|
-
out = out.slice(0, s) + out.slice(er); continue;
|
|
150
|
-
}
|
|
151
|
-
out = out.slice(0, s) + out.slice(em);
|
|
152
|
-
}
|
|
153
|
-
return [arr, out];
|
|
154
|
-
}
|
|
155
|
-
|
|
156
131
|
function extractNested(p, txt) {
|
|
157
|
-
const
|
|
158
|
-
const rx = new RegExp(`${esc(p.symbols.sigil)}${esc(p.symbols.pattern)}\\s*${esc(p.symbols.open)}`, "g");
|
|
132
|
+
const loc = [], S = p.symbols.sigil, O = p.symbols.open;
|
|
159
133
|
let out = txt;
|
|
134
|
+
const rx = new RegExp(`${esc(S)}${esc(p.symbols.pattern)}\\s*${esc(O)}`, "g");
|
|
160
135
|
while (1) {
|
|
161
136
|
rx.lastIndex = 0; const m = rx.exec(out); if (!m) break;
|
|
162
|
-
const s = m.index, o = m.index + m[0].length -
|
|
137
|
+
const s = m.index, o = m.index + m[0].length - O.length;
|
|
163
138
|
const [mp, em] = extractBlock(p, out, o); let k = em;
|
|
164
139
|
while (k < out.length && /\s/.test(out[k])) k++;
|
|
165
|
-
if (k < out.length && out.substring(k, k +
|
|
140
|
+
if (k < out.length && out.substring(k, k + O.length) === O) {
|
|
166
141
|
const [rp, er] = extractBlock(p, out, k);
|
|
167
|
-
|
|
142
|
+
loc.push({ m: mp.trim(), r: rp.trim() });
|
|
168
143
|
out = out.slice(0, s) + out.slice(er); continue;
|
|
169
144
|
}
|
|
170
145
|
out = out.slice(0, s) + out.slice(em);
|
|
171
146
|
}
|
|
172
|
-
return [
|
|
147
|
+
return [loc, out];
|
|
173
148
|
}
|
|
174
149
|
|
|
175
150
|
function extractEvals(p, txt) {
|
|
176
151
|
const ev = [], S = p.symbols.sigil, O = p.symbols.open;
|
|
177
152
|
let i = 0, out = txt, off = 0;
|
|
178
153
|
while (i < txt.length) {
|
|
179
|
-
if (txt.substring(i, i + S.length) === S
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
154
|
+
if (txt.substring(i, i + S.length) === S) {
|
|
155
|
+
const rest = txt.substring(i + S.length);
|
|
156
|
+
if (rest.startsWith(p.symbols.eval)) {
|
|
157
|
+
let j = i + S.length + p.symbols.eval.length;
|
|
158
|
+
while (j < txt.length && /\s/.test(txt[j])) j++;
|
|
159
|
+
if (j < txt.length && txt.substring(j, j + O.length) === O) {
|
|
160
|
+
const sp = i, bp = j, [c, ep] = extractBlock(p, txt, bp);
|
|
161
|
+
ev.push({ code: c, sp: sp - off, ep: ep - off });
|
|
162
|
+
const ph = `__E${ev.length - 1}__`;
|
|
163
|
+
out = out.substring(0, sp - off) + ph + out.substring(ep - off);
|
|
164
|
+
off += (ep - sp) - ph.length; i = ep; continue;
|
|
165
|
+
}
|
|
189
166
|
}
|
|
190
167
|
}
|
|
191
168
|
i++;
|
|
@@ -206,7 +183,6 @@ function applyEvals(p, txt, ev) {
|
|
|
206
183
|
}
|
|
207
184
|
|
|
208
185
|
function applyPats(p, src, pats) {
|
|
209
|
-
let last = "", S = p.symbols.sigil;
|
|
210
186
|
for (const pat of pats) {
|
|
211
187
|
const tok = parsePattern(p, pat.m);
|
|
212
188
|
let n = '', pos = 0, ok = false;
|
|
@@ -215,16 +191,16 @@ function applyPats(p, src, pats) {
|
|
|
215
191
|
if (m) {
|
|
216
192
|
ok = true;
|
|
217
193
|
let r = pat.r;
|
|
218
|
-
const [
|
|
219
|
-
r =
|
|
220
|
-
|
|
221
|
-
r = r.replace(new RegExp(esc(k) + '(?![A-Za-z0-9_])', 'g'),
|
|
222
|
-
}
|
|
223
|
-
if (
|
|
194
|
+
const [loc, cln] = extractNested(p, r);
|
|
195
|
+
r = cln;
|
|
196
|
+
Object.keys(m.captures).forEach(k => {
|
|
197
|
+
r = r.replace(new RegExp(esc(k) + '(?![A-Za-z0-9_])', 'g'), m.captures[k]);
|
|
198
|
+
});
|
|
199
|
+
if (loc.length) r = applyPats(p, r, loc);
|
|
224
200
|
p.match = src.slice(pos, m.endPos);
|
|
225
201
|
const [ev, ct] = extractEvals(p, r);
|
|
226
202
|
if (ev.length) r = applyEvals(p, ct, ev);
|
|
227
|
-
n += r;
|
|
203
|
+
n += r; pos = m.endPos;
|
|
228
204
|
} else { n += src[pos]; pos++; }
|
|
229
205
|
}
|
|
230
206
|
if (ok) src = n;
|
|
@@ -243,23 +219,28 @@ function unescapeDelim(s) {
|
|
|
243
219
|
}
|
|
244
220
|
|
|
245
221
|
export class Papagaio {
|
|
246
|
-
constructor(sigil = '$', open = '{', close = '}', pattern = 'pattern',
|
|
247
|
-
this.
|
|
248
|
-
this.symbols = { pattern, open, close, sigil, eval: evalKeyword, block: blockKeyword, regex: regexKeyword };
|
|
222
|
+
constructor(sigil = '$', open = '{', close = '}', pattern = 'pattern', evalKw = 'eval', blockKw = 'block', regexKw = 'regex') {
|
|
223
|
+
this.symbols = { pattern, open, close, sigil, eval: evalKw, block: blockKw, regex: regexKw };
|
|
249
224
|
this.content = "";
|
|
250
225
|
this.match = "";
|
|
251
|
-
this._regexCache = new Map();
|
|
252
226
|
}
|
|
253
227
|
process(input) {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
228
|
+
const [loc, cln] = extractNested(this, input);
|
|
229
|
+
const [evals, ph] = extractEvals(this, cln);
|
|
230
|
+
let proc = applyEvals(this, ph, evals);
|
|
231
|
+
if (loc.length === 0) {
|
|
232
|
+
this.content = proc;
|
|
233
|
+
return proc;
|
|
234
|
+
}
|
|
235
|
+
let src = proc, last = null;
|
|
236
|
+
while (src !== last) {
|
|
237
|
+
last = src;
|
|
238
|
+
src = applyPats(this, src, loc);
|
|
239
|
+
const [nested] = extractNested(this, src);
|
|
240
|
+
if (nested.length === 0) break;
|
|
262
241
|
}
|
|
263
|
-
|
|
242
|
+
this.content = src;
|
|
243
|
+
if (typeof this.exit == "function") this.exit();
|
|
244
|
+
return this.content;
|
|
264
245
|
}
|
|
265
246
|
}
|
package/tests/tests.json
CHANGED
|
@@ -3,211 +3,211 @@
|
|
|
3
3
|
{
|
|
4
4
|
"id": 1,
|
|
5
5
|
"name": "Basic variable substitution",
|
|
6
|
-
"code": "pattern {$x $y} {$y, $x}\nhello world",
|
|
6
|
+
"code": "$pattern {$x $y} {$y, $x}\nhello world",
|
|
7
7
|
"expected": "world, hello"
|
|
8
8
|
},
|
|
9
9
|
{
|
|
10
10
|
"id": 2,
|
|
11
11
|
"name": "Flexible whitespace with $",
|
|
12
|
-
"code": "pattern {$x and $y} {$x & $y}\nhello and world",
|
|
12
|
+
"code": "$pattern {$x and $y} {$x & $y}\nhello and world",
|
|
13
13
|
"expected": "hello & world"
|
|
14
14
|
},
|
|
15
15
|
{
|
|
16
16
|
"id": 3,
|
|
17
17
|
"name": "Block with custom delimiters",
|
|
18
|
-
"code": "pattern {$block content {(}{)}} {[$content]}\ndata (hello world)",
|
|
18
|
+
"code": "$pattern {$block content {(}{)}} {[$content]}\ndata (hello world)",
|
|
19
19
|
"expected": "data [hello world]"
|
|
20
20
|
},
|
|
21
21
|
{
|
|
22
22
|
"id": 4,
|
|
23
23
|
"name": "Block with multi-char delimiters",
|
|
24
|
-
"code": "pattern {$block code {<<}{>>}} {CODE[$code]}\n<<console.log()>>",
|
|
24
|
+
"code": "$pattern {$block code {<<}{>>}} {CODE[$code]}\n<<console.log()>>",
|
|
25
25
|
"expected": "CODE[console.log()]"
|
|
26
26
|
},
|
|
27
27
|
{
|
|
28
28
|
"id": 5,
|
|
29
29
|
"name": "Block with nested delimiters",
|
|
30
|
-
"code": "pattern {$block txt {<}{>}} {[$txt]}\nouter <middle <inner> content>",
|
|
30
|
+
"code": "$pattern {$block txt {<}{>}} {[$txt]}\nouter <middle <inner> content>",
|
|
31
31
|
"expected": "outer [middle <inner> content]"
|
|
32
32
|
},
|
|
33
33
|
{
|
|
34
34
|
"id": 6,
|
|
35
35
|
"name": "Eval expression with arithmetic",
|
|
36
|
-
"code": "pattern {sum $a $b} {$eval{return parseInt($a) + parseInt($b)}}\nsum 5 3",
|
|
36
|
+
"code": "$pattern {sum $a $b} {$eval{return parseInt($a) + parseInt($b)}}\nsum 5 3",
|
|
37
37
|
"expected": "8"
|
|
38
38
|
},
|
|
39
39
|
{
|
|
40
40
|
"id": 7,
|
|
41
41
|
"name": "Eval with arithmetic and variable",
|
|
42
|
-
"code": "pattern {multiply $n} {Result: $eval{return $n * 3}}\nmultiply 5",
|
|
42
|
+
"code": "$pattern {multiply $n} {Result: $eval{return $n * 3}}\nmultiply 5",
|
|
43
43
|
"expected": "Result: 15"
|
|
44
44
|
},
|
|
45
45
|
{
|
|
46
46
|
"id": 8,
|
|
47
47
|
"name": "Multiple patterns cascade",
|
|
48
|
-
"code": "pattern {a} {b}\
|
|
48
|
+
"code": "$pattern {a} {b}\n$pattern {b} {c}\na",
|
|
49
49
|
"expected": "c"
|
|
50
50
|
},
|
|
51
51
|
{
|
|
52
52
|
"id": 9,
|
|
53
53
|
"name": "Global pattern matching",
|
|
54
|
-
"code": "pattern {x} {X}\nx y x z x",
|
|
54
|
+
"code": "$pattern {x} {X}\nx y x z x",
|
|
55
55
|
"expected": "X y X z X"
|
|
56
56
|
},
|
|
57
57
|
{
|
|
58
58
|
"id": 10,
|
|
59
59
|
"name": "Pattern with special regex characters",
|
|
60
|
-
"code": "pattern {a.*b} {MATCHED}\na.*b",
|
|
60
|
+
"code": "$pattern {a.*b} {MATCHED}\na.*b",
|
|
61
61
|
"expected": "MATCHED"
|
|
62
62
|
},
|
|
63
63
|
{
|
|
64
64
|
"id": 11,
|
|
65
65
|
"name": "Multiple blocks in one pattern",
|
|
66
|
-
"code": "pattern {$block a {(}{)} and $block b {[}{]}} {$a|$b}\n(first) and [second]",
|
|
66
|
+
"code": "$pattern {$block a {(}{)} and $block b {[}{]}} {$a|$b}\n(first) and [second]",
|
|
67
67
|
"expected": "first|second"
|
|
68
68
|
},
|
|
69
69
|
{
|
|
70
70
|
"id": 12,
|
|
71
71
|
"name": "Variable reuse in replacement",
|
|
72
|
-
"code": "pattern {$x} {$x:$x}\ndata",
|
|
72
|
+
"code": "$pattern {$x} {$x:$x}\ndata",
|
|
73
73
|
"expected": "data:data"
|
|
74
74
|
},
|
|
75
75
|
{
|
|
76
76
|
"id": 13,
|
|
77
77
|
"name": "Literal dollar sign in replacement",
|
|
78
|
-
"code": "pattern {price} {$50}\nprice",
|
|
78
|
+
"code": "$pattern {price} {$50}\nprice",
|
|
79
79
|
"expected": "$50"
|
|
80
80
|
},
|
|
81
81
|
{
|
|
82
82
|
"id": 14,
|
|
83
83
|
"name": "Optional whitespace with $ optional",
|
|
84
|
-
"code": "pattern {hello$world} {HI}\nhello\n\nworld",
|
|
84
|
+
"code": "$pattern {hello$world} {HI}\nhello\n\nworld",
|
|
85
85
|
"expected": "HI"
|
|
86
86
|
},
|
|
87
87
|
{
|
|
88
88
|
"id": 15,
|
|
89
89
|
"name": "Block with angle brackets",
|
|
90
|
-
"code": "pattern {$block inner {<}{>}} {WRAPPED[$inner]}\n<content>",
|
|
90
|
+
"code": "$pattern {$block inner {<}{>}} {WRAPPED[$inner]}\n<content>",
|
|
91
91
|
"expected": "WRAPPED[content]"
|
|
92
92
|
},
|
|
93
93
|
{
|
|
94
94
|
"id": 16,
|
|
95
95
|
"name": "Pattern with literal characters",
|
|
96
|
-
"code": "pattern {Mr. $name} {Hello $name}\nMr. Smith",
|
|
96
|
+
"code": "$pattern {Mr. $name} {Hello $name}\nMr. Smith",
|
|
97
97
|
"expected": "Hello Smith"
|
|
98
98
|
},
|
|
99
99
|
{
|
|
100
100
|
"id": 17,
|
|
101
101
|
"name": "Eval with division and decimals",
|
|
102
|
-
"code": "pattern {div $a $b} {$eval{return $a / $b}}\ndiv 7 2",
|
|
102
|
+
"code": "$pattern {div $a $b} {$eval{return $a / $b}}\ndiv 7 2",
|
|
103
103
|
"expected": "3.5"
|
|
104
104
|
},
|
|
105
105
|
{
|
|
106
106
|
"id": 18,
|
|
107
107
|
"name": "Block capturing with square brackets",
|
|
108
|
-
"code": "pattern {$block arr {[}{]}} {ARRAY[$arr]}\n[1, 2, 3]",
|
|
108
|
+
"code": "$pattern {$block arr {[}{]}} {ARRAY[$arr]}\n[1, 2, 3]",
|
|
109
109
|
"expected": "ARRAY[1, 2, 3]"
|
|
110
110
|
},
|
|
111
111
|
{
|
|
112
112
|
"id": 19,
|
|
113
113
|
"name": "Pattern without whitespace matching",
|
|
114
|
-
"code": "pattern {$a,$b} {$b,$a} one,two",
|
|
114
|
+
"code": "$pattern {$a,$b} {$b,$a} one,two",
|
|
115
115
|
"expected": "two,one"
|
|
116
116
|
},
|
|
117
117
|
{
|
|
118
118
|
"id": 20,
|
|
119
119
|
"name": "Nested pattern in replacement",
|
|
120
|
-
"code": "pattern {$x} {->$x<-}\ntext",
|
|
120
|
+
"code": "$pattern {$x} {->$x<-}\ntext",
|
|
121
121
|
"expected": "->text<-"
|
|
122
122
|
},
|
|
123
123
|
{
|
|
124
124
|
"id": 21,
|
|
125
125
|
"name": "First pattern applies, second doesn't match",
|
|
126
|
-
"code": "pattern {hello} {goodbye}\
|
|
126
|
+
"code": "$pattern {hello} {goodbye}\n$pattern {goodbye} {hi}\nhello",
|
|
127
127
|
"expected": "hi"
|
|
128
128
|
},
|
|
129
129
|
{
|
|
130
130
|
"id": 22,
|
|
131
131
|
"name": "Block delimiter balancing - parentheses",
|
|
132
|
-
"code": "pattern {$block code {(}{)}} {RESULT[$code]}\n(a (b (c) d) e)",
|
|
132
|
+
"code": "$pattern {$block code {(}{)}} {RESULT[$code]}\n(a (b (c) d) e)",
|
|
133
133
|
"expected": "RESULT[a (b (c) d) e]"
|
|
134
134
|
},
|
|
135
135
|
{
|
|
136
136
|
"id": 23,
|
|
137
137
|
"name": "Block delimiter balancing - angle brackets",
|
|
138
|
-
"code": "pattern {$block inner {<}{>}} {X[$inner]}\n<outer <middle <deep>> more>",
|
|
138
|
+
"code": "$pattern {$block inner {<}{>}} {X[$inner]}\n<outer <middle <deep>> more>",
|
|
139
139
|
"expected": "X[outer <middle <deep>> more]"
|
|
140
140
|
},
|
|
141
141
|
{
|
|
142
142
|
"id": 24,
|
|
143
143
|
"name": "Block delimiter balancing - square brackets",
|
|
144
|
-
"code": "pattern {$block data {[}{]}} {DATA[$data]}\n[outer [inner [deep]] more]",
|
|
144
|
+
"code": "$pattern {$block data {[}{]}} {DATA[$data]}\n[outer [inner [deep]] more]",
|
|
145
145
|
"expected": "DATA[outer [inner [deep]] more]"
|
|
146
146
|
},
|
|
147
147
|
{
|
|
148
148
|
"id": 25,
|
|
149
149
|
"name": "Block delimiter balancing - curly braces",
|
|
150
|
-
"code": "pattern {$block obj {}{}} {OBJ[$obj]}\n{key: {nested: {value}}}",
|
|
150
|
+
"code": "$pattern {$block obj {}{}} {OBJ[$obj]}\n{key: {nested: {value}}}",
|
|
151
151
|
"expected": "OBJ[key: {nested: {value}}]"
|
|
152
152
|
},
|
|
153
153
|
{
|
|
154
154
|
"id": 26,
|
|
155
155
|
"name": "Multiple nested delimiters in same pattern",
|
|
156
|
-
"code": "pattern {$block a {(}{)} $block b {[}{]}} {[$a|$b]}\n(test1) [test2]",
|
|
156
|
+
"code": "$pattern {$block a {(}{)} $block b {[}{]}} {[$a|$b]}\n(test1) [test2]",
|
|
157
157
|
"expected": "[test1|test2]"
|
|
158
158
|
},
|
|
159
159
|
{
|
|
160
160
|
"id": 27,
|
|
161
161
|
"name": "Simple pattern cascade",
|
|
162
|
-
"code": "pattern {START $x END} {RESULT[$x]}\nSTART data END",
|
|
162
|
+
"code": "$pattern {START $x END} {RESULT[$x]}\nSTART data END",
|
|
163
163
|
"expected": "RESULT[data]"
|
|
164
164
|
},
|
|
165
165
|
{
|
|
166
166
|
"id": 28,
|
|
167
167
|
"name": "Block with quotes as delimiters",
|
|
168
|
-
"code": "pattern {$block str {\"}{\"}} {STRING[$str]}\n\"hello world\"",
|
|
168
|
+
"code": "$pattern {$block str {\"}{\"}} {STRING[$str]}\n\"hello world\"",
|
|
169
169
|
"expected": "STRING[hello world]"
|
|
170
170
|
},
|
|
171
171
|
{
|
|
172
172
|
"id": 29,
|
|
173
173
|
"name": "Block with pipe delimiters",
|
|
174
|
-
"code": "pattern {$block val {|}{|}} {PIPE[$val]}\n|content here|",
|
|
174
|
+
"code": "$pattern {$block val {|}{|}} {PIPE[$val]}\n|content here|",
|
|
175
175
|
"expected": "PIPE[content here]"
|
|
176
176
|
},
|
|
177
177
|
{
|
|
178
178
|
"id": 30,
|
|
179
179
|
"name": "Deeply nested block delimiters",
|
|
180
|
-
"code": "pattern {$block deep {<}{>}} {[$deep]}\n<a <b <c <d> c> b> a>",
|
|
180
|
+
"code": "$pattern {$block deep {<}{>}} {[$deep]}\n<a <b <c <d> c> b> a>",
|
|
181
181
|
"expected": "[a <b <c <d> c> b> a]"
|
|
182
182
|
},
|
|
183
183
|
{
|
|
184
184
|
"id": 31,
|
|
185
185
|
"name": "Two sequential patterns",
|
|
186
|
-
"code": "pattern {$block content {(}{)}} {BLOCK [$content]}\
|
|
186
|
+
"code": "$pattern {$block content {(}{)}} {BLOCK [$content]}\n$pattern {BLOCK $x} {RESULT: $x}\n(inner data)",
|
|
187
187
|
"expected": "RESULT: [inner data]"
|
|
188
188
|
},
|
|
189
189
|
{
|
|
190
190
|
"id": 32,
|
|
191
191
|
"name": "Multiple eval operations in sequence",
|
|
192
|
-
"code": "pattern {calc $x} {$eval{return parseInt($x) * 2}}\ncalc 3",
|
|
192
|
+
"code": "$pattern {calc $x} {$eval{return parseInt($x) * 2}}\ncalc 3",
|
|
193
193
|
"expected": "6"
|
|
194
194
|
},
|
|
195
195
|
{
|
|
196
196
|
"id": 33,
|
|
197
197
|
"name": "Block delimiter edge case - empty content",
|
|
198
|
-
"code": "pattern {$block empty {(}{)}} {EMPTY[$empty]}\n()",
|
|
198
|
+
"code": "$pattern {$block empty {(}{)}} {EMPTY[$empty]}\n()",
|
|
199
199
|
"expected": "EMPTY[]"
|
|
200
200
|
},
|
|
201
201
|
{
|
|
202
202
|
"id": 34,
|
|
203
203
|
"name": "Multi-char delimiters with nesting",
|
|
204
|
-
"code": "pattern {$block code {<<}{>>}} {CODE[$code]}\n<<outer <<inner>> more>>",
|
|
204
|
+
"code": "$pattern {$block code {<<}{>>}} {CODE[$code]}\n<<outer <<inner>> more>>",
|
|
205
205
|
"expected": "CODE[outer <<inner>> more]"
|
|
206
206
|
},
|
|
207
207
|
{
|
|
208
208
|
"id": 35,
|
|
209
209
|
"name": "Cascading pattern transformations",
|
|
210
|
-
"code": "pattern {START $x END} {STEP1[$x]}\
|
|
210
|
+
"code": "$pattern {START $x END} {STEP1[$x]}\n$pattern {STEP1 $y} {STEP2[$y]}\n$pattern {STEP2 $z} {FINAL[$z]}\nSTART test END",
|
|
211
211
|
"expected": "FINAL[[[test]]]"
|
|
212
212
|
}
|
|
213
213
|
]
|