Advertisement
BlakPilar

Untitled

May 8th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. grammar Hadean;
  2.  
  3. options {
  4. language=CSharp3;
  5. TokenLabelType=CommonToken;
  6. output=AST;
  7. ASTLabelType=CommonTree;
  8. }
  9.  
  10. @lexer::namespace { Hadean.Compiler.ANTLR }
  11. @parser::namespace { Hadean.Compiler.ANTLR }
  12.  
  13. // PARSER
  14. public
  15. compileUnit
  16. : typeSpecification EOF
  17. ;
  18.  
  19. modifier
  20. : 'public' | 'private'
  21. | 'const' | 'static'
  22. | 'abstract' | 'sealed'
  23. ;
  24.  
  25. type
  26. : 'byte' | 'sbyte'
  27. | 'word' | 'uword' // Same as short
  28. | 'int' | 'uint'
  29. | 'long' | 'ulong'
  30. | 'float' | 'double'
  31. | 'char' | 'string'
  32. | 'bool' | 'var' // Var will be object
  33. | IDENT // Custom types
  34. // | typeSpecification
  35. ;
  36.  
  37. header
  38. : /* stmt_Preprocessor* */ stmt_Import
  39. ;
  40.  
  41. stmt_Preprocessor
  42. : 'import' IDENT 'from' IDENT ('.' IDENT)* ';'
  43. ;
  44.  
  45. typeSpecification
  46. : header
  47. ( type_Class
  48. | type_Enum
  49. | type_Namespace
  50. | type_Struct
  51. )
  52. ;
  53.  
  54. type_Class
  55. // IS <interface> EXTENDS <class>
  56. : modifier* 'class' IDENT ('is' IDENT (',' IDENT)*)? ('extends' IDENT)? body
  57. ;
  58.  
  59. type_Enum
  60. : modifier* 'enum' IDENT
  61. SCOPE_BEGIN
  62. type_Enum_Enumeration (',' type_Enum_Enumeration)*
  63. SCOPE_END
  64. ;
  65.  
  66. type_Enum_Enumeration
  67. : IDENT ('=' (NUMBER | HEX))?
  68. ;
  69.  
  70. type_Namespace
  71. : 'namespace' IDENT body
  72. ;
  73.  
  74. type_Struct
  75. : modifier* 'struct' IDENT body
  76. ;
  77.  
  78. statement
  79. : stmt_Assign
  80. | stmt_Decl
  81. | stmt_DoWhile
  82. | stmt_For
  83. | stmt_ForEach
  84. | stmt_If
  85. | stmt_MethCall
  86. | stmt_Property
  87. | stmt_While
  88. ;
  89.  
  90. stmt_Assign
  91. : '=' (NUMBER | HEX | STRING_LITERAL | CHAR_LITERAL) ';'
  92. | '=' 'new' IDENT '(' parameters? ')' ';'
  93. | '=' ('true' | 'false') ';'
  94. | '=' expression ';'
  95. | '=' stmt_Assign_Array ';'
  96. ;
  97.  
  98. stmt_Assign_Array
  99. : 'new' type '['
  100. ;
  101.  
  102. stmt_Decl
  103. : type ('[' ','* ']')? IDENT stmt_Assign? ';'
  104. ;
  105.  
  106. stmt_DoWhile
  107. : 'do' body
  108. 'while' '(' expression ')'
  109. ;
  110.  
  111. stmt_For
  112. : 'for' '(' type IDENT ';' expression ';' statement* ')' body
  113. ;
  114.  
  115. stmt_ForEach
  116. : 'foreach' '(' type IDENT ':' IDENT ')' body
  117. ;
  118.  
  119. stmt_If
  120. : 'if' '(' expression ')' body
  121. ( 'elseif' '(' expression ')' body)*
  122. ( 'else' body )?
  123. ;
  124.  
  125. stmt_MethCall
  126. : IDENT '(' parameters ')' ';'
  127. ;
  128.  
  129. stmt_Property
  130. : modifier* type IDENT
  131. ( SCOPE_BEGIN
  132. ('private'? 'get' (body | ';'))?
  133. ('private'? 'set' (body | ';'))?
  134. SCOPE_END
  135. )
  136. | ';'
  137. ;
  138.  
  139. expression
  140. : stmt_MethCall
  141. ;
  142.  
  143. parameters
  144. : parameter (',' parameter)*
  145. ;
  146.  
  147. parameter
  148. : type IDENT
  149. ;
  150.  
  151. body
  152. : SCOPE_BEGIN statement* SCOPE_END
  153. ;
  154.  
  155. // LEXER
  156.  
  157. WS
  158. : ' '
  159. | '\t'
  160. | '\r'
  161. | '\n'
  162. ;
  163.  
  164. IDENT
  165. : (LETTER | '_') (LETTER | DIGIT | '_')*
  166. ;
  167.  
  168. NUMBER
  169. : DIGIT+
  170. ;
  171.  
  172. HEX
  173. : '0x' (DIGIT | 'a'..'f' | 'A'..'F')+
  174. ;
  175.  
  176. SCOPE_BEGIN
  177. : '{'
  178. ;
  179.  
  180. SCOPE_END
  181. : '}'
  182. ;
  183.  
  184. fragment DIGIT
  185. : '0'..'9'
  186. ;
  187.  
  188. fragment LETTER
  189. : 'a'..'z' | 'A'..'Z'
  190. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement