Skip to content

Instantly share code, notes, and snippets.

@chrisortman
Created August 17, 2009 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisortman/168816 to your computer and use it in GitHub Desktop.
Save chrisortman/168816 to your computer and use it in GitHub Desktop.
grammar Brail;
options {output=template; rewrite=true;}
doc : mixedContent;
mixedContent
: .* brailExpression mixedContent?
;
brailExpression
: '${' expr '}'
| '<%' WS* expr WS* '%>';
expr
: variableRef
| variableRef '.' expr
| methodCall
| stringExpr
| dictExpr
-> template(d={$dictExpr.text}) "new <d>.ToDictionary()"
| symbol
-> template(s={$symbol.text}) "\"<s>\""
| ifExpr
| elseExpr
| endExpr
| INT
;
ifExpr : 'if' WS* expr WS* ':'
-> template(s={$expr.text}) "if(<s>) {" ;
endExpr
: 'end' -> template() "}" ;
elseExpr
: 'else:' -> template() "} else {" ;
dictExpr
: '{' dictEntry (',' dictEntry)* '}' ;
dictEntry
: dictKey ':' dictValue -> template(k={$dictKey.text},v={$dictValue.text}) "<k> = <v>";
dictKey
: symbol
| stringExpr
;
dictValue
: expr
;
symbol
: '@' ID -> template(id={$ID.text}) "<id>"
;
stringExpr
: '"' .* '"'
| squote .* squote
-> template(s={$stringExpr.text}) "\"<s>\""
;
squote : '\'' -> template() "" ;
variableRef
: ID ;
methodCall
: methodName '(' methodArgs? ')' ;
methodName
: ID ;
methodArgs
: expr (',' methodArgs)? ;
INT : ('0'..'9')+ ;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
WS : (' '|'\t'|'\n'|'\r')+ ;
SPECIAL_CHARS
: ('<' | '>' | '=' | '/') ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment