Giter Site home page Giter Site logo

lex-parser's Introduction

lex-parser

A parser for lexical grammars used by jison 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

%%

<action>[^{}]+          return 'ACTION_BODY'
<action>"{"             yy.depth++; return '{'
<action>"}"             yy.depth == 0 ? this.begin('trail') : yy.depth--; return '}'

<conditions>{NAME}      return 'NAME'
<conditions>">"         this.popState(); return '>'
<conditions>","         return ','
<conditions>"*"         return '*'

<rules>\n+              /* */
<rules>\s+              this.begin('indented')
<rules>"%%"             this.begin('code'); return '%%'
<rules>[a-zA-Z0-9_]+    return 'CHARACTER_LIT'

<options>{NAME}         yy.options[yytext] = true
<options>\n+            this.begin('INITIAL')
<options>\s+\n+         this.begin('INITIAL')
<options>\s+            /* empty */

<start_condition>{NAME}         return 'START_COND'
<start_condition>\n+            this.begin('INITIAL')
<start_condition>\s+\n+         this.begin('INITIAL')
<start_condition>\s+            /* empty */

<trail>.*\n+                    this.begin('rules')

<indented>"{"                   yy.depth = 0; this.begin('action'); return '{'
<indented>"%{"(.|\n)*?"%}"      this.begin('trail'); yytext = yytext.substr(2, yytext.length-4);return 'ACTION'
"%{"(.|\n)*?"%}"                yytext = yytext.substr(2, yytext.length-4); return 'ACTION'
<indented>.+                    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 ','
"<<EOF>>"                       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 */
<*><<EOF>>                      return 'EOF'

<code>(.|\n)+                   return 'CODE'

%%

license

MIT

lex-parser's People

Contributors

gerhobbelt avatar lhchavez avatar redchair123 avatar zaach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lex-parser's Issues

please tag releases

We use github tarballs for debian package and without tags, we have to manually find the commits for each release and get a snapshot. But still dbeian's tools like uscan cannot do this automatically. It would be good if you can tag your releases.

end of line ($) doesn't work as expected

just tried to use jison, and this got me lost for hours.

$ doesn't seem to work.

jison documentation points to bison, where $ is widely used.

in the samples here the \n+ is been used instead.

Create LICENSE file

Would you please create a LICENSE or LICENSE.md file with your copyright information and the text of the MIT license? The MIT license specifically states that the license text must accompany the source code.

^ (start of line) doesn't work as expected

^ is working, but it seems to be recognized in more situations than it should.

start of line is very handy when writing parsers where end-of-line is an entry delimiter (such as when parsing HTTP)

^{header}\={value}$

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.