Skip to content

Commit 39ccead

Browse files
committed
chore: formatting
1 parent 7e519af commit 39ccead

File tree

12 files changed

+64
-94
lines changed

12 files changed

+64
-94
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,56 @@
1-
import { parseArgs } from 'util'
2-
import jsdom from 'jsdom'
3-
import fs from 'fs'
4-
const { JSDOM } = jsdom
1+
import {parseArgs} from 'util';
2+
import jsdom from 'jsdom';
3+
import fs from 'fs';
4+
const {JSDOM} = jsdom;
55

6-
const USAGE =
7-
`Usage: extract-markdown.mjs <grammar_file>
6+
const USAGE = `Usage: extract-markdown.mjs <grammar_file>
87
98
Options:
109
-h --help Print this message.
11-
`
12-
13-
const extractProductions = (specText) => {
10+
`;
1411

12+
const extractProductions = specText => {
1513
const dom = new JSDOM(specText, {
16-
includeNodeLocations: true,
17-
})
14+
includeNodeLocations: true
15+
});
1816

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

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

28-
const grammarkdownText = Array.from(emuNodes, (e) => {
29-
return [`@line ${dom.nodeLocation(e).startLine}`, e.textContent].join('\n')
30-
}).join('\n')
26+
const grammarkdownText = Array.from(emuNodes, e => {
27+
return [`@line ${dom.nodeLocation(e).startLine}`, e.textContent].join('\n');
28+
}).join('\n');
3129

32-
return grammarkdownText
33-
}
30+
return grammarkdownText;
31+
};
3432

3533
const main = () => {
3634
const parseConfig = {
3735
allowPositionals: true,
3836
options: {
3937
help: {
4038
short: 'h',
41-
type: 'boolean',
39+
type: 'boolean'
4240
}
4341
}
44-
}
42+
};
4543

46-
const args = parseArgs(parseConfig)
44+
const args = parseArgs(parseConfig);
4745

4846
if (args.values.help || args.positionals.length < 1) {
49-
console.error(USAGE)
50-
process.exit(!args.values.help)
47+
console.error(USAGE);
48+
process.exit(!args.values.help);
5149
}
5250

53-
const spec = fs.readFileSync(args.positionals[0], {encoding: 'utf-8'})
54-
const grammarText = extractProductions(spec)
55-
console.log(grammarText)
56-
}
57-
58-
main()
59-
60-
61-
62-
51+
const spec = fs.readFileSync(args.positionals[0], {encoding: 'utf-8'});
52+
const grammarText = extractProductions(spec);
53+
console.log(grammarText);
54+
};
6355

56+
main();

examples/ecmascript/scripts/generate-ecmascript-grammar.mjs

+27-19
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,21 @@ let ruleOverrides = {};
4444
function mkRuleOverrides(substitutions) {
4545
const overrides = {};
4646
for (const substitution of substitutions) {
47-
const keys = Object.keys(substitution)
47+
const keys = Object.keys(substitution);
4848
if (keys.includes('override')) {
4949
overrides[substitution.name] = substitution.override;
5050
continue;
5151
}
5252
if (keys.includes('pattern') && keys.includes('replacement')) {
5353
let postlude = '';
5454
if (keys.includes('postlude')) {
55-
postlude = substitution.postlude
55+
postlude = substitution.postlude;
5656
}
57-
overrides[substitution.name] = (rhs, defaultBody) => (safelyReplace(defaultBody, substitution.pattern, substitution.replacement) + postlude)
57+
overrides[substitution.name] = (rhs, defaultBody) =>
58+
safelyReplace(defaultBody, substitution.pattern, substitution.replacement) + postlude;
5859
}
5960
}
60-
return overrides
61+
return overrides;
6162
}
6263

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

75-
7676
const PRELUDE = raw`
7777
Start = Script
7878
@@ -115,10 +115,15 @@ let grammarName; // Initialized in main(), below.
115115
semantics.addOperation(
116116
'toOhm()',
117117
(() => {
118-
function handleProduction(nonterminal, rhs, parameterListOpt = undefined, kind = 'LEXICAL') {
118+
function handleProduction(
119+
nonterminal,
120+
rhs,
121+
parameterListOpt = undefined,
122+
kind = 'LEXICAL'
123+
) {
119124
// const isLexical = parameterListOpt === undefined;
120-
const isLexical = kind === 'LEXICAL'
121-
const strippedNonTerminal = nonterminal.sourceString.replaceAll('|', '')
125+
const isLexical = kind === 'LEXICAL';
126+
const strippedNonTerminal = nonterminal.sourceString.replaceAll('|', '');
122127
const ruleName = isLexical
123128
? lexicalRuleName(strippedNonTerminal)
124129
: syntacticRuleName(strippedNonTerminal);
@@ -132,28 +137,30 @@ semantics.addOperation(
132137
const handlePositiveLookahead = (_, _op, exprList) => {
133138
const parsedExprList = exprList.isIteration()
134139
? exprList.children.map(c => c.toOhm())
135-
: [ exprList.toOhm() ]
140+
: [exprList.toOhm()];
136141

137142
if (parsedExprList.length > 1) {
138143
return `&(${parsedExprList.join('|')})`;
139144
}
140145
return `&${parsedExprList}`;
141-
}
146+
};
142147
const handleNegativeLookahead = (_, _op, exprList) => {
143148
const parsedExprList = exprList.isIteration()
144149
? exprList.children.map(c => c.toOhm())
145-
: [ exprList.toOhm() ]
150+
: [exprList.toOhm()];
146151
if (parsedExprList.length > 1) {
147152
return `~(${parsedExprList.join('|')})`;
148153
}
149154
return `~${parsedExprList}`;
150-
}
155+
};
151156

152157
return {
153158
Productions(productionIter) {
154159
const rules = productionIter.children.map(c => c.toOhm());
155160
for (const param of new Set(this.allParameters)) {
156-
rules.push(...[`with${param} = /* fixme */`, `no${param} = ~any /* is this right? */`]);
161+
rules.push(
162+
...[`with${param} = /* fixme */`, `no${param} = ~any /* is this right? */`]
163+
);
157164
}
158165
const prettyRules = [...rules].join('\n\n ');
159166
const indentedAdditionalRules = this.getAdditionalRules().map(str => ` ${str}`);
@@ -228,7 +235,7 @@ semantics.addOperation(
228235
: ohmTerminal;
229236
},
230237
term_link(_) {
231-
return ''
238+
return '';
232239
},
233240
assertion(_open, assertionContents, _close) {
234241
return assertionContents.toOhm();
@@ -305,7 +312,7 @@ semantics.addOperation(
305312
},
306313
nonterminal(_optPipe, _, _2, _optPipe2) {
307314
const {sourceString} = this;
308-
const trimmedSourceString = sourceString.replaceAll('|', '')
315+
const trimmedSourceString = sourceString.replaceAll('|', '');
309316
const root = this.context.productions;
310317
if (root.productionsByName.has(trimmedSourceString)) {
311318
return trimmedSourceString;
@@ -327,7 +334,7 @@ semantics.addOperation(
327334
case ';':
328335
return '#sc';
329336
}
330-
return `"${sourceString.replace('\\','\\\\')}"`;
337+
return `"${sourceString.replace('\\', '\\\\')}"`;
331338
},
332339
literal(_open, charIter, _close) {
333340
const name = charIter.sourceString;
@@ -595,15 +602,16 @@ function addContext(semantics, getActions) {
595602
grammarName = process.argv[2];
596603
const inputFilename = process.argv[3];
597604
if (process.argv.length > 4) {
598-
const overrideConfig = JSON.parse(readFileSync(process.argv[4], { encoding: 'utf-8'}));
605+
const overrideConfig = JSON.parse(readFileSync(process.argv[4], {encoding: 'utf-8'}));
599606
if (overrideConfig.substitutions) {
600607
ruleOverrides = mkRuleOverrides(overrideConfig.substitutions);
601608
}
602609
if (overrideConfig.reservedWords) {
603-
reservedWordProductions = mkReservedWordProductions(overrideConfig.reservedWords)
610+
reservedWordProductions = mkReservedWordProductions(overrideConfig.reservedWords);
604611
// Add a rule override for each of the reserved word productions.
605612
for (const prod of reservedWordProductions) {
606-
ruleOverrides[lexicalRuleName(prod)] = (rhs, defaultBody) => terminalsToRules(defaultBody);
613+
ruleOverrides[lexicalRuleName(prod)] = (rhs, defaultBody) =>
614+
terminalsToRules(defaultBody);
607615
}
608616
}
609617
}

examples/ecmascript/spec/es2015/overrides.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
2-
"reservedWords": [
3-
"Keyword",
4-
"FutureReservedWord",
5-
"NullLiteral",
6-
"BooleanLiteral"
7-
],
2+
"reservedWords": ["Keyword", "FutureReservedWord", "NullLiteral", "BooleanLiteral"],
83
"substitutions": [
94
{
105
"name": "unicodeIDStart",

examples/ecmascript/spec/es2016/overrides.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
2-
"reservedWords": [
3-
"Keyword",
4-
"FutureReservedWord",
5-
"NullLiteral",
6-
"BooleanLiteral"
7-
],
2+
"reservedWords": ["Keyword", "FutureReservedWord", "NullLiteral", "BooleanLiteral"],
83
"substitutions": [
94
{
105
"name": "unicodeIDStart",

examples/ecmascript/spec/es2017/overrides.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
2-
"reservedWords": [
3-
"Keyword",
4-
"FutureReservedWord",
5-
"NullLiteral",
6-
"BooleanLiteral"
7-
],
2+
"reservedWords": ["Keyword", "FutureReservedWord", "NullLiteral", "BooleanLiteral"],
83
"substitutions": [
94
{
105
"name": "unicodeIDStart",

examples/ecmascript/spec/es2018/overrides.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
2-
"reservedWords": [
3-
"Keyword",
4-
"FutureReservedWord",
5-
"NullLiteral",
6-
"BooleanLiteral"
7-
],
2+
"reservedWords": ["Keyword", "FutureReservedWord", "NullLiteral", "BooleanLiteral"],
83
"substitutions": [
94
{
105
"name": "unicodeIDStart",

examples/ecmascript/spec/es2019/overrides.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
2-
"reservedWords": [
3-
"Keyword",
4-
"FutureReservedWord",
5-
"NullLiteral",
6-
"BooleanLiteral"
7-
],
2+
"reservedWords": ["Keyword", "FutureReservedWord", "NullLiteral", "BooleanLiteral"],
83
"substitutions": [
94
{
105
"name": "unicodeIDStart",

examples/ecmascript/spec/es2020/overrides.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
2-
"reservedWords": [
3-
"ReservedWord"
4-
],
2+
"reservedWords": ["ReservedWord"],
53
"substitutions": [
64
{
75
"name": "unicodeIDStart",

examples/ecmascript/spec/es2021/overrides.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
2-
"reservedWords": [
3-
"ReservedWord"
4-
],
2+
"reservedWords": ["ReservedWord"],
53
"substitutions": [
64
{
75
"name": "unicodeIDStart",

examples/ecmascript/spec/es2022/overrides.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
2-
"reservedWords": [
3-
"ReservedWord"
4-
],
2+
"reservedWords": ["ReservedWord"],
53
"substitutions": [
64
{
75
"name": "unicodeIDStart",

packages/ohm-js/extras/storedAttributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function addStoredAttribute(semantics, attrName, initSignature, fn) {
77
semantics.addAttribute(attrName, {
88
_default() {
99
throw new Error(`Attribute '${attrName}' not initialized`);
10-
}
10+
},
1111
});
1212

1313
// Create the attribute setter, which the semantic actions of the init

packages/ohm-js/test/extras/test-storedAttributes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test('stored attributes', t => {
3333
// By default, inherit polarity from the parent node.
3434
setPolarity(this, this.args.pol);
3535
children.forEach(c => c.initPolarity(this.args.pol));
36-
}
36+
},
3737
}));
3838
exp.initPolarity('=');
3939
t.is(exp.polarity, '='); // Exp
@@ -43,6 +43,6 @@ test('stored attributes', t => {
4343
t.is(exp.child(0).child(0).child(0).child(0).polarity, '+'); // AddExp_plus
4444

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

0 commit comments

Comments
 (0)