papagaio 0.38.2 → 0.41.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 CHANGED
@@ -36,7 +36,6 @@ Patterns are composed of whitespace-separated tokens. The engine uses a "flex-ma
36
36
  - **Literal**: Matches exact text.
37
37
  - **Variable**: `$name` (captures a sequence up to the next pattern match).
38
38
  - **Optional**: `$name?` or `literal?` (marker is configurable, e.g., `MAYBE`, via `$changesymbols`).
39
- - **Escaping**: Use `$$` to match a literal `$`.
40
39
 
41
40
  ### Modifiers
42
41
  Modifiers specify the data type or constraints of a match:
@@ -147,6 +146,56 @@ This changes the sigil to `@`, delimiters to `< >`, and the optional marker to `
147
146
 
148
147
  ---
149
148
 
149
+ ## Dynamic Built-in Operators
150
+
151
+ Papagaio provides several built-in operators that give you access to the engine's internal syntax configuration and allow you to precisely inject unrepresentable characters (such as whitespace and binary ASCII) directly into patterns or output text.
152
+
153
+ > [!NOTE]
154
+ > All built-in operators support the **trailing sigil** syntax (e.g., `$sigil$`). This allows the operator to immediately consume all following whitespace or safely concatenate with adjacent alphanumeric text, exactly like standard variables.
155
+
156
+ ### Reserved Symbol Emitters
157
+ When generating macros or writing complex rules, you may need to emit a literal sigil without it being evaluated. Instead of complex double-escaping, you can use:
158
+ - **`$sigil`**: Emits the current sigil (e.g., `$`)
159
+ - **`$open`**: Emits the current open delimiter (e.g., `{`)
160
+ - **`$close`**: Emits the current close delimiter (e.g., `}`)
161
+ - **`$marker`**: Emits the current optional marker (e.g., `?`)
162
+
163
+ **Solving the Infinite Interpretation Problem:**
164
+ ```text
165
+ $pattern {hello} {world $sigil$A$sigil$from{X}}
166
+ hello
167
+ ```
168
+ *Output: `world $A$from{X}`* — Since `$sigil` is processed during the single initial pass, it emits a literal `$` that safely bypasses any subsequent evaluation.
169
+
170
+ ### Formatting and Binary Control
171
+ For precise layout control, especially inside flex-matched `$pattern` rules that normally skip extra whitespace:
172
+ - **`$space`**: Emits a literal space character (`' '`)
173
+ - **`$newline`**: Emits a literal newline (`\n`)
174
+ - **`$tab`**: Emits a literal tab character (`\t`)
175
+
176
+ To generate specific binary characters (such as null bytes) or handle complex ASCII injection:
177
+ - **`$ascii$code`** (Inline): E.g., `$ascii$65` outputs `A`
178
+ - **`$ascii{code}`** (Block): E.g., `$ascii{0}` outputs a binary null byte (`\0`)
179
+
180
+ ### Mathematical Evaluation
181
+ The **`$math{...}`** operator allows native numerical processing and comparison directly on the preprocessor pipeline. Powered internally by `tinyexpr`, it supports floating point arithmetic, trigonometric functions, exponents, and logical evaluations. The mathematical string is processed recursively by Papagaio before being evaluated, meaning you can easily inject your workflow variables.
182
+
183
+ ```text
184
+ $X$from{5.5}
185
+ $Y$from{4.5}
186
+ $math{sqrt($X^2 + $Y^2 - 0.5)}
187
+ ```
188
+ *Output: `7.07107`*
189
+
190
+ **Comparisons and Logic Gates**
191
+ Since comparison operators (`<`, `>`, `==`, `!=`) evaluate to `1` (true) or `0` (false), `$math` is perfect for chained conditionals when paired with Papagaio's flow controllers:
192
+ ```text
193
+ $math{10 > 5}$compare{1}$then{ Math confirms 10 is greater than 5! }
194
+ ```
195
+ *(Note: If the expression contains syntax errors like `$math{5 + *}`, the operator fails silently and emits an empty string to maintain engine stability).*
196
+
197
+ ---
198
+
150
199
  ## Recursive Priority System
151
200
 
152
201
  Papagaio allows you to control the order of execution and side-effects (such as pattern definitions) using the **`$priority$N`** directive.
@@ -178,6 +227,14 @@ The **`$from`** operator allows you to capture processed content and assign it t
178
227
  2. **Immediate Registration**: The variable is registered as an exact-match rule as soon as it is parsed. This allows for **chained assignments**.
179
228
  3. **Output Suppression**: The entire `$from` directive is removed from the output text.
180
229
 
230
+ ### Lexical Scopes and Sandboxing
231
+
232
+ Every block operator or preprocessing field evaluated dynamically (such as inside `$from`, `$then`, `$else`, `$while`, `$repeat`, and `$until`) is executed in its own **local nested scope**.
233
+
234
+ - **Sandboxing**: Any variable declared inside a local scope (e.g. `$B$from{local_val}` inside `$A$from{...}`) that does *not* exist in a parent scope is treated as a local variable. It will be completely destroyed and freed when the block finishes evaluating (sandboxed).
235
+ - **Shadowing & Upward Updates**: If a variable updated inside a local scope already exists in a parent/ancestor scope, Papagaio avoids shadowing it. Instead, it propagates the update upwards, modifying the existing variable in the parent scope.
236
+ - **Recursive Nesting**: Local scopes can be nested arbitrarily. Each child scope has full read/write access to variables in parent scopes, but parent or sibling scopes do not have access to variables declared exclusively in child scopes.
237
+
181
238
  ### Examples
182
239
 
183
240
  #### Chained Assignments
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "papagaio",
3
- "version": "0.38.2",
3
+ "version": "0.41.1",
4
4
  "description": "easy yet powerful preprocessor",
5
5
  "main": "dist/wasm/papagaio.js",
6
6
  "bin": {
@@ -26,7 +26,6 @@
26
26
  "eval",
27
27
  "parser",
28
28
  "codegen",
29
- "regex",
30
29
  "syntax",
31
30
  "easy"
32
31
  ],