zaach / jison

Bison in JavaScript.
http://jison.org
4.33k stars 448 forks source link

How to include parser in angular project #382

Open mluis opened 5 years ago

mluis commented 5 years ago

After a successful generated parser one might not need jison to be included in a project for parsers generation. My question is how to include the generated parser in a project like angular, for example ?

BR

liqiang372 commented 5 years ago

@mluis jison currently only supports commonjs/amd module or global variable. listed here https://github.com/zaach/jison#usage-from-the-command-line. Unfortunately it doesn't support es6 module and if you want it the es6 way, you can write a small script to append one line

export default <parserName>;

at the bottom of file. so that you can do import Parser from 'path-to-generated-parser-file' in your angular code.

drewwiens commented 5 years ago

When trying to import either a generated parser or jison itself in an Angular 7 project I get errors about the lack of node modules fs and path:

Module not found: Error: Can't resolve 'fs' Module not found: Error: Can't resolve 'path'

I can get the generated parser to work by removing the two lines referencing fs and path, but there must be a better way, especially so that one could use the jison npm package.

Jele0794 commented 4 years ago

You can use the option moduleType as follows:

parser.generate({ moduleName: 'moduleName', moduleType: 'js' })

It will generate the parser without those lines. Be careful with the lexer —if you are using a custom one— and custom functionality that you pass through the 'yy' object.