Advertisement
Guest User

Untitled

a guest
Jan 13th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BNF 2.59 KB | None | 0 0
  1. // This grammar is supposed to parse this type of syntax:
  2. /*
  3. // this is a comment
  4.  
  5. pub static let a: number = 10;
  6.  
  7. func add(a: number, b: number) : number {
  8.     return a + b;
  9. }
  10.  
  11. func test() : number {
  12.     let b = 5;
  13.     if (b > 10) {
  14.         return 42 * b;
  15.     }
  16.     return 42 * a;
  17. }
  18.  
  19. test();
  20. */
  21.  
  22. grammar lang;
  23.  
  24. // PARSER RULES
  25. program : func_body?;
  26.  
  27. return_stmt : 'return' test ';';
  28. if_stmt
  29.     : 'if' test '{' stmt* '}' ('else if' test '{' stmt* '}')* ('else' '{' stmt* '}')?
  30.     | 'if' paren_test stmt ('else if' paren_test stmt)* ('else' stmt)?
  31.     ;
  32.  
  33. param : ID (':' ID) ('=' test)?;
  34. let_params : param (',' param)*;
  35. func_body : stmt+;
  36.  
  37. func_def_stmt
  38.     : 'func' ID '(' let_params? ')' (':' ID) '{' func_body? '}'
  39.     ;
  40.  
  41. let_def_stmt
  42.     : 'pub'? ('final' | 'static')? 'let' let_params ';'
  43.     ;
  44.  
  45. stmt
  46.     : ';'
  47.     | test '=' test ';'
  48.     | test op_augassign test ';'
  49.     | test ('++' | '--') ';'
  50.     | ('++' | '--') test ';'
  51.     | if_stmt
  52.     | paren_test ';'
  53.     | test ';'
  54.     | let_def_stmt
  55.     | func_def_stmt
  56.     ;
  57.  
  58. paren_test: '(' test ')';
  59. test: or_test ('?' test ':' test)?
  60.     ;
  61. or_test: and_test ('||' and_test)*
  62.     ;
  63. and_test: not_test ('&&' not_test)*
  64.     ;
  65. not_test: '!' not_test | comparison
  66.     ;
  67. comparison: expr (op_comp expr)*
  68.     ;
  69.  
  70. op_augassign : '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=';
  71. op_comp : '<' | '>' | '<=' | '>=' | '!=' | '==' | 'is' | 'has';
  72.  
  73. expr: xor_expr ('|' xor_expr)*
  74.     ;
  75. xor_expr: and_expr ('^' and_expr)*
  76.     ;
  77. and_expr: shift_expr ('&' shift_expr)*
  78.     ;
  79. shift_expr: arith_expr (('<<'|'>>') arith_expr)*
  80.     ;
  81. arith_expr: term (('+'|'-') term)*
  82.     ;
  83. term: factor (('*'|'/'|'%') factor)*
  84.     ;
  85. factor: ('+'|'-'|'~') factor | power
  86.     ;
  87. power: atom trailer* ('**' factor)?
  88.     ;
  89.  
  90. atom : ID | NUMBER | STRING;
  91.  
  92. trailer
  93.     : '(' (arglist)? ')' | '[' test ']' | '.' ID
  94.     ;
  95.  
  96. arglist
  97.     : test (',' test)*
  98.     ;
  99.  
  100. // LEXER RULES
  101. ID : [a-zA-Z_] [a-zA-Z0-9_]+;
  102.  
  103. STRING : '"' (ESC_SEQ | ~('\\'|'"'))* '"';
  104. CHAR : '\'' (ESC_SEQ | ~('\\'|'\'')) '\'';
  105.  
  106. NUMBER
  107.     : SIGN? [0-9]+ EXPONENT?
  108.     | SIGN? [0-9]+ '.' [0-9]+ EXPONENT? 'f'?
  109.     | SIGN? '.' [0-9]+ EXPONENT? 'f'?
  110.     | SIGN? HEX_NUMBER
  111.     ;
  112.  
  113. HEX_NUMBER
  114.     : '0' [xX] HEX+
  115.     ;
  116.  
  117. fragment
  118. HEX
  119.     : [a-fA-F0-9]
  120.     ;
  121.  
  122. EXPONENT
  123.     : [eE] SIGN? [0-9]+
  124.     ;
  125.  
  126. fragment
  127. ESC_SEQ
  128.     : '\\' ('b'|'t'|'n'|'f'|'r'|'"'|'\''|'\\')
  129.     | UNICODE_ESC
  130.     | OCTAL_ESC
  131.     ;
  132.  
  133. fragment
  134. OCTAL_ESC
  135.     : '\\' ('0'..'3') ('0'..'7') ('0'..'7')
  136.     | '\\' ('0'..'7') ('0'..'7')
  137.     | '\\' ('0'..'7')
  138.     ;
  139.  
  140. fragment
  141. UNICODE_ESC
  142.     : '\\' 'u' HEX HEX HEX HEX
  143.     ;
  144.  
  145. WS
  146.   : [ \r\n\t] + -> skip
  147.   ;
  148.  
  149. UNKNOWN : . -> skip;
  150.  
  151. fragment
  152. SIGN
  153.     : [+-]
  154.     ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement