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' "SAY" ';' '[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)); *] ;