Advertisement
Fel1x

NuML grammar

Jan 13th, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. grammar NuML;
  2.  
  3. compile_unit: module* EOF;
  4.  
  5. module: 'module' ID stat* 'end';
  6.  
  7. stat: decl | withcall;
  8.  
  9. withcall: 'with' exp call_arg+;
  10.  
  11. decl: 'val' bind #var_decl
  12. | 'val' name=ID funct_args ':' type '==>' block #funct_decl
  13. ;
  14.  
  15. funct_args: arg+;
  16. arg: ID ':' type;
  17.  
  18. block: stat* exp;
  19.  
  20. bind: ID ':' type '=' exp;
  21.  
  22.  
  23. Asterisk: '*';
  24.  
  25. OP_INFIX: Asterisk | '/' | '+' | '-' | '<>' | ('<' | '>' | '=') '='?;
  26.  
  27. exp: atom #atom_exp
  28. | variable_name #variable_exp
  29. | tuple #tuple_exp
  30. | left=exp OP_INFIX right=exp #infix_op
  31. | exp 'where' bind (',' bind)* 'end' #where
  32. | lambda #lambda_exp
  33. | exp call_arg+ #call
  34. | 'if' pred=exp 'then' then=exp 'else' else=exp #branch
  35. | '[' explist? ']' #array
  36. | exp '.' ID #field_access
  37. ;
  38.  
  39. type: variable_name #flat_type
  40. | type Asterisk type #tuple_type
  41. | '[' type ']' #array_type
  42. | <right_assoc> type '->' type #funct_type
  43. | '(' type ')' #paren_type
  44. // | type '[' type (',' type)* ']' #generic_type
  45. ;
  46.  
  47. variable_name: (namespace=(ID | NATIVE_VAR_PREFIX) '::')? name=ID;
  48.  
  49. lambda: 'funct' funct_args '==>' block;
  50.  
  51. call_arg: (ID 'as')? exp;
  52.  
  53. tuple: '(' explist? ')';
  54.  
  55. explist: exp (',' exp)*;
  56.  
  57. atom: number | string | bool | nil;
  58.  
  59. number: INT_MOD | FLOAT;
  60. string: STRING;
  61. bool: TRUE | FALSE;
  62. nil: NIL;
  63.  
  64. NATIVE_VAR_PREFIX: '$$';
  65.  
  66. TRUE: 'true';
  67. FALSE: 'false';
  68. NIL: 'nil';
  69.  
  70. ID: [a-zA-Z_]+ [a-zA-Z0-9_]*;
  71. WS: [ \t\n\r]+ -> channel(HIDDEN);
  72. INT_MOD: SIGN? INT_POS;
  73.  
  74. FLOAT: INT_MOD [.] INT_POS EXP? // 1.35, 1.35E-9, 0.3, -4.5
  75. | INT_MOD EXP // 1e10 -3e4
  76. | INT_MOD // -3, 45
  77. ;
  78.  
  79. STRING: QUOTE (~["\\] | QUOTE_ESCAPED)* QUOTE;
  80.  
  81. fragment INT_POS: [0] | DIGIT_POS [0-9]*; // no leading zeros
  82. fragment EXP: [Ee] SIGN? INT_POS;
  83. fragment SIGN: [+~];
  84. fragment DIGIT_POS: [1-9];
  85. fragment DIGIT: [0] | DIGIT_POS;
  86. fragment QUOTE: ["];
  87. fragment QUOTE_ESCAPED: [\\] QUOTE;
  88.  
  89. LINE_COMMENT: '//|' .*? [\n] -> channel(HIDDEN);
  90. COMMENT: '/|' .*? '|\\' -> channel(HIDDEN);
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement