Skip to content

Commit

Permalink
Demo: printAst
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Oct 25, 2024
1 parent b277ad9 commit ecb7eb0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ Sets the JavaScript language version for generated patterns and flags. Later tar

*Default: `'ES2024'`.*

## Unicode, mixed case sensitivity
## Unicode, mixed case-sensitivity

Oniguruma-To-ES fully supports mixed case sensitivity (and handles the Unicode edge cases) regardless of JavaScript [target](#target). It also restricts Unicode properties to those supported by Oniguruma and the target JavaScript version.
Oniguruma-To-ES fully supports mixed case-sensitivity (and handles the Unicode edge cases) regardless of JavaScript [target](#target). It also restricts Unicode properties to those supported by Oniguruma and the target JavaScript version.

Oniguruma-To-ES focuses on being lightweight to make it better for use in browsers. This is partly achieved by not including heavyweight Unicode character data, which imposes a couple of minor/rare restrictions:

- Character class intersection and nested negated classes are unsupported with target `ES2018`. Use target `ES2024` or later if you need support for these Oniguruma features.
- A handful of Unicode properties that target a specific character case (ex: `\p{Lower}`) can't be used case-insensitively in patterns that contain other characters with a specific case that are used case-sensitively.
- In other words, almost every usage is fine, inluding `A\p{Lower}`, `(?i:A\p{Lower})`, `(?i:A)\p{Lower}`, `(?i:A(?-i:\p{Lower}))`, and `\w(?i:\p{Lower})`, but not `A(?i:\p{Lower})`.
- Using these properties case-insensitively is basically never done intentionally, so you're unlikely to encounter this error unless it's actually catching a mistake.
- Using these properties case-insensitively is basically never done intentionally, so you're unlikely to encounter this error unless it's catching a mistake.

## Similar projects

Expand Down
33 changes: 32 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,43 @@ <h2>Try it</h2>
</p>
</details>
<pre id="output"></pre>
<p><small>The output shows the result of calling <code>toRegExp</code>. Oniguruma-To-ES includes functions to generate additional formats: <code>compile</code> (returns an object with <code>pattern</code> and <code>flags</code> strings), <code>toOnigurumaAst</code>, and <code>toRegexAst</code> (for an AST based on <a href="https://github.com/slevithan/regex"><code>regex</code></a>). You can run all of these from the console on this page. <code>compile</code> and <code>toRegExp</code> accept pattern and flags strings and an options object. <code>toOnigurumaAst</code> and <code>toRegexAst</code> accept a pattern and flags.</small></p>
<p><small>The output shows the result of calling <code>toRegExp</code>. Oniguruma-To-ES includes functions to generate additional formats: <code>compile</code> (returns an object with <code>pattern</code> and <code>flags</code> strings), <code>toOnigurumaAst</code>, and <code>toRegexAst</code> (for an AST based on <a href="https://github.com/slevithan/regex"><code>regex</code></a>). You can run all of these from the console on this page. <code>compile</code> and <code>toRegExp</code> accept pattern and flags strings and an options object. <code>toOnigurumaAst</code> and <code>toRegexAst</code> accept a pattern and flags. You can also pass ASTs to <code>printAst</code>.</small></p>
</main>

<script src="../dist/index.min.js"></script>
<script>
Object.assign(globalThis, OnigurumaToES);

function printAst(ast) {
if (ast?.type !== 'Regex') {
throw new Error('Oniguruma or `regex` AST expected');
}
const isObject = value => ({}).toString.call(value) === '[object Object]';
const nodeIds = new Map();
let counter = 0;
console.log(JSON.stringify(ast, function (key, value) {
if (isObject(this) && !nodeIds.get(this) && key !== '') {
nodeIds.set(this, ++counter);
}
if (key === 'type') {
return `${value} [${nodeIds.get(this)}]`;
}
if (key === 'parent') {
let parentId = null;
if (isObject(value)) {
if (!nodeIds.has(value)) {
nodeIds.set(value, ++counter);
}
parentId = nodeIds.get(value);
}
return value ? `[${value.type}:${parentId}]` : value;
}
if (key === 'max' && value === Infinity) {
return '[Infinity]';
}
return value;
}, '│ '));
}
</script>
<script src="./demo.js"></script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ function generate(ast, options) {
}

const result = gen(ast);
// By default, `regex` implicitly chooses flag u or v; control it instead
if (!minTargetEs2024) {
// Switch from flag v to u. By default, `regex` implicitly chooses; control it instead
delete result.options.force.v;
result.options.disable.v = true;
result.options.unicodeSetsPlugin = null;
Expand Down

0 comments on commit ecb7eb0

Please sign in to comment.