Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zaach/lex-parser
A parser for lexical grammars used by jison.
https://github.com/zaach/lex-parser
Last synced: about 1 month ago
JSON representation
A parser for lexical grammars used by jison.
- Host: GitHub
- URL: https://github.com/zaach/lex-parser
- Owner: zaach
- Created: 2012-08-30T07:32:14.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2023-03-18T18:41:15.000Z (almost 2 years ago)
- Last Synced: 2024-04-16T00:26:34.613Z (8 months ago)
- Language: JavaScript
- Size: 116 KB
- Stars: 25
- Watchers: 3
- Forks: 15
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# lex-parser
A parser for lexical grammars used by [jison](http://jison.org) and jison-lex.
## install
npm install lex-parser
## build
To build the parser yourself, clone the git repo then run:
make
This will generate `lex-parser.js`.
## usage
var lexParser = require("lex-parser");
// parse a lexical grammar and return JSON
lexParser.parse("%% ... ");## example
The parser can parse its own lexical grammar, shown below:
NAME [a-zA-Z_][a-zA-Z0-9_-]*
%s indented trail rules
%x code start_condition options conditions action%%
[^{}]+ return 'ACTION_BODY'
"{" yy.depth++; return '{'
"}" yy.depth == 0 ? this.begin('trail') : yy.depth--; return '}'{NAME} return 'NAME'
">" this.popState(); return '>'
"," return ','
"*" return '*'\n+ /* */
\s+ this.begin('indented')
"%%" this.begin('code'); return '%%'
[a-zA-Z0-9_]+ return 'CHARACTER_LIT'{NAME} yy.options[yytext] = true
\n+ this.begin('INITIAL')
\s+\n+ this.begin('INITIAL')
\s+ /* empty */{NAME} return 'START_COND'
\n+ this.begin('INITIAL')
\s+\n+ this.begin('INITIAL')
\s+ /* empty */.*\n+ this.begin('rules')
"{" yy.depth = 0; this.begin('action'); return '{'
"%{"(.|\n)*?"%}" this.begin('trail'); yytext = yytext.substr(2, yytext.length-4);return 'ACTION'
"%{"(.|\n)*?"%}" yytext = yytext.substr(2, yytext.length-4); return 'ACTION'
.+ this.begin('rules'); return 'ACTION'"/*"(.|\n|\r)*?"*/" /* ignore */
"//".* /* ignore */\n+ /* */
\s+ /* */
{NAME} return 'NAME'
\"("\\\\"|'\"'|[^"])*\" yytext = yytext.replace(/\\"/g,'"');return 'STRING_LIT'
"'"("\\\\"|"\'"|[^'])*"'" yytext = yytext.replace(/\\'/g,"'");return 'STRING_LIT'
"|" return '|'
"["("\\\\"|"\]"|[^\]])*"]" return 'ANY_GROUP_REGEX'
"(?:" return 'SPECIAL_GROUP'
"(?=" return 'SPECIAL_GROUP'
"(?!" return 'SPECIAL_GROUP'
"(" return '('
")" return ')'
"+" return '+'
"*" return '*'
"?" return '?'
"^" return '^'
"," return ','
"<>" return '$'
"<" this.begin('conditions'); return '<'
"/!" return '/!'
"/" return '/'
"\\"([0-7]{1,3}|[rfntvsSbBwWdD\\*+()${}|[\]\/.^?]|"c"[A-Z]|"x"[0-9A-F]{2}|"u"[a-fA-F0-9]{4}) return 'ESCAPE_CHAR'
"\\". yytext = yytext.replace(/^\\/g,''); return 'ESCAPE_CHAR'
"$" return '$'
"." return '.'
"%options" yy.options = {}; this.begin('options')
"%s" this.begin('start_condition');return 'START_INC'
"%x" this.begin('start_condition');return 'START_EXC'
"%%" this.begin('rules'); return '%%'
"{"\d+(","\s?\d+|",")?"}" return 'RANGE_REGEX'
"{"{NAME}"}" return 'NAME_BRACE'
"{" return '{'
"}" return '}'
. /* ignore bad characters */
<*><> return 'EOF'
(.|\n)+ return 'CODE'
%%
## license
MIT