@@ -3,34 +3,33 @@ var {ElectroGrammarParser} = require('./ElectroGrammarParser');
33var { ElectroGrammarListener} = require ( './ElectroGrammarListener' ) ;
44var antlr4 = require ( 'antlr4' ) ;
55
6- var KeyPrinter = function ( ) {
6+ var ElectroGrammarToObjectListener = function ( ) {
77 ElectroGrammarListener . call ( this ) ;
8+ this . obj = { } ;
89 return this ;
910} ;
1011
11- KeyPrinter . prototype = Object . create ( ElectroGrammarListener . prototype ) ;
12- KeyPrinter . prototype . constructor = KeyPrinter ;
12+ ElectroGrammarToObjectListener . prototype = Object . create ( ElectroGrammarListener . prototype ) ;
13+ ElectroGrammarToObjectListener . prototype . constructor = ElectroGrammarToObjectListener ;
1314
14- KeyPrinter . prototype . exitCapacitance = function ( ctx ) {
15- var prefix = ctx
16- . CPREFIX ( )
17- . getText ( )
18- . toLowerCase ( ) ;
19- if ( prefix === 'micro' ) {
20- prefix = 'u' ;
21- } else {
22- prefix = prefix [ 0 ] ;
23- }
24- var number = Number ( ctx . NUMBER ( ) . getText ( ) ) ;
25- console . log ( 'capacitance' , `${ number } ${ prefix } F` ) ;
15+ ElectroGrammarToObjectListener . prototype . enterCapacitance = function ( ctx ) {
16+ var cprefix_lookup = { 'u' : 10e-6 , 'n' : 10e-9 , 'p' : 10e-12 } ;
17+ var number = Number ( ctx . NUMBER ( ) . getText ( ) ) ;
18+ var cprefix = cprefix_lookup [ ctx . CPREFIX ( ) . getText ( ) ] ;
19+ this . obj [ 'capacitance' ] = number * cprefix ;
2620} ;
2721
28- var input = '100 micro farad \n' ;
29- var chars = new antlr4 . InputStream ( input ) ;
30- var lexer = new ElectroGrammarLexer ( chars ) ;
31- var tokens = new antlr4 . CommonTokenStream ( lexer ) ;
32- var parser = new ElectroGrammarParser ( tokens ) ;
33- parser . buildParseTrees = true ;
34- var tree = parser . capacitance ( ) ;
35- var printer = new KeyPrinter ( ) ;
36- antlr4 . tree . ParseTreeWalker . DEFAULT . walk ( printer , tree ) ;
22+ var parse = function ( input ) {
23+ var chars = new antlr4 . InputStream ( input ) ;
24+ var lexer = new ElectroGrammarLexer ( chars ) ;
25+ var tokens = new antlr4 . CommonTokenStream ( lexer ) ;
26+ var parser = new ElectroGrammarParser ( tokens ) ;
27+ parser . buildParseTrees = true ;
28+
29+ var tree = parser . electro_grammar ( ) ;
30+ var listener = new ElectroGrammarToObjectListener ( ) ;
31+ var walker = antlr4 . tree . ParseTreeWalker . DEFAULT . walk ( listener , tree ) ;
32+ return listener . obj ;
33+ } ;
34+
35+ module . exports = { 'parse' : parse } ;
0 commit comments