@alu0101017396/lexer-generator-alu0101017396 v1.0.0
lexerGenerator
A small library providing a method that creates a lexic analyzer from regular expressions.
Installation
npm install @ULL-ESIT-PL-2021/lexgen-code-alu0101017396 --save
or
npm install --global @ULL-ESIT-PL-2021/lexgen-code-alu0101017396
Usage
How to use the package as a library
Add the followin lines to the head of your program:
let buildLexer = require('@ULL-ESIT-PL-2021/lexgen-code-alu0101017396');
The function buildLexer creates a lexic analyzer from the array of pairs with regular expressions and his names that has to receive from parameter. It returns a function that receives a string and that will do the lexic analysis of it.
An example of use would be:
let buildLexer = require('@ULL-ESIT-PL-2021/lexgen-code-alu0101017396');
const str = 'const varName = "value" 3a';
console.log(str);
const SPACE = /(?<SPACE>\s+)/;
const RESERVEDWORD = /(?<RESERVEDWORD>\b(const|let)\b)/;
const ID = /(?<ID>([a-z_]\w+))/;
const STRING = /(?<STRING>"([^\\"]|\\.")*")/;
const OP = /(?<OP>[+*\/=-])/;
const tokens = [
['SPACE', SPACE], ['RESERVEDWORD', RESERVEDWORD], ['ID', ID],
['STRING', STRING], ['OP', OP]
];
let analysis = buildLexer(tokens);
let res = analysis(str);
console.log(res);
With the following output:
[
{ type: 'RESERVEDWORD', value: 'const' },
{ type: 'ID', value: 'varName' },
{ type: 'OP', value: '=' },
{ type: 'STRING', value: '"value"' },
{ type: 'ERROR', value: '3a' }
]
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
Tests
If you change any functionality of the package you can add tests to the tests
directory and run them with:
npm test
Documentation
https://ull-esit-pl-2021.github.io/lexer-generator-alu0101017396/
Release History
- 0.1.0 Initial release
3 years ago