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 1 commit
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
14 changes: 12 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,16 @@ 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');
}
return (
a.terminals.length === b.terminals.length &&
a.terminals.every((ae, i) => ae.text === b.terminals![i].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
44 changes: 44 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 \`a\`
</emu-grammar>
<emu-alg>
1. Return *true*.
</emu-alg>
</emu-clause>`,
{
ruleId: 'grammar-shape',
nodeType: 'emu-grammar',
message: 'could not find definition for rhs "a"',
},
);
});

it('negative', async () => {
await assertErrorFree(`
<emu-grammar type="definition">
Expand Down Expand Up @@ -1167,5 +1192,24 @@ ${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>
`);
});
});
});