grammar.txt 9.4 KB

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