|
| 1 | +import * as ohm from 'ohm-js'; |
| 2 | + |
| 3 | +export const grammarsSource = String.raw` |
| 4 | + /* |
| 5 | + Superset of the Ohm grammar that allows examples to be embedded in comments. |
| 6 | + Any valid Ohm grammar will also be matched by this grammar. |
| 7 | + */ |
| 8 | +
|
| 9 | + // Example: |
| 10 | + //+ "//+ \"x\"\nG {\n//- \"\"\nstart = \"x\"}" |
| 11 | + OhmWithExamples <: Ohm { |
| 12 | + // The default start rule for Ohm is 'Grammars', which is syntactic rule. |
| 13 | + // When the start rule is a syntactic rule, there's no way to get access to |
| 14 | + // leading space (including comments). So, for this grammar to be useful, |
| 15 | + // you have to explicit use this rule as the start rule. |
| 16 | + grammarsWithExamples = (exampleComments applySyntactic<Grammar>)* |
| 17 | +
|
| 18 | + Grammar := ident SuperGrammar? "{" (#exampleComments Rule)* "}" |
| 19 | +
|
| 20 | + exampleComments = (spacesNoExampleComment exampleComment)* |
| 21 | +
|
| 22 | + // Examples: |
| 23 | + //+ "//+ \"blah\"" |
| 24 | + //+ "//- \"one\", \"two\"" |
| 25 | + //- "// - \"x\", "//-\"x\" |
| 26 | + exampleComment |
| 27 | + = "//+" examples -- positive |
| 28 | + | "//-" examples -- negative |
| 29 | +
|
| 30 | + examples = spaceNoNl+ nonemptyListOf<jsonString, exampleSep> spaceNoNl* |
| 31 | + exampleSep = "," spaces |
| 32 | +
|
| 33 | + exampleCommentPrefix = "//+" | "//-" |
| 34 | +
|
| 35 | + spaceNoNl = ~"\n" space |
| 36 | + spacesNoExampleComment = (~exampleCommentPrefix space)* |
| 37 | +
|
| 38 | + jsonString = "\"" jsonChar* "\"" |
| 39 | +
|
| 40 | + jsonChar |
| 41 | + = jsonEscape |
| 42 | + | ~"\\" ~"\"" "\u{0020}".."\u{10FFFF}" |
| 43 | +
|
| 44 | + //+ "\\n", "\\u1234" |
| 45 | + jsonEscape (a JSON escape sequence) |
| 46 | + = "\\\"" |
| 47 | + | "\\\\" |
| 48 | + | "\\/" |
| 49 | + | "\\b" |
| 50 | + | "\\f" |
| 51 | + | "\\n" |
| 52 | + | "\\r" |
| 53 | + | "\\t" |
| 54 | + | "\\u" hexDigit hexDigit hexDigit hexDigit -- unicodeEscape |
| 55 | + } |
| 56 | +
|
| 57 | + /* |
| 58 | + A stricter version of the grammar that prevents many malformed example |
| 59 | + comments from being parsed as regular comments. |
| 60 | + */ |
| 61 | + OhmWithExamplesStrict <: OhmWithExamples { |
| 62 | + // Redefine 'comment' to avoid malformed example comments from silently |
| 63 | + // being parsed as regular comments. |
| 64 | + comment := |
| 65 | + | ~exampleCommentPrefix comment_singleLine |
| 66 | + | comment_multiLine |
| 67 | + } |
| 68 | +`; |
| 69 | + |
| 70 | +export const grammars = ohm.grammars(grammarsSource, {Ohm: ohm.ohmGrammar}); |
| 71 | + |
| 72 | +const semantics = grammars.OhmWithExamples.createSemantics().addOperation('hasExamples', { |
| 73 | + _iter(...children) { |
| 74 | + return children.some(c => c.hasExamples()); |
| 75 | + }, |
| 76 | + exampleComments(_, commentIter) { |
| 77 | + return commentIter.numChildren > 0; |
| 78 | + }, |
| 79 | +}); |
| 80 | + |
| 81 | +semantics.addOperation('examples', { |
| 82 | + grammarsWithExamples(exampleCommentsIter, grammarIter) { |
| 83 | + const result = []; |
| 84 | + for (const [i, child] of Object.entries(grammarIter.children)) { |
| 85 | + if (exampleCommentsIter.hasExamples()) { |
| 86 | + const defaultExamples = exampleCommentsIter.child(i).examples(); |
| 87 | + const grammar = child.grammarName(); |
| 88 | + result.push(...defaultExamples.map(ex => ({...ex, grammar, rule: ''}))); |
| 89 | + } |
| 90 | + result.push(...child.examples()); |
| 91 | + } |
| 92 | + return result; |
| 93 | + }, |
| 94 | + Grammar(name, _, _open, exampleCommentsIter, ruleIter, _close) { |
| 95 | + const result = []; |
| 96 | + const grammar = this.grammarName(); |
| 97 | + for (let i = 0; i < ruleIter.numChildren; i++) { |
| 98 | + const rule = ruleIter.child(i).ruleName(); |
| 99 | + |
| 100 | + // Augment each of the examples with the grammar and rule name. |
| 101 | + const examples = exampleCommentsIter.child(i).examples(); |
| 102 | + const augmentedExamples = examples.map(ex => ({...ex, grammar, rule})); |
| 103 | + |
| 104 | + result.push(...augmentedExamples); |
| 105 | + } |
| 106 | + return result; |
| 107 | + }, |
| 108 | + exampleComments(_, commentIter) { |
| 109 | + return commentIter.children.flatMap(c => c.examples()); |
| 110 | + }, |
| 111 | + exampleComment_positive(_, examples) { |
| 112 | + return examples.examples().map(ex => ({...ex, shouldMatch: true})); |
| 113 | + }, |
| 114 | + exampleComment_negative(_, examples) { |
| 115 | + return examples.examples().map(ex => ({...ex, shouldMatch: false})); |
| 116 | + }, |
| 117 | + examples(_ws, jsonStringList, _) { |
| 118 | + return jsonStringList.asIteration().children.map(t => { |
| 119 | + return {example: JSON.parse(t.sourceString)}; |
| 120 | + }); |
| 121 | + }, |
| 122 | + comment_singleLine(_, commentCharIter, _nl) { |
| 123 | + return []; |
| 124 | + }, |
| 125 | + comment_multiLine(_, commentCharIter, _nl) { |
| 126 | + return []; |
| 127 | + }, |
| 128 | +}); |
| 129 | + |
| 130 | +semantics.addOperation('grammarName', { |
| 131 | + Grammar(name, _, _open, exampleCommentsIter, ruleIter, _close) { |
| 132 | + return name.sourceString; |
| 133 | + }, |
| 134 | +}); |
| 135 | + |
| 136 | +semantics.addOperation('ruleName', { |
| 137 | + Rule_define(ident, _formals, _desc, _, _body) { |
| 138 | + return ident.sourceString; |
| 139 | + }, |
| 140 | + Rule_override(ident, _formals, _, _body) { |
| 141 | + return ident.sourceString; |
| 142 | + }, |
| 143 | + Rule_extend(ident, _formals, _, _body) { |
| 144 | + return ident.sourceString; |
| 145 | + }, |
| 146 | +}); |
| 147 | + |
| 148 | +/** @typedef {{grammar: string, rule: string, example: string, shouldMatch: boolean}} Example */ |
| 149 | + |
| 150 | +/** |
| 151 | + * @param {string} grammarsDef - A string containing one or more grammar definitions. |
| 152 | + * @return {[Example]} |
| 153 | + */ |
| 154 | +export function extractExamples(grammarsDef) { |
| 155 | + const matchResult = grammars.OhmWithExamples.match(grammarsDef, 'grammarsWithExamples'); |
| 156 | + if (matchResult.failed()) { |
| 157 | + throw new Error(matchResult.message); |
| 158 | + } |
| 159 | + return semantics(matchResult).examples(); |
| 160 | +} |
0 commit comments