@malloydata/malloy 0.0.56-dev230707210748 → 0.0.56-dev230710162623
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lang/lib/Malloy/MalloyParser.d.ts +43 -32
- package/dist/lang/lib/Malloy/MalloyParser.js +1363 -1276
- package/dist/lang/lib/Malloy/MalloyParserListener.d.ts +11 -0
- package/dist/lang/lib/Malloy/MalloyParserVisitor.d.ts +7 -0
- package/dist/lang/malloy-to-ast.d.ts +4 -5
- package/dist/lang/malloy-to-ast.js +82 -114
- package/dist/lang/parse-tree-walkers/find-external-references.js +26 -11
- package/dist/lang/parse-utils.d.ts +51 -0
- package/dist/lang/parse-utils.js +238 -0
- package/dist/lang/test/strings.spec.js +108 -13
- package/package.json +1 -1
- package/dist/lang/string-parser.d.ts +0 -11
- package/dist/lang/string-parser.js +0 -105
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright 2023 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
* a copy of this software and associated documentation files
|
|
7
|
+
* (the "Software"), to deal in the Software without restriction,
|
|
8
|
+
* including without limitation the rights to use, copy, modify, merge,
|
|
9
|
+
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
10
|
+
* and to permit persons to whom the Software is furnished to do so,
|
|
11
|
+
* subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be
|
|
14
|
+
* included in all copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
20
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
22
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.unIndent = exports.getOptionalId = exports.getId = exports.getIsNotes = exports.getNotes = exports.parseString = exports.getStringParts = exports.getStringIfShort = exports.getShortString = void 0;
|
|
26
|
+
/**
|
|
27
|
+
* Take the text of a matched string, including the matching quote
|
|
28
|
+
* characters, and return the actual contents of the string after
|
|
29
|
+
* \ processing.
|
|
30
|
+
* @param cx Any parse context which contains a string
|
|
31
|
+
* @returns Decocded string
|
|
32
|
+
*/
|
|
33
|
+
function getShortString(scx) {
|
|
34
|
+
var _a, _b;
|
|
35
|
+
const str = ((_a = scx.DQ_STRING()) === null || _a === void 0 ? void 0 : _a.text) || ((_b = scx.SQ_STRING()) === null || _b === void 0 ? void 0 : _b.text);
|
|
36
|
+
if (str) {
|
|
37
|
+
return parseString(str, str[0]);
|
|
38
|
+
}
|
|
39
|
+
// shortString: DQ_STRING | SQ_STRING; So this will never happen
|
|
40
|
+
return '';
|
|
41
|
+
}
|
|
42
|
+
exports.getShortString = getShortString;
|
|
43
|
+
function getStringIfShort(cx) {
|
|
44
|
+
const scx = cx.string().shortString();
|
|
45
|
+
if (scx) {
|
|
46
|
+
return getShortString(scx);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.getStringIfShort = getStringIfShort;
|
|
50
|
+
function* getStringParts(cx) {
|
|
51
|
+
var _a;
|
|
52
|
+
if (cx) {
|
|
53
|
+
for (const part of cx.sqlInterpolation()) {
|
|
54
|
+
const upToOpen = part.OPEN_CODE().text;
|
|
55
|
+
if (upToOpen.length > 2) {
|
|
56
|
+
yield upToOpen.slice(0, upToOpen.length - 2);
|
|
57
|
+
}
|
|
58
|
+
if (part.query()) {
|
|
59
|
+
yield part.query();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const lastChars = (_a = cx.SQL_END()) === null || _a === void 0 ? void 0 : _a.text.slice(0, -3);
|
|
63
|
+
if (lastChars && lastChars.length > 0) {
|
|
64
|
+
yield lastChars;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.getStringParts = getStringParts;
|
|
69
|
+
var ParseState;
|
|
70
|
+
(function (ParseState) {
|
|
71
|
+
ParseState[ParseState["Normal"] = 0] = "Normal";
|
|
72
|
+
ParseState[ParseState["ReverseVirgule"] = 1] = "ReverseVirgule";
|
|
73
|
+
ParseState[ParseState["Unicode"] = 2] = "Unicode";
|
|
74
|
+
})(ParseState || (ParseState = {}));
|
|
75
|
+
/**
|
|
76
|
+
* Parses the interior of a string, doing all \ substitutions. In most cases
|
|
77
|
+
* a lexical analyzer has already recognized this as a string. As a convenience,
|
|
78
|
+
* strip off the quoting outer chartacters if asked, then parse the interior of
|
|
79
|
+
* the string. The intention is to be compatible with JSON strings, in terms
|
|
80
|
+
* of which \X substitutions are processed.
|
|
81
|
+
* @param str is the string to parse
|
|
82
|
+
* @param surround is the quoting character, default means quotes already stripped
|
|
83
|
+
* @returns a string with the \ processing completed
|
|
84
|
+
*/
|
|
85
|
+
function parseString(str, surround = '') {
|
|
86
|
+
let inner = str.slice(surround.length);
|
|
87
|
+
let state = ParseState.Normal;
|
|
88
|
+
if (surround.length) {
|
|
89
|
+
inner = inner.slice(0, -surround.length);
|
|
90
|
+
}
|
|
91
|
+
let out = '';
|
|
92
|
+
let unicode = '';
|
|
93
|
+
for (const c of inner) {
|
|
94
|
+
switch (state) {
|
|
95
|
+
case ParseState.Normal: {
|
|
96
|
+
if (c === '\\') {
|
|
97
|
+
state = ParseState.ReverseVirgule;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
out += c;
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
case ParseState.ReverseVirgule: {
|
|
105
|
+
let outc = c;
|
|
106
|
+
if (c === 'u') {
|
|
107
|
+
state = ParseState.Unicode;
|
|
108
|
+
unicode = '';
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (c === 'b') {
|
|
112
|
+
outc = '\b';
|
|
113
|
+
}
|
|
114
|
+
else if (c === 'f') {
|
|
115
|
+
outc = '\f';
|
|
116
|
+
}
|
|
117
|
+
else if (c === 'n') {
|
|
118
|
+
outc = '\n';
|
|
119
|
+
}
|
|
120
|
+
else if (c === 'r') {
|
|
121
|
+
outc = '\r';
|
|
122
|
+
}
|
|
123
|
+
else if (c === 't') {
|
|
124
|
+
outc = '\t';
|
|
125
|
+
}
|
|
126
|
+
out += outc;
|
|
127
|
+
state = ParseState.Normal;
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
case ParseState.Unicode: {
|
|
131
|
+
if ('ABCDEFabcdef0123456789'.includes(c)) {
|
|
132
|
+
unicode += c;
|
|
133
|
+
if (unicode.length === 4) {
|
|
134
|
+
out += String.fromCharCode(parseInt(unicode, 16));
|
|
135
|
+
state = ParseState.Normal;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
// Don't think we ever get here ...
|
|
140
|
+
state = ParseState.Normal;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return out;
|
|
146
|
+
}
|
|
147
|
+
exports.parseString = parseString;
|
|
148
|
+
/**
|
|
149
|
+
* Get all the possibly missing annotations from this parse rule
|
|
150
|
+
* @param cx Any parse context which has an ANNOTATION* rules
|
|
151
|
+
* @returns Array of texts for the annotations
|
|
152
|
+
*/
|
|
153
|
+
function getNotes(cx) {
|
|
154
|
+
return cx.ANNOTATION().map(a => a.text);
|
|
155
|
+
}
|
|
156
|
+
exports.getNotes = getNotes;
|
|
157
|
+
function getIsNotes(cx) {
|
|
158
|
+
const before = getNotes(cx._beforeIs);
|
|
159
|
+
return before.concat(getNotes(cx._afterIs));
|
|
160
|
+
}
|
|
161
|
+
exports.getIsNotes = getIsNotes;
|
|
162
|
+
/**
|
|
163
|
+
* An identifier is either a sequence of id characters or a `quoted`
|
|
164
|
+
* This parses either to simply the resulting text.
|
|
165
|
+
* @param cx A context which has an identifier
|
|
166
|
+
* @returns The indenftifier text
|
|
167
|
+
*/
|
|
168
|
+
function getId(cx) {
|
|
169
|
+
const quoted = cx.id().BQ_STRING();
|
|
170
|
+
if (quoted) {
|
|
171
|
+
return parseString(quoted.text, '`');
|
|
172
|
+
}
|
|
173
|
+
return cx.id().text;
|
|
174
|
+
}
|
|
175
|
+
exports.getId = getId;
|
|
176
|
+
function getOptionalId(cx) {
|
|
177
|
+
function containsID(cx) {
|
|
178
|
+
return 'id' in cx;
|
|
179
|
+
}
|
|
180
|
+
if (containsID(cx) && cx.id()) {
|
|
181
|
+
return getId(cx);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
exports.getOptionalId = getOptionalId;
|
|
185
|
+
function* linesOf(allText) {
|
|
186
|
+
while (allText.length > 0) {
|
|
187
|
+
const lineMatch = allText.match(/^.*?\r?\n/);
|
|
188
|
+
let nextLine = allText;
|
|
189
|
+
if (lineMatch) {
|
|
190
|
+
nextLine = lineMatch[0];
|
|
191
|
+
}
|
|
192
|
+
yield nextLine;
|
|
193
|
+
allText = allText.slice(nextLine.length);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function minIndent(allText) {
|
|
197
|
+
let leftMost;
|
|
198
|
+
for (const line of linesOf(allText)) {
|
|
199
|
+
// look for lines starting with spaces, that have a non blank character somewhere
|
|
200
|
+
const leadingMatch = line.match(/^( *).*[^\s]/);
|
|
201
|
+
if (leadingMatch) {
|
|
202
|
+
const indentBy = leadingMatch[1].length;
|
|
203
|
+
if (leftMost === undefined || indentBy < leftMost) {
|
|
204
|
+
leftMost = indentBy;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return leftMost;
|
|
209
|
+
}
|
|
210
|
+
function unIndent(parts) {
|
|
211
|
+
let removeSpaces;
|
|
212
|
+
for (const part of parts) {
|
|
213
|
+
if (typeof part === 'string') {
|
|
214
|
+
const newLeft = minIndent(part);
|
|
215
|
+
if (newLeft !== undefined &&
|
|
216
|
+
(removeSpaces === undefined || newLeft < removeSpaces)) {
|
|
217
|
+
removeSpaces = newLeft;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (removeSpaces) {
|
|
222
|
+
for (let i = 0; i <= parts.length; i += 1) {
|
|
223
|
+
const ent = parts[i];
|
|
224
|
+
if (typeof ent === 'string') {
|
|
225
|
+
let newEnt = '';
|
|
226
|
+
for (let line of linesOf(ent)) {
|
|
227
|
+
if (line[0] === ' ') {
|
|
228
|
+
line = line.slice(removeSpaces);
|
|
229
|
+
}
|
|
230
|
+
newEnt += line;
|
|
231
|
+
}
|
|
232
|
+
parts[i] = newEnt;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
exports.unIndent = unIndent;
|
|
238
|
+
//# sourceMappingURL=parse-utils.js.map
|
|
@@ -23,38 +23,133 @@
|
|
|
23
23
|
*/
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
25
|
require("./parse-expects");
|
|
26
|
-
const
|
|
27
|
-
|
|
26
|
+
const parse_utils_1 = require("../parse-utils");
|
|
27
|
+
const test_translator_1 = require("./test-translator");
|
|
28
|
+
describe('quote comprehension inside strings', () => {
|
|
28
29
|
test('\\b', () => {
|
|
29
|
-
expect((0,
|
|
30
|
+
expect((0, parse_utils_1.parseString)('\\b')).toEqual('\b');
|
|
30
31
|
});
|
|
31
32
|
test('\\f', () => {
|
|
32
|
-
expect((0,
|
|
33
|
+
expect((0, parse_utils_1.parseString)('\\f')).toEqual('\f');
|
|
33
34
|
});
|
|
34
35
|
test('\\n', () => {
|
|
35
|
-
expect((0,
|
|
36
|
+
expect((0, parse_utils_1.parseString)('\\n')).toEqual('\n');
|
|
36
37
|
});
|
|
37
38
|
test('\\r', () => {
|
|
38
|
-
expect((0,
|
|
39
|
+
expect((0, parse_utils_1.parseString)('\\r')).toEqual('\r');
|
|
39
40
|
});
|
|
40
41
|
test('\\t', () => {
|
|
41
|
-
expect((0,
|
|
42
|
+
expect((0, parse_utils_1.parseString)('\\t')).toEqual('\t');
|
|
42
43
|
});
|
|
43
44
|
test('unicode ?', () => {
|
|
44
|
-
expect((0,
|
|
45
|
-
expect((0,
|
|
45
|
+
expect((0, parse_utils_1.parseString)('\\u003f')).toEqual('?');
|
|
46
|
+
expect((0, parse_utils_1.parseString)('\\u003F')).toEqual('?');
|
|
46
47
|
});
|
|
47
48
|
test('normal stuff', () => {
|
|
48
|
-
expect((0,
|
|
49
|
+
expect((0, parse_utils_1.parseString)('normal stuff')).toEqual('normal stuff');
|
|
49
50
|
});
|
|
50
51
|
test('stuff & nonsense', () => {
|
|
51
|
-
expect((0,
|
|
52
|
+
expect((0, parse_utils_1.parseString)('stuff \\u0026 nonsense')).toEqual('stuff & nonsense');
|
|
52
53
|
});
|
|
53
54
|
test('one thing\\nnext thing', () => {
|
|
54
|
-
expect((0,
|
|
55
|
+
expect((0, parse_utils_1.parseString)('one thing\\nnext thing')).toEqual('one thing\nnext thing');
|
|
55
56
|
});
|
|
56
57
|
test('quote stripping works', () => {
|
|
57
|
-
expect((0,
|
|
58
|
+
expect((0, parse_utils_1.parseString)('|42|', '|')).toEqual('42');
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
describe('string parsing in language', () => {
|
|
62
|
+
const tz = 'America/Mexico_City';
|
|
63
|
+
test('multi-line indent increasing', () => {
|
|
64
|
+
const checking = new test_translator_1.BetaExpression(`"""
|
|
65
|
+
left
|
|
66
|
+
mid
|
|
67
|
+
right
|
|
68
|
+
"""`);
|
|
69
|
+
expect(checking).toTranslate();
|
|
70
|
+
const v = checking.generated().value[0];
|
|
71
|
+
expect(v).toMatchObject({ literal: '\nleft\n mid\n right\n' });
|
|
72
|
+
});
|
|
73
|
+
test('multi-line indent decreasing', () => {
|
|
74
|
+
const checking = new test_translator_1.BetaExpression(`"""
|
|
75
|
+
right
|
|
76
|
+
mid
|
|
77
|
+
left
|
|
78
|
+
"""`);
|
|
79
|
+
expect(checking).toTranslate();
|
|
80
|
+
const v = checking.generated().value[0];
|
|
81
|
+
expect(v).toMatchObject({ literal: '\n right\n mid\nleft\n' });
|
|
82
|
+
});
|
|
83
|
+
test('multi-line indent keep', () => {
|
|
84
|
+
const checking = new test_translator_1.BetaExpression(`"""right
|
|
85
|
+
mid
|
|
86
|
+
left"""`);
|
|
87
|
+
expect(checking).toTranslate();
|
|
88
|
+
const v = checking.generated().value[0];
|
|
89
|
+
expect(v).toMatchObject({ literal: 'right\n mid\n left' });
|
|
90
|
+
});
|
|
91
|
+
test('timezone single quote', () => {
|
|
92
|
+
const m = new test_translator_1.TestTranslator(`run: a-> {timezone: '${tz}'; project: *}`);
|
|
93
|
+
expect(m).toParse();
|
|
94
|
+
});
|
|
95
|
+
test('timezone double quote', () => {
|
|
96
|
+
const m = new test_translator_1.TestTranslator(`run: a-> {timezone: "${tz}"; project: *}`);
|
|
97
|
+
expect(m).toParse();
|
|
98
|
+
});
|
|
99
|
+
test('timezone triple quote', () => {
|
|
100
|
+
const m = new test_translator_1.TestTranslator(`run: a->{timezone: """${tz}"""; project: *}`);
|
|
101
|
+
expect(m).toParse();
|
|
102
|
+
});
|
|
103
|
+
test('timezone with illegal query', () => {
|
|
104
|
+
expect(`run: a->{timezone: """${tz}%{ab->aturtle}%"""; project: *}`).translationToFailWith('%{ query }% illegal in this string');
|
|
105
|
+
});
|
|
106
|
+
test('table single quote', () => {
|
|
107
|
+
const m = new test_translator_1.TestTranslator("source: n is bigquery.table('n')");
|
|
108
|
+
expect(m).toParse();
|
|
109
|
+
});
|
|
110
|
+
test('table double quote', () => {
|
|
111
|
+
const m = new test_translator_1.TestTranslator('source: n is bigquery.table("n")');
|
|
112
|
+
expect(m).toParse();
|
|
113
|
+
});
|
|
114
|
+
test('table triple quote', () => {
|
|
115
|
+
const m = new test_translator_1.TestTranslator('source: n is bigquery.table("""n""")');
|
|
116
|
+
expect(m).toParse();
|
|
117
|
+
});
|
|
118
|
+
test('sql single quote', () => {
|
|
119
|
+
const m = new test_translator_1.TestTranslator("source: n is bigquery.sql('n')");
|
|
120
|
+
expect(m).toParse();
|
|
121
|
+
});
|
|
122
|
+
test('sql double quote', () => {
|
|
123
|
+
const m = new test_translator_1.TestTranslator('source: n is bigquery.sql("n")');
|
|
124
|
+
expect(m).toParse();
|
|
125
|
+
});
|
|
126
|
+
test('sql triple quote', () => {
|
|
127
|
+
const m = new test_translator_1.TestTranslator('source: n is bigquery.sql("""n""")');
|
|
128
|
+
expect(m).toParse();
|
|
129
|
+
});
|
|
130
|
+
test('import single quote', () => {
|
|
131
|
+
const m = new test_translator_1.TestTranslator("import 'a'");
|
|
132
|
+
expect(m).toParse();
|
|
133
|
+
});
|
|
134
|
+
test('import double quote', () => {
|
|
135
|
+
const m = new test_translator_1.TestTranslator('import "a"');
|
|
136
|
+
expect(m).toParse();
|
|
137
|
+
});
|
|
138
|
+
test('import triple quote', () => {
|
|
139
|
+
const m = new test_translator_1.TestTranslator('import """a"""');
|
|
140
|
+
expect(m).toParse();
|
|
141
|
+
});
|
|
142
|
+
test('literal single quote', () => {
|
|
143
|
+
const x = new test_translator_1.BetaExpression("'x'");
|
|
144
|
+
expect(x).toParse();
|
|
145
|
+
});
|
|
146
|
+
test('literal double quote', () => {
|
|
147
|
+
const x = new test_translator_1.BetaExpression('"x"');
|
|
148
|
+
expect(x).toParse();
|
|
149
|
+
});
|
|
150
|
+
test('literal triple quote', () => {
|
|
151
|
+
const x = new test_translator_1.BetaExpression('"""x"""');
|
|
152
|
+
expect(x).toParse();
|
|
58
153
|
});
|
|
59
154
|
});
|
|
60
155
|
//# sourceMappingURL=strings.spec.js.map
|
package/package.json
CHANGED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parses the interior of a string, doing all \ substitutions. In most cases
|
|
3
|
-
* a lexical analyzer has already recognized this as a string. As a convenience,
|
|
4
|
-
* strip off the quoting outer chartacters if asked, then parse the interior of
|
|
5
|
-
* the string. The intention is to be compatible with JSON strings, in terms
|
|
6
|
-
* of which \X substitutions are processed.
|
|
7
|
-
* @param str is the string to parse
|
|
8
|
-
* @param surround is the quoting character, default means quotes already stripped
|
|
9
|
-
* @returns a string with the \ processing completed
|
|
10
|
-
*/
|
|
11
|
-
export declare function parseString(str: string, surround?: string): string;
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright 2023 Google LLC
|
|
4
|
-
*
|
|
5
|
-
* Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
-
* a copy of this software and associated documentation files
|
|
7
|
-
* (the "Software"), to deal in the Software without restriction,
|
|
8
|
-
* including without limitation the rights to use, copy, modify, merge,
|
|
9
|
-
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
10
|
-
* and to permit persons to whom the Software is furnished to do so,
|
|
11
|
-
* subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be
|
|
14
|
-
* included in all copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
-
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
-
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
-
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
20
|
-
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
-
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
22
|
-
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.parseString = void 0;
|
|
26
|
-
var ParseState;
|
|
27
|
-
(function (ParseState) {
|
|
28
|
-
ParseState[ParseState["Normal"] = 0] = "Normal";
|
|
29
|
-
ParseState[ParseState["ReverseVirgule"] = 1] = "ReverseVirgule";
|
|
30
|
-
ParseState[ParseState["Unicode"] = 2] = "Unicode";
|
|
31
|
-
})(ParseState || (ParseState = {}));
|
|
32
|
-
/**
|
|
33
|
-
* Parses the interior of a string, doing all \ substitutions. In most cases
|
|
34
|
-
* a lexical analyzer has already recognized this as a string. As a convenience,
|
|
35
|
-
* strip off the quoting outer chartacters if asked, then parse the interior of
|
|
36
|
-
* the string. The intention is to be compatible with JSON strings, in terms
|
|
37
|
-
* of which \X substitutions are processed.
|
|
38
|
-
* @param str is the string to parse
|
|
39
|
-
* @param surround is the quoting character, default means quotes already stripped
|
|
40
|
-
* @returns a string with the \ processing completed
|
|
41
|
-
*/
|
|
42
|
-
function parseString(str, surround = '') {
|
|
43
|
-
let inner = str.slice(surround.length);
|
|
44
|
-
let state = ParseState.Normal;
|
|
45
|
-
if (surround.length) {
|
|
46
|
-
inner = inner.slice(0, -surround.length);
|
|
47
|
-
}
|
|
48
|
-
let out = '';
|
|
49
|
-
let unicode = '';
|
|
50
|
-
for (const c of inner) {
|
|
51
|
-
switch (state) {
|
|
52
|
-
case ParseState.Normal: {
|
|
53
|
-
if (c === '\\') {
|
|
54
|
-
state = ParseState.ReverseVirgule;
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
out += c;
|
|
58
|
-
}
|
|
59
|
-
break;
|
|
60
|
-
}
|
|
61
|
-
case ParseState.ReverseVirgule: {
|
|
62
|
-
let outc = c;
|
|
63
|
-
if (c === 'u') {
|
|
64
|
-
state = ParseState.Unicode;
|
|
65
|
-
unicode = '';
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
if (c === 'b') {
|
|
69
|
-
outc = '\b';
|
|
70
|
-
}
|
|
71
|
-
else if (c === 'f') {
|
|
72
|
-
outc = '\f';
|
|
73
|
-
}
|
|
74
|
-
else if (c === 'n') {
|
|
75
|
-
outc = '\n';
|
|
76
|
-
}
|
|
77
|
-
else if (c === 'r') {
|
|
78
|
-
outc = '\r';
|
|
79
|
-
}
|
|
80
|
-
else if (c === 't') {
|
|
81
|
-
outc = '\t';
|
|
82
|
-
}
|
|
83
|
-
out += outc;
|
|
84
|
-
state = ParseState.Normal;
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
case ParseState.Unicode: {
|
|
88
|
-
if ('ABCDEFabcdef0123456789'.includes(c)) {
|
|
89
|
-
unicode += c;
|
|
90
|
-
if (unicode.length === 4) {
|
|
91
|
-
out += String.fromCharCode(parseInt(unicode, 16));
|
|
92
|
-
state = ParseState.Normal;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
// Don't think we ever get here ...
|
|
97
|
-
state = ParseState.Normal;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return out;
|
|
103
|
-
}
|
|
104
|
-
exports.parseString = parseString;
|
|
105
|
-
//# sourceMappingURL=string-parser.js.map
|