UNPKG

antlr-ng

Version:

Next generation ANTLR Tool

1,303 lines (1,302 loc) 15.4 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import * as antlr from "antlr4ng"; import { Character } from "../support/Character.js"; const attrValueExpr = /[^=][^;]*/; const id = /[a-zA-Z_][a-zA-Z0-9_]*/; const setNonLocalAttr = new RegExp(`\\$(?<x>${id.source})::(?<y>${id.source})(\\s)?=(\\s)?(?<expr>${attrValueExpr.source});`); const nonLocalAttr = new RegExp(`\\$(?<x>${id.source})::(?<y>${id.source})`); const qualifiedAttr = new RegExp(`\\$(?<x>${id.source}).(?<y>${id.source})`); const setAttr = new RegExp(`\\$(?<x>${id.source})(?<s1>\\s)?=(?<s2>\\s)?(?<expr>${attrValueExpr.source});`); const attr = new RegExp(`\\$(?<x>${id.source})`); class ActionSplitter extends antlr.Lexer { static { __name(this, "ActionSplitter"); } static COMMENT = 1; static LINE_COMMENT = 2; static SET_NONLOCAL_ATTR = 3; static NONLOCAL_ATTR = 4; static QUALIFIED_ATTR = 5; static SET_ATTR = 6; static ATTR = 7; static TEXT = 8; static channelNames = [ "DEFAULT_TOKEN_CHANNEL", "HIDDEN" ]; static literalNames = []; static symbolicNames = [ null, "COMMENT", "LINE_COMMENT", "SET_NONLOCAL_ATTR", "NONLOCAL_ATTR", "QUALIFIED_ATTR", "SET_ATTR", "ATTR", "TEXT" ]; static modeNames = [ "DEFAULT_MODE" ]; static ruleNames = [ "COMMENT", "LINE_COMMENT", "SET_NONLOCAL_ATTR", "NONLOCAL_ATTR", "QUALIFIED_ATTR", "SET_ATTR", "ATTR", "TEXT", "ID", "ATTR_VALUE_EXPR", "WS" ]; /** Force filtering (and return tokens). Sends token values to the delegate. */ getActionTokens(delegate, refToken) { const tokens = this.getAllTokens(); for (let i = 0; i < tokens.length; i++) { const t = tokens[i]; switch (t.type) { case ActionSplitter.COMMENT: case ActionSplitter.LINE_COMMENT: case ActionSplitter.TEXT: { const text = t.text; let result = ""; for (let i2 = 0; i2 < text.length; i2++) { switch (text[i2]) { case "\\": { if (i2 + 1 >= text.length) { result += "\\"; break; } if (text[i2 + 1] === "$") { result += "$"; i2++; } else { result += text[i2++]; result += text[i2]; } break; } case "$": { if (i2 + 1 >= text.length) { result += "$"; break; } const c = text[i2 + 1]; if (!(c == "_" || Character.isLetter(c.codePointAt(0)))) { result += "$"; } break; } default: { result += text[i2]; break; } } } delegate.text(result); break; } case ActionSplitter.SET_NONLOCAL_ATTR: { const text = t.text; const match = text.match(setNonLocalAttr); if (match === null) { throw new Error(`Mismatched input '${text}'`); } const { x, y, expr } = match.groups; const [xToken, yToken] = this.createTokens(t, x.length + 2, refToken); xToken.text = x; yToken.text = y; delegate.setNonLocalAttr(text, xToken, yToken, expr); break; } case ActionSplitter.NONLOCAL_ATTR: { const text = t.text; const match = text.match(nonLocalAttr); if (match === null) { throw new Error(`Mismatched input '${text}'`); } const { x, y } = match.groups; const [xToken, yToken] = this.createTokens(t, x.length + 2, refToken); xToken.text = x; yToken.text = y; delegate.nonLocalAttr(text, xToken, yToken); break; } case ActionSplitter.QUALIFIED_ATTR: { let text = t.text; const match = text.match(qualifiedAttr); if (match === null) { throw new Error(`Mismatched input '${text}'`); } const { x, y } = match.groups; const [xToken, yToken] = this.createTokens(t, x.length + 1, refToken); xToken.text = x; yToken.text = y; if (i + 1 < tokens.length && tokens[i + 1].text?.startsWith("(")) { delegate.attr("$" + x, xToken); text = "." + y + tokens[++i].text; delegate.text(text); break; } delegate.qualifiedAttr(text, xToken, yToken); break; } case ActionSplitter.SET_ATTR: { const text = t.text; const match = text.match(setAttr); if (match === null) { throw new Error(`Mismatched input '${text}'`); } const { x, s1 = "", s2 = "", expr } = match.groups; const [xToken, yToken] = this.createTokens(t, x.length + s1.length + s2.length + 2, refToken); xToken.text = x; yToken.text = expr; delegate.setAttr(text, xToken, yToken); break; } case ActionSplitter.ATTR: { const text = t.text; const match = text.match(attr); if (match === null) { throw new Error(`Mismatched input '${text}'`); } const { x } = match.groups; const [xToken] = this.createTokens(t, void 0, refToken); xToken.text = x; delegate.attr(text, xToken); break; } default: } } return tokens; } createTokens(sourceToken, offset, refToken) { let line = 1; let column = 1; if (refToken) { if (sourceToken.line === 1) { column = refToken.column; } line = refToken.line; } column += sourceToken.column; line += sourceToken.line - 1; const xToken = antlr.CommonToken.fromToken(sourceToken); xToken.line = line; xToken.column = column; let yToken; if (offset !== void 0) { yToken = antlr.CommonToken.fromToken(sourceToken); yToken.line = line; yToken.column = column + offset; } return [xToken, yToken]; } constructor(input) { super(input); this.interpreter = new antlr.LexerATNSimulator(this, ActionSplitter._ATN, ActionSplitter.decisionsToDFA, new antlr.PredictionContextCache()); } get grammarFileName() { return "ActionSplitter.g4"; } get literalNames() { return ActionSplitter.literalNames; } get symbolicNames() { return ActionSplitter.symbolicNames; } get ruleNames() { return ActionSplitter.ruleNames; } get serializedATN() { return ActionSplitter._serializedATN; } get channelNames() { return ActionSplitter.channelNames; } get modeNames() { return ActionSplitter.modeNames; } static _serializedATN = [ 4, 0, 8, 114, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 1, 0, 1, 0, 1, 0, 1, 0, 5, 0, 28, 8, 0, 10, 0, 12, 0, 31, 9, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 40, 8, 1, 10, 1, 12, 1, 43, 9, 1, 1, 1, 3, 1, 46, 8, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3, 2, 57, 8, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 5, 1, 5, 1, 5, 3, 5, 78, 8, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 6, 1, 6, 1, 6, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 4, 7, 92, 8, 7, 11, 7, 12, 7, 93, 1, 8, 1, 8, 5, 8, 98, 8, 8, 10, 8, 12, 8, 101, 9, 8, 1, 9, 1, 9, 5, 9, 105, 8, 9, 10, 9, 12, 9, 108, 9, 9, 1, 10, 4, 10, 111, 8, 10, 11, 10, 12, 10, 112, 1, 29, 0, 11, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8, 17, 0, 19, 0, 21, 0, 1, 0, 7, 2, 0, 10, 10, 13, 13, 1, 0, 36, 36, 3, 0, 65, 90, 95, 95, 97, 122, 4, 0, 48, 57, 65, 90, 95, 95, 97, 122, 1, 0, 61, 61, 1, 0, 59, 59, 3, 0, 9, 10, 13, 13, 32, 32, 121, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 15, 1, 0, 0, 0, 1, 23, 1, 0, 0, 0, 3, 35, 1, 0, 0, 0, 5, 49, 1, 0, 0, 0, 7, 62, 1, 0, 0, 0, 9, 69, 1, 0, 0, 0, 11, 74, 1, 0, 0, 0, 13, 83, 1, 0, 0, 0, 15, 91, 1, 0, 0, 0, 17, 95, 1, 0, 0, 0, 19, 102, 1, 0, 0, 0, 21, 110, 1, 0, 0, 0, 23, 24, 5, 47, 0, 0, 24, 25, 5, 42, 0, 0, 25, 29, 1, 0, 0, 0, 26, 28, 9, 0, 0, 0, 27, 26, 1, 0, 0, 0, 28, 31, 1, 0, 0, 0, 29, 30, 1, 0, 0, 0, 29, 27, 1, 0, 0, 0, 30, 32, 1, 0, 0, 0, 31, 29, 1, 0, 0, 0, 32, 33, 5, 42, 0, 0, 33, 34, 5, 47, 0, 0, 34, 2, 1, 0, 0, 0, 35, 36, 5, 47, 0, 0, 36, 37, 5, 47, 0, 0, 37, 41, 1, 0, 0, 0, 38, 40, 8, 0, 0, 0, 39, 38, 1, 0, 0, 0, 40, 43, 1, 0, 0, 0, 41, 39, 1, 0, 0, 0, 41, 42, 1, 0, 0, 0, 42, 45, 1, 0, 0, 0, 43, 41, 1, 0, 0, 0, 44, 46, 5, 13, 0, 0, 45, 44, 1, 0, 0, 0, 45, 46, 1, 0, 0, 0, 46, 47, 1, 0, 0, 0, 47, 48, 5, 10, 0, 0, 48, 4, 1, 0, 0, 0, 49, 50, 5, 36, 0, 0, 50, 51, 3, 17, 8, 0, 51, 52, 5, 58, 0, 0, 52, 53, 5, 58, 0, 0, 53, 54, 1, 0, 0, 0, 54, 56, 3, 17, 8, 0, 55, 57, 3, 21, 10, 0, 56, 55, 1, 0, 0, 0, 56, 57, 1, 0, 0, 0, 57, 58, 1, 0, 0, 0, 58, 59, 5, 61, 0, 0, 59, 60, 3, 19, 9, 0, 60, 61, 5, 59, 0, 0, 61, 6, 1, 0, 0, 0, 62, 63, 5, 36, 0, 0, 63, 64, 3, 17, 8, 0, 64, 65, 5, 58, 0, 0, 65, 66, 5, 58, 0, 0, 66, 67, 1, 0, 0, 0, 67, 68, 3, 17, 8, 0, 68, 8, 1, 0, 0, 0, 69, 70, 5, 36, 0, 0, 70, 71, 3, 17, 8, 0, 71, 72, 5, 46, 0, 0, 72, 73, 3, 17, 8, 0, 73, 10, 1, 0, 0, 0, 74, 75, 5, 36, 0, 0, 75, 77, 3, 17, 8, 0, 76, 78, 3, 21, 10, 0, 77, 76, 1, 0, 0, 0, 77, 78, 1, 0, 0, 0, 78, 79, 1, 0, 0, 0, 79, 80, 5, 61, 0, 0, 80, 81, 3, 19, 9, 0, 81, 82, 5, 59, 0, 0, 82, 12, 1, 0, 0, 0, 83, 84, 5, 36, 0, 0, 84, 85, 3, 17, 8, 0, 85, 14, 1, 0, 0, 0, 86, 92, 8, 1, 0, 0, 87, 88, 5, 92, 0, 0, 88, 92, 5, 36, 0, 0, 89, 90, 5, 36, 0, 0, 90, 92, 8, 2, 0, 0, 91, 86, 1, 0, 0, 0, 91, 87, 1, 0, 0, 0, 91, 89, 1, 0, 0, 0, 92, 93, 1, 0, 0, 0, 93, 91, 1, 0, 0, 0, 93, 94, 1, 0, 0, 0, 94, 16, 1, 0, 0, 0, 95, 99, 7, 2, 0, 0, 96, 98, 7, 3, 0, 0, 97, 96, 1, 0, 0, 0, 98, 101, 1, 0, 0, 0, 99, 97, 1, 0, 0, 0, 99, 100, 1, 0, 0, 0, 100, 18, 1, 0, 0, 0, 101, 99, 1, 0, 0, 0, 102, 106, 8, 4, 0, 0, 103, 105, 8, 5, 0, 0, 104, 103, 1, 0, 0, 0, 105, 108, 1, 0, 0, 0, 106, 104, 1, 0, 0, 0, 106, 107, 1, 0, 0, 0, 107, 20, 1, 0, 0, 0, 108, 106, 1, 0, 0, 0, 109, 111, 7, 6, 0, 0, 110, 109, 1, 0, 0, 0, 111, 112, 1, 0, 0, 0, 112, 110, 1, 0, 0, 0, 112, 113, 1, 0, 0, 0, 113, 22, 1, 0, 0, 0, 11, 0, 29, 41, 45, 56, 77, 91, 93, 99, 106, 112, 0 ]; static __ATN; static get _ATN() { if (!ActionSplitter.__ATN) { ActionSplitter.__ATN = new antlr.ATNDeserializer().deserialize(ActionSplitter._serializedATN); } return ActionSplitter.__ATN; } static vocabulary = new antlr.Vocabulary(ActionSplitter.literalNames, ActionSplitter.symbolicNames, []); get vocabulary() { return ActionSplitter.vocabulary; } static decisionsToDFA = ActionSplitter._ATN.decisionToState.map((ds, index) => new antlr.DFA(ds, index)); } export { ActionSplitter };