grammar.txt 10.0 KB

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