UNPKG

1.19 kBJavaScriptView Raw
1import Expression from './expression';
2import RRText from '../ui/rrtext';
3import GrammarToRRDiagram from './grammartorrdiagram';
4import RRElement from '../ui/rrelement';
5import GrammarToBNF from './grammartobnf';
6
7export default class Literal extends Expression {
8
9 /**
10 * @param {string} text
11 */
12 constructor(text) {
13 super();
14 this.text = text;
15 }
16
17 /**
18 * @param {GrammarToRRDiagram} grammarToRRDiagram
19 * @return {RRElement}
20 */
21 toRRElement(grammarToRRDiagram) {
22 return new RRText(RRText.Type.LITERAL, this.text, null);
23 }
24
25 /**
26 * @param {GrammarToBNF} grammarToBNF
27 * @param {string[]} sb
28 * @param {boolean} isNested
29 */
30 toBNF(grammarToBNF, sb, isNested) {
31 const c = grammarToBNF.literalDefinitionSign == GrammarToBNF.LiteralDefinitionSign.DOUBLE_QUOTE ? '"' : '\'';
32 sb.push(c);
33 sb.push(this.text);
34 sb.push(c);
35 }
36
37 /**
38 * @param {*} o
39 * @return {boolean}
40 */
41 equals(o) {
42 if(!(o instanceof Literal)) {
43 return false;
44 }
45 return this.text == o.text;
46 }
47
48}