Skip to content

Commit

Permalink
chore: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pdubroy committed Aug 26, 2023
1 parent 7e519af commit 39ccead
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 94 deletions.
59 changes: 26 additions & 33 deletions examples/ecmascript/scripts/extract-grammarkdown.mjs
Original file line number Diff line number Diff line change
@@ -1,63 +1,56 @@
import { parseArgs } from 'util'
import jsdom from 'jsdom'
import fs from 'fs'
const { JSDOM } = jsdom
import {parseArgs} from 'util';
import jsdom from 'jsdom';
import fs from 'fs';
const {JSDOM} = jsdom;

const USAGE =
`Usage: extract-markdown.mjs <grammar_file>
const USAGE = `Usage: extract-markdown.mjs <grammar_file>
Options:
-h --help Print this message.
`

const extractProductions = (specText) => {
`;

const extractProductions = specText => {
const dom = new JSDOM(specText, {
includeNodeLocations: true,
})
includeNodeLocations: true
});

const emuNodes = dom.window.document.querySelectorAll('emu-grammar[type=definition]')
const emuNodes = dom.window.document.querySelectorAll('emu-grammar[type=definition]');

// Older versions of the grammars prior to ES2018 didn't use the
// `type="definition"` attribute on <emu-grammar> elements, so we use
// the less specific selector & manually remove extraneous productions
if (emuNodes.length === 0) {
emuNodes = dom.window.document.querySelectorAll('emu-grammar')
emuNodes = dom.window.document.querySelectorAll('emu-grammar');
}

const grammarkdownText = Array.from(emuNodes, (e) => {
return [`@line ${dom.nodeLocation(e).startLine}`, e.textContent].join('\n')
}).join('\n')
const grammarkdownText = Array.from(emuNodes, e => {
return [`@line ${dom.nodeLocation(e).startLine}`, e.textContent].join('\n');
}).join('\n');

return grammarkdownText
}
return grammarkdownText;
};

const main = () => {
const parseConfig = {
allowPositionals: true,
options: {
help: {
short: 'h',
type: 'boolean',
type: 'boolean'
}
}
}
};

const args = parseArgs(parseConfig)
const args = parseArgs(parseConfig);

if (args.values.help || args.positionals.length < 1) {
console.error(USAGE)
process.exit(!args.values.help)
console.error(USAGE);
process.exit(!args.values.help);
}

const spec = fs.readFileSync(args.positionals[0], {encoding: 'utf-8'})
const grammarText = extractProductions(spec)
console.log(grammarText)
}

main()




const spec = fs.readFileSync(args.positionals[0], {encoding: 'utf-8'});
const grammarText = extractProductions(spec);
console.log(grammarText);
};

main();
46 changes: 27 additions & 19 deletions examples/ecmascript/scripts/generate-ecmascript-grammar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,21 @@ let ruleOverrides = {};
function mkRuleOverrides(substitutions) {
const overrides = {};
for (const substitution of substitutions) {
const keys = Object.keys(substitution)
const keys = Object.keys(substitution);
if (keys.includes('override')) {
overrides[substitution.name] = substitution.override;
continue;
}
if (keys.includes('pattern') && keys.includes('replacement')) {
let postlude = '';
if (keys.includes('postlude')) {
postlude = substitution.postlude
postlude = substitution.postlude;
}
overrides[substitution.name] = (rhs, defaultBody) => (safelyReplace(defaultBody, substitution.pattern, substitution.replacement) + postlude)
overrides[substitution.name] = (rhs, defaultBody) =>
safelyReplace(defaultBody, substitution.pattern, substitution.replacement) + postlude;
}
}
return overrides
return overrides;
}

let reservedWordProductions = [];
Expand All @@ -72,7 +73,6 @@ function mkReservedWordProductions(productions) {
// E.g., `"blah" | "blarg"` => `blah | blarg`.
const terminalsToRules = ohmString => ohmString.replace(/"/g, '');


const PRELUDE = raw`
Start = Script
Expand Down Expand Up @@ -115,10 +115,15 @@ let grammarName; // Initialized in main(), below.
semantics.addOperation(
'toOhm()',
(() => {
function handleProduction(nonterminal, rhs, parameterListOpt = undefined, kind = 'LEXICAL') {
function handleProduction(
nonterminal,
rhs,
parameterListOpt = undefined,
kind = 'LEXICAL'
) {
// const isLexical = parameterListOpt === undefined;
const isLexical = kind === 'LEXICAL'
const strippedNonTerminal = nonterminal.sourceString.replaceAll('|', '')
const isLexical = kind === 'LEXICAL';
const strippedNonTerminal = nonterminal.sourceString.replaceAll('|', '');
const ruleName = isLexical
? lexicalRuleName(strippedNonTerminal)
: syntacticRuleName(strippedNonTerminal);
Expand All @@ -132,28 +137,30 @@ semantics.addOperation(
const handlePositiveLookahead = (_, _op, exprList) => {
const parsedExprList = exprList.isIteration()
? exprList.children.map(c => c.toOhm())
: [ exprList.toOhm() ]
: [exprList.toOhm()];

if (parsedExprList.length > 1) {
return `&(${parsedExprList.join('|')})`;
}
return `&${parsedExprList}`;
}
};
const handleNegativeLookahead = (_, _op, exprList) => {
const parsedExprList = exprList.isIteration()
? exprList.children.map(c => c.toOhm())
: [ exprList.toOhm() ]
: [exprList.toOhm()];
if (parsedExprList.length > 1) {
return `~(${parsedExprList.join('|')})`;
}
return `~${parsedExprList}`;
}
};

return {
Productions(productionIter) {
const rules = productionIter.children.map(c => c.toOhm());
for (const param of new Set(this.allParameters)) {
rules.push(...[`with${param} = /* fixme */`, `no${param} = ~any /* is this right? */`]);
rules.push(
...[`with${param} = /* fixme */`, `no${param} = ~any /* is this right? */`]
);
}
const prettyRules = [...rules].join('\n\n ');
const indentedAdditionalRules = this.getAdditionalRules().map(str => ` ${str}`);
Expand Down Expand Up @@ -228,7 +235,7 @@ semantics.addOperation(
: ohmTerminal;
},
term_link(_) {
return ''
return '';
},
assertion(_open, assertionContents, _close) {
return assertionContents.toOhm();
Expand Down Expand Up @@ -305,7 +312,7 @@ semantics.addOperation(
},
nonterminal(_optPipe, _, _2, _optPipe2) {
const {sourceString} = this;
const trimmedSourceString = sourceString.replaceAll('|', '')
const trimmedSourceString = sourceString.replaceAll('|', '');
const root = this.context.productions;
if (root.productionsByName.has(trimmedSourceString)) {
return trimmedSourceString;
Expand All @@ -327,7 +334,7 @@ semantics.addOperation(
case ';':
return '#sc';
}
return `"${sourceString.replace('\\','\\\\')}"`;
return `"${sourceString.replace('\\', '\\\\')}"`;
},
literal(_open, charIter, _close) {
const name = charIter.sourceString;
Expand Down Expand Up @@ -595,15 +602,16 @@ function addContext(semantics, getActions) {
grammarName = process.argv[2];
const inputFilename = process.argv[3];
if (process.argv.length > 4) {
const overrideConfig = JSON.parse(readFileSync(process.argv[4], { encoding: 'utf-8'}));
const overrideConfig = JSON.parse(readFileSync(process.argv[4], {encoding: 'utf-8'}));
if (overrideConfig.substitutions) {
ruleOverrides = mkRuleOverrides(overrideConfig.substitutions);
}
if (overrideConfig.reservedWords) {
reservedWordProductions = mkReservedWordProductions(overrideConfig.reservedWords)
reservedWordProductions = mkReservedWordProductions(overrideConfig.reservedWords);
// Add a rule override for each of the reserved word productions.
for (const prod of reservedWordProductions) {
ruleOverrides[lexicalRuleName(prod)] = (rhs, defaultBody) => terminalsToRules(defaultBody);
ruleOverrides[lexicalRuleName(prod)] = (rhs, defaultBody) =>
terminalsToRules(defaultBody);
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions examples/ecmascript/spec/es2015/overrides.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
"reservedWords": [
"Keyword",
"FutureReservedWord",
"NullLiteral",
"BooleanLiteral"
],
"reservedWords": ["Keyword", "FutureReservedWord", "NullLiteral", "BooleanLiteral"],
"substitutions": [
{
"name": "unicodeIDStart",
Expand Down
7 changes: 1 addition & 6 deletions examples/ecmascript/spec/es2016/overrides.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
"reservedWords": [
"Keyword",
"FutureReservedWord",
"NullLiteral",
"BooleanLiteral"
],
"reservedWords": ["Keyword", "FutureReservedWord", "NullLiteral", "BooleanLiteral"],
"substitutions": [
{
"name": "unicodeIDStart",
Expand Down
7 changes: 1 addition & 6 deletions examples/ecmascript/spec/es2017/overrides.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
"reservedWords": [
"Keyword",
"FutureReservedWord",
"NullLiteral",
"BooleanLiteral"
],
"reservedWords": ["Keyword", "FutureReservedWord", "NullLiteral", "BooleanLiteral"],
"substitutions": [
{
"name": "unicodeIDStart",
Expand Down
7 changes: 1 addition & 6 deletions examples/ecmascript/spec/es2018/overrides.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
"reservedWords": [
"Keyword",
"FutureReservedWord",
"NullLiteral",
"BooleanLiteral"
],
"reservedWords": ["Keyword", "FutureReservedWord", "NullLiteral", "BooleanLiteral"],
"substitutions": [
{
"name": "unicodeIDStart",
Expand Down
7 changes: 1 addition & 6 deletions examples/ecmascript/spec/es2019/overrides.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
"reservedWords": [
"Keyword",
"FutureReservedWord",
"NullLiteral",
"BooleanLiteral"
],
"reservedWords": ["Keyword", "FutureReservedWord", "NullLiteral", "BooleanLiteral"],
"substitutions": [
{
"name": "unicodeIDStart",
Expand Down
4 changes: 1 addition & 3 deletions examples/ecmascript/spec/es2020/overrides.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"reservedWords": [
"ReservedWord"
],
"reservedWords": ["ReservedWord"],
"substitutions": [
{
"name": "unicodeIDStart",
Expand Down
4 changes: 1 addition & 3 deletions examples/ecmascript/spec/es2021/overrides.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"reservedWords": [
"ReservedWord"
],
"reservedWords": ["ReservedWord"],
"substitutions": [
{
"name": "unicodeIDStart",
Expand Down
4 changes: 1 addition & 3 deletions examples/ecmascript/spec/es2022/overrides.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"reservedWords": [
"ReservedWord"
],
"reservedWords": ["ReservedWord"],
"substitutions": [
{
"name": "unicodeIDStart",
Expand Down
2 changes: 1 addition & 1 deletion packages/ohm-js/extras/storedAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function addStoredAttribute(semantics, attrName, initSignature, fn) {
semantics.addAttribute(attrName, {
_default() {
throw new Error(`Attribute '${attrName}' not initialized`);
}
},
});

// Create the attribute setter, which the semantic actions of the init
Expand Down
4 changes: 2 additions & 2 deletions packages/ohm-js/test/extras/test-storedAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('stored attributes', t => {
// By default, inherit polarity from the parent node.
setPolarity(this, this.args.pol);
children.forEach(c => c.initPolarity(this.args.pol));
}
},
}));
exp.initPolarity('=');
t.is(exp.polarity, '='); // Exp
Expand All @@ -43,6 +43,6 @@ test('stored attributes', t => {
t.is(exp.child(0).child(0).child(0).child(0).polarity, '+'); // AddExp_plus

t.throws(() => exp.child(0).child(0).child(1).polarity, {
message: 'Attribute \'polarity\' not initialized'
message: "Attribute 'polarity' not initialized",
});
});

0 comments on commit 39ccead

Please sign in to comment.