Grammar

The grammar’s start symbol is module.

  1. # This file is generated by compiler/parser.nim.
  2. module = stmt ^* (';' / IND{=})
  3. comma = ',' COMMENT?
  4. semicolon = ';' COMMENT?
  5. colon = ':' COMMENT?
  6. colcom = ':' COMMENT?
  7. operator = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9
  8. | 'or' | 'xor' | 'and'
  9. | 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' | 'from'
  10. | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..'
  11. prefixOperator = operator
  12. optInd = COMMENT? IND?
  13. optPar = (IND{>} | IND{=})?
  14. simpleExpr = arrowExpr (OP0 optInd arrowExpr)* pragma?
  15. arrowExpr = assignExpr (OP1 optInd assignExpr)*
  16. assignExpr = orExpr (OP2 optInd orExpr)*
  17. orExpr = andExpr (OP3 optInd andExpr)*
  18. andExpr = cmpExpr (OP4 optInd cmpExpr)*
  19. cmpExpr = sliceExpr (OP5 optInd sliceExpr)*
  20. sliceExpr = ampExpr (OP6 optInd ampExpr)*
  21. ampExpr = plusExpr (OP7 optInd plusExpr)*
  22. plusExpr = mulExpr (OP8 optInd mulExpr)*
  23. mulExpr = dollarExpr (OP9 optInd dollarExpr)*
  24. dollarExpr = primary (OP10 optInd primary)*
  25. symbol = '`' (KEYW|IDENT|literal|(operator|'('|')'|'['|']'|'{'|'}'|'=')+)+ '`'
  26. | IDENT | KEYW
  27. exprColonEqExpr = expr (':'|'=' expr)?
  28. exprList = expr ^+ comma
  29. exprColonEqExprList = exprColonEqExpr (comma exprColonEqExpr)* (comma)?
  30. dotExpr = expr '.' optInd (symbol | '[:' exprList ']')
  31. explicitGenericInstantiation = '[:' exprList ']' ( '(' exprColonEqExpr ')' )?
  32. qualifiedIdent = symbol ('.' optInd symbol)?
  33. setOrTableConstr = '{' ((exprColonEqExpr comma)* | ':' ) '}'
  34. castExpr = 'cast' ('[' optInd typeDesc optPar ']' '(' optInd expr optPar ')') /
  35. parKeyw = 'discard' | 'include' | 'if' | 'while' | 'case' | 'try'
  36. | 'finally' | 'except' | 'for' | 'block' | 'const' | 'let'
  37. | 'when' | 'var' | 'mixin'
  38. par = '(' optInd
  39. ( &parKeyw (ifExpr \ complexOrSimpleStmt) ^+ ';'
  40. | ';' (ifExpr \ complexOrSimpleStmt) ^+ ';'
  41. | pragmaStmt
  42. | simpleExpr ( ('=' expr (';' (ifExpr \ complexOrSimpleStmt) ^+ ';' )? )
  43. | (':' expr (',' exprColonEqExpr ^+ ',' )? ) ) )
  44. optPar ')'
  45. literal = | INT_LIT | INT8_LIT | INT16_LIT | INT32_LIT | INT64_LIT
  46. | UINT_LIT | UINT8_LIT | UINT16_LIT | UINT32_LIT | UINT64_LIT
  47. | FLOAT_LIT | FLOAT32_LIT | FLOAT64_LIT
  48. | STR_LIT | RSTR_LIT | TRIPLESTR_LIT
  49. | CHAR_LIT
  50. | NIL
  51. generalizedLit = GENERALIZED_STR_LIT | GENERALIZED_TRIPLESTR_LIT
  52. identOrLiteral = generalizedLit | symbol | literal
  53. | par | arrayConstr | setOrTableConstr
  54. | castExpr
  55. tupleConstr = '(' optInd (exprColonEqExpr comma?)* optPar ')'
  56. arrayConstr = '[' optInd (exprColonEqExpr comma?)* optPar ']'
  57. primarySuffix = '(' (exprColonEqExpr comma?)* ')'
  58. | '.' optInd symbol generalizedLit?
  59. | '[' optInd exprColonEqExprList optPar ']'
  60. | '{' optInd exprColonEqExprList optPar '}'
  61. | &( '`'|IDENT|literal|'cast'|'addr'|'type') expr # command syntax
  62. pragma = '{.' optInd (exprColonEqExpr comma?)* optPar ('.}' | '}')
  63. identVis = symbol OPR? # postfix position
  64. identVisDot = symbol '.' optInd symbol OPR?
  65. identWithPragma = identVis pragma?
  66. identWithPragmaDot = identVisDot pragma?
  67. declColonEquals = identWithPragma (comma identWithPragma)* comma?
  68. (':' optInd typeDesc)? ('=' optInd expr)?
  69. identColonEquals = IDENT (comma IDENT)* comma?
  70. (':' optInd typeDesc)? ('=' optInd expr)?)
  71. inlTupleDecl = 'tuple'
  72. '[' optInd (identColonEquals (comma/semicolon)?)* optPar ']'
  73. extTupleDecl = 'tuple'
  74. COMMENT? (IND{>} identColonEquals (IND{=} identColonEquals)*)?
  75. tupleClass = 'tuple'
  76. paramList = '(' declColonEquals ^* (comma/semicolon) ')'
  77. paramListArrow = paramList? ('->' optInd typeDesc)?
  78. paramListColon = paramList? (':' optInd typeDesc)?
  79. doBlock = 'do' paramListArrow pragma? colcom stmt
  80. procExpr = 'proc' paramListColon pragma? ('=' COMMENT? stmt)?
  81. distinct = 'distinct' optInd typeDesc
  82. forStmt = 'for' (identWithPragma ^+ comma) 'in' expr colcom stmt
  83. forExpr = forStmt
  84. expr = (blockExpr
  85. | ifExpr
  86. | whenExpr
  87. | caseStmt
  88. | forExpr
  89. | tryExpr)
  90. / simpleExpr
  91. typeKeyw = 'var' | 'out' | 'ref' | 'ptr' | 'shared' | 'tuple'
  92. | 'proc' | 'iterator' | 'distinct' | 'object' | 'enum'
  93. primary = typeKeyw optInd typeDesc
  94. / prefixOperator* identOrLiteral primarySuffix*
  95. / 'bind' primary
  96. typeDesc = simpleExpr ('not' expr)?
  97. typeDefAux = simpleExpr ('not' expr)?
  98. | 'concept' typeClass
  99. postExprBlocks = ':' stmt? ( IND{=} doBlock
  100. | IND{=} 'of' exprList ':' stmt
  101. | IND{=} 'elif' expr ':' stmt
  102. | IND{=} 'except' exprList ':' stmt
  103. | IND{=} 'else' ':' stmt )*
  104. exprStmt = simpleExpr
  105. (( '=' optInd expr colonBody? )
  106. / ( expr ^+ comma
  107. postExprBlocks
  108. ))?
  109. importStmt = 'import' optInd expr
  110. ((comma expr)*
  111. / 'except' optInd (expr ^+ comma))
  112. exportStmt = 'export' optInd expr
  113. ((comma expr)*
  114. / 'except' optInd (expr ^+ comma))
  115. includeStmt = 'include' optInd expr ^+ comma
  116. fromStmt = 'from' expr 'import' optInd expr (comma expr)*
  117. returnStmt = 'return' optInd expr?
  118. raiseStmt = 'raise' optInd expr?
  119. yieldStmt = 'yield' optInd expr?
  120. discardStmt = 'discard' optInd expr?
  121. breakStmt = 'break' optInd expr?
  122. continueStmt = 'break' optInd expr?
  123. condStmt = expr colcom stmt COMMENT?
  124. (IND{=} 'elif' expr colcom stmt)*
  125. (IND{=} 'else' colcom stmt)?
  126. ifStmt = 'if' condStmt
  127. whenStmt = 'when' condStmt
  128. condExpr = expr colcom expr optInd
  129. ('elif' expr colcom expr optInd)*
  130. 'else' colcom expr
  131. ifExpr = 'if' condExpr
  132. whenExpr = 'when' condExpr
  133. whileStmt = 'while' expr colcom stmt
  134. ofBranch = 'of' exprList colcom stmt
  135. ofBranches = ofBranch (IND{=} ofBranch)*
  136. (IND{=} 'elif' expr colcom stmt)*
  137. (IND{=} 'else' colcom stmt)?
  138. caseStmt = 'case' expr ':'? COMMENT?
  139. (IND{>} ofBranches DED
  140. | IND{=} ofBranches)
  141. tryStmt = 'try' colcom stmt &(IND{=}? 'except'|'finally')
  142. (IND{=}? 'except' exprList colcom stmt)*
  143. (IND{=}? 'finally' colcom stmt)?
  144. tryExpr = 'try' colcom stmt &(optInd 'except'|'finally')
  145. (optInd 'except' exprList colcom stmt)*
  146. (optInd 'finally' colcom stmt)?
  147. exceptBlock = 'except' colcom stmt
  148. blockStmt = 'block' symbol? colcom stmt
  149. blockExpr = 'block' symbol? colcom stmt
  150. staticStmt = 'static' colcom stmt
  151. deferStmt = 'defer' colcom stmt
  152. asmStmt = 'asm' pragma? (STR_LIT | RSTR_LIT | TRIPLESTR_LIT)
  153. genericParam = symbol (comma symbol)* (colon expr)? ('=' optInd expr)?
  154. genericParamList = '[' optInd
  155. genericParam ^* (comma/semicolon) optPar ']'
  156. pattern = '{' stmt '}'
  157. indAndComment = (IND{>} COMMENT)? | COMMENT?
  158. routine = optInd identVis pattern? genericParamList?
  159. paramListColon pragma? ('=' COMMENT? stmt)? indAndComment
  160. commentStmt = COMMENT
  161. section(RULE) = COMMENT? RULE / (IND{>} (RULE / COMMENT)^+IND{=} DED)
  162. enum = 'enum' optInd (symbol pragma? optInd ('=' optInd expr COMMENT?)? comma?)+
  163. objectWhen = 'when' expr colcom objectPart COMMENT?
  164. ('elif' expr colcom objectPart COMMENT?)*
  165. ('else' colcom objectPart COMMENT?)?
  166. objectBranch = 'of' exprList colcom objectPart
  167. objectBranches = objectBranch (IND{=} objectBranch)*
  168. (IND{=} 'elif' expr colcom objectPart)*
  169. (IND{=} 'else' colcom objectPart)?
  170. objectCase = 'case' identWithPragma ':' typeDesc ':'? COMMENT?
  171. (IND{>} objectBranches DED
  172. | IND{=} objectBranches)
  173. objectPart = IND{>} objectPart^+IND{=} DED
  174. / objectWhen / objectCase / 'nil' / 'discard' / declColonEquals
  175. object = 'object' pragma? ('of' typeDesc)? COMMENT? objectPart
  176. typeClassParam = ('var' | 'out')? symbol
  177. typeClass = typeClassParam ^* ',' (pragma)? ('of' typeDesc ^* ',')?
  178. &IND{>} stmt
  179. typeDef = identWithPragmaDot genericParamList? '=' optInd typeDefAux
  180. indAndComment? / identVisDot genericParamList? pragma '=' optInd typeDefAux
  181. indAndComment?
  182. varTuple = '(' optInd identWithPragma ^+ comma optPar ')' '=' optInd expr
  183. colonBody = colcom stmt postExprBlocks?
  184. variable = (varTuple / identColonEquals) colonBody? indAndComment
  185. constant = (varTuple / identWithPragma) (colon typeDesc)? '=' optInd expr indAndComment
  186. bindStmt = 'bind' optInd qualifiedIdent ^+ comma
  187. mixinStmt = 'mixin' optInd qualifiedIdent ^+ comma
  188. pragmaStmt = pragma (':' COMMENT? stmt)?
  189. simpleStmt = ((returnStmt | raiseStmt | yieldStmt | discardStmt | breakStmt
  190. | continueStmt | pragmaStmt | importStmt | exportStmt | fromStmt
  191. | includeStmt | commentStmt) / exprStmt) COMMENT?
  192. complexOrSimpleStmt = (ifStmt | whenStmt | whileStmt
  193. | tryStmt | forStmt
  194. | blockStmt | staticStmt | deferStmt | asmStmt
  195. | 'proc' routine
  196. | 'method' routine
  197. | 'func' routine
  198. | 'iterator' routine
  199. | 'macro' routine
  200. | 'template' routine
  201. | 'converter' routine
  202. | 'type' section(typeDef)
  203. | 'const' section(constant)
  204. | ('let' | 'var' | 'using') section(variable)
  205. | bindStmt | mixinStmt)
  206. / simpleStmt
  207. stmt = (IND{>} complexOrSimpleStmt^+(IND{=} / ';') DED)
  208. / simpleStmt ^+ ';'