@malloydata/malloy-tag 0.0.360 → 0.0.361
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/index.d.ts +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/parser.d.ts +24 -4
- package/dist/parser.js +52 -15
- package/dist/parser.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/parser.ts +59 -15
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -36,7 +36,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
36
36
|
};
|
|
37
37
|
})();
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.ParseUtil = exports.TagParser = exports.parseTag = void 0;
|
|
39
|
+
exports.ParseUtil = exports.TagParser = exports.parseAnnotation = exports.parseTag = void 0;
|
|
40
40
|
/*
|
|
41
41
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
42
42
|
*
|
|
@@ -46,6 +46,7 @@ exports.ParseUtil = exports.TagParser = exports.parseTag = void 0;
|
|
|
46
46
|
__exportStar(require("./tags"), exports);
|
|
47
47
|
var parser_1 = require("./parser");
|
|
48
48
|
Object.defineProperty(exports, "parseTag", { enumerable: true, get: function () { return parser_1.parseTag; } });
|
|
49
|
+
Object.defineProperty(exports, "parseAnnotation", { enumerable: true, get: function () { return parser_1.parseAnnotation; } });
|
|
49
50
|
Object.defineProperty(exports, "TagParser", { enumerable: true, get: function () { return parser_1.TagParser; } });
|
|
50
51
|
exports.ParseUtil = __importStar(require("./util"));
|
|
51
52
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;GAKG;AACH,yCAAuB;AACvB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;GAKG;AACH,yCAAuB;AACvB,mCAA8D;AAAtD,kGAAA,QAAQ,OAAA;AAAE,yGAAA,eAAe,OAAA;AAAE,mGAAA,SAAS,OAAA;AAE5C,oDAAoC"}
|
package/dist/parser.d.ts
CHANGED
|
@@ -11,22 +11,42 @@ export interface SourceOrigin {
|
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
13
|
* Session-based parser for Malloy tag language. Create an instance,
|
|
14
|
-
* call parse() for each line, then finish() to
|
|
14
|
+
* call parse() or parseAnnotation() for each line, then finish() to
|
|
15
|
+
* get the final Tag.
|
|
15
16
|
*/
|
|
16
17
|
export declare class TagParser {
|
|
17
18
|
private session;
|
|
18
19
|
private origins;
|
|
19
20
|
constructor();
|
|
21
|
+
/**
|
|
22
|
+
* Parse raw MOTLY text. No prefix stripping is performed.
|
|
23
|
+
*/
|
|
20
24
|
parse(source: string, origin?: SourceOrigin): TagParse;
|
|
25
|
+
/**
|
|
26
|
+
* Parse annotation text (starting with #). The annotation prefix
|
|
27
|
+
* is unconditionally stripped before parsing the MOTLY content.
|
|
28
|
+
*/
|
|
29
|
+
parseAnnotation(source: string, origin?: SourceOrigin): TagParse;
|
|
30
|
+
private parseSource;
|
|
21
31
|
finish(): Tag;
|
|
22
32
|
}
|
|
23
33
|
/**
|
|
24
|
-
* Parse
|
|
34
|
+
* Parse raw MOTLY text into a Tag. No prefix stripping is performed.
|
|
25
35
|
*
|
|
26
|
-
* @param source - A single string or array of strings to parse.
|
|
27
|
-
* starts with #, all characters up to the first space are skipped.
|
|
36
|
+
* @param source - A single string or array of strings to parse.
|
|
28
37
|
* When an array is provided, strings are parsed sequentially and merged.
|
|
29
38
|
* @returns TagParse with the resulting tag and any errors. For arrays,
|
|
30
39
|
* error line numbers indicate the index in the array where the error occurred.
|
|
31
40
|
*/
|
|
32
41
|
export declare function parseTag(source: string | string[]): TagParse;
|
|
42
|
+
/**
|
|
43
|
+
* Parse Malloy annotation text into a Tag. The annotation prefix
|
|
44
|
+
* (e.g., "# ", "#@ ", "#(docs) ") is unconditionally stripped before
|
|
45
|
+
* parsing the MOTLY content.
|
|
46
|
+
*
|
|
47
|
+
* @param source - A single string or array of annotation strings to parse.
|
|
48
|
+
* When an array is provided, strings are parsed sequentially and merged.
|
|
49
|
+
* @returns TagParse with the resulting tag and any errors. For arrays,
|
|
50
|
+
* error line numbers indicate the index in the array where the error occurred.
|
|
51
|
+
*/
|
|
52
|
+
export declare function parseAnnotation(source: string | string[]): TagParse;
|
package/dist/parser.js
CHANGED
|
@@ -6,20 +6,21 @@
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.TagParser = void 0;
|
|
8
8
|
exports.parseTag = parseTag;
|
|
9
|
+
exports.parseAnnotation = parseAnnotation;
|
|
9
10
|
const motly_ts_parser_1 = require("@malloydata/motly-ts-parser");
|
|
10
11
|
const tags_1 = require("./tags");
|
|
11
12
|
/**
|
|
12
|
-
* Strip the Malloy
|
|
13
|
+
* Strip the Malloy annotation prefix (e.g., "# " or "#(docs) ") from source.
|
|
14
|
+
* Annotation text starts with # followed by routing characters and a
|
|
15
|
+
* space or newline delimiter. Everything up to and including that delimiter
|
|
16
|
+
* is stripped, leaving just the MOTLY content.
|
|
13
17
|
*/
|
|
14
18
|
function stripPrefix(source) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return source.slice(skipTo);
|
|
19
|
-
}
|
|
20
|
-
return '';
|
|
19
|
+
const skipTo = source.search(/[ \n]/);
|
|
20
|
+
if (skipTo > 0) {
|
|
21
|
+
return source.slice(skipTo);
|
|
21
22
|
}
|
|
22
|
-
return
|
|
23
|
+
return '';
|
|
23
24
|
}
|
|
24
25
|
/**
|
|
25
26
|
* Map a MOTLYError (from motly-ts parser) to a TagError.
|
|
@@ -100,19 +101,32 @@ function hydrate(pv, parent, origins) {
|
|
|
100
101
|
}
|
|
101
102
|
/**
|
|
102
103
|
* Session-based parser for Malloy tag language. Create an instance,
|
|
103
|
-
* call parse() for each line, then finish() to
|
|
104
|
+
* call parse() or parseAnnotation() for each line, then finish() to
|
|
105
|
+
* get the final Tag.
|
|
104
106
|
*/
|
|
105
107
|
class TagParser {
|
|
106
108
|
constructor() {
|
|
107
109
|
this.origins = new Map();
|
|
108
110
|
this.session = new motly_ts_parser_1.MOTLYSession();
|
|
109
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Parse raw MOTLY text. No prefix stripping is performed.
|
|
114
|
+
*/
|
|
110
115
|
parse(source, origin) {
|
|
116
|
+
return this.parseSource(source, 0, origin);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Parse annotation text (starting with #). The annotation prefix
|
|
120
|
+
* is unconditionally stripped before parsing the MOTLY content.
|
|
121
|
+
*/
|
|
122
|
+
parseAnnotation(source, origin) {
|
|
111
123
|
const stripped = stripPrefix(source);
|
|
112
|
-
const
|
|
124
|
+
const prefixLen = source.length - stripped.length;
|
|
125
|
+
return this.parseSource(stripped, prefixLen, origin);
|
|
126
|
+
}
|
|
127
|
+
parseSource(source, prefixLen, origin) {
|
|
128
|
+
const { parseId, errors } = this.session.parse(source);
|
|
113
129
|
if (origin) {
|
|
114
|
-
// Adjust column for the stripped prefix
|
|
115
|
-
const prefixLen = source.length - stripped.length;
|
|
116
130
|
this.origins.set(parseId, {
|
|
117
131
|
url: origin.url,
|
|
118
132
|
startLine: origin.startLine,
|
|
@@ -131,10 +145,9 @@ class TagParser {
|
|
|
131
145
|
}
|
|
132
146
|
exports.TagParser = TagParser;
|
|
133
147
|
/**
|
|
134
|
-
* Parse
|
|
148
|
+
* Parse raw MOTLY text into a Tag. No prefix stripping is performed.
|
|
135
149
|
*
|
|
136
|
-
* @param source - A single string or array of strings to parse.
|
|
137
|
-
* starts with #, all characters up to the first space are skipped.
|
|
150
|
+
* @param source - A single string or array of strings to parse.
|
|
138
151
|
* When an array is provided, strings are parsed sequentially and merged.
|
|
139
152
|
* @returns TagParse with the resulting tag and any errors. For arrays,
|
|
140
153
|
* error line numbers indicate the index in the array where the error occurred.
|
|
@@ -153,4 +166,28 @@ function parseTag(source) {
|
|
|
153
166
|
}
|
|
154
167
|
return { tag: session.finish(), log: allErrs };
|
|
155
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Parse Malloy annotation text into a Tag. The annotation prefix
|
|
171
|
+
* (e.g., "# ", "#@ ", "#(docs) ") is unconditionally stripped before
|
|
172
|
+
* parsing the MOTLY content.
|
|
173
|
+
*
|
|
174
|
+
* @param source - A single string or array of annotation strings to parse.
|
|
175
|
+
* When an array is provided, strings are parsed sequentially and merged.
|
|
176
|
+
* @returns TagParse with the resulting tag and any errors. For arrays,
|
|
177
|
+
* error line numbers indicate the index in the array where the error occurred.
|
|
178
|
+
*/
|
|
179
|
+
function parseAnnotation(source) {
|
|
180
|
+
const session = new TagParser();
|
|
181
|
+
if (typeof source === 'string') {
|
|
182
|
+
return session.parseAnnotation(source);
|
|
183
|
+
}
|
|
184
|
+
const allErrs = [];
|
|
185
|
+
for (let i = 0; i < source.length; i++) {
|
|
186
|
+
const result = session.parseAnnotation(source[i]);
|
|
187
|
+
for (const err of result.log) {
|
|
188
|
+
allErrs.push({ ...err, line: i + err.line });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return { tag: session.finish(), log: allErrs };
|
|
192
|
+
}
|
|
156
193
|
//# sourceMappingURL=parser.js.map
|
package/dist/parser.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;
|
|
1
|
+
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AA8LH,4BAcC;AAYD,0CAcC;AA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AAC,CAAC,CAAC,CAAC,CAAC;QACxC,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,GAAG,EAAE,CAAC;YAC7B,OAAO,CAAC,IAAI,CAAC,EAAC,GAAG,GAAG,EAAE,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC,IAAI,EAAC,CAAC,CAAC;QAC7C,CAAC;IACH,CAAC;IACD,OAAO,EAAC,GAAG,EAAE,OAAO,CAAC,MAAM,EAAE,EAAE,GAAG,EAAE,OAAO,EAAC,CAAC;AAC/C,CAAC;AAED;;;;;;;;;GASG;AACH,SAAgB,eAAe,CAAC,MAAyB;IACvD,MAAM,OAAO,GAAG,IAAI,SAAS,EAAE,CAAC;IAChC,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;IACzC,CAAC;IAED,MAAM,OAAO,GAAe,EAAE,CAAC;IAC/B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,MAAM,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QAClD,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,GAAG,EAAE,CAAC;YAC7B,OAAO,CAAC,IAAI,CAAC,EAAC,GAAG,GAAG,EAAE,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC,IAAI,EAAC,CAAC,CAAC;QAC7C,CAAC;IACH,CAAC;IACD,OAAO,EAAC,GAAG,EAAE,OAAO,CAAC,MAAM,EAAE,EAAE,GAAG,EAAE,OAAO,EAAC,CAAC;AAC/C,CAAC"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
export * from './tags';
|
|
8
|
-
export {parseTag, TagParser} from './parser';
|
|
8
|
+
export {parseTag, parseAnnotation, TagParser} from './parser';
|
|
9
9
|
export type {SourceOrigin} from './parser';
|
|
10
10
|
export * as ParseUtil from './util';
|
package/src/parser.ts
CHANGED
|
@@ -23,17 +23,17 @@ export interface SourceOrigin {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
|
-
* Strip the Malloy
|
|
26
|
+
* Strip the Malloy annotation prefix (e.g., "# " or "#(docs) ") from source.
|
|
27
|
+
* Annotation text starts with # followed by routing characters and a
|
|
28
|
+
* space or newline delimiter. Everything up to and including that delimiter
|
|
29
|
+
* is stripped, leaving just the MOTLY content.
|
|
27
30
|
*/
|
|
28
31
|
function stripPrefix(source: string): string {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return source.slice(skipTo);
|
|
33
|
-
}
|
|
34
|
-
return '';
|
|
32
|
+
const skipTo = source.search(/[ \n]/);
|
|
33
|
+
if (skipTo > 0) {
|
|
34
|
+
return source.slice(skipTo);
|
|
35
35
|
}
|
|
36
|
-
return
|
|
36
|
+
return '';
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
/**
|
|
@@ -130,7 +130,8 @@ function hydrate(
|
|
|
130
130
|
|
|
131
131
|
/**
|
|
132
132
|
* Session-based parser for Malloy tag language. Create an instance,
|
|
133
|
-
* call parse() for each line, then finish() to
|
|
133
|
+
* call parse() or parseAnnotation() for each line, then finish() to
|
|
134
|
+
* get the final Tag.
|
|
134
135
|
*/
|
|
135
136
|
export class TagParser {
|
|
136
137
|
private session: MOTLYSession;
|
|
@@ -140,12 +141,30 @@ export class TagParser {
|
|
|
140
141
|
this.session = new MOTLYSession();
|
|
141
142
|
}
|
|
142
143
|
|
|
144
|
+
/**
|
|
145
|
+
* Parse raw MOTLY text. No prefix stripping is performed.
|
|
146
|
+
*/
|
|
143
147
|
parse(source: string, origin?: SourceOrigin): TagParse {
|
|
148
|
+
return this.parseSource(source, 0, origin);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Parse annotation text (starting with #). The annotation prefix
|
|
153
|
+
* is unconditionally stripped before parsing the MOTLY content.
|
|
154
|
+
*/
|
|
155
|
+
parseAnnotation(source: string, origin?: SourceOrigin): TagParse {
|
|
144
156
|
const stripped = stripPrefix(source);
|
|
145
|
-
const
|
|
157
|
+
const prefixLen = source.length - stripped.length;
|
|
158
|
+
return this.parseSource(stripped, prefixLen, origin);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private parseSource(
|
|
162
|
+
source: string,
|
|
163
|
+
prefixLen: number,
|
|
164
|
+
origin?: SourceOrigin
|
|
165
|
+
): TagParse {
|
|
166
|
+
const {parseId, errors} = this.session.parse(source);
|
|
146
167
|
if (origin) {
|
|
147
|
-
// Adjust column for the stripped prefix
|
|
148
|
-
const prefixLen = source.length - stripped.length;
|
|
149
168
|
this.origins.set(parseId, {
|
|
150
169
|
url: origin.url,
|
|
151
170
|
startLine: origin.startLine,
|
|
@@ -165,10 +184,9 @@ export class TagParser {
|
|
|
165
184
|
}
|
|
166
185
|
|
|
167
186
|
/**
|
|
168
|
-
* Parse
|
|
187
|
+
* Parse raw MOTLY text into a Tag. No prefix stripping is performed.
|
|
169
188
|
*
|
|
170
|
-
* @param source - A single string or array of strings to parse.
|
|
171
|
-
* starts with #, all characters up to the first space are skipped.
|
|
189
|
+
* @param source - A single string or array of strings to parse.
|
|
172
190
|
* When an array is provided, strings are parsed sequentially and merged.
|
|
173
191
|
* @returns TagParse with the resulting tag and any errors. For arrays,
|
|
174
192
|
* error line numbers indicate the index in the array where the error occurred.
|
|
@@ -188,3 +206,29 @@ export function parseTag(source: string | string[]): TagParse {
|
|
|
188
206
|
}
|
|
189
207
|
return {tag: session.finish(), log: allErrs};
|
|
190
208
|
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Parse Malloy annotation text into a Tag. The annotation prefix
|
|
212
|
+
* (e.g., "# ", "#@ ", "#(docs) ") is unconditionally stripped before
|
|
213
|
+
* parsing the MOTLY content.
|
|
214
|
+
*
|
|
215
|
+
* @param source - A single string or array of annotation strings to parse.
|
|
216
|
+
* When an array is provided, strings are parsed sequentially and merged.
|
|
217
|
+
* @returns TagParse with the resulting tag and any errors. For arrays,
|
|
218
|
+
* error line numbers indicate the index in the array where the error occurred.
|
|
219
|
+
*/
|
|
220
|
+
export function parseAnnotation(source: string | string[]): TagParse {
|
|
221
|
+
const session = new TagParser();
|
|
222
|
+
if (typeof source === 'string') {
|
|
223
|
+
return session.parseAnnotation(source);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const allErrs: TagError[] = [];
|
|
227
|
+
for (let i = 0; i < source.length; i++) {
|
|
228
|
+
const result = session.parseAnnotation(source[i]);
|
|
229
|
+
for (const err of result.log) {
|
|
230
|
+
allErrs.push({...err, line: i + err.line});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return {tag: session.finish(), log: allErrs};
|
|
234
|
+
}
|