1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const Builder_1 = require("./Builder");
|
13 | const utils_1 = require("./lint/utils");
|
14 | const grammarkdown_1 = require("grammarkdown");
|
15 | const endTagRe = /<\/?(emu-\w+|h?\d|p|ul|table|pre|code)\b[^>]*>/i;
|
16 | const globalEndTagRe = /<\/?(emu-\w+|h?\d|p|ul|table|pre|code)\b[^>]*>/gi;
|
17 |
|
18 | class Grammar extends Builder_1.default {
|
19 | static enter({ spec, node, clauseStack }) {
|
20 | return __awaiter(this, void 0, void 0, function* () {
|
21 | if ('grammarkdownOut' in node) {
|
22 |
|
23 |
|
24 | node.innerHTML = node.grammarkdownOut;
|
25 | return;
|
26 | }
|
27 |
|
28 |
|
29 | const location = spec.locate(node);
|
30 | let content;
|
31 | let possiblyMalformed = true;
|
32 |
|
33 |
|
34 |
|
35 | if (location) {
|
36 | if (location.startTag && location.endTag) {
|
37 |
|
38 | const start = location.startTag.endOffset;
|
39 | const end = location.endTag.startOffset;
|
40 | content = location.source.slice(start, end);
|
41 | }
|
42 | else {
|
43 |
|
44 |
|
45 |
|
46 | const start = (globalEndTagRe.lastIndex = location.endOffset);
|
47 | const match = globalEndTagRe.exec(location.source);
|
48 | const end = match ? match.index : location.source.length;
|
49 | content = location.source.slice(start, end);
|
50 |
|
51 | possiblyMalformed = false;
|
52 | globalEndTagRe.lastIndex = 0;
|
53 | }
|
54 | }
|
55 | else {
|
56 |
|
57 | content = node.innerHTML.replace(/>/g, '>');
|
58 | }
|
59 | if (possiblyMalformed) {
|
60 |
|
61 |
|
62 | const match = endTagRe.exec(content);
|
63 | if (match) {
|
64 | content = content.slice(0, match.index);
|
65 | }
|
66 | }
|
67 | const options = {
|
68 | format: grammarkdown_1.EmitFormat.ecmarkup,
|
69 | noChecks: true,
|
70 | };
|
71 | const grammarHost = grammarkdown_1.CoreAsyncHost.forFile(content);
|
72 | const grammar = new grammarkdown_1.Grammar([grammarHost.file], options, grammarHost);
|
73 | yield grammar.parse();
|
74 | if (spec.opts.lintSpec && spec.locate(node) != null && !node.hasAttribute('example')) {
|
75 |
|
76 |
|
77 | const clause = clauseStack[clauseStack.length - 1];
|
78 | const namespace = clause ? clause.namespace : spec.namespace;
|
79 | const nonterminals = utils_1.collectNonterminalsFromGrammar(grammar).map(({ name, loc }) => ({
|
80 | name,
|
81 | loc,
|
82 | node,
|
83 | namespace,
|
84 | }));
|
85 | spec._ntStringRefs = spec._ntStringRefs.concat(nonterminals);
|
86 | }
|
87 | yield grammar.emit(undefined, (file, source) => {
|
88 | node.innerHTML = source;
|
89 | });
|
90 | });
|
91 | }
|
92 | }
|
93 | exports.default = Grammar;
|
94 | Grammar.elements = ['EMU-GRAMMAR'];
|