@stordata/grammars 1.0.20230421155415

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,50 @@
1
+ import { expect } from 'chai';
2
+
3
+ import { CustomMetricListener, CustomMetricParser, walk } from '../index.js';
4
+
5
+ import parseCustomMetric from './parseCustomMetric.js';
6
+
7
+ class CountStringsAndStringDividersListener extends CustomMetricListener {
8
+
9
+ constructor() {
10
+ super();
11
+
12
+ this.strings = [];
13
+ this.dividers = [];
14
+ }
15
+
16
+ enterString(ctx) {
17
+ this.strings.push(ctx.getText());
18
+ }
19
+
20
+ enterDivision(ctx) {
21
+ if (ctx.right instanceof CustomMetricParser.StringContext) {
22
+ this.dividers.push(ctx.right.getText());
23
+ }
24
+ }
25
+
26
+ }
27
+
28
+ describe('The parseCustomMetric function', function() {
29
+
30
+ it('should not parseCustomMetric invalid syntax', function() {
31
+ expect(() => parseCustomMetric('a')).to.throw("mismatched input 'a'");
32
+ });
33
+
34
+ it('should parseCustomMetric valid syntax with only arithmetics', function() {
35
+ const tree = parseCustomMetric('1+2*(3- 2 )'),
36
+ listener = walk(tree, new CountStringsAndStringDividersListener());
37
+
38
+ expect(listener.strings).to.have.length(0);
39
+ expect(listener.dividers).to.have.length(0);
40
+ });
41
+
42
+ it('should parseCustomMetric valid syntax with strings, and divisions by strings', function() {
43
+ const tree = parseCustomMetric('0.5 * ("abcd" / "efgh")/42'),
44
+ listener = walk(tree, new CountStringsAndStringDividersListener());
45
+
46
+ expect(listener.strings).to.deep.equal(['"abcd"', '"efgh"']);
47
+ expect(listener.dividers).to.deep.equal(['"efgh"']);
48
+ });
49
+
50
+ });
package/src/index.js ADDED
@@ -0,0 +1,20 @@
1
+ import CustomMetricListener from '../generated/src/grammars/CustomMetricListener.js';
2
+ import CustomMetricParser from '../generated/src/grammars/CustomMetricParser.js';
3
+ import CustomMetricLexer from '../generated/src/grammars/CustomMetricLexer.js';
4
+
5
+ import GrammarSyntaxError from './GrammarSyntaxError.js';
6
+ import parse from './parse.js';
7
+ import walk from './walk.js';
8
+ import parseCustomMetric from './grammars/parseCustomMetric.js';
9
+
10
+ export {
11
+ parse,
12
+ walk,
13
+ // The CustomMetric grammar
14
+ parseCustomMetric,
15
+ CustomMetricLexer,
16
+ CustomMetricParser,
17
+ CustomMetricListener,
18
+ // Helpers
19
+ GrammarSyntaxError
20
+ };
package/src/parse.js ADDED
@@ -0,0 +1,18 @@
1
+ import { CommonTokenStream, InputStream } from 'antlr4';
2
+
3
+ import ThrowingErrorListener from './ThrowingErrorListener.js';
4
+
5
+ export default function parse(input, Lexer, Parser) {
6
+ const parser = new Parser(
7
+ new CommonTokenStream(
8
+ new Lexer(
9
+ new InputStream(input)
10
+ )
11
+ )
12
+ );
13
+
14
+ parser.removeErrorListeners(); // There's a default one
15
+ parser.addErrorListener(new ThrowingErrorListener()); // Will throw on first error
16
+
17
+ return parser.all(); // The "all" rule gotta exist!
18
+ }
package/src/walk.js ADDED
@@ -0,0 +1,7 @@
1
+ import { ParseTreeWalker } from 'antlr4';
2
+
3
+ export default function walk(tree, listener) {
4
+ ParseTreeWalker.DEFAULT.walk(listener, tree);
5
+
6
+ return listener;
7
+ }
@@ -0,0 +1,8 @@
1
+ export default {
2
+ mode: 'production',
3
+ entry: './src/index.js',
4
+ output: {
5
+ path: new URL('dist', import.meta.url).pathname,
6
+ filename: 'main.cjs'
7
+ }
8
+ };