grammar.txt 9.3 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 (comma 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)?
  98. postExprBlocks = ':' stmt? ( IND{=} doBlock
  99. | IND{=} 'of' exprList ':' stmt
  100. | IND{=} 'elif' expr ':' stmt
  101. | IND{=} 'except' exprList ':' stmt
  102. | IND{=} 'finally' ':' stmt
  103. | IND{=} 'else' ':' stmt )*
  104. exprStmt = simpleExpr
  105. (( '=' optInd expr colonBody? )
  106. / ( expr ^+ comma
  107. postExprBlocks
  108. ))?
  109. importStmt = 'import' optInd expr
  110. ((comma expr)*
  111. / 'except' optInd (expr ^+ comma))
  112. exportStmt = 'export' optInd expr
  113. ((comma expr)*
  114. / 'except' optInd (expr ^+ comma))
  115. includeStmt = 'include' optInd expr ^+ comma
  116. fromStmt = 'from' expr 'import' optInd expr (comma expr)*
  117. returnStmt = 'return' optInd expr?
  118. raiseStmt = 'raise' optInd expr?
  119. yieldStmt = 'yield' optInd expr?
  120. discardStmt = 'discard' optInd expr?
  121. breakStmt = 'break' optInd expr?
  122. continueStmt = 'continue' optInd expr?
  123. condStmt = expr colcom stmt COMMENT?
  124. (IND{=} 'elif' expr colcom stmt)*
  125. (IND{=} 'else' colcom stmt)?
  126. ifStmt = 'if' condStmt
  127. whenStmt = 'when' condStmt
  128. condExpr = expr colcom expr optInd
  129. ('elif' expr colcom expr optInd)*
  130. 'else' colcom expr
  131. ifExpr = 'if' condExpr
  132. whenExpr = 'when' condExpr
  133. whileStmt = 'while' expr colcom stmt
  134. ofBranch = 'of' exprList colcom stmt
  135. ofBranches = ofBranch (IND{=} ofBranch)*
  136. (IND{=} 'elif' expr colcom stmt)*
  137. (IND{=} 'else' colcom stmt)?
  138. caseStmt = 'case' expr ':'? COMMENT?
  139. (IND{>} ofBranches DED
  140. | IND{=} ofBranches)
  141. tryStmt = 'try' colcom stmt &(IND{=}? 'except'|'finally')
  142. (IND{=}? 'except' exprList colcom stmt)*
  143. (IND{=}? 'finally' colcom stmt)?
  144. tryExpr = 'try' colcom stmt &(optInd 'except'|'finally')
  145. (optInd 'except' exprList colcom stmt)*
  146. (optInd 'finally' 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(RULE) = COMMENT? RULE / (IND{>} (RULE / COMMENT)^+IND{=} DED)
  161. enumDecl = 'enum' optInd (symbol pragma? optInd ('=' optInd expr COMMENT?)? comma?)+
  162. objectWhen = 'when' expr colcom objectPart COMMENT?
  163. ('elif' expr colcom objectPart COMMENT?)*
  164. ('else' colcom objectPart COMMENT?)?
  165. objectBranch = 'of' exprList colcom objectPart
  166. objectBranches = objectBranch (IND{=} objectBranch)*
  167. (IND{=} 'elif' expr colcom objectPart)*
  168. (IND{=} 'else' colcom objectPart)?
  169. objectCase = 'case' declColonEquals ':'? COMMENT?
  170. (IND{>} objectBranches DED
  171. | IND{=} objectBranches)
  172. objectPart = IND{>} objectPart^+IND{=} DED
  173. / objectWhen / objectCase / 'nil' / 'discard' / declColonEquals
  174. objectDecl = 'object' ('of' typeDesc)? COMMENT? objectPart
  175. conceptParam = ('var' | 'out')? symbol
  176. conceptDecl = 'concept' conceptParam ^* ',' (pragma)? ('of' typeDesc ^* ',')?
  177. &IND{>} stmt
  178. typeDef = 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 ^+ ';'