Skip to content

Commit

Permalink
expression parser: skip contents of <del> tags (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot authored Oct 17, 2022
1 parent 9994777 commit 034a432
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
25 changes: 24 additions & 1 deletion src/expr-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ function emptyThingHasNewline(s: Seq) {
);
}

function getTagName(tok: ProsePart): 'open-del' | 'close-del' | null {
if (tok.name !== 'tag') {
return null;
}
const lowcase = tok.contents.toLowerCase();
if (lowcase.startsWith('<del>') || lowcase.startsWith('<del ')) {
return 'open-del';
} else if (lowcase.startsWith('</del>') || lowcase.startsWith('</del ')) {
return 'close-del';
} else {
return null;
}
}

class ExprParser {
declare src: FragmentNode[];
declare opNames: Set<String>;
Expand Down Expand Up @@ -218,6 +232,15 @@ class ExprParser {
}
++this.srcIndex;
this.textTokOffset = null;
// skip anything in `<del>`
if (getTagName(tok) === 'open-del') {
while (
this.srcIndex < this.src.length &&
getTagName(this.src[this.srcIndex]) !== 'close-del'
) {
++this.srcIndex;
}
}
continue;
}
const { groups } = match;
Expand Down Expand Up @@ -302,7 +325,7 @@ class ExprParser {
return { type: 'seq', items };
}
case 'eof': {
if (items.length === 0 || !close.includes('eof')) {
if (!close.includes('eof')) {
throw new ParseFailure(`unexpected eof (expected ${formatClose(close)})`, next.offset);
}
return { type: 'seq', items };
Expand Down
65 changes: 64 additions & 1 deletion test/expr-parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
'use strict';

let { assertLint, positioned, lintLocationMarker: M, assertLintFree } = require('./utils.js');
let {
assertLint,
positioned,
lintLocationMarker: M,
assertLintFree,
getBiblio,
} = require('./utils.js');

describe('expression parsing', () => {
describe('valid', () => {
Expand Down Expand Up @@ -286,4 +292,61 @@ describe('expression parsing', () => {
);
});
});

describe('handling of <del>', () => {
let biblio;
before(async () => {
biblio = await getBiblio(`
<emu-clause id="del-complex" type="abstract operation">
<h1>
Example (
_x_: unknown,
_y_: unknown,
): unknown
</h1>
<dl class="header"></dl>
</emu-clause>
`);
});

it('ignores contents of <del> tags', async () => {
await assertLint(
positioned`
<emu-alg>
1. Perform ${M}Example(x<del>, y</del>).
</emu-alg>
`,
{
ruleId: 'typecheck',
nodeType: 'emu-alg',
message: 'Example takes 2 arguments, but this invocation passes 1',
},
{
extraBiblios: [biblio],
}
);

await assertLintFree(
`
<emu-alg>
1. Perform Example(x, y<del>, z</del>).
</emu-alg>
`,
{
extraBiblios: [biblio],
}
);

await assertLintFree(
`
<emu-alg>
1. <del>Perform Example(x, y, z).</del>
</emu-alg>
`,
{
extraBiblios: [biblio],
}
);
});
});
});

0 comments on commit 034a432

Please sign in to comment.