parser-lr 0.5.0 → 0.6.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 @@
1
+ CASE IS < 0
@@ -0,0 +1,26 @@
1
+ name "nested-production-repro" ;
2
+
3
+ tokens
4
+ kw_is = /IS/i ;
5
+ kw_case = /CASE/i ;
6
+ less = /</ ;
7
+ integer = /[0-9]+/ ;
8
+
9
+ skip
10
+ whitespace = /[ \t\r\n]+/ ;
11
+
12
+ start line ;
13
+
14
+ grammar
15
+ line = kw_case case_selector ;
16
+ case_selector = #relational kw_is comparison_op integer ;
17
+ comparison_op = #less [tok]:less ;
18
+
19
+ ast
20
+ comparison_op =
21
+ #less [tok]:less
22
+ ;
23
+
24
+ transform
25
+ comparison_op ->
26
+ #less comparison_op.#less(tok) ;
@@ -0,0 +1,190 @@
1
+ {
2
+ "version": 2,
3
+ "algorithm": "lalr",
4
+ "grammarName": "calc",
5
+ "startSymbol": "expr",
6
+ "tokens": [
7
+ "number",
8
+ "plus",
9
+ "$eof"
10
+ ],
11
+ "tokenRules": [
12
+ {
13
+ "name": "number",
14
+ "pattern": "[0-9]+",
15
+ "flags": ""
16
+ },
17
+ {
18
+ "name": "plus",
19
+ "pattern": "\\+",
20
+ "flags": ""
21
+ }
22
+ ],
23
+ "skipRules": [
24
+ {
25
+ "name": "whitespace",
26
+ "pattern": "[ \\t\\r\\n]+",
27
+ "flags": ""
28
+ }
29
+ ],
30
+ "states": [],
31
+ "parserStateCount": 5,
32
+ "productions": [
33
+ {
34
+ "id": 0,
35
+ "name": "expr",
36
+ "rhs": [
37
+ "[left]:expr",
38
+ "[operator]:plus",
39
+ "[right]:number"
40
+ ],
41
+ "variant": "binary",
42
+ "origin": "expr"
43
+ },
44
+ {
45
+ "id": 1,
46
+ "name": "expr",
47
+ "rhs": [
48
+ "number"
49
+ ],
50
+ "variant": "literal",
51
+ "origin": "expr"
52
+ },
53
+ {
54
+ "id": 2,
55
+ "name": "$accept",
56
+ "rhs": [
57
+ "expr"
58
+ ],
59
+ "variant": null,
60
+ "origin": "$accept"
61
+ }
62
+ ],
63
+ "actions": [
64
+ {
65
+ "state": 0,
66
+ "symbol": "number",
67
+ "kind": "shift",
68
+ "target": 2
69
+ },
70
+ {
71
+ "state": 1,
72
+ "symbol": "$eof",
73
+ "kind": "accept"
74
+ },
75
+ {
76
+ "state": 1,
77
+ "symbol": "plus",
78
+ "kind": "shift",
79
+ "target": 3
80
+ },
81
+ {
82
+ "state": 2,
83
+ "symbol": "$eof",
84
+ "kind": "reduce",
85
+ "productionId": 1
86
+ },
87
+ {
88
+ "state": 2,
89
+ "symbol": "plus",
90
+ "kind": "reduce",
91
+ "productionId": 1
92
+ },
93
+ {
94
+ "state": 3,
95
+ "symbol": "number",
96
+ "kind": "shift",
97
+ "target": 4
98
+ },
99
+ {
100
+ "state": 4,
101
+ "symbol": "$eof",
102
+ "kind": "reduce",
103
+ "productionId": 0
104
+ },
105
+ {
106
+ "state": 4,
107
+ "symbol": "plus",
108
+ "kind": "reduce",
109
+ "productionId": 0
110
+ }
111
+ ],
112
+ "gotos": [
113
+ {
114
+ "state": 0,
115
+ "symbol": "expr",
116
+ "target": 1
117
+ }
118
+ ],
119
+ "ast": [
120
+ {
121
+ "name": "expr",
122
+ "expression": {
123
+ "kind": "choice",
124
+ "alternatives": [
125
+ {
126
+ "label": "binary",
127
+ "expression": {
128
+ "kind": "sequence",
129
+ "elements": [
130
+ {
131
+ "kind": "boundReference",
132
+ "binding": "left",
133
+ "name": "expr"
134
+ },
135
+ {
136
+ "kind": "boundReference",
137
+ "binding": "operator",
138
+ "name": "plus"
139
+ },
140
+ {
141
+ "kind": "boundReference",
142
+ "binding": "right",
143
+ "name": "number"
144
+ }
145
+ ]
146
+ }
147
+ },
148
+ {
149
+ "label": "literal",
150
+ "expression": {
151
+ "kind": "reference",
152
+ "name": "number"
153
+ }
154
+ }
155
+ ]
156
+ }
157
+ }
158
+ ],
159
+ "transform": [
160
+ {
161
+ "production": "expr",
162
+ "alternatives": [
163
+ {
164
+ "label": "binary",
165
+ "expression": {
166
+ "kind": "build",
167
+ "typeName": "expr",
168
+ "variant": "binary",
169
+ "arguments": [
170
+ "left",
171
+ "operator",
172
+ "right"
173
+ ]
174
+ }
175
+ },
176
+ {
177
+ "label": "literal",
178
+ "expression": {
179
+ "kind": "build",
180
+ "typeName": "expr",
181
+ "variant": "literal",
182
+ "arguments": [
183
+ "number"
184
+ ]
185
+ }
186
+ }
187
+ ]
188
+ }
189
+ ]
190
+ }
@@ -0,0 +1,38 @@
1
+ const { readFileSync } = require('node:fs');
2
+ const { join } = require('node:path');
3
+
4
+ const fs = require('node:fs');
5
+ const originalReadFileSync = fs.readFileSync;
6
+ let readMetaGrammar = false;
7
+
8
+ fs.readFileSync = function patchedReadFileSync(path, ...rest)
9
+ {
10
+ if (String(path).endsWith('grammar.json'))
11
+ {
12
+ readMetaGrammar = true;
13
+ }
14
+
15
+ return originalReadFileSync.call(this, path, ...rest);
16
+ };
17
+
18
+ const { ParseContext } = require('parser-lr');
19
+
20
+ const tableJson = readFileSync(
21
+ join(__dirname, 'calc.table.json'),
22
+ 'utf8',
23
+ );
24
+
25
+ const context = ParseContext.fromTableJson(tableJson);
26
+ const ast = context.parseSource('1 + 2');
27
+
28
+ if (ast === null)
29
+ {
30
+ throw new Error('table-only smoke (cjs): expected a non-null AST');
31
+ }
32
+
33
+ if (readMetaGrammar)
34
+ {
35
+ throw new Error('table-only smoke (cjs): unexpected grammar.json read');
36
+ }
37
+
38
+ process.stdout.write(`cjs ${ast.symbol}\n`);
@@ -0,0 +1,18 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { dirname, join } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+
5
+ import { ParseContext } from 'parser-lr';
6
+
7
+ const here = dirname(fileURLToPath(import.meta.url));
8
+ const tableJson = readFileSync(join(here, 'calc.table.json'), 'utf8');
9
+
10
+ const context = ParseContext.fromTableJson(tableJson);
11
+ const ast = context.parseSource('1 + 2');
12
+
13
+ if (ast === null)
14
+ {
15
+ throw new Error('table-only smoke (mjs): expected a non-null AST');
16
+ }
17
+
18
+ process.stdout.write(`mjs ${ast.symbol}\n`);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "parser-lr",
3
3
  "author": "Adam R Moss <adamrmoss@gmail.com>",
4
- "version": "0.5.0",
4
+ "version": "0.6.0",
5
5
  "description": "Shift-reduce parser library for EBNF grammars",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -49,6 +49,7 @@
49
49
  "build:lib": "node scripts/build-lib.mjs",
50
50
  "build:cli": "rollup -c rollup.cli.config.js && chmod +x bin/parser-lr.js",
51
51
  "cloc": "cloc src --exclude-dir=node_modules,dist",
52
+ "acceptance": "node scripts/acceptance-tarball.mjs",
52
53
  "test": "jest",
53
54
  "test:coverage": "jest --coverage",
54
55
  "prepack": "npm run build"