11const { ElectroGrammarLexer} = require ( './ElectroGrammarLexer' ) ;
22const { ElectroGrammarParser} = require ( './ElectroGrammarParser' ) ;
3- const { ElectroGrammarListener } = require ( './ElectroGrammarListener ' ) ;
3+ const { ToObjectListener } = require ( './to_object_listener ' ) ;
44const antlr4 = require ( 'antlr4' ) ;
55
6- function handle_unit ( ctx , prefix ) {
7- return Number ( ctx . NUMBER ( ) . getText ( ) + prefix ) ;
8- }
9-
10- function handle_prefix ( ctx ) {
11- if ( 'GIGA' in ctx && ctx . GIGA ( ) ) {
12- return 'e9' ;
13- }
14- if ( 'MEGA' in ctx && ctx . MEGA ( ) ) {
15- return 'e6' ;
16- }
17- if ( 'KILO' in ctx && ctx . KILO ( ) ) {
18- return 'e3' ;
19- }
20- if ( 'MILI' in ctx && ctx . MILI ( ) ) {
21- return 'e-3' ;
22- }
23- if ( 'MICRO' in ctx && ctx . MICRO ( ) ) {
24- return 'e-6' ;
25- }
26- if ( 'NANO' in ctx && ctx . NANO ( ) ) {
27- return 'e-9' ;
28- }
29- if ( 'PICO' in ctx && ctx . PICO ( ) ) {
30- return 'e-12' ;
31- }
32- return '' ;
33- }
34-
35- function handle_package_size ( ctx ) {
36- if ( 'METRIC_SIZE' in ctx && ctx . METRIC_SIZE ( ) ) {
37- return ctx . METRIC_SIZE ( ) . getText ( ) ;
38- }
39- if ( 'IMPERIAL_SIZE' in ctx && ctx . IMPERIAL_SIZE ( ) ) {
40- return ctx . IMPERIAL_SIZE ( ) . getText ( ) ;
41- }
42- if ( 'AMBIGUOUS_SIZE' in ctx && ctx . AMBIGUOUS_SIZE ( ) ) {
43- return ctx . AMBIGUOUS_SIZE ( ) . getText ( ) ;
44- }
45- return '' ;
46- }
47-
48- class ElectroGrammarToObjectListener extends ElectroGrammarListener {
49- constructor ( ) {
50- super ( ) ;
51- this . obj = { } ;
52- this . prefix = '' ;
53- }
54- exitVprefix ( ctx ) {
55- this . prefix = handle_prefix ( ctx ) ;
56- }
57- exitVoltage ( ctx ) {
58- this . obj [ 'voltage' ] = handle_unit ( ctx , this . prefix ) ;
59- this . prefix = '' ;
60- }
61- exitAprefix ( ctx ) {
62- this . prefix = handle_prefix ( ctx ) ;
63- }
64- exitCurrent ( ctx ) {
65- this . obj [ 'current' ] = handle_unit ( ctx , this . prefix ) ;
66- this . prefix = '' ;
67- }
68- exitPprefix ( ctx ) {
69- this . prefix = handle_prefix ( ctx ) ;
70- }
71- exitPower ( ctx ) {
72- this . obj [ 'power' ] = handle_unit ( ctx , this . prefix ) ;
73- this . prefix = '' ;
74- }
75- exitRprefix ( ctx ) {
76- this . prefix = handle_prefix ( ctx ) ;
77- }
78- exitResistance ( ctx ) {
79- this . obj [ 'resistance' ] = handle_unit ( ctx , this . prefix ) ;
80- this . prefix = '' ;
81- }
82- exitCprefix ( ctx ) {
83- this . prefix = handle_prefix ( ctx ) ;
84- }
85- exitCapacitance ( ctx ) {
86- this . obj [ 'capacitance' ] = handle_unit ( ctx , this . prefix ) ;
87- this . prefix = '' ;
88- }
89- exitLprefix ( ctx ) {
90- this . prefix = handle_prefix ( ctx ) ;
91- }
92- exitInductance ( ctx ) {
93- this . obj [ 'inductance' ] = handle_unit ( ctx , this . prefix ) ;
94- this . prefix = '' ;
95- }
96- exitFprefix ( ctx ) {
97- this . prefix = handle_prefix ( ctx ) ;
98- }
99- exitFrequency ( ctx ) {
100- this . obj [ 'frequency' ] = handle_unit ( ctx , this . prefix ) ;
101- this . prefix = '' ;
102- }
103- exitTprefix ( ctx ) {
104- this . prefix = handle_prefix ( ctx , this . prefix ) ;
105- }
106- exitTime ( ctx ) {
107- this . obj [ 'time' ] = handle_unit ( ctx , this . prefix ) ;
108- this . prefix = '' ;
109- }
110- exitTemperature ( ctx ) {
111- this . obj [ 'temperature' ] = handle_unit ( ctx , '' ) ;
112- }
113- exitTolerance ( ctx ) {
114- this . obj [ 'tolerance' ] = handle_unit ( ctx , '' ) ;
115- }
116- exitMetric_size ( ctx ) {
117- var imperial_lookup = {
118- '0201' : '008004' ,
119- '03015' : '009005' ,
120- '0402' : '01005' ,
121- '0603' : '0201' ,
122- '1005' : '0402' ,
123- '1608' : '0603' ,
124- '2012' : '0805' ,
125- '2520' : '1008' ,
126- '3216' : '1206' ,
127- '3225' : '1210' ,
128- '4516' : '1806' ,
129- '4532' : '1812' ,
130- '4564' : '1825' ,
131- '5025' : '2010' ,
132- '6332' : '2512' ,
133- '7451' : '2920'
134- } ;
135- this . obj [ 'package_size' ] = imperial_lookup [ handle_package_size ( ctx ) ] ;
136- }
137- exitImperial_size ( ctx ) {
138- this . obj [ 'package_size' ] = handle_package_size ( ctx ) ;
139- }
140- exitAmbiguous_size ( ctx ) {
141- console . log ( 'Warn: Ambiguous package size found' ) ;
142- this . obj [ 'package_size' ] = handle_package_size ( ctx ) ;
143- }
144- exitClass1 ( ctx ) {
145- var dielectric ;
146- if ( ctx . M7G ( ) ) {
147- dielectric = 'M7G' ;
148- }
149- else if ( ctx . C0G ( ) ) {
150- dielectric = 'C0G' ;
151- }
152- else if ( ctx . H2G ( ) ) {
153- dielectric = 'H2G' ;
154- }
155- else if ( ctx . L2G ( ) ) {
156- dielectric = 'L2G' ;
157- }
158- else if ( ctx . P2H ( ) ) {
159- dielectric = 'P2H' ;
160- }
161- else if ( ctx . R2H ( ) ) {
162- dielectric = 'R2H' ;
163- }
164- else if ( ctx . S2H ( ) ) {
165- dielectric = 'S2H' ;
166- }
167- else if ( ctx . T2H ( ) ) {
168- dielectric = 'T2H' ;
169- }
170- else if ( ctx . U2J ( ) ) {
171- dielectric = 'U2J' ;
172- }
173- else if ( ctx . Q3K ( ) ) {
174- dielectric = 'Q3K' ;
175- }
176- else if ( ctx . P3K ( ) ) {
177- dielectric = 'P3K' ;
178- }
179-
180- this . obj [ 'dielectric' ] = dielectric ;
181- }
182- exitClass2 ( ctx ) {
183- this . obj [ 'dielectric' ] = ctx . CLASS2 ( ) . getText ( ) . toUpperCase ( ) ;
184- }
185- exitAlu ( ctx ) {
186- this . obj [ 'dielectric' ] = 'ALU' ;
187- }
188- exitTan ( ctx ) {
189- this . obj [ 'dielectric' ] = 'TAN' ;
190- }
191- exitResistor ( ctx ) {
192- this . obj [ 'type' ] = 'resistor' ;
193- }
194- exitCapacitor ( ctx ) {
195- this . obj [ 'type' ] = 'capacitor' ;
196- }
197- exitInductor ( ctx ) {
198- this . obj [ 'type' ] = 'inductor' ;
199- }
200- exitOscillator ( ctx ) {
201- this . obj [ 'type' ] = 'oscillator' ;
202- }
203- exitRtype ( ctx ) {
204- if ( ctx . POT ( ) )
205- this . obj [ 'rtype' ] = 'pot' ;
206- }
207- exitDiode ( ctx ) {
208- this . obj [ 'type' ] = 'diode' ;
209- }
210- exitDcode ( ctx ) {
211- this . obj [ 'code' ] = ctx . DCODE ( ) . getText ( ) . toUpperCase ( ) ;
212- }
213- exitSignal ( ctx ) {
214- this . obj [ 'dtype' ] = 'signal' ;
215- }
216- exitRectifier ( ctx ) {
217- this . obj [ 'dtype' ] = 'rectifier' ;
218- }
219- exitLed ( ctx ) {
220- this . obj [ 'dtype' ] = 'led' ;
221- }
222- exitSchottky ( ctx ) {
223- this . obj [ 'dtype' ] = 'schottky' ;
224- }
225- exitZener ( ctx ) {
226- this . obj [ 'dtype' ] = 'zener' ;
227- }
228- exitColor ( ctx ) {
229- this . obj [ 'color' ] = ctx . COLOR ( ) . getText ( ) . toLowerCase ( ) ;
230- }
231- exitTransistor ( ctx ) {
232- this . obj [ 'type' ] = 'transistor' ;
233- }
234- exitTtype ( ctx ) {
235- this . obj [ 'ttype' ] = ctx . TTYPE ( ) . getText ( ) . toLowerCase ( ) ;
236- }
237- exitTcode ( ctx ) {
238- this . obj [ 'code' ] = ctx . TCODE ( ) . getText ( ) . toUpperCase ( ) ;
239- }
240- }
241-
242- function get_parser ( start_rule ) {
6+ function get_parser ( parser , rule ) {
2437 function parse ( input ) {
2448 const chars = new antlr4 . InputStream ( input ) ;
2459 const lexer = new ElectroGrammarLexer ( chars ) ;
@@ -256,10 +20,14 @@ function get_parser(start_rule) {
25620
25721 parser . buildParseTrees = true ;
25822
259- const tree = parser [ start_rule ] ( ) ;
260- const listener = new ElectroGrammarToObjectListener ( ) ;
23+ if ( ! ( rule in parser ) ) {
24+ throw new Error ( 'Unknown rule ' + rule ) ;
25+ }
26+
27+ const tree = parser [ rule ] ( ) ;
28+ const listener = new ToObjectListener ( ) ;
26129 const walker = antlr4 . tree . ParseTreeWalker . DEFAULT . walk ( listener , tree ) ;
262- return listener . obj ;
30+ return { result : listener . obj } ;
26331 }
26432 return parse ;
26533}
0 commit comments