Skip to content

Instantly share code, notes, and snippets.

@virtix
Created November 15, 2010 17:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virtix/700678 to your computer and use it in GitHub Desktop.
Save virtix/700678 to your computer and use it in GitHub Desktop.
keeping notes.
import relative/path/to/directory;
import path/to/File;
script extends="foo" {
//var defs w/assignment
foo = 123;
bar = foo;
//var defs w/out assignments
a,b,c;
//list of vars w/assignment
x=1,y=2,z=3;
//list of vars w & without assignment
r,q,p;
function doSomething() {
z=111;
}
function doSomethineWithParams(a,b,c){
print( "n00b" );
}
} //end cfc
grammar CFrunt;
options {
backtrack=true;
memoize=true;
}
@header {
package org.foo.bar;
}
@lexer::header {
package org.foo.bar;
}
@members {
HashMap symtab = new HashMap();
}
@rulecatch{
catch (RecognitionException re){
throw re;
//new Throwable(e);
}
}
script
: (importStatement ';')* componentDefinition
;
importStatement
: 'import' ID ( ('/'|'.') ID )*
;
//Parse rules
componentDefinition
: ('script'|'component') componentDefAttributes* '{' componentBody '}' {System.out.println("in componentDefinition"); }
;
componentDefAttributes
: 'extends' '=' ( ID ('.' ID)* | QUOTE? ID ('.' ID)* QUOTE?)
| 'static' '=' (('true'|'false') | QUOTE? ('true'|'false') QUOTE?)
;
componentBody
: variableDeclaration* ';' function* {System.out.println("in componentBody"); }
;
function
: functionDeclaration '{' functionBody '}' //closure ... TODO: scope
;
functionDeclaration
: 'function' ID '(' argumentList* ')'
;
argumentList //scope?
: ID (',' ID)* ';'
| ID '=' (ID|expression) (',' ID '=' (ID|expression) )*
;
functionBody
: variableDeclaration*
| statement*
;
statement
: expression
| print
;
variableDeclaration
: ID (',' ID)* ';'
| ID '=' (ID|expression) (',' ID '=' (ID|expression) )* ';'
;
expression returns [int value]
: e=multexpression {$value = $e.value;}
( '+' e=multexpression {$value += $e.value;}
| '-' e=multexpression {$value -= $e.value;}
)*
;
multexpression returns [int value]
: e=atom {$value = $e.value;} ('*' e=atom {$value *= $e.value;})*
;
atom returns [int value]
: INT {$value = Integer.parseInt($INT.text);}
| ID
{
Integer v = (Integer)symtab.get($ID.text);
if ( v!=null ) $value = v.intValue();
else System.err.println("undefined variable "+$ID.text);
}
| '(' e=expression ')' {$value = $e.value;}
;
//util functions
print
: 'print' '(' QUOTE? ID QUOTE? ')' ';' {System.out.println($ID.text);}// printRule[a]
;
printRule [Object o]
: {System.out.println($o.toString());}
;
//Token refs
QUOTE : '\"';
INT : '0'..'9'+ ;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
| '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
;
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
CHAR: '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
;
fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
fragment
ESC_SEQ
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
| UNICODE_ESC
| OCTAL_ESC
;
fragment
OCTAL_ESC
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;
fragment
UNICODE_ESC
: '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment