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

Merged
merged 5 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 10 additions & 2 deletions src/lint/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export function rhsMatches(a: RightHandSide | OneOfList, b: RightHandSide | OneO
}
return symbolSpanMatches(aHead, bHead);
}
default:
throw new Error('unknown rhs type ' + a.constructor.name);
case SyntaxKind.OneOfList:
return oneOfListMatches(a, b as OneOfList);
}
}

Expand Down Expand Up @@ -171,6 +171,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\`
</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"',
},
);
});

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\`
</emu-grammar>

<emu-clause id="sec-example" type="sdo">
<h1>Static Semantics: Example</h1>
<dl class='header'></dl>
<emu-grammar>
Foo :: one of \`a\`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should read

Suggested change
Foo :: one of \`a\`
Foo :: \`a\`

We should prohibit "one of" with only one alternative.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the test to Foo :: one of `a` `b` instead. Allowing Foo :: `a` should probably be done in a separate PR since it requires changing rhsMatches to compare different kinds of productions. Also disallowing Foo :: one of `a` should probably be done in the linter in a separate PR.

</emu-grammar>
<emu-alg>
1. Return *true*.
</emu-alg>
<emu-grammar>
Foo :: one of \`b\`
</emu-grammar>
<emu-alg>
1. Return *false*.
</emu-alg>
</emu-clause>
`);
});
});
});