Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6c9770c
Ignore java directory.
dvc94ch Jan 22, 2018
59cf669
antlr: Parse package sizes.
dvc94ch Jan 20, 2018
86e6622
antlr: Parse units.
dvc94ch Jan 21, 2018
9293266
antlr: Parse dielectrics.
dvc94ch Jan 21, 2018
4457c00
antlr: Parse passive components.
dvc94ch Jan 21, 2018
b14c36f
antlr: Parse semiconductors.
dvc94ch Jan 21, 2018
8bf4ce6
Add "prettier" code formatter
kasbah Jan 21, 2018
016d7fa
Turn python package into a module.
dvc94ch Jan 21, 2018
8828b1b
Use es2015 in index.js
kasbah Jan 21, 2018
60a64b7
Correct unit conversion and tests for it
kasbah Jan 21, 2018
5a22c6d
Re-enable travis testing
kasbah Jan 21, 2018
cd25b63
Start a lax parser
kasbah Jan 21, 2018
1c02e00
Rename test_units to test_strict_units
kasbah Jan 21, 2018
520f768
Add "prettier" code formatter
kasbah Jan 21, 2018
bd86c26
Add ambiguity diagnostic output to index.js
kasbah Jan 22, 2018
006dd32
Start ignorer error listeners in lax_parser
kasbah Jan 22, 2018
d03cc1b
python3: Add ambiguity diagnostics
kasbah Jan 22, 2018
eea48b4
js: set ambiguity output directly like in python
kasbah Jan 22, 2018
da2febf
python3: Add unit parsing tests.
dvc94ch Jan 22, 2018
3d9e6e3
python3: Add tolerance test case.
dvc94ch Jan 22, 2018
3a467c0
python3: Add package size tests.
dvc94ch Jan 22, 2018
312f9c5
python3: Add dielectric tests.
dvc94ch Jan 22, 2018
af3bf78
python3: Add passive tests.
dvc94ch Jan 22, 2018
6feeebf
python3: Add semiconductor tests.
dvc94ch Jan 22, 2018
65341af
WIP: js port.
dvc94ch Jan 22, 2018
d663ca3
js: Feature parity with python port.
dvc94ch Jan 23, 2018
6de3e3c
antlr: Add some missing unit specifiers.
dvc94ch Jan 23, 2018
e6fcb43
Fix prepare cpl.
dvc94ch Jan 23, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ coverage/
node_modules/
test-lib/

java/
js/**/ElectroGrammar*
python3/**/ElectroGrammar*
js/**/cpl_*
python3/**/ElectroGrammar*
python3/**/cpl_*
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ matrix:
include:
- node_js: node
env: TEST_BROWSERS=true
before_install:
- cd travis
- wget --no-check-certificate https://www.antlr.org/download/antlr-4.7.1-complete.jar
- export CLASSPATH=".:$TRAVIS_BUILD_DIR/travis/antlr-4.7.1-complete.jar:$CLASSPATH"
- export PATH=$PATH:$TRAVIS_BUILD_DIR/travis
- cd ..
script:
- make
- cd js
- yarn
- yarn test
- yarn demo
- git diff --quiet
- if [ "${TEST_BROWSERS}" = "true" ] && [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then yarn test:sauce; fi
- if [ "${TEST_BROWSERS}" = "true" ] && [ "${TRAVIS_BRANCH}" = "master" ]; then yarn test:sauce; fi
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ clean-java:


python3: $(GRAMMAR) $(GRAMMAR_FILES)
$(ANTLR) -Dlanguage=Python3 $< -o python3/
$(ANTLR) -Dlanguage=Python3 $< -o python3/electro_grammar/

clean-python3:
rm -f python3/ElectroGrammar*
rm -f python3/electro_grammar/ElectroGrammar*


js: $(GRAMMAR) $(GRAMMAR_FILES)
Expand Down
5 changes: 5 additions & 0 deletions js/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tabWidth: 2
semi: true
singleQuote: true
bracketSpacing: false
trailingComma: es5
306 changes: 271 additions & 35 deletions js/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,271 @@
var {ElectroGrammarLexer} = require('./ElectroGrammarLexer');
var {ElectroGrammarParser} = require('./ElectroGrammarParser');
var {ElectroGrammarListener} = require('./ElectroGrammarListener');
var antlr4 = require('antlr4');

var ElectroGrammarToObjectListener = function() {
ElectroGrammarListener.call(this);
this.obj = {};
return this;
};

ElectroGrammarToObjectListener.prototype = Object.create(ElectroGrammarListener.prototype);
ElectroGrammarToObjectListener.prototype.constructor = ElectroGrammarToObjectListener;

ElectroGrammarToObjectListener.prototype.enterCapacitance = function(ctx) {
var cprefix_lookup = {'u': 10e-6, 'n': 10e-9, 'p': 10e-12};
var number = Number(ctx.NUMBER().getText());
var cprefix = cprefix_lookup[ctx.CPREFIX().getText()];
this.obj['capacitance'] = number * cprefix;
};

var parse = function(input) {
var chars = new antlr4.InputStream(input);
var lexer = new ElectroGrammarLexer(chars);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new ElectroGrammarParser(tokens);
parser.buildParseTrees = true;

var tree = parser.electro_grammar();
var listener = new ElectroGrammarToObjectListener();
var walker = antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree);
return listener.obj;
};

module.exports = {'parse': parse};
const {ElectroGrammarLexer} = require('./ElectroGrammarLexer');
const {ElectroGrammarParser} = require('./ElectroGrammarParser');
const {ElectroGrammarListener} = require('./ElectroGrammarListener');
const antlr4 = require('antlr4');

function handle_unit(ctx, prefix) {
return Number(ctx.NUMBER().getText() + prefix);
}

function handle_prefix(ctx) {
if ('GIGA' in ctx && ctx.GIGA()) {
return 'e9';
}
if ('MEGA' in ctx && ctx.MEGA()) {
return 'e6';
}
if ('KILO' in ctx && ctx.KILO()) {
return 'e3';
}
if ('MILI' in ctx && ctx.MILI()) {
return 'e-3';
}
if ('MICRO' in ctx && ctx.MICRO()) {
return 'e-6';
}
if ('NANO' in ctx && ctx.NANO()) {
return 'e-9';
}
if ('PICO' in ctx && ctx.PICO()) {
return 'e-12';
}
return '';
}

function handle_package_size(ctx) {
if ('METRIC_SIZE' in ctx && ctx.METRIC_SIZE()) {
return ctx.METRIC_SIZE().getText();
}
if ('IMPERIAL_SIZE' in ctx && ctx.IMPERIAL_SIZE()) {
return ctx.IMPERIAL_SIZE().getText();
}
if ('AMBIGUOUS_SIZE' in ctx && ctx.AMBIGUOUS_SIZE()) {
return ctx.AMBIGUOUS_SIZE().getText();
}
return '';
}

class ElectroGrammarToObjectListener extends ElectroGrammarListener {
constructor() {
super();
this.obj = {};
this.prefix = '';
}
exitVprefix(ctx) {
this.prefix = handle_prefix(ctx);
}
exitVoltage(ctx) {
this.obj['voltage'] = handle_unit(ctx, this.prefix);
this.prefix = '';
}
exitAprefix(ctx) {
this.prefix = handle_prefix(ctx);
}
exitCurrent(ctx) {
this.obj['current'] = handle_unit(ctx, this.prefix);
this.prefix = '';
}
exitPprefix(ctx) {
this.prefix = handle_prefix(ctx);
}
exitPower(ctx) {
this.obj['power'] = handle_unit(ctx, this.prefix);
this.prefix = '';
}
exitRprefix(ctx) {
this.prefix = handle_prefix(ctx);
}
exitResistance(ctx) {
this.obj['resistance'] = handle_unit(ctx, this.prefix);
this.prefix = '';
}
exitCprefix(ctx) {
this.prefix = handle_prefix(ctx);
}
exitCapacitance(ctx) {
this.obj['capacitance'] = handle_unit(ctx, this.prefix);
this.prefix = '';
}
exitLprefix(ctx) {
this.prefix = handle_prefix(ctx);
}
exitInductance(ctx) {
this.obj['inductance'] = handle_unit(ctx, this.prefix);
this.prefix = '';
}
exitFprefix(ctx) {
this.prefix = handle_prefix(ctx);
}
exitFrequency(ctx) {
this.obj['frequency'] = handle_unit(ctx, this.prefix);
this.prefix = '';
}
exitTprefix(ctx) {
this.prefix = handle_prefix(ctx, this.prefix);
}
exitTime(ctx) {
this.obj['time'] = handle_unit(ctx, this.prefix);
this.prefix = '';
}
exitTemperature(ctx) {
this.obj['temperature'] = handle_unit(ctx, '');
}
exitTolerance(ctx) {
this.obj['tolerance'] = handle_unit(ctx, '');
}
exitMetric_size(ctx) {
var imperial_lookup = {
'0201': '008004',
'03015': '009005',
'0402': '01005',
'0603': '0201',
'1005': '0402',
'1608': '0603',
'2012': '0805',
'2520': '1008',
'3216': '1206',
'3225': '1210',
'4516': '1806',
'4532': '1812',
'4564': '1825',
'5025': '2010',
'6332': '2512',
'7451': '2920'
};
this.obj['package_size'] = imperial_lookup[handle_package_size(ctx)];
}
exitImperial_size(ctx) {
this.obj['package_size'] = handle_package_size(ctx);
}
exitAmbiguous_size(ctx) {
console.log('Warn: Ambiguous package size found');
this.obj['package_size'] = handle_package_size(ctx);
}
exitClass1(ctx) {
var dielectric;
if (ctx.M7G()) {
dielectric = 'M7G';
}
else if (ctx.C0G()) {
dielectric = 'C0G';
}
else if (ctx.H2G()) {
dielectric = 'H2G';
}
else if (ctx.L2G()) {
dielectric = 'L2G';
}
else if (ctx.P2H()) {
dielectric = 'P2H';
}
else if (ctx.R2H()) {
dielectric = 'R2H';
}
else if (ctx.S2H()) {
dielectric = 'S2H';
}
else if (ctx.T2H()) {
dielectric = 'T2H';
}
else if (ctx.U2J()) {
dielectric = 'U2J';
}
else if (ctx.Q3K()) {
dielectric = 'Q3K';
}
else if (ctx.P3K()) {
dielectric = 'P3K';
}

this.obj['dielectric'] = dielectric;
}
exitClass2(ctx) {
this.obj['dielectric'] = ctx.CLASS2().getText().toUpperCase();
}
exitAlu(ctx) {
this.obj['dielectric'] = 'ALU';
}
exitTan(ctx) {
this.obj['dielectric'] = 'TAN';
}
exitResistor(ctx) {
this.obj['type'] = 'resistor';
}
exitCapacitor(ctx) {
this.obj['type'] = 'capacitor';
}
exitInductor(ctx) {
this.obj['type'] = 'inductor';
}
exitOscillator(ctx) {
this.obj['type'] = 'oscillator';
}
exitRtype(ctx) {
if (ctx.POT())
this.obj['rtype'] = 'pot';
}
exitDiode(ctx) {
this.obj['type'] = 'diode';
}
exitDcode(ctx) {
this.obj['code'] = ctx.DCODE().getText().toUpperCase();
}
exitSignal(ctx) {
this.obj['dtype'] = 'signal';
}
exitRectifier(ctx) {
this.obj['dtype'] = 'rectifier';
}
exitLed(ctx) {
this.obj['dtype'] = 'led';
}
exitSchottky(ctx) {
this.obj['dtype'] = 'schottky';
}
exitZener(ctx) {
this.obj['dtype'] = 'zener';
}
exitColor(ctx) {
this.obj['color'] = ctx.COLOR().getText().toLowerCase();
}
exitTransistor(ctx) {
this.obj['type'] = 'transistor';
}
exitTtype(ctx) {
this.obj['ttype'] = ctx.TTYPE().getText().toLowerCase();
}
exitTcode(ctx) {
this.obj['code'] = ctx.TCODE().getText().toUpperCase();
}
}

function get_parser(start_rule) {
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);

// enable grammar ambiguity diagnostic output
// see Antlr book ch 9.2, page 156
// https://github.com/antlr/antlr4/issues/2206
parser.removeErrorListeners();
parser.addErrorListener(new antlr4.error.DiagnosticErrorListener());
parser._interp.PredictionMode =
antlr4.atn.PredictionMode.LL_EXACT_AMBIG_DETECTION;

parser.buildParseTrees = true;

const tree = parser[start_rule]();
const listener = new ElectroGrammarToObjectListener();
const walker = antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree);
return listener.obj;
}
return parse;
}

function parse(input) {
return get_parser('electro_grammar')(input);
}

module.exports = {get_parser, parse};
Loading