-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
64 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters