Skip to content

Commit 84e3e8c

Browse files
committed
wasm: Refactor emitCaseInsensitiveTerminal into separate method
1 parent 29f2923 commit 84e3e8c

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

packages/wasm/src/index.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,16 +1633,15 @@ export class Compiler {
16331633
);
16341634
}
16351635

1636-
emitTerminal({value, caseInsensitive}) {
1636+
emitCaseInsensitiveTerminal({value}) {
16371637
const {asm} = this;
1638-
16391638
assert(
1640-
!caseInsensitive || [...value].every(c => c <= '\x7f'),
1639+
[...value].every(c => c <= '\x7f'),
16411640
'not supported: case-insensitive Unicode',
16421641
);
16431642

1644-
const str = caseInsensitive ? value.toLowerCase() : value;
1645-
asm.emit(JSON.stringify(str));
1643+
const str = value.toLowerCase();
1644+
asm.emit(JSON.stringify(`caseInsensitive:${value}`));
16461645
this.wrapTerminalLike(() => {
16471646
// TODO:
16481647
// - proper UTF-8!
@@ -1651,7 +1650,7 @@ export class Compiler {
16511650
for (const c of [...str]) {
16521651
asm.i32Const(c.charCodeAt(0));
16531652
asm.currCharCode();
1654-
if (caseInsensitive && /[a-zA-Z]/.test(c)) {
1653+
if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) {
16551654
// Cute trick: the diff between upper and lower case is bit 5.
16561655
asm.i32Const(0x20);
16571656
asm.emit(instr.i32.or);
@@ -1664,13 +1663,34 @@ export class Compiler {
16641663

16651664
// Case-insensitive terminals are inlined, but should appear in the CST
16661665
// as if they are actual applications.
1667-
if (caseInsensitive) {
1668-
asm.localGet('ret');
1669-
asm.if(w.blocktype.empty, () => {
1670-
asm.newCaseInsensitiveNode(this.ruleId('caseInsensitive'));
1671-
asm.localSet('ret');
1672-
});
1666+
asm.localGet('ret');
1667+
asm.if(w.blocktype.empty, () => {
1668+
asm.newCaseInsensitiveNode(this.ruleId('caseInsensitive'));
1669+
asm.localSet('ret');
1670+
});
1671+
}
1672+
1673+
emitTerminal(exp) {
1674+
if (exp.caseInsensitive) {
1675+
this.emitCaseInsensitiveTerminal(exp);
1676+
return;
16731677
}
1678+
1679+
const {asm} = this;
1680+
asm.emit(JSON.stringify(exp.value));
1681+
this.wrapTerminalLike(() => {
1682+
// TODO:
1683+
// - proper UTF-8!
1684+
// - handle longer terminals with a loop
1685+
// - SIMD
1686+
for (const c of [...exp.value]) {
1687+
asm.i32Const(c.charCodeAt(0));
1688+
asm.currCharCode();
1689+
asm.emit(instr.i32.ne);
1690+
asm.condBreak(asm.depthOf('failure'));
1691+
asm.incPos();
1692+
}
1693+
});
16741694
}
16751695

16761696
emitUnicodeChar(exp) {

0 commit comments

Comments
 (0)