|
| 1 | +const GRAMMAR_CONSTANTS = require('../constants/grammars'); |
| 2 | + |
| 3 | +const INTERMEDIATE_GRAMMAR_PREFIX = 'IntermediateRule'; |
| 4 | +const TERMINAL_GRAMMARS = ['dataName', 'literal', 'node']; |
| 5 | + |
| 6 | +/** |
| 7 | + * Class to convert a CSS formal syntax into a JSON grammar. |
| 8 | + * See https://developer.mozilla.org/en-US/docs/Web/CSS/Value_definition_syntax for more info on CSS value definition |
| 9 | + * syntax. |
| 10 | + * @type {JsonGrammarFormatter} |
| 11 | + */ |
| 12 | +module.exports = class JsonGrammarFormatter { |
| 13 | + |
| 14 | + /** |
| 15 | + * Creates a JsonGrammarFormatter which can be used to convert a CSS formal syntax string into a JSON grammar. |
| 16 | + * |
| 17 | + * @param {Object} formalSyntaxGrammar - an ohm grammar to parse a CSS formal syntax |
| 18 | + */ |
| 19 | + constructor(formalSyntaxGrammar) { |
| 20 | + this.formalSyntaxGrammar = formalSyntaxGrammar; |
| 21 | + this.formalSyntaxSemantics = this._generateSemantics(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Attempts to format a formal syntax string into a JSON grammar. Returns a JSON grammar if the formal syntax |
| 26 | + * matches the given formal syntax grammar. If the match fails, an error in thrown. |
| 27 | + * |
| 28 | + * @param {string} propertyName - the property this formal syntax is associated with. Should be in kebab-case format. |
| 29 | + * For example, "bg-color" or "color()". |
| 30 | + * @param {string} formalSyntax - the css formal syntax string. For example, "white | blue | red || green" |
| 31 | + * @returns {Object} - a JSON grammar if the formal syntax matches the formal syntax grammar, else throws |
| 32 | + * a an error indicating why the match failed. |
| 33 | + */ |
| 34 | + formatFormalSyntax(propertyName, formalSyntax) { |
| 35 | + this.intermediateGrammarIndex = 0; |
| 36 | + this.intermediateGrammars = []; |
| 37 | + this.grammarsToResolve = new Set(); |
| 38 | + this.propertyName = propertyName; |
| 39 | + const match = this.formalSyntaxGrammar.match(formalSyntax); |
| 40 | + |
| 41 | + if (match.succeeded()) { |
| 42 | + const baseOhmGrammar = this.formalSyntaxSemantics(match).eval(); |
| 43 | + const grammarsToResolve = Array.from(this.grammarsToResolve).map(grammarName => [grammarName]); |
| 44 | + return [ |
| 45 | + [GRAMMAR_CONSTANTS.LEXICAL_BASE_KEY, baseOhmGrammar], |
| 46 | + ...this.intermediateGrammars, |
| 47 | + ...grammarsToResolve, |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + throw new Error(`Formal syntax: ${formalSyntax}, failed to match: ${match.message}`); |
| 52 | + } |
| 53 | + |
| 54 | + /* eslint-disable no-unused-vars */ |
| 55 | + /** |
| 56 | + * Generates the Ohm Semantics object corresponding to the formal syntax grammar found in |
| 57 | + * "../grammars/formalSyntax.ohm". |
| 58 | + * |
| 59 | + * @returns {Object} - the Ohm Semantics object based on the formal syntax grammar |
| 60 | + * @private |
| 61 | + */ |
| 62 | + _generateSemantics() { |
| 63 | + const grammarFormatter = this; |
| 64 | + |
| 65 | + return this.formalSyntaxGrammar.createSemantics().addOperation('eval', { |
| 66 | + // simply the root formal syntax |
| 67 | + Exp(baseExpression) { |
| 68 | + return baseExpression.eval(); |
| 69 | + }, |
| 70 | + |
| 71 | + // syntax of the form: "[ <expression> ]" |
| 72 | + Brackets(leftBracket, e, rightBracket) { |
| 73 | + return `( ${e.eval()} )`; |
| 74 | + }, |
| 75 | + |
| 76 | + // syntax of the form: "<expression> <expression>" |
| 77 | + Juxtaposition(expression1, expression2) { |
| 78 | + return `${expression1.eval()} ${expression2.eval()}`; |
| 79 | + }, |
| 80 | + |
| 81 | + // syntax of the form: "<expression> && <expression>" |
| 82 | + DoubleAmpersand(expression1, doubleAmp, expression2) { |
| 83 | + const expression1Eval = expression1.eval(); |
| 84 | + const expression2Eval = expression2.eval(); |
| 85 | + |
| 86 | + return `( ${expression1Eval} ${expression2Eval} ) | ( ${expression2Eval} ${expression1} )`; |
| 87 | + }, |
| 88 | + |
| 89 | + // syntax of the form: "<expression> || <expression>" |
| 90 | + DoubleBar(expression1, doubleBar, expression2) { |
| 91 | + const expressionEvaluations = [expression1, expression2] |
| 92 | + .map(expression => [expression, grammarFormatter._getNodeName(expression)]) |
| 93 | + .map(([expression, nodeName]) => [expression, TERMINAL_GRAMMARS.includes(nodeName)]) |
| 94 | + .map(([expression, isTerminal]) => { |
| 95 | + if (!isTerminal) { |
| 96 | + const intermediateGrammarRuleName = grammarFormatter._generateIntermediateGrammarRuleName(); |
| 97 | + grammarFormatter.intermediateGrammars.push([intermediateGrammarRuleName, expression.eval()]); |
| 98 | + return intermediateGrammarRuleName; |
| 99 | + } |
| 100 | + |
| 101 | + return expression.eval(); |
| 102 | + }) |
| 103 | + .join(' , '); |
| 104 | + |
| 105 | + return `${GRAMMAR_CONSTANTS.DOUBLE_BAR_PARAMETERIZED_RULE_NAME}< ${expressionEvaluations} >`; |
| 106 | + }, |
| 107 | + |
| 108 | + // syntax of the form: "<expression> | <expression>" |
| 109 | + SingleBar(expression1, singleBar, expression2) { |
| 110 | + return `${expression1.eval()} | ${expression2.eval()}`; |
| 111 | + }, |
| 112 | + |
| 113 | + // syntax of the form: "<expression>*" |
| 114 | + Asterisk(expression, asterisk) { |
| 115 | + return `${expression.eval()}*`; |
| 116 | + }, |
| 117 | + |
| 118 | + // syntax of the form: "<expression>+" |
| 119 | + Plus(expression, plus) { |
| 120 | + return `${expression.eval()}+`; |
| 121 | + }, |
| 122 | + |
| 123 | + // syntax of the form: "<expression>?" |
| 124 | + QuestionMark(expression, questionMark) { |
| 125 | + return `${expression.eval()}?`; |
| 126 | + }, |
| 127 | + |
| 128 | + // syntax of the form: "<expression>{<integer>, <integer>}" |
| 129 | + CurlyBraces(expression, b1, lowerLimit, comma, upperLimit, b2) { |
| 130 | + const min = +lowerLimit.sourceString; |
| 131 | + const max = +upperLimit.sourceString; |
| 132 | + const minimumString = new Array(min).fill().map(() => expression.eval()).join(' '); |
| 133 | + const maximumString = new Array(max - min).fill().map(() => `${expression.eval()}?`).join(' '); |
| 134 | + |
| 135 | + return `${minimumString} ${maximumString}`; |
| 136 | + }, |
| 137 | + |
| 138 | + // syntax of the form: "<data-name>" or "<'data-name'> |
| 139 | + node(leftBracket, leftQuote, dataName, rightQuote, rightBracket) { |
| 140 | + const dataNameValue = dataName.eval(); |
| 141 | + |
| 142 | + grammarFormatter.grammarsToResolve.add(`<${dataNameValue}>`); |
| 143 | + return `<${dataNameValue}>`; |
| 144 | + }, |
| 145 | + |
| 146 | + // any string |
| 147 | + dataName(e) { |
| 148 | + return this.sourceString; |
| 149 | + }, |
| 150 | + |
| 151 | + // a character literal like "," or "/" |
| 152 | + literal(e) { |
| 153 | + return `"${this.sourceString}"`; |
| 154 | + }, |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Returns the node name of the given node |
| 160 | + * @param {Object} node - an Ohm AST node |
| 161 | + * @returns {string} - the node name of the given node |
| 162 | + * @private |
| 163 | + */ |
| 164 | + _getNodeName(node) { |
| 165 | + return node.numChildren && node.children[0].ctorName; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Generates the next intermediate grammar rule name. This is used in when parsing double bar expressions |
| 170 | + * that don't contain terminal expressions. For example, given the formal syntax "(a | b) || c", we would have |
| 171 | + * to create a intermediate rule for the left hand side of the double bar expression. Thus we would want something |
| 172 | + * like: |
| 173 | + * exp = UnorderedOptionalTuple< IntermediateRule1, c > |
| 174 | + * IntermediateRule1 = (a | b) |
| 175 | + * |
| 176 | + * @returns {string} |
| 177 | + * @private |
| 178 | + */ |
| 179 | + _generateIntermediateGrammarRuleName() { |
| 180 | + return `${this.propertyName}${INTERMEDIATE_GRAMMAR_PREFIX}${this.intermediateGrammarIndex++}`; |
| 181 | + } |
| 182 | +}; |
0 commit comments