-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
38 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const {ElectroGrammarLexer} = require('./ElectroGrammarLexer'); | ||
const {ElectroGrammarParser} = require('./ElectroGrammarParser'); | ||
const {ElectroGrammarListener} = require('./ElectroGrammarListener'); | ||
const antlr4 = require('antlr4'); | ||
|
||
class ElectroGrammarToObjectListener extends ElectroGrammarListener { | ||
constructor() { | ||
super(); | ||
this.obj = {}; | ||
} | ||
enterCapacitance(ctx) { | ||
const cprefix_lookup = {u: 'e-6', n: 'e-9', p: 'e-12'}; | ||
const number = ctx.NUMBER().getText(); | ||
const cprefix = cprefix_lookup[ctx.CPREFIX().getText()]; | ||
this.obj.capacitance = Number(number + cprefix); | ||
this.obj.type = 'capacitor'; | ||
} | ||
} | ||
|
||
function parse(input) { | ||
const chars = new antlr4.InputStream(input); | ||
const lexer = new ElectroGrammarLexer(chars); | ||
const tokens = new antlr4.CommonTokenStream(lexer); | ||
const parser = new ElectroGrammarParser(tokens); | ||
parser.buildParseTrees = true; | ||
|
||
const tree = parser.electro_grammar(); | ||
const listener = new ElectroGrammarToObjectListener(); | ||
const walker = antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree); | ||
return listener.obj; | ||
} | ||
|
||
module.exports = {parse}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters