Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression parser: skip contents of <del> tags #494

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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],
}
);
});
});
});