1 | export function isFuzzyActionArr(what) {
|
2 | return (Array.isArray(what));
|
3 | }
|
4 | export function isFuzzyAction(what) {
|
5 | return !isFuzzyActionArr(what);
|
6 | }
|
7 | export function isString(what) {
|
8 | return (typeof what === 'string');
|
9 | }
|
10 | export function isIAction(what) {
|
11 | return !isString(what);
|
12 | }
|
13 | export function empty(s) {
|
14 | return (s ? false : true);
|
15 | }
|
16 | export function fixCase(lexer, str) {
|
17 | return (lexer.ignoreCase && str ? str.toLowerCase() : str);
|
18 | }
|
19 | export function sanitize(s) {
|
20 | return s.replace(/[&<>'"_]/g, '-');
|
21 | }
|
22 | export function log(lexer, msg) {
|
23 | console.log(lexer.languageId + ": " + msg);
|
24 | }
|
25 | export function createError(lexer, msg) {
|
26 | return new Error(lexer.languageId + ": " + msg);
|
27 | }
|
28 | var substitutionCache = {};
|
29 | export function compileSubstitution(str) {
|
30 | var parts = [];
|
31 | var i = 0;
|
32 | var l = str.length;
|
33 | var part = '';
|
34 | var sub = 0;
|
35 | while (i < l) {
|
36 | var chr = str[i++];
|
37 | if (chr == '$') {
|
38 | var next = str[i++];
|
39 | if (next == '$') {
|
40 | part += '$';
|
41 | continue;
|
42 | }
|
43 | if (part)
|
44 | parts.push(part);
|
45 | part = '';
|
46 | if (next == '#') {
|
47 | parts.push(0);
|
48 | }
|
49 | else if (next == 'S') {
|
50 | parts.push(parseInt(str[i++]) + 100);
|
51 | }
|
52 | else {
|
53 | parts.push(parseInt(next) + 1);
|
54 | }
|
55 | }
|
56 | else {
|
57 | part += chr;
|
58 | }
|
59 | }
|
60 | if (part)
|
61 | parts.push(part);
|
62 | substitutionCache[str] = parts;
|
63 | return parts;
|
64 | }
|
65 | export function substituteMatches(lexer, str, id, matches, state) {
|
66 | var stateMatches = null;
|
67 | var parts = substitutionCache[str] || compileSubstitution(str);
|
68 | var out = "";
|
69 | for (var i = 0; i < parts.length; i++) {
|
70 | var part = parts[i];
|
71 | if (typeof part == 'string') {
|
72 | out += part;
|
73 | }
|
74 | else if (part > 100) {
|
75 | if (stateMatches === null)
|
76 | stateMatches = state.split('.');
|
77 | out += (stateMatches[part - 101] || '');
|
78 | }
|
79 | else if (part === 100) {
|
80 | out += state;
|
81 | }
|
82 | else if (part === 0) {
|
83 | out += id;
|
84 | }
|
85 | else if (part > 0) {
|
86 | out += matches[part - 1];
|
87 | }
|
88 | }
|
89 | return out;
|
90 | }
|
91 | export function substituteMatchesOld(lexer, str, id, matches, state) {
|
92 | var re = /\$((\$)|(#)|(\d\d?)|[sS](\d\d?)|@(\w+))/g;
|
93 | var stateMatches = null;
|
94 | return str.replace(re, function (full, sub, dollar, hash, n, s, attr, ofs, total) {
|
95 | if (!empty(dollar)) {
|
96 | return '$';
|
97 | }
|
98 | if (!empty(hash)) {
|
99 | return fixCase(lexer, id);
|
100 | }
|
101 | if (!empty(n) && n < matches.length) {
|
102 | return fixCase(lexer, matches[n]);
|
103 | }
|
104 | if (!empty(attr) && lexer && typeof (lexer[attr]) === 'string') {
|
105 | return lexer[attr];
|
106 | }
|
107 | if (stateMatches === null) {
|
108 | stateMatches = state.split('.');
|
109 | stateMatches.unshift(state);
|
110 | }
|
111 | if (!empty(s) && s < stateMatches.length) {
|
112 | return fixCase(lexer, stateMatches[s]);
|
113 | }
|
114 | return '';
|
115 | });
|
116 | }
|
117 | var FIND_RULES_MAP = {};
|
118 | export function findRules(lexer, inState) {
|
119 | var state = inState;
|
120 | if (FIND_RULES_MAP[state]) {
|
121 | return lexer.tokenizer[FIND_RULES_MAP[state]];
|
122 | }
|
123 | while (state && state.length > 0) {
|
124 | var rules = lexer.tokenizer[state];
|
125 | if (rules) {
|
126 | FIND_RULES_MAP[inState] = state;
|
127 | return rules;
|
128 | }
|
129 | var idx = state.lastIndexOf('.');
|
130 | if (idx < 0) {
|
131 | state = null;
|
132 | }
|
133 | else {
|
134 | state = state.substr(0, idx);
|
135 | }
|
136 | }
|
137 | return null;
|
138 | }
|
139 | export function stateExists(lexer, inState) {
|
140 | var state = inState;
|
141 | while (state && state.length > 0) {
|
142 | var exist = lexer.stateNames[state];
|
143 | if (exist) {
|
144 | return true;
|
145 | }
|
146 | var idx = state.lastIndexOf('.');
|
147 | if (idx < 0) {
|
148 | state = null;
|
149 | }
|
150 | else {
|
151 | state = state.substr(0, idx);
|
152 | }
|
153 | }
|
154 | return false;
|
155 | }
|