electro-grammar
v1.3.0
Published
Parser electronic components descriptions such as surface-mount resistors and capacitors.
Downloads
115
Readme
Electro Grammar
This is a parser using Nearley that defines a grammar for describing generic electronic components such as surface mount resistors, capacitors and LEDs. A function to match the result to parts in the Common Parts Library is also provided.
npm install electro-grammar
const {parse, matchCPL} = require('electro-grammar')
Where is this used?
- 1-click BOM browser extension
- Kitspace BOM Builder (currently in alpha)
- electron-lang for
@cpl
Parsing
Capacitors
Parses capacitance, package size, characteristic, tolerance and voltage rating for capacitors.
> parse('100nF 0603 C0G 10% 25V')
{ type: 'capacitor',
capacitance: 1e-7,
size: '0603',
characteristic: 'C0G',
tolerance: 10,
voltage_rating: 25 }
For class 1 ceramic names and EIA letter codes are understood. For class 2 only EIA letter codes are understood. In both cases only EIA letter codes are returned.
> parse('10pF C0G/NP0')
{ type: 'capacitor', capacitance: 1e-11, characteristic: 'C0G' }
> parse('10pF NP0')
{ type: 'capacitor', capacitance: 1e-11, characteristic: 'C0G' }
> parse('10pF X7R')
{ type: 'capacitor', capacitance: 1e-11, characteristic: 'X7R' }
Resistors
Parses resistance, package size, tolerance and power rating for resistors.
> parse('1k 0805 5% 125mW')
{ type: 'resistor',
resistance: 1000,
size: '0805',
tolerance: 5,
power_rating: 0.125 }
Electro-grammar supports several different ways to express resistance.
> parse('1.5k')
{ type: 'resistor', resistance: 1500 }
> parse('1k5')
{ type: 'resistor', resistance: 1500 }
> parse('500R')
{ type: 'resistor', resistance: 500 }
> parse('1500 ohm')
{ type: 'resistor', resistance: 1500 }
> parse('1500.0 ohm')
{ type: 'resistor', resistance: 1500 }
> parse('1500 Ω')
{ type: 'resistor', resistance: 1500 }
> parse('resistor 1500')
{ type: 'resistor', resistance: 1500 }
LEDs
LEDs need to include the word 'LED' or 'led'.
> parse('LED red')
{ type: 'led', color: 'red' }
> parse('LED 0603')
{ type: 'led', size: '0603' }
> parse('green led 1206')
{ type: 'led', color: 'green', size: '1206' }
Parsing Details
Converts all units to floating point numbers.
> parse('100nF')
{ type: 'capacitor', capacitance: 1e-7 }
> parse('0.1uF')
{ type: 'capacitor', capacitance: 1e-7 }
The order of the terms doesn't matter.
> parse('1% 0603 1uF')
{ type: 'capacitor'
capacitance: 0.000001,
tolerance: 1,
size: "0603" }
> parse('0603 1% 1uF')
{ type: 'capacitor',
capacitance: 0.000001,
tolerance: 1,
size: "0603" }
If no match is found an empty object is returned.
> parse('')
{}
> parse('NE555P')
{}
But invalid input types will throw.
> parse({})
TypeError: str.split is not a function
Text that is not part of the grammar is simply ignored.
> parse('NE555P 1uF')
{ type: 'capacitor', capacitance: 0.000001 }
> parse('these words 1k are ignored 0805')
{ type: 'resistor', resistance: 1000, size: '0805' }
You can use metric package sizes as long as you make it clear by using the metric
keyword.
Output for package sizes is always in imperial.
> parse('1k metric 0603')
{ type: 'resistor', resistance: 1000, size: '0201' }
> parse('1k 0603 metric')
{ type: 'resistor', resistance: 1000, size: '0201' }
You can give it a hint to the type of component by starting off with that. For instance you can leave of F or farad if you start off with 'capacitor' or 'c'
> parse('capacitor 1u')
{ type: 'capacitor', capacitance: 0.000001 }
> parse('c 1u')
{ type: 'capacitor', capacitance: 0.000001 }
> parse('resistor 100')
{ type: 'resistor', resistance: 100 }
> parse('res 100')
{ type: 'resistor', resistance: 100 }
> parse('r 100')
{ type: 'resistor', resistance: 100 }
CPL Matching
matchCPL
tries to find as many matches as it can from the Common Parts Library and returns an array of CPL IDs.
You could match these against CPL data or search for them on Octopart to get exact part numbers.
If no matches are found or the function is given invalid input an empty array is returned.
> c = parse('0.1uF 0805 25V')
{ type: 'capacitor',
capacitance: 1e-7,
size: '0805',
voltage_rating: 25 }
> matchCPL(c)
[ 'CPL-CAP-X7R-0805-100NF-50V' ]
> r = parse('10k 0603')
{ type: 'resistor', resistance: 10000, size: '0603' }
> matchCPL(r)
[ 'CPL-RES-0603-10K-0.1W' ]
> // I don't think it's possible to make such a resistor
> r = parse('1k 1000000W')
{ type: 'resistor', resistance: 1000, power_rating: 1000000 }
> matchCPL(r)
[]
> matchCPL({invalid: 'input'})
[]
> matchCPL(null)
[]
Roadmap
We are currently working on v2 of Electro Grammar which will have parsers in many more languages:
v1
- JavaScript only
- Capacitors, resistors and LEDs (SMD only)
- Lax parser only (any-order, ignores invalid input)
v2
- Work in progress!
- Uses Antlr4: JavaScript (API compatible with v1), Python, Java, C (& C++), Go
- Capacitors, resistors, LEDs, diodes, transistors (SMD & through-hole)
- Strict and lax parser
Head to the issue tracker or the Gitter Room if you want to help or need to know more details.
License
Electro Grammar is MIT licensed. It can be freely used in open source and propietary work as long as you include the copyright notice in all copies. See the LICENSE.md file for details.