grammar.txt 10 KB

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