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

Fix 'oneof' in syntax-directed operations #637

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/lint/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export function rhsMatches(a: RightHandSide | OneOfList, b: RightHandSide | OneO
}
return symbolSpanMatches(aHead, bHead);
}
case SyntaxKind.OneOfList:
return oneOfListMatches(a, b as OneOfList);
default:
// @ts-expect-error: Callers might pass other kinds of nodes.
throw new Error('unknown rhs type ' + a.constructor.name);
}
}
Expand Down Expand Up @@ -171,6 +174,14 @@ function argumentListMatches(a: ArgumentList, b: ArgumentList) {
);
}

function oneOfListMatches(a: OneOfList, b: OneOfList) {
if (a.terminals === undefined || b.terminals === undefined) {
throw new Error('OneOfList must have terminals');
}
// The terminals in a must be a subset of the terminals in b.
return a.terminals.every(ae => b.terminals!.find(be => ae.text === be.text));
}

// this is only for use with single-file grammars
export function getLocationInGrammarFile(file: SourceFile, pos: number) {
const posWithoutWhitespace = skipTrivia(file.text, pos, file.text.length);
Expand Down
69 changes: 69 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,31 @@ ${M} </pre>
);
});

it('unknown oneof', async () => {
await assertError(
positioned`
<emu-grammar type="definition">
Foo :: one of \`a\` \`b\` \`c\`
</emu-grammar>

<emu-clause id="sec-example" type="sdo">
<h1>Static Semantics: Example</h1>
<dl class='header'></dl>
<emu-grammar>
Foo :: ${M}one of \`d\` \`e\`
</emu-grammar>
<emu-alg>
1. Return *true*.
</emu-alg>
</emu-clause>`,
{
ruleId: 'grammar-shape',
nodeType: 'emu-grammar',
message: 'could not find definition for rhs "d e"',
},
);
});

it('negative', async () => {
await assertErrorFree(`
<emu-grammar type="definition">
Expand Down Expand Up @@ -1167,5 +1192,49 @@ ${M} </pre>
},
);
});

it('negative: oneof', async () => {
await assertErrorFree(`
<emu-grammar type="definition">
Foo :: one of \`a\` \`b\` \`c\`
</emu-grammar>

<emu-clause id="sec-example" type="sdo">
<h1>Static Semantics: Example</h1>
<dl class='header'></dl>
<emu-grammar>
Foo :: one of \`a\` \`b\` \`c\`
</emu-grammar>
<emu-alg>
1. Return *true*.
</emu-alg>
</emu-clause>
`);
});

it('negative: oneof (subset)', async () => {
await assertErrorFree(`
<emu-grammar type="definition">
Foo :: one of \`a\` \`b\` \`c\` \`d\`
</emu-grammar>

<emu-clause id="sec-example" type="sdo">
<h1>Static Semantics: Example</h1>
<dl class='header'></dl>
<emu-grammar>
Foo :: one of \`a\` \`b\`
</emu-grammar>
<emu-alg>
1. Return *true*.
</emu-alg>
<emu-grammar>
Foo :: one of \`c\` \`d\`
</emu-grammar>
<emu-alg>
1. Return *false*.
</emu-alg>
</emu-clause>
`);
});
});
});