Skip to content

Commit 070878c

Browse files
committed
WIP: use an "unknown" rule that matches everything
1 parent f95e301 commit 070878c

5 files changed

Lines changed: 38 additions & 61 deletions

File tree

js/bin/electro-grammar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const cli = require('cli');
2-
const lib = require('../lib/index');
2+
const lib = require('../lib/lax_parser');
33

44
const args = cli.parse({
55
parser: [ 'p', 'Parser to use (strict | lax)', 'string', 'strict' ],

js/lib/lax_parser.js

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,46 @@
11
const {ElectroGrammarLexer} = require('./ElectroGrammarLexer');
22
const {ElectroGrammarParser} = require('./ElectroGrammarParser');
3+
const {ElectroGrammarListener} = require('./ElectroGrammarListener');
34
const {ToObjectListener} = require('./to_object_listener');
45
const antlr4 = require('antlr4');
56

6-
class LexerIgnoreListener extends antlr4.error.ErrorListener {
7+
class IgnoreListener extends ElectroGrammarListener {
78
constructor() {
89
super();
9-
this.ignored = '';
10+
this.obj = '';
1011
}
11-
syntaxError(lexer, offendingSymbol, line, char, err) {
12-
console.error('lexer:', err);
13-
// hopefully there is a better way to get the input, but I haven't found it
14-
let input = err.split(':')[1].trim();
15-
input = input.substring(1, input.length - 1);
16-
this.ignored += input;
12+
exitIgnored(ctx) {
13+
ctx.UNKNOWN().forEach(s => {
14+
this.obj += s.getText();
15+
});
1716
}
1817
}
1918

20-
class ParserIgnoreListener extends antlr4.error.ErrorListener {
21-
constructor() {
22-
super();
23-
this.ignored = '';
24-
}
25-
syntaxError(lexer, offendingSymbol, line, char, err) {
26-
console.error('parser:', err);
27-
//this.ignored += input;
19+
function get_parser(start_rule, listener) {
20+
function parse(input) {
21+
const chars = new antlr4.InputStream(input);
22+
const lexer = new ElectroGrammarLexer(chars);
23+
const tokens = new antlr4.CommonTokenStream(lexer);
24+
const parser = new ElectroGrammarParser(tokens);
25+
parser.buildParseTrees = true;
26+
27+
const tree = parser[start_rule]();
28+
antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree);
29+
return listener.obj;
2830
}
31+
return parse;
2932
}
3033

3134
function parse(input) {
32-
const chars = new antlr4.InputStream(input);
33-
const lexer = new ElectroGrammarLexer(chars);
34-
const lexerIgnorer = new LexerIgnoreListener();
35-
lexer.removeErrorListeners();
36-
lexer.addErrorListener(lexerIgnorer);
37-
const tokens = new antlr4.CommonTokenStream(lexer);
38-
const parser = new ElectroGrammarParser(tokens);
39-
const parserIgnorer = new ParserIgnoreListener();
40-
parser.buildParseTrees = true;
41-
parser.removeErrorListeners();
42-
parser.addErrorListener(parserIgnorer);
35+
return get_parser('electro_grammar')(input);
36+
}
4337

44-
const tree = parser.electro_grammar();
45-
const listener = new ToObjectListener();
46-
const walker = antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree);
47-
console.log({lexer: lexerIgnorer.ignored, parser: parserIgnorer.ignored});
48-
return {component: listener.obj, ignored: lexerIgnorer.ignored};
38+
function parse(input) {
39+
const parseComponent = get_parser('electro_grammar', new ToObjectListener());
40+
const component = parseComponent(input);
41+
const parseIgnored = get_parser('ignored', new IgnoreListener());
42+
const ignored = parseIgnored(input);
43+
return {component, ignored};
4944
}
5045

5146
module.exports = {parse};

src/ElectroGrammar.g4

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
/*
2-
* Lexer Rules
3-
*/
4-
51
grammar ElectroGrammar;
6-
import Passive, Semi;
2+
import Passive;
3+
4+
electro_grammar: passive EOF;
75

8-
electro_grammar: (passive | semi) EOF;
6+
ignored: UNKNOWN* EOF;
97

10-
WHITESPACE: [\p{White_Space}] -> skip;
8+
UNKNOWN: .+?;

src/Passive.g4

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
11
grammar Passive;
2-
import Dielectric, Package, Units;
2+
import Units;
33

4-
passive: resistor | capacitor | inductor | oscillator;
4+
passive: resistor;
55

6-
passive: resistor | capacitor | inductor | oscillator;
7-
resistor: resistance rspec*;
8-
rspec: rtype | rpackage | power;
9-
rtype: POT;
10-
rpackage: package_chip;
11-
POT: P O T | P O T E N T I O M E T E R;
12-
13-
capacitor: capacitance cspec*;
14-
cspec: dielectric | cpackage | voltage;
15-
cpackage: package_chip;
16-
17-
inductor: inductance lspec*;
18-
lspec: lpackage | current;
19-
lpackage: package_chip;
20-
21-
oscillator: frequency ospec*;
22-
ospec: capacitance;
6+
resistor: resistance;

src/Units.g4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ aprefix: MILI | MICRO | NANO | PICO;
4848
power: NUMBER pprefix? WATT tolerance?;
4949
pprefix: MILI;
5050

51-
resistance: NUMBER (rprefix | OHM) tolerance?;
52-
rprefix: MEGA | KILO | MILI;
51+
resistance: NUMBER (RPREFIX | OHM);
52+
RPREFIX: MEGA | KILO | MILI;
5353

5454
capacitance: NUMBER cprefix FARAD tolerance?;
5555
cprefix: MICRO | NANO | PICO;

0 commit comments

Comments
 (0)