papagaio 0.5.2 → 0.6.2
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.mjs +153 -0
- 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/bin/cli.js +0 -34
- package/bin/cli.qjs +0 -57
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.mjs
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Detecta o runtime
|
|
3
|
+
const isQuickJS = typeof scriptArgs !== 'undefined';
|
|
4
|
+
const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
|
|
5
|
+
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// MAIN FUNCTION
|
|
8
|
+
// ============================================================================
|
|
9
|
+
async function main() {
|
|
10
|
+
// ============================================================================
|
|
11
|
+
// IMPORTS - Branch por runtime
|
|
12
|
+
// ============================================================================
|
|
13
|
+
let Papagaio, std, os, fs, pkg;
|
|
14
|
+
|
|
15
|
+
if (isQuickJS) {
|
|
16
|
+
// QuickJS imports
|
|
17
|
+
const stdModule = await import("std");
|
|
18
|
+
const osModule = await import("os");
|
|
19
|
+
std = stdModule;
|
|
20
|
+
os = osModule;
|
|
21
|
+
const { Papagaio: P } = await import("../src/papagaio.js");
|
|
22
|
+
Papagaio = P;
|
|
23
|
+
} else {
|
|
24
|
+
// Node.js imports
|
|
25
|
+
const fsModule = await import("fs");
|
|
26
|
+
fs = fsModule.default;
|
|
27
|
+
|
|
28
|
+
// Load package.json usando fs ao invés de require
|
|
29
|
+
const pkgPath = new URL("../package.json", import.meta.url);
|
|
30
|
+
const pkgContent = fs.readFileSync(pkgPath, "utf8");
|
|
31
|
+
pkg = JSON.parse(pkgContent);
|
|
32
|
+
|
|
33
|
+
const { Papagaio: P } = await import("../src/papagaio.js");
|
|
34
|
+
Papagaio = P;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ============================================================================
|
|
38
|
+
// ABSTRAÇÃO DE CONSOLE/STD
|
|
39
|
+
// ============================================================================
|
|
40
|
+
const output = {
|
|
41
|
+
log: isQuickJS ? (msg) => std.out.puts(msg + "\n") : console.log,
|
|
42
|
+
error: isQuickJS ? (msg) => std.err.puts(msg + "\n") : console.error,
|
|
43
|
+
exit: isQuickJS ? std.exit : process.exit
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// ============================================================================
|
|
47
|
+
// PARSE ARGUMENTS
|
|
48
|
+
// ============================================================================
|
|
49
|
+
const args = isQuickJS ? scriptArgs.slice(1) : process.argv.slice(2);
|
|
50
|
+
const VERSION = isQuickJS ? "0.6.0" : pkg.version;
|
|
51
|
+
|
|
52
|
+
// Help & Version
|
|
53
|
+
if (args.includes("-v") || args.includes("--version")) {
|
|
54
|
+
output.log(VERSION);
|
|
55
|
+
output.exit(0);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (args.includes("-h") || args.includes("--help")) {
|
|
59
|
+
output.log(`Usage: papagaio [options] <file1> [file2] [...]
|
|
60
|
+
|
|
61
|
+
Options:
|
|
62
|
+
-h, --help Show this help message
|
|
63
|
+
-v, --version Show version number
|
|
64
|
+
--sigil <symbol> Set sigil symbol
|
|
65
|
+
--open <symbol> Set open symbol
|
|
66
|
+
--close <symbol> Set close symbol
|
|
67
|
+
|
|
68
|
+
Examples:
|
|
69
|
+
papagaio input.txt
|
|
70
|
+
papagaio file1.txt file2.txt file3.txt
|
|
71
|
+
papagaio *.txt
|
|
72
|
+
papagaio --sigil @ --open [ --close ] input.txt`);
|
|
73
|
+
output.exit(0);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Parse options
|
|
77
|
+
const sigilIndex = args.findIndex(arg => arg === "--sigil");
|
|
78
|
+
const openIndex = args.findIndex(arg => arg === "--open");
|
|
79
|
+
const closeIndex = args.findIndex(arg => arg === "--close");
|
|
80
|
+
|
|
81
|
+
const sigil = sigilIndex !== -1 ? args[sigilIndex + 1] : undefined;
|
|
82
|
+
const open = openIndex !== -1 ? args[openIndex + 1] : undefined;
|
|
83
|
+
const close = closeIndex !== -1 ? args[closeIndex + 1] : undefined;
|
|
84
|
+
|
|
85
|
+
// Get input files
|
|
86
|
+
const files = args.filter((arg, i) => {
|
|
87
|
+
if (arg.startsWith("-")) return false;
|
|
88
|
+
if (i > 0 && (args[i - 1] === "--sigil" || args[i - 1] === "--open" || args[i - 1] === "--close")) return false;
|
|
89
|
+
return true;
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (files.length === 0) {
|
|
93
|
+
output.error("Error: no input file specified.\nUse --help for usage.");
|
|
94
|
+
output.exit(1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ============================================================================
|
|
98
|
+
// FILE READING ABSTRACTION
|
|
99
|
+
// ============================================================================
|
|
100
|
+
function readFile(filepath) {
|
|
101
|
+
if (isQuickJS) {
|
|
102
|
+
const f = std.open(filepath, "r");
|
|
103
|
+
if (!f) {
|
|
104
|
+
throw new Error(`cannot open file '${filepath}'`);
|
|
105
|
+
}
|
|
106
|
+
const content = f.readAsString();
|
|
107
|
+
f.close();
|
|
108
|
+
return content;
|
|
109
|
+
} else {
|
|
110
|
+
if (!fs.existsSync(filepath)) {
|
|
111
|
+
throw new Error(`file not found: ${filepath}`);
|
|
112
|
+
}
|
|
113
|
+
return fs.readFileSync(filepath, "utf8");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ============================================================================
|
|
118
|
+
// READ AND CONCATENATE FILES
|
|
119
|
+
// ============================================================================
|
|
120
|
+
let concatenatedSrc = "";
|
|
121
|
+
let hasErrors = false;
|
|
122
|
+
|
|
123
|
+
for (const file of files) {
|
|
124
|
+
try {
|
|
125
|
+
const src = readFile(file);
|
|
126
|
+
concatenatedSrc += src;
|
|
127
|
+
} catch (error) {
|
|
128
|
+
output.error(`Error reading ${file}: ${error.message || error}`);
|
|
129
|
+
hasErrors = true;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (hasErrors) {
|
|
134
|
+
output.exit(1);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ============================================================================
|
|
138
|
+
// PROCESS CONCATENATED INPUT
|
|
139
|
+
// ============================================================================
|
|
140
|
+
const p = new Papagaio(sigil, open, close);
|
|
141
|
+
const out = p.process(concatenatedSrc);
|
|
142
|
+
output.log(out);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Executa main
|
|
146
|
+
main().catch(err => {
|
|
147
|
+
const output = isQuickJS
|
|
148
|
+
? (msg) => std.err.puts(msg + "\n")
|
|
149
|
+
: console.error;
|
|
150
|
+
output("Fatal error: " + (err.message || err));
|
|
151
|
+
const exit = isQuickJS ? std.exit : process.exit;
|
|
152
|
+
exit(1);
|
|
153
|
+
});
|
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>
|