"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.2/pony.g" (3 Feb 2020, 11146 Bytes) of package /linux/misc/ponyc-0.33.2.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "pony.g": 0.33.1_vs_0.33.2.

    1 // ANTLR v3 grammar
    2 grammar pony;
    3 
    4 options
    5 {
    6   output = AST;
    7   k = 1;
    8 }
    9 
   10 // Parser
   11 
   12 module
   13   : STRING? use* class_def* 
   14   ;
   15 
   16 use
   17   : 'use' (ID '=')? (STRING | use_ffi) ('if' infix)?
   18   ;
   19 
   20 use_ffi
   21   : '@' (ID | STRING) typeargs ('(' | LPAREN_NEW) params? ')' '?'?
   22   ;
   23 
   24 class_def
   25   : ('type' | 'interface' | 'trait' | 'primitive' | 'struct' | 'class' | 'actor') ('\\' ID (',' ID)* '\\')? '@'? cap? ID typeparams? ('is' type)? STRING? members
   26   ;
   27 
   28 members
   29   : field* method*
   30   ;
   31 
   32 field
   33   : ('var' | 'let' | 'embed') ID ':' type ('=' infix)? STRING?
   34   ;
   35 
   36 method
   37   : ('fun' | 'be' | 'new') ('\\' ID (',' ID)* '\\')? (cap | '@')? ID typeparams? ('(' | LPAREN_NEW) params? ')' (':' type)? '?'? STRING? ('=>' rawseq)?
   38   ;
   39 
   40 annotatedrawseq
   41   : ('\\' ID (',' ID)* '\\')? (exprseq | jump)
   42   ;
   43 
   44 rawseq
   45   : exprseq
   46   | jump
   47   ;
   48 
   49 exprseq
   50   : assignment (semiexpr | nosemi)?
   51   ;
   52 
   53 nextexprseq
   54   : nextassignment (semiexpr | nosemi)?
   55   ;
   56 
   57 nosemi
   58   : nextexprseq
   59   | jump
   60   ;
   61 
   62 semiexpr
   63   : ';' (exprseq | jump)
   64   ;
   65 
   66 jump
   67   : ('return' | 'break' | 'continue' | 'error' | 'compile_intrinsic' | 'compile_error') rawseq?
   68   ;
   69 
   70 nextassignment
   71   : nextinfix ('=' assignment)?
   72   ;
   73 
   74 assignment
   75   : infix ('=' assignment)?
   76   ;
   77 
   78 nextinfix
   79   : nextterm antlr_0*
   80   ;
   81 
   82 infix
   83   : term antlr_1*
   84   ;
   85 
   86 isop
   87   : ('is' | 'isnt') term
   88   ;
   89 
   90 binop
   91   : ('and' | 'or' | 'xor' | '+' | '-' | '*' | '/' | '%' | '%%' | '+~' | '-~' | '*~' | '/~' | '%~' | '%%~' | '<<' | '>>' | '<<~' | '>>~' | '==' | '!=' | '<' | '<=' | '>=' | '>' | '==~' | '!=~' | '<~' | '<=~' | '>=~' | '>~') '?'? term
   92   ;
   93 
   94 nextterm
   95   : 'if' ('\\' ID (',' ID)* '\\')? rawseq 'then' rawseq (elseif | ('else' annotatedrawseq))? 'end'
   96   | 'ifdef' ('\\' ID (',' ID)* '\\')? infix 'then' rawseq (elseifdef | ('else' annotatedrawseq))? 'end'
   97   | 'iftype' ('\\' ID (',' ID)* '\\')? iftype (elseiftype | ('else' annotatedrawseq))? 'end'
   98   | 'match' ('\\' ID (',' ID)* '\\')? rawseq caseexpr* ('else' annotatedrawseq)? 'end'
   99   | 'while' ('\\' ID (',' ID)* '\\')? rawseq 'do' rawseq ('else' annotatedrawseq)? 'end'
  100   | 'repeat' ('\\' ID (',' ID)* '\\')? rawseq 'until' annotatedrawseq ('else' annotatedrawseq)? 'end'
  101   | 'for' ('\\' ID (',' ID)* '\\')? idseq 'in' rawseq 'do' rawseq ('else' annotatedrawseq)? 'end'
  102   | 'with' ('\\' ID (',' ID)* '\\')? (withelem (',' withelem)*) 'do' rawseq ('else' annotatedrawseq)? 'end'
  103   | 'try' ('\\' ID (',' ID)* '\\')? rawseq ('else' annotatedrawseq)? ('then' annotatedrawseq)? 'end'
  104   | 'recover' ('\\' ID (',' ID)* '\\')? cap? rawseq 'end'
  105   | 'consume' cap? term
  106   | nextpattern
  107   | '#' postfix
  108   ;
  109 
  110 term
  111   : 'if' ('\\' ID (',' ID)* '\\')? rawseq 'then' rawseq (elseif | ('else' annotatedrawseq))? 'end'
  112   | 'ifdef' ('\\' ID (',' ID)* '\\')? infix 'then' rawseq (elseifdef | ('else' annotatedrawseq))? 'end'
  113   | 'iftype' ('\\' ID (',' ID)* '\\')? iftype (elseiftype | ('else' annotatedrawseq))? 'end'
  114   | 'match' ('\\' ID (',' ID)* '\\')? rawseq caseexpr* ('else' annotatedrawseq)? 'end'
  115   | 'while' ('\\' ID (',' ID)* '\\')? rawseq 'do' rawseq ('else' annotatedrawseq)? 'end'
  116   | 'repeat' ('\\' ID (',' ID)* '\\')? rawseq 'until' annotatedrawseq ('else' annotatedrawseq)? 'end'
  117   | 'for' ('\\' ID (',' ID)* '\\')? idseq 'in' rawseq 'do' rawseq ('else' annotatedrawseq)? 'end'
  118   | 'with' ('\\' ID (',' ID)* '\\')? (withelem (',' withelem)*) 'do' rawseq ('else' annotatedrawseq)? 'end'
  119   | 'try' ('\\' ID (',' ID)* '\\')? rawseq ('else' annotatedrawseq)? ('then' annotatedrawseq)? 'end'
  120   | 'recover' ('\\' ID (',' ID)* '\\')? cap? rawseq 'end'
  121   | 'consume' cap? term
  122   | pattern
  123   | '#' postfix
  124   ;
  125 
  126 withelem
  127   : idseq '=' rawseq
  128   ;
  129 
  130 caseexpr
  131   : '|' ('\\' ID (',' ID)* '\\')? casepattern? ('if' rawseq)? ('=>' rawseq)?
  132   ;
  133 
  134 elseiftype
  135   : 'elseif' ('\\' ID (',' ID)* '\\')? iftype (elseiftype | ('else' annotatedrawseq))?
  136   ;
  137 
  138 iftype
  139   : type '<:' type 'then' rawseq
  140   ;
  141 
  142 elseifdef
  143   : 'elseif' ('\\' ID (',' ID)* '\\')? infix 'then' rawseq (elseifdef | ('else' annotatedrawseq))?
  144   ;
  145 
  146 elseif
  147   : 'elseif' ('\\' ID (',' ID)* '\\')? rawseq 'then' rawseq (elseif | ('else' annotatedrawseq))?
  148   ;
  149 
  150 idseq
  151   : ID
  152   | ('(' | LPAREN_NEW) idseq_in_seq (',' idseq_in_seq)* ')'
  153   ;
  154 
  155 idseq_in_seq
  156   : ID
  157   | ('(' | LPAREN_NEW) idseq_in_seq (',' idseq_in_seq)* ')'
  158   ;
  159 
  160 nextpattern
  161   : ('var' | 'let' | 'embed') ID (':' type)?
  162   | nextparampattern
  163   ;
  164 
  165 casepattern
  166   : ('var' | 'let' | 'embed') ID (':' type)?
  167   | caseparampattern
  168   ;
  169 
  170 pattern
  171   : ('var' | 'let' | 'embed') ID (':' type)?
  172   | parampattern
  173   ;
  174 
  175 nextparampattern
  176   : ('not' | 'addressof' | MINUS_NEW | MINUS_TILDE_NEW | 'digestof') parampattern
  177   | nextpostfix
  178   ;
  179 
  180 caseparampattern
  181   : ('not' | 'addressof' | '-' | '-~' | MINUS_NEW | MINUS_TILDE_NEW | 'digestof') caseparampattern
  182   | casepostfix
  183   ;
  184 
  185 parampattern
  186   : ('not' | 'addressof' | '-' | '-~' | MINUS_NEW | MINUS_TILDE_NEW | 'digestof') parampattern
  187   | postfix
  188   ;
  189 
  190 nextpostfix
  191   : nextatom antlr_2*
  192   ;
  193 
  194 casepostfix
  195   : caseatom antlr_3*
  196   ;
  197 
  198 postfix
  199   : atom antlr_4*
  200   ;
  201 
  202 call
  203   : '(' positional? named? ')' '?'?
  204   ;
  205 
  206 chain
  207   : '.>' ID
  208   ;
  209 
  210 tilde
  211   : '~' ID
  212   ;
  213 
  214 dot
  215   : '.' ID
  216   ;
  217 
  218 nextatom
  219   : ID
  220   | 'this'
  221   | literal
  222   | LPAREN_NEW rawseq tuple? ')'
  223   | LSQUARE_NEW ('as' type ':')? rawseq? ']'
  224   | 'object' ('\\' ID (',' ID)* '\\')? cap? ('is' type)? members 'end'
  225   | '{' ('\\' ID (',' ID)* '\\')? cap? ID? typeparams? ('(' | LPAREN_NEW) lambdaparams? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq '}' cap?
  226   | '@{' ('\\' ID (',' ID)* '\\')? cap? ID? typeparams? ('(' | LPAREN_NEW) lambdaparams? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq '}' cap?
  227   | '@' (ID | STRING) typeargs? ('(' | LPAREN_NEW) positional? named? ')' '?'?
  228   | '__loc'
  229   | 'if' ('\\' ID (',' ID)* '\\')? rawseq 'then' rawseq (elseif | ('else' annotatedrawseq))? 'end'
  230   | 'while' ('\\' ID (',' ID)* '\\')? rawseq 'do' rawseq ('else' annotatedrawseq)? 'end'
  231   | 'for' ('\\' ID (',' ID)* '\\')? idseq 'in' rawseq 'do' rawseq ('else' annotatedrawseq)? 'end'
  232   ;
  233 
  234 caseatom
  235   : ID
  236   | 'this'
  237   | literal
  238   | ('(' | LPAREN_NEW) rawseq tuple? ')'
  239   | ('[' | LSQUARE_NEW) ('as' type ':')? rawseq? ']'
  240   | 'object' ('\\' ID (',' ID)* '\\')? cap? ('is' type)? members 'end'
  241   | '{' ('\\' ID (',' ID)* '\\')? cap? ID? typeparams? ('(' | LPAREN_NEW) lambdaparams? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq '}' cap?
  242   | '@{' ('\\' ID (',' ID)* '\\')? cap? ID? typeparams? ('(' | LPAREN_NEW) lambdaparams? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq '}' cap?
  243   | '@' (ID | STRING) typeargs? ('(' | LPAREN_NEW) positional? named? ')' '?'?
  244   | '__loc'
  245   | 'while' ('\\' ID (',' ID)* '\\')? rawseq 'do' rawseq ('else' annotatedrawseq)? 'end'
  246   | 'for' ('\\' ID (',' ID)* '\\')? idseq 'in' rawseq 'do' rawseq ('else' annotatedrawseq)? 'end'
  247   ;
  248 
  249 atom
  250   : ID
  251   | 'this'
  252   | literal
  253   | ('(' | LPAREN_NEW) rawseq tuple? ')'
  254   | ('[' | LSQUARE_NEW) ('as' type ':')? rawseq? ']'
  255   | 'object' ('\\' ID (',' ID)* '\\')? cap? ('is' type)? members 'end'
  256   | '{' ('\\' ID (',' ID)* '\\')? cap? ID? typeparams? ('(' | LPAREN_NEW) lambdaparams? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq '}' cap?
  257   | '@{' ('\\' ID (',' ID)* '\\')? cap? ID? typeparams? ('(' | LPAREN_NEW) lambdaparams? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq '}' cap?
  258   | '@' (ID | STRING) typeargs? ('(' | LPAREN_NEW) positional? named? ')' '?'?
  259   | '__loc'
  260   | 'if' ('\\' ID (',' ID)* '\\')? rawseq 'then' rawseq (elseif | ('else' annotatedrawseq))? 'end'
  261   | 'while' ('\\' ID (',' ID)* '\\')? rawseq 'do' rawseq ('else' annotatedrawseq)? 'end'
  262   | 'for' ('\\' ID (',' ID)* '\\')? idseq 'in' rawseq 'do' rawseq ('else' annotatedrawseq)? 'end'
  263   ;
  264 
  265 tuple
  266   : ',' rawseq (',' rawseq)*
  267   ;
  268 
  269 lambdacaptures
  270   : ('(' | LPAREN_NEW) (lambdacapture | 'this') (',' (lambdacapture | 'this'))* ')'
  271   ;
  272 
  273 lambdacapture
  274   : ID (':' type)? ('=' infix)?
  275   ;
  276 
  277 lambdaparams
  278   : lambdaparam (',' lambdaparam)*
  279   ;
  280 
  281 lambdaparam
  282   : ID (':' type)? ('=' infix)?
  283   ;
  284 
  285 positional
  286   : rawseq (',' rawseq)*
  287   ;
  288 
  289 named
  290   : 'where' namedarg (',' namedarg)*
  291   ;
  292 
  293 namedarg
  294   : ID '=' rawseq
  295   ;
  296 
  297 type
  298   : atomtype ('->' type)?
  299   ;
  300 
  301 atomtype
  302   : 'this'
  303   | cap
  304   | ('(' | LPAREN_NEW) infixtype tupletype? ')'
  305   | nominal
  306   | lambdatype
  307   | barelambdatype
  308   ;
  309 
  310 barelambdatype
  311   : '@{' cap? ID? typeparams? ('(' | LPAREN_NEW) (type (',' type)*)? ')' (':' type)? '?'? '}' (cap | gencap)? ('^' | '!')?
  312   ;
  313 
  314 lambdatype
  315   : '{' cap? ID? typeparams? ('(' | LPAREN_NEW) (type (',' type)*)? ')' (':' type)? '?'? '}' (cap | gencap)? ('^' | '!')?
  316   ;
  317 
  318 tupletype
  319   : ',' infixtype (',' infixtype)*
  320   ;
  321 
  322 infixtype
  323   : type antlr_5*
  324   ;
  325 
  326 isecttype
  327   : '&' type
  328   ;
  329 
  330 uniontype
  331   : '|' type
  332   ;
  333 
  334 nominal
  335   : ID ('.' ID)? typeargs? (cap | gencap)? ('^' | '!')?
  336   ;
  337 
  338 gencap
  339   : '#read'
  340   | '#send'
  341   | '#share'
  342   | '#alias'
  343   | '#any'
  344   ;
  345 
  346 cap
  347   : 'iso'
  348   | 'trn'
  349   | 'ref'
  350   | 'val'
  351   | 'box'
  352   | 'tag'
  353   ;
  354 
  355 typeargs
  356   : '[' typearg (',' typearg)* ']'
  357   ;
  358 
  359 typeparams
  360   : ('[' | LSQUARE_NEW) typeparam (',' typeparam)* ']'
  361   ;
  362 
  363 params
  364   : (param | '...') (',' (param | '...'))*
  365   ;
  366 
  367 typeparam
  368   : ID (':' type)? ('=' typearg)?
  369   ;
  370 
  371 typearg
  372   : type
  373   | literal
  374   | '#' postfix
  375   ;
  376 
  377 literal
  378   : 'true'
  379   | 'false'
  380   | INT
  381   | FLOAT
  382   | STRING
  383   ;
  384 
  385 param
  386   : ID ':' type ('=' infix)?
  387   ;
  388 
  389 antlr_0
  390   : binop
  391   | isop
  392   | 'as' type
  393   ;
  394 
  395 antlr_1
  396   : binop
  397   | isop
  398   | 'as' type
  399   ;
  400 
  401 antlr_2
  402   : dot
  403   | tilde
  404   | chain
  405   | typeargs
  406   | call
  407   ;
  408 
  409 antlr_3
  410   : dot
  411   | tilde
  412   | chain
  413   | typeargs
  414   | call
  415   ;
  416 
  417 antlr_4
  418   : dot
  419   | tilde
  420   | chain
  421   | typeargs
  422   | call
  423   ;
  424 
  425 antlr_5
  426   : uniontype
  427   | isecttype
  428   ;
  429 
  430 // Rules of the form antlr_* are only present to avoid a bug in the
  431 // interpreter
  432 
  433 /* Precedence
  434 
  435 Value:
  436 1. postfix
  437 2. unop
  438 3. binop
  439 4. =
  440 5. seq
  441 6. ,
  442 
  443 Type:
  444 1. ->
  445 2. & |
  446 3. ,
  447 */
  448 
  449 // Lexer
  450 
  451 ID
  452   : LETTER (LETTER | DIGIT | '_' | '\'')*
  453   | '_' (LETTER | DIGIT | '_' | '\'')*
  454   ;
  455 
  456 INT
  457   : DIGIT (DIGIT | '_')*
  458   | '0' 'x' (HEX | '_')+
  459   | '0' 'b' (BINARY | '_')+
  460   | '\'' CHAR_CHAR* '\''
  461   ;
  462 
  463 FLOAT
  464   : DIGIT (DIGIT | '_')* ('.' DIGIT (DIGIT | '_')*)? EXP?
  465   ;
  466 
  467 STRING
  468   : '"' STRING_CHAR* '"'
  469   | '"""' (('"' | '""') ? ~'"')* '"""' '"'*
  470   ;
  471 
  472 LPAREN_NEW
  473   : NEWLINE '('
  474   ;
  475 
  476 LSQUARE_NEW
  477   : NEWLINE '['
  478   ;
  479 
  480 MINUS_NEW
  481   : NEWLINE '-'
  482   ;
  483 
  484 MINUS_TILDE_NEW
  485   : NEWLINE '-~'
  486   ;
  487 
  488 LINECOMMENT
  489   : '//' ~('\n')* {$channel = HIDDEN;}
  490   ;
  491 
  492 NESTEDCOMMENT
  493   : '/*' (NESTEDCOMMENT | '/' ~'*' | ~('*' | '/') | ('*'+ ~('*' | '/')))* '*'+ '/' {$channel = HIDDEN;}
  494   ;
  495 
  496 WS
  497   : (' ' | '\t' | '\r')+ {$channel = HIDDEN;}
  498   ;
  499 
  500 NEWLINE
  501   : '\n' (' ' | '\t' | '\r')* {$channel = HIDDEN;}
  502   ;
  503 
  504 fragment
  505 CHAR_CHAR
  506   : '\\' '\'' | CHAR_ESC
  507   | ~('\'' | '\\')
  508   ;
  509 
  510 fragment
  511 STRING_CHAR
  512   : '\\' '"' | ESC
  513   | ~('"' | '\\')
  514   ;
  515 
  516 fragment
  517 EXP
  518   : ('e' | 'E') ('+' | '-')? (DIGIT | '_')+
  519   ;
  520 
  521 fragment
  522 LETTER
  523   : 'a'..'z' | 'A'..'Z'
  524   ;
  525 
  526 fragment
  527 BINARY
  528   : '0'..'1'
  529   ;
  530 
  531 fragment
  532 DIGIT
  533   : '0'..'9'
  534   ;
  535 
  536 fragment
  537 HEX
  538   : DIGIT | 'a'..'f' | 'A'..'F'
  539   ;
  540 
  541 fragment
  542 ESC
  543   : CHAR_ESC
  544   | UNICODE_ESC
  545   | UNICODE2_ESC
  546   ;
  547 
  548 fragment
  549 CHAR_ESC
  550   : '\\' ('a' | 'b' | 'e' | 'f' | 'n' | 'r' | 't' | 'v' | '\\' | '0')
  551   | HEX_ESC
  552   ;
  553 
  554 fragment
  555 HEX_ESC
  556   : '\\' 'x' HEX HEX
  557   ;
  558 
  559 fragment
  560 UNICODE_ESC
  561   : '\\' 'u' HEX HEX HEX HEX
  562   ;
  563 
  564 fragment
  565 UNICODE2_ESC
  566   : '\\' 'U' HEX HEX HEX HEX HEX HEX
  567   ;
  568