tagset-parser 0.0.0

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.
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TagSetVisitor = void 0;
4
+ const antlr4ng_1 = require("antlr4ng");
5
+ /**
6
+ * This interface defines a complete generic visitor for a parse tree produced
7
+ * by `TagSetParser`.
8
+ *
9
+ * @param <Result> The return type of the visit operation. Use `void` for
10
+ * operations with no return type.
11
+ */
12
+ class TagSetVisitor extends antlr4ng_1.AbstractParseTreeVisitor {
13
+ /**
14
+ * Visit a parse tree produced by `TagSetParser.program`.
15
+ * @param ctx the parse tree
16
+ * @return the visitor result
17
+ */
18
+ visitProgram;
19
+ /**
20
+ * Visit a parse tree produced by `TagSetParser.line`.
21
+ * @param ctx the parse tree
22
+ * @return the visitor result
23
+ */
24
+ visitLine;
25
+ /**
26
+ * Visit a parse tree produced by `TagSetParser.setDecl`.
27
+ * @param ctx the parse tree
28
+ * @return the visitor result
29
+ */
30
+ visitSetDecl;
31
+ /**
32
+ * Visit a parse tree produced by `TagSetParser.labelPart`.
33
+ * @param ctx the parse tree
34
+ * @return the visitor result
35
+ */
36
+ visitLabelPart;
37
+ /**
38
+ * Visit a parse tree produced by `TagSetParser.itemDecl`.
39
+ * @param ctx the parse tree
40
+ * @return the visitor result
41
+ */
42
+ visitItemDecl;
43
+ /**
44
+ * Visit a parse tree produced by `TagSetParser.sugarDecl`.
45
+ * @param ctx the parse tree
46
+ * @return the visitor result
47
+ */
48
+ visitSugarDecl;
49
+ /**
50
+ * Visit a parse tree produced by `TagSetParser.pattern`.
51
+ * @param ctx the parse tree
52
+ * @return the visitor result
53
+ */
54
+ visitPattern;
55
+ /**
56
+ * Visit a parse tree produced by `TagSetParser.valueList`.
57
+ * @param ctx the parse tree
58
+ * @return the visitor result
59
+ */
60
+ visitValueList;
61
+ /**
62
+ * Visit a parse tree produced by `TagSetParser.valueItem`.
63
+ * @param ctx the parse tree
64
+ * @return the visitor result
65
+ */
66
+ visitValueItem;
67
+ }
68
+ exports.TagSetVisitor = TagSetVisitor;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parse = void 0;
4
+ var parser_js_1 = require("./parser.js");
5
+ Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parser_js_1.parse; } });
@@ -0,0 +1 @@
1
+ {"type": "commonjs"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parse = parse;
4
+ const antlr4ng_1 = require("antlr4ng");
5
+ const TagSetLexer_js_1 = require("./generated/TagSetLexer.js");
6
+ const TagSetParser_js_1 = require("./generated/TagSetParser.js");
7
+ const visitor_js_1 = require("./visitor.js");
8
+ function parse(input) {
9
+ const chars = antlr4ng_1.CharStream.fromString(input);
10
+ const lexer = new TagSetLexer_js_1.TagSetLexer(chars);
11
+ const tokens = new antlr4ng_1.CommonTokenStream(lexer);
12
+ const parser = new TagSetParser_js_1.TagSetParser(tokens);
13
+ const tree = parser.program();
14
+ const visitor = new visitor_js_1.TagSetASTVisitor();
15
+ tree.accept(visitor);
16
+ return visitor.buildAST();
17
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TagSetASTVisitor = void 0;
4
+ const TagSetVisitor_js_1 = require("./generated/TagSetVisitor.js");
5
+ const bitmask_js_1 = require("./bitmask.js");
6
+ function extractPatternIds(ctx) {
7
+ return ctx
8
+ .WORD()
9
+ .map((w) => w.getText())
10
+ .filter((t) => t !== '_');
11
+ }
12
+ function extractValues(ctx) {
13
+ return ctx.valueItem().map((vi) => {
14
+ const parts = [
15
+ ...vi.WORD().map((w) => w.getText()),
16
+ ...vi.QUOTED_STRING().map((q) => {
17
+ const text = q.getText();
18
+ return text.startsWith('"') && text.endsWith('"')
19
+ ? text.slice(1, -1)
20
+ : text;
21
+ }),
22
+ ];
23
+ return parts.join(' ');
24
+ });
25
+ }
26
+ function autoDetectSets(explicitSets, rawItems) {
27
+ const explicitIds = new Set(explicitSets.map((s) => s.id));
28
+ const discovered = [];
29
+ for (const item of rawItems) {
30
+ for (const id of item.patternIds) {
31
+ if (!explicitIds.has(id) && !discovered.includes(id)) {
32
+ discovered.push(id);
33
+ }
34
+ }
35
+ }
36
+ let nextIndex = explicitSets.length;
37
+ const autoSets = discovered.map((id) => ({
38
+ id,
39
+ label: id,
40
+ index: nextIndex++,
41
+ }));
42
+ return [...explicitSets, ...autoSets];
43
+ }
44
+ class TagSetASTVisitor extends TagSetVisitor_js_1.TagSetVisitor {
45
+ explicitSets = [];
46
+ rawItems = [];
47
+ setIndex = 0;
48
+ visitProgram = (ctx) => {
49
+ for (const line of ctx.line()) {
50
+ line.accept(this);
51
+ }
52
+ };
53
+ visitSetDecl = (ctx) => {
54
+ const id = ctx.WORD().getText();
55
+ const label = ctx
56
+ .labelPart()
57
+ .map((lp) => lp.getText())
58
+ .join(' ');
59
+ this.explicitSets.push({ id, label, index: this.setIndex++ });
60
+ };
61
+ visitItemDecl = (ctx) => {
62
+ const patternCtx = ctx.pattern();
63
+ const patternIds = extractPatternIds(patternCtx);
64
+ const values = extractValues(ctx.valueList());
65
+ this.rawItems.push({ patternIds, values });
66
+ };
67
+ visitSugarDecl = (ctx) => {
68
+ const patternIds = ctx
69
+ .WORD()
70
+ .map((w) => w.getText())
71
+ .filter((t) => t !== '_');
72
+ const values = extractValues(ctx.valueList());
73
+ this.rawItems.push({ patternIds, values });
74
+ };
75
+ buildAST() {
76
+ const sets = autoDetectSets(this.explicitSets, this.rawItems);
77
+ const items = this.rawItems.map((raw) => {
78
+ const pattern = raw.patternIds.join('&');
79
+ const bitmask = (0, bitmask_js_1.calcBitmask)(pattern, sets);
80
+ return { pattern, bitmask, values: raw.values };
81
+ });
82
+ return { sets, items };
83
+ }
84
+ }
85
+ exports.TagSetASTVisitor = TagSetASTVisitor;
@@ -0,0 +1,15 @@
1
+ export function calcBitmask(pattern, sets) {
2
+ const tokens = pattern.split('&');
3
+ let mask = 0;
4
+ for (const token of tokens) {
5
+ const t = token.trim();
6
+ if (t !== '' && t !== '_') {
7
+ for (const set of sets) {
8
+ if (set.id === t) {
9
+ mask = mask | (1 << set.index);
10
+ }
11
+ }
12
+ }
13
+ }
14
+ return mask;
15
+ }
@@ -0,0 +1,75 @@
1
+ import * as antlr from "antlr4ng";
2
+ export class TagSetLexer extends antlr.Lexer {
3
+ static SET = 1;
4
+ static ITEM = 2;
5
+ static AMP = 3;
6
+ static COMMA = 4;
7
+ static COLON = 5;
8
+ static QUOTED_STRING = 6;
9
+ static WORD = 7;
10
+ static NL = 8;
11
+ static WS = 9;
12
+ static channelNames = [
13
+ "DEFAULT_TOKEN_CHANNEL", "HIDDEN"
14
+ ];
15
+ static literalNames = [
16
+ null, "'set'", "'item'", "'&'", "','", "':'"
17
+ ];
18
+ static symbolicNames = [
19
+ null, "SET", "ITEM", "AMP", "COMMA", "COLON", "QUOTED_STRING", "WORD",
20
+ "NL", "WS"
21
+ ];
22
+ static modeNames = [
23
+ "DEFAULT_MODE",
24
+ ];
25
+ static ruleNames = [
26
+ "SET", "ITEM", "AMP", "COMMA", "COLON", "QUOTED_STRING", "WORD",
27
+ "NL", "WS",
28
+ ];
29
+ constructor(input) {
30
+ super(input);
31
+ this.interpreter = new antlr.LexerATNSimulator(this, TagSetLexer._ATN, TagSetLexer.decisionsToDFA, new antlr.PredictionContextCache());
32
+ }
33
+ get grammarFileName() { return "TagSet.g4"; }
34
+ get literalNames() { return TagSetLexer.literalNames; }
35
+ get symbolicNames() { return TagSetLexer.symbolicNames; }
36
+ get ruleNames() { return TagSetLexer.ruleNames; }
37
+ get serializedATN() { return TagSetLexer._serializedATN; }
38
+ get channelNames() { return TagSetLexer.channelNames; }
39
+ get modeNames() { return TagSetLexer.modeNames; }
40
+ static _serializedATN = [
41
+ 4, 0, 9, 60, 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,
42
+ 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1,
43
+ 2, 1, 3, 1, 3, 1, 4, 1, 4, 1, 5, 1, 5, 5, 5, 37, 8, 5, 10, 5, 12, 5, 40, 9, 5, 1, 5, 1, 5, 1,
44
+ 6, 4, 6, 45, 8, 6, 11, 6, 12, 6, 46, 1, 7, 4, 7, 50, 8, 7, 11, 7, 12, 7, 51, 1, 8, 4, 8, 55,
45
+ 8, 8, 11, 8, 12, 8, 56, 1, 8, 1, 8, 0, 0, 9, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15,
46
+ 8, 17, 9, 1, 0, 4, 3, 0, 10, 10, 13, 13, 34, 34, 7, 0, 9, 10, 13, 13, 32, 32, 34, 34, 38,
47
+ 38, 44, 44, 58, 58, 2, 0, 10, 10, 13, 13, 2, 0, 9, 9, 32, 32, 63, 0, 1, 1, 0, 0, 0, 0, 3,
48
+ 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,
49
+ 0, 0, 0, 0, 15, 1, 0, 0, 0, 0, 17, 1, 0, 0, 0, 1, 19, 1, 0, 0, 0, 3, 23, 1, 0, 0, 0, 5, 28, 1,
50
+ 0, 0, 0, 7, 30, 1, 0, 0, 0, 9, 32, 1, 0, 0, 0, 11, 34, 1, 0, 0, 0, 13, 44, 1, 0, 0, 0, 15, 49,
51
+ 1, 0, 0, 0, 17, 54, 1, 0, 0, 0, 19, 20, 5, 115, 0, 0, 20, 21, 5, 101, 0, 0, 21, 22, 5, 116,
52
+ 0, 0, 22, 2, 1, 0, 0, 0, 23, 24, 5, 105, 0, 0, 24, 25, 5, 116, 0, 0, 25, 26, 5, 101, 0, 0,
53
+ 26, 27, 5, 109, 0, 0, 27, 4, 1, 0, 0, 0, 28, 29, 5, 38, 0, 0, 29, 6, 1, 0, 0, 0, 30, 31, 5,
54
+ 44, 0, 0, 31, 8, 1, 0, 0, 0, 32, 33, 5, 58, 0, 0, 33, 10, 1, 0, 0, 0, 34, 38, 5, 34, 0, 0,
55
+ 35, 37, 8, 0, 0, 0, 36, 35, 1, 0, 0, 0, 37, 40, 1, 0, 0, 0, 38, 36, 1, 0, 0, 0, 38, 39, 1,
56
+ 0, 0, 0, 39, 41, 1, 0, 0, 0, 40, 38, 1, 0, 0, 0, 41, 42, 5, 34, 0, 0, 42, 12, 1, 0, 0, 0, 43,
57
+ 45, 8, 1, 0, 0, 44, 43, 1, 0, 0, 0, 45, 46, 1, 0, 0, 0, 46, 44, 1, 0, 0, 0, 46, 47, 1, 0, 0,
58
+ 0, 47, 14, 1, 0, 0, 0, 48, 50, 7, 2, 0, 0, 49, 48, 1, 0, 0, 0, 50, 51, 1, 0, 0, 0, 51, 49,
59
+ 1, 0, 0, 0, 51, 52, 1, 0, 0, 0, 52, 16, 1, 0, 0, 0, 53, 55, 7, 3, 0, 0, 54, 53, 1, 0, 0, 0,
60
+ 55, 56, 1, 0, 0, 0, 56, 54, 1, 0, 0, 0, 56, 57, 1, 0, 0, 0, 57, 58, 1, 0, 0, 0, 58, 59, 6,
61
+ 8, 0, 0, 59, 18, 1, 0, 0, 0, 5, 0, 38, 46, 51, 56, 1, 6, 0, 0
62
+ ];
63
+ static __ATN;
64
+ static get _ATN() {
65
+ if (!TagSetLexer.__ATN) {
66
+ TagSetLexer.__ATN = new antlr.ATNDeserializer().deserialize(TagSetLexer._serializedATN);
67
+ }
68
+ return TagSetLexer.__ATN;
69
+ }
70
+ static vocabulary = new antlr.Vocabulary(TagSetLexer.literalNames, TagSetLexer.symbolicNames, []);
71
+ get vocabulary() {
72
+ return TagSetLexer.vocabulary;
73
+ }
74
+ static decisionsToDFA = TagSetLexer._ATN.decisionToState.map((ds, index) => new antlr.DFA(ds, index));
75
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * This interface defines a complete listener for a parse tree produced by
3
+ * `TagSetParser`.
4
+ */
5
+ export class TagSetListener {
6
+ /**
7
+ * Enter a parse tree produced by `TagSetParser.program`.
8
+ * @param ctx the parse tree
9
+ */
10
+ enterProgram;
11
+ /**
12
+ * Exit a parse tree produced by `TagSetParser.program`.
13
+ * @param ctx the parse tree
14
+ */
15
+ exitProgram;
16
+ /**
17
+ * Enter a parse tree produced by `TagSetParser.line`.
18
+ * @param ctx the parse tree
19
+ */
20
+ enterLine;
21
+ /**
22
+ * Exit a parse tree produced by `TagSetParser.line`.
23
+ * @param ctx the parse tree
24
+ */
25
+ exitLine;
26
+ /**
27
+ * Enter a parse tree produced by `TagSetParser.setDecl`.
28
+ * @param ctx the parse tree
29
+ */
30
+ enterSetDecl;
31
+ /**
32
+ * Exit a parse tree produced by `TagSetParser.setDecl`.
33
+ * @param ctx the parse tree
34
+ */
35
+ exitSetDecl;
36
+ /**
37
+ * Enter a parse tree produced by `TagSetParser.labelPart`.
38
+ * @param ctx the parse tree
39
+ */
40
+ enterLabelPart;
41
+ /**
42
+ * Exit a parse tree produced by `TagSetParser.labelPart`.
43
+ * @param ctx the parse tree
44
+ */
45
+ exitLabelPart;
46
+ /**
47
+ * Enter a parse tree produced by `TagSetParser.itemDecl`.
48
+ * @param ctx the parse tree
49
+ */
50
+ enterItemDecl;
51
+ /**
52
+ * Exit a parse tree produced by `TagSetParser.itemDecl`.
53
+ * @param ctx the parse tree
54
+ */
55
+ exitItemDecl;
56
+ /**
57
+ * Enter a parse tree produced by `TagSetParser.sugarDecl`.
58
+ * @param ctx the parse tree
59
+ */
60
+ enterSugarDecl;
61
+ /**
62
+ * Exit a parse tree produced by `TagSetParser.sugarDecl`.
63
+ * @param ctx the parse tree
64
+ */
65
+ exitSugarDecl;
66
+ /**
67
+ * Enter a parse tree produced by `TagSetParser.pattern`.
68
+ * @param ctx the parse tree
69
+ */
70
+ enterPattern;
71
+ /**
72
+ * Exit a parse tree produced by `TagSetParser.pattern`.
73
+ * @param ctx the parse tree
74
+ */
75
+ exitPattern;
76
+ /**
77
+ * Enter a parse tree produced by `TagSetParser.valueList`.
78
+ * @param ctx the parse tree
79
+ */
80
+ enterValueList;
81
+ /**
82
+ * Exit a parse tree produced by `TagSetParser.valueList`.
83
+ * @param ctx the parse tree
84
+ */
85
+ exitValueList;
86
+ /**
87
+ * Enter a parse tree produced by `TagSetParser.valueItem`.
88
+ * @param ctx the parse tree
89
+ */
90
+ enterValueItem;
91
+ /**
92
+ * Exit a parse tree produced by `TagSetParser.valueItem`.
93
+ * @param ctx the parse tree
94
+ */
95
+ exitValueItem;
96
+ visitTerminal(node) { }
97
+ visitErrorNode(node) { }
98
+ enterEveryRule(node) { }
99
+ exitEveryRule(node) { }
100
+ }