语法

语法的起始符号是 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'
  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 complexOrSimpleStmt ^+ ';'
  40. | ';' complexOrSimpleStmt ^+ ';'
  41. | pragmaStmt
  42. | simpleExpr ( ('=' expr (';' 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?)* ')' doBlocks?
  58. | doBlocks
  59. | '.' optInd symbol generalizedLit?
  60. | '[' optInd indexExprList optPar ']'
  61. | '{' optInd indexExprList optPar '}'
  62. | &( '`'|IDENT|literal|'cast'|'addr'|'type') expr # command syntax
  63. condExpr = expr colcom expr optInd
  64. ('elif' expr colcom expr optInd)*
  65. 'else' colcom expr
  66. ifExpr = 'if' condExpr
  67. whenExpr = 'when' condExpr
  68. pragma = '{.' optInd (exprColonExpr comma?)* optPar ('.}' | '}')
  69. identVis = symbol opr? # postfix position
  70. identVisDot = symbol '.' optInd symbol opr?
  71. identWithPragma = identVis pragma?
  72. identWithPragmaDot = identVisDot pragma?
  73. declColonEquals = identWithPragma (comma identWithPragma)* comma?
  74. (':' optInd typeDesc)? ('=' optInd expr)?
  75. identColonEquals = ident (comma ident)* comma?
  76. (':' optInd typeDesc)? ('=' optInd expr)?)
  77. inlTupleDecl = 'tuple'
  78. [' optInd (identColonEquals (comma/semicolon)?)* optPar ']'
  79. extTupleDecl = 'tuple'
  80. COMMENT? (IND{>} identColonEquals (IND{=} identColonEquals)*)?
  81. tupleClass = 'tuple'
  82. paramList = '(' declColonEquals ^* (comma/semicolon) ')'
  83. paramListArrow = paramList? ('->' optInd typeDesc)?
  84. paramListColon = paramList? (':' optInd typeDesc)?
  85. doBlock = 'do' paramListArrow pragmas? colcom stmt
  86. procExpr = 'proc' paramListColon pragmas? ('=' COMMENT? stmt)?
  87. distinct = 'distinct' optInd typeDesc
  88. forStmt = 'for' (identWithPragma ^+ comma) 'in' expr colcom stmt
  89. forExpr = forStmt
  90. expr = (blockExpr
  91. | ifExpr
  92. | whenExpr
  93. | caseExpr
  94. | forExpr
  95. | tryExpr)
  96. / simpleExpr
  97. typeKeyw = 'var' | 'out' | 'ref' | 'ptr' | 'shared' | 'tuple'
  98. | 'proc' | 'iterator' | 'distinct' | 'object' | 'enum'
  99. primary = typeKeyw typeDescK
  100. / prefixOperator* identOrLiteral primarySuffix*
  101. / 'bind' primary
  102. typeDesc = simpleExpr
  103. typeDefAux = simpleExpr
  104. | 'concept' typeClass
  105. postExprBlocks = ':' stmt? ( IND{=} doBlock
  106. | IND{=} 'of' exprList ':' stmt
  107. | IND{=} 'elif' expr ':' stmt
  108. | IND{=} 'except' exprList ':' stmt
  109. | IND{=} 'else' ':' stmt )*
  110. exprStmt = simpleExpr
  111. (( '=' optInd expr colonBody? )
  112. / ( expr ^+ comma
  113. doBlocks
  114. / macroColon
  115. ))?
  116. importStmt = 'import' optInd expr
  117. ((comma expr)*
  118. / 'except' optInd (expr ^+ comma))
  119. includeStmt = 'include' optInd expr ^+ comma
  120. fromStmt = 'from' moduleName 'import' optInd expr (comma expr)*
  121. returnStmt = 'return' optInd expr?
  122. raiseStmt = 'raise' optInd expr?
  123. yieldStmt = 'yield' optInd expr?
  124. discardStmt = 'discard' optInd expr?
  125. breakStmt = 'break' optInd expr?
  126. continueStmt = 'break' optInd expr?
  127. condStmt = expr colcom stmt COMMENT?
  128. (IND{=} 'elif' expr colcom stmt)*
  129. (IND{=} 'else' colcom stmt)?
  130. ifStmt = 'if' condStmt
  131. whenStmt = 'when' condStmt
  132. whileStmt = 'while' expr colcom stmt
  133. ofBranch = 'of' exprList colcom stmt
  134. ofBranches = ofBranch (IND{=} ofBranch)*
  135. (IND{=} 'elif' expr colcom stmt)*
  136. (IND{=} 'else' colcom stmt)?
  137. caseStmt = 'case' expr ':'? COMMENT?
  138. (IND{>} ofBranches DED
  139. | IND{=} ofBranches)
  140. tryStmt = 'try' colcom stmt &(IND{=}? 'except'|'finally')
  141. (IND{=}? 'except' exprList colcom stmt)*
  142. (IND{=}? 'finally' colcom stmt)?
  143. tryExpr = 'try' colcom stmt &(optInd 'except'|'finally')
  144. (optInd 'except' exprList colcom stmt)*
  145. (optInd 'finally' colcom stmt)?
  146. exceptBlock = 'except' colcom stmt
  147. blockStmt = 'block' symbol? colcom stmt
  148. blockExpr = 'block' symbol? colcom stmt
  149. staticStmt = 'static' colcom stmt
  150. deferStmt = 'defer' colcom stmt
  151. asmStmt = 'asm' pragma? (STR_LIT | RSTR_LIT | TRIPLESTR_LIT)
  152. genericParam = symbol (comma symbol)* (colon expr)? ('=' optInd expr)?
  153. genericParamList = '[' optInd
  154. genericParam ^* (comma/semicolon) optPar ']'
  155. pattern = '{' stmt '}'
  156. indAndComment = (IND{>} COMMENT)? | COMMENT?
  157. routine = optInd identVis pattern? genericParamList?
  158. paramListColon pragma? ('=' COMMENT? stmt)? indAndComment
  159. commentStmt = COMMENT
  160. section(p) = COMMENT? p / (IND{>} (p / COMMENT)^+IND{=} DED)
  161. constant = identWithPragma (colon typeDesc)? '=' optInd expr indAndComment
  162. enum = 'enum' optInd (symbol 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?
  181. varTuple = '(' optInd identWithPragma ^+ comma optPar ')' '=' optInd expr
  182. colonBody = colcom stmt doBlocks?
  183. variable = (varTuple / identColonEquals) colonBody? indAndComment
  184. bindStmt = 'bind' optInd qualifiedIdent ^+ comma
  185. mixinStmt = 'mixin' optInd qualifiedIdent ^+ comma
  186. pragmaStmt = pragma (':' COMMENT? stmt)?
  187. simpleStmt = ((returnStmt | raiseStmt | yieldStmt | discardStmt | breakStmt
  188. | continueStmt | pragmaStmt | importStmt | exportStmt | fromStmt
  189. | includeStmt | commentStmt) / exprStmt) COMMENT?
  190. complexOrSimpleStmt = (ifStmt | whenStmt | whileStmt
  191. | tryStmt | forStmt
  192. | blockStmt | staticStmt | deferStmt | asmStmt
  193. | 'proc' routine
  194. | 'method' routine
  195. | 'iterator' routine
  196. | 'macro' routine
  197. | 'template' routine
  198. | 'converter' routine
  199. | 'type' section(typeDef)
  200. | 'const' section(constant)
  201. | ('let' | 'var' | 'using') section(variable)
  202. | bindStmt | mixinStmt)
  203. / simpleStmt
  204. stmt = (IND{>} complexOrSimpleStmt^+(IND{=} / ';') DED)
  205. / simpleStmt ^+ ';'