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 =
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.
> 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.
> type: 'capacitor' capacitance: 1e-11 characteristic: 'C0G' > type: 'capacitor' capacitance: 1e-11 characteristic: 'C0G' > type: 'capacitor' capacitance: 1e-11 characteristic: 'X7R'
Resistors
Parses resistance, package size, tolerance and power rating for resistors.
> type: 'resistor' resistance: 1000 size: '0805' tolerance: 5 power_rating: 0125
Electro-grammar supports several different ways to express resistance.
> type: 'resistor' resistance: 1500 > type: 'resistor' resistance: 1500 > type: 'resistor' resistance: 500 > type: 'resistor' resistance: 1500 > type: 'resistor' resistance: 1500 > type: 'resistor' resistance: 1500 > type: 'resistor' resistance: 1500
LEDs
LEDs need to include the word 'LED' or 'led'.
> type: 'led' color: 'red' > type: 'led' size: '0603' > type: 'led' color: 'green' size: '1206'
Parsing Details
Converts all units to floating point numbers.
> type: 'capacitor' capacitance: 1e-7 > type: 'capacitor' capacitance: 1e-7
The order of the terms doesn't matter.
> type: 'capacitor' capacitance: 0000001 tolerance: 1 size: "0603" > type: 'capacitor' capacitance: 0000001 tolerance: 1 size: "0603"
If no match is found an empty object is returned.
> {}> {}
But invalid input types will throw.
> TypeError: strsplit is not a
Text that is not part of the grammar is simply ignored.
> type: 'capacitor' capacitance: 0000001 > 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.
> type: 'resistor' resistance: 1000 size: '0201' > 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'
> type: 'capacitor' capacitance: 0000001 > type: 'capacitor' capacitance: 0000001 > type: 'resistor' resistance: 100 > type: 'resistor' resistance: 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 = type: 'capacitor' capacitance: 1e-7 size: '0805' voltage_rating: 25 > 'CPL-CAP-X7R-0805-100NF-50V' > r = type: 'resistor' resistance: 10000 size: '0603' > 'CPL-RES-0603-10K-0.1W' > // I don't think it's possible to make such a resistor> r = type: 'resistor' resistance: 1000 power_rating: 1000000 > > >
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.