
Untitled
By:
BlakPilar on
May 8th, 2012 | syntax:
None | size: 2.73 KB | hits: 23 | expires: Never
grammar Hadean;
options {
language=CSharp3;
TokenLabelType=CommonToken;
output=AST;
ASTLabelType=CommonTree;
}
@lexer::namespace { Hadean.Compiler.ANTLR }
@parser::namespace { Hadean.Compiler.ANTLR }
// PARSER
public
compileUnit
: typeSpecification EOF
;
modifier
: 'public' | 'private'
| 'const' | 'static'
| 'abstract' | 'sealed'
;
type
: 'byte' | 'sbyte'
| 'word' | 'uword' // Same as short
| 'int' | 'uint'
| 'long' | 'ulong'
| 'float' | 'double'
| 'char' | 'string'
| 'bool' | 'var' // Var will be object
| IDENT // Custom types
// | typeSpecification
;
header
: /* stmt_Preprocessor* */ stmt_Import
;
stmt_Preprocessor
: 'import' IDENT 'from' IDENT ('.' IDENT)* ';'
;
typeSpecification
: header
( type_Class
| type_Enum
| type_Namespace
| type_Struct
)
;
type_Class
// IS <interface> EXTENDS <class>
: modifier* 'class' IDENT ('is' IDENT (',' IDENT)*)? ('extends' IDENT)? body
;
type_Enum
: modifier* 'enum' IDENT
SCOPE_BEGIN
type_Enum_Enumeration (',' type_Enum_Enumeration)*
SCOPE_END
;
type_Enum_Enumeration
: IDENT ('=' (NUMBER | HEX))?
;
type_Namespace
: 'namespace' IDENT body
;
type_Struct
: modifier* 'struct' IDENT body
;
statement
: stmt_Assign
| stmt_Decl
| stmt_DoWhile
| stmt_For
| stmt_ForEach
| stmt_If
| stmt_MethCall
| stmt_Property
| stmt_While
;
stmt_Assign
: '=' (NUMBER | HEX | STRING_LITERAL | CHAR_LITERAL) ';'
| '=' 'new' IDENT '(' parameters? ')' ';'
| '=' ('true' | 'false') ';'
| '=' expression ';'
| '=' stmt_Assign_Array ';'
;
stmt_Assign_Array
: 'new' type '['
;
stmt_Decl
: type ('[' ','* ']')? IDENT stmt_Assign? ';'
;
stmt_DoWhile
: 'do' body
'while' '(' expression ')'
;
stmt_For
: 'for' '(' type IDENT ';' expression ';' statement* ')' body
;
stmt_ForEach
: 'foreach' '(' type IDENT ':' IDENT ')' body
;
stmt_If
: 'if' '(' expression ')' body
( 'elseif' '(' expression ')' body)*
( 'else' body )?
;
stmt_MethCall
: IDENT '(' parameters ')' ';'
;
stmt_Property
: modifier* type IDENT
( SCOPE_BEGIN
('private'? 'get' (body | ';'))?
('private'? 'set' (body | ';'))?
SCOPE_END
)
| ';'
;
expression
: stmt_MethCall
;
parameters
: parameter (',' parameter)*
;
parameter
: type IDENT
;
body
: SCOPE_BEGIN statement* SCOPE_END
;
// LEXER
WS
: ' '
| '\t'
| '\r'
| '\n'
;
IDENT
: (LETTER | '_') (LETTER | DIGIT | '_')*
;
NUMBER
: DIGIT+
;
HEX
: '0x' (DIGIT | 'a'..'f' | 'A'..'F')+
;
SCOPE_BEGIN
: '{'
;
SCOPE_END
: '}'
;
fragment DIGIT
: '0'..'9'
;
fragment LETTER
: 'a'..'z' | 'A'..'Z'
;