Skip to content

Commit

Permalink
Start a lax parser
Browse files Browse the repository at this point in the history
  • Loading branch information
kasbah committed Jan 21, 2018
1 parent a767bd3 commit 7d0fcd7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion js/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function parse(input) {
const tree = parser.electro_grammar();
const listener = new ElectroGrammarToObjectListener();
const walker = antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree);
return listener.obj
return listener.obj;
}

module.exports = {parse};
33 changes: 33 additions & 0 deletions js/lib/lax_parser.js
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};
2 changes: 1 addition & 1 deletion js/test/test_equals.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const assert = require('better-assert')
const equals = require('../lib/equals')
const parse = require('../lib/parse')
const parse = require('../lib/lax_parser')
describe('equality check', () => {
it('does not equal components of different types', () => {
Expand Down
5 changes: 3 additions & 2 deletions js/test/test_parse.js → js/test/test_lax_parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*const assert = require('better-assert');
const assert = require('better-assert');

const {parse} = require('../lib/index');
const {parse} = require('../lib/lax_parser')

describe('parsing', () => {
it("doesn't parse nonsense", () => {
Expand Down Expand Up @@ -267,6 +267,7 @@ describe('SMD Capacitors', () => {
})
})

/*
describe('SMD Resistors', () => {
it('parses a resistor', () => {
const c = parse('1k 0603').component
Expand Down

0 comments on commit 7d0fcd7

Please sign in to comment.