Screwing around with JS/CC parser generator

I've been looking to add more functionality to hatch templates and after searching I feel like I've finally gotten somewhere. Js/cc is a parser and lexical analyzer generator. I can use it to create my own "language" and utilize javascript for processing.

Although I've benefited from parsers and lexers for a long time, I've never had to think about how they work. Writing my own token definitions and working through processing patterns has been challenging but ultimately exciting and rewarding. Testing what I write in js/cc is like re-living my first "hello world" in basic. I have a feeling its going to be a long process to develop a fully-cooked (hell, "half-cooked") implementation for all the functionality I want in hatch templates but I've got to start somewhere.

Below is a super simple implementation of an "echo" function and a substring() function which can be tested with the following:

  • say 'some string'
  • say 'some string'.substr(1,3);
/~ creating an echo parsing function 
   to test enter: say 'some_string';
   substring function: say 'some_string'.substr(first_num, last_num);

!     ' |\r|\n|\t|//[^\n]*\n'
      '[A-Za-z_][A-Za-z0-9_]*' Identifier
      '\'([^\']|\'\')*\'' String [* %match = %match.substr( 1,%match.length - 2 );
                                    %match = %match.replace( /''/g,"\'" );
      '\.substr' SUBSTR
      '[0-9]+' NUM 

Program:     Program Stmt     

Stmt_List:   Stmt_List Stmt

Stmt:        SAY String ';'    [* alert(%2); *]
             | SAY String SUBSTR '(' NUM ',' NUM ')' ';'   [* alert(%2.substring(%5,%7)); *]

Tagged as hatch , js/cc jscc , lexer parser
Written by Andrew Konkol on July 16th, 2013


Log in with Twitter, Google, Facebook, LinkedIn to leave a comment.