@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.
- package/.eslintrc.json +13 -0
- package/.gitlab-ci.yml +49 -0
- package/.nvmrc +2 -0
- package/README.md +26 -0
- package/dist/main.cjs +2 -0
- package/dist/main.cjs.LICENSE.txt +1 -0
- package/generated/src/grammars/CustomMetric.interp +55 -0
- package/generated/src/grammars/CustomMetric.tokens +30 -0
- package/generated/src/grammars/CustomMetricLexer.interp +80 -0
- package/generated/src/grammars/CustomMetricLexer.js +94 -0
- package/generated/src/grammars/CustomMetricLexer.tokens +30 -0
- package/generated/src/grammars/CustomMetricListener.js +81 -0
- package/generated/src/grammars/CustomMetricParser.js +579 -0
- package/package.json +42 -0
- package/src/GrammarSyntaxError.js +10 -0
- package/src/ThrowingErrorListener.js +11 -0
- package/src/grammars/CustomMetric.g4 +38 -0
- package/src/grammars/parseCustomMetric.js +7 -0
- package/src/grammars/parseCustomMetric.spec.js +50 -0
- package/src/index.js +20 -0
- package/src/parse.js +18 -0
- package/src/walk.js +7 -0
- package/webpack.config.js +8 -0
|
@@ -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