File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const { ElectroGrammarLexer} = require ( './ElectroGrammarLexer' ) ;
2+ const { ElectroGrammarParser} = require ( './ElectroGrammarParser' ) ;
3+ const { ElectroGrammarListener} = require ( './ElectroGrammarListener' ) ;
4+ const antlr4 = require ( 'antlr4' ) ;
5+
6+ class ElectroGrammarToObjectListener extends ElectroGrammarListener {
7+ constructor ( ) {
8+ super ( ) ;
9+ this . obj = { } ;
10+ }
11+ enterCapacitance ( ctx ) {
12+ const cprefix_lookup = { u : 'e-6' , n : 'e-9' , p : 'e-12' } ;
13+ const number = ctx . NUMBER ( ) . getText ( ) ;
14+ const cprefix = cprefix_lookup [ ctx . CPREFIX ( ) . getText ( ) ] ;
15+ this . obj . capacitance = Number ( number + cprefix ) ;
16+ this . obj . type = 'capacitor' ;
17+ }
18+ }
19+
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 . electro_grammar ( ) ;
28+ const listener = new ElectroGrammarToObjectListener ( ) ;
29+ const walker = antlr4 . tree . ParseTreeWalker . DEFAULT . walk ( listener , tree ) ;
30+ return listener . obj
31+ }
32+
33+ module . exports = { parse} ;
Original file line number Diff line number Diff line change 22const assert = require('better-assert')
33
44const equals = require('../lib/equals')
5- const parse = require('../lib/parse ')
5+ const parse = require('../lib/lax_parser ')
66
77describe('equality check', () => {
88 it('does not equal components of different types', () => {
Original file line number Diff line number Diff line change 1- /* const assert = require('better-assert');
1+ const assert = require ( 'better-assert' ) ;
22
3- const {parse} = require('../lib/index');
3+ const { parse} = require ( '../lib/lax_parser' )
44
55describe ( 'parsing' , ( ) => {
66 it ( "doesn't parse nonsense" , ( ) => {
@@ -267,6 +267,7 @@ describe('SMD Capacitors', () => {
267267 } )
268268} )
269269
270+ /*
270271describe('SMD Resistors', () => {
271272 it('parses a resistor', () => {
272273 const c = parse('1k 0603').component
You can’t perform that action at this time.
0 commit comments