语法

语法的起始符号是 module 。

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