grammar.txt 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. module = stmt ^* (';' / IND{=})
  2. comma = ',' COMMENT?
  3. semicolon = ';' COMMENT?
  4. colon = ':' COMMENT?
  5. colcom = ':' COMMENT?
  6. operator = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9
  7. | 'or' | 'xor' | 'and'
  8. | 'is' | 'isnot' | 'in' | 'notin' | 'of'
  9. | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..'
  10. prefixOperator = operator
  11. optInd = COMMENT?
  12. optPar = (IND{>} | IND{=})?
  13. simpleExpr = arrowExpr (OP0 optInd arrowExpr)* pragma?
  14. arrowExpr = assignExpr (OP1 optInd assignExpr)*
  15. assignExpr = orExpr (OP2 optInd orExpr)*
  16. orExpr = andExpr (OP3 optInd andExpr)*
  17. andExpr = cmpExpr (OP4 optInd cmpExpr)*
  18. cmpExpr = sliceExpr (OP5 optInd sliceExpr)*
  19. sliceExpr = ampExpr (OP6 optInd ampExpr)*
  20. ampExpr = plusExpr (OP7 optInd plusExpr)*
  21. plusExpr = mulExpr (OP8 optInd mulExpr)*
  22. mulExpr = dollarExpr (OP9 optInd dollarExpr)*
  23. dollarExpr = primary (OP10 optInd primary)*
  24. symbol = '`' (KEYW|IDENT|literal|(operator|'('|')'|'['|']'|'{'|'}'|'=')+)+ '`'
  25. | IDENT | KEYW
  26. exprColonEqExpr = expr (':'|'=' expr)?
  27. exprList = expr ^+ comma
  28. dotExpr = expr '.' optInd symbol
  29. qualifiedIdent = symbol ('.' optInd symbol)?
  30. exprColonEqExprList = exprColonEqExpr (comma exprColonEqExpr)* (comma)?
  31. setOrTableConstr = '{' ((exprColonEqExpr comma)* | ':' ) '}'
  32. castExpr = 'cast' '[' optInd typeDesc optPar ']' '(' optInd expr optPar ')'
  33. parKeyw = 'discard' | 'include' | 'if' | 'while' | 'case' | 'try'
  34. | 'finally' | 'except' | 'for' | 'block' | 'const' | 'let'
  35. | 'when' | 'var' | 'mixin'
  36. par = '(' optInd
  37. ( &parKeyw complexOrSimpleStmt ^+ ';'
  38. | ';' complexOrSimpleStmt ^+ ';'
  39. | pragmaStmt
  40. | simpleExpr ( ('=' expr (';' complexOrSimpleStmt ^+ ';' )? )
  41. | (':' expr (',' exprColonEqExpr ^+ ',' )? ) ) )
  42. optPar ')'
  43. literal = | INT_LIT | INT8_LIT | INT16_LIT | INT32_LIT | INT64_LIT
  44. | UINT_LIT | UINT8_LIT | UINT16_LIT | UINT32_LIT | UINT64_LIT
  45. | FLOAT_LIT | FLOAT32_LIT | FLOAT64_LIT
  46. | STR_LIT | RSTR_LIT | TRIPLESTR_LIT
  47. | CHAR_LIT
  48. | NIL
  49. generalizedLit = GENERALIZED_STR_LIT | GENERALIZED_TRIPLESTR_LIT
  50. identOrLiteral = generalizedLit | symbol | literal
  51. | par | arrayConstr | setOrTableConstr
  52. | castExpr
  53. tupleConstr = '(' optInd (exprColonEqExpr comma?)* optPar ')'
  54. arrayConstr = '[' optInd (exprColonEqExpr comma?)* optPar ']'
  55. primarySuffix = '(' (exprColonEqExpr comma?)* ')' doBlocks?
  56. | doBlocks
  57. | '.' optInd symbol generalizedLit?
  58. | '[' optInd indexExprList optPar ']'
  59. | '{' optInd indexExprList optPar '}'
  60. | &( '`'|IDENT|literal|'cast'|'addr'|'type') expr # command syntax
  61. condExpr = expr colcom expr optInd
  62. ('elif' expr colcom expr optInd)*
  63. 'else' colcom expr
  64. ifExpr = 'if' condExpr
  65. whenExpr = 'when' condExpr
  66. pragma = '{.' optInd (exprColonExpr comma?)* optPar ('.}' | '}')
  67. identVis = symbol opr? # postfix position
  68. identVisDot = symbol '.' optInd symbol opr?
  69. identWithPragma = identVis pragma?
  70. identWithPragmaDot = identVisDot pragma?
  71. declColonEquals = identWithPragma (comma identWithPragma)* comma?
  72. (':' optInd typeDesc)? ('=' optInd expr)?
  73. identColonEquals = ident (comma ident)* comma?
  74. (':' optInd typeDesc)? ('=' optInd expr)?)
  75. inlTupleDecl = 'tuple'
  76. [' optInd (identColonEquals (comma/semicolon)?)* optPar ']'
  77. extTupleDecl = 'tuple'
  78. COMMENT? (IND{>} identColonEquals (IND{=} identColonEquals)*)?
  79. tupleClass = 'tuple'
  80. paramList = '(' declColonEquals ^* (comma/semicolon) ')'
  81. paramListArrow = paramList? ('->' optInd typeDesc)?
  82. paramListColon = paramList? (':' optInd typeDesc)?
  83. doBlock = 'do' paramListArrow pragmas? colcom stmt
  84. procExpr = 'proc' paramListColon pragmas? ('=' COMMENT? stmt)?
  85. distinct = 'distinct' optInd typeDesc
  86. expr = (ifExpr
  87. | whenExpr
  88. | caseExpr
  89. | tryExpr)
  90. / simpleExpr
  91. typeKeyw = 'var' | 'out' | 'ref' | 'ptr' | 'shared' | 'tuple'
  92. | 'proc' | 'iterator' | 'distinct' | 'object' | 'enum'
  93. primary = typeKeyw typeDescK
  94. / prefixOperator* identOrLiteral primarySuffix*
  95. / 'static' primary
  96. / 'bind' primary
  97. typeDesc = simpleExpr
  98. typeDefAux = simpleExpr
  99. | 'concept' typeClass
  100. postExprBlocks = ':' stmt? ( IND{=} doBlock
  101. | IND{=} 'of' exprList ':' stmt
  102. | IND{=} 'elif' expr ':' stmt
  103. | IND{=} 'except' exprList ':' stmt
  104. | IND{=} 'else' ':' stmt )*
  105. exprStmt = simpleExpr
  106. (( '=' optInd expr colonBody? )
  107. / ( expr ^+ comma
  108. doBlocks
  109. / macroColon
  110. ))?
  111. importStmt = 'import' optInd expr
  112. ((comma expr)*
  113. / 'except' optInd (expr ^+ comma))
  114. includeStmt = 'include' optInd expr ^+ comma
  115. fromStmt = 'from' moduleName '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 = 'break' 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. whileStmt = 'while' expr colcom stmt
  128. ofBranch = 'of' exprList colcom stmt
  129. ofBranches = ofBranch (IND{=} ofBranch)*
  130. (IND{=} 'elif' expr colcom stmt)*
  131. (IND{=} 'else' colcom stmt)?
  132. caseStmt = 'case' expr ':'? COMMENT?
  133. (IND{>} ofBranches DED
  134. | IND{=} ofBranches)
  135. tryStmt = 'try' colcom stmt &(IND{=}? 'except'|'finally')
  136. (IND{=}? 'except' exprList colcom stmt)*
  137. (IND{=}? 'finally' colcom stmt)?
  138. tryExpr = 'try' colcom stmt &(optInd 'except'|'finally')
  139. (optInd 'except' exprList colcom stmt)*
  140. (optInd 'finally' colcom stmt)?
  141. exceptBlock = 'except' colcom stmt
  142. forStmt = 'for' (identWithPragma ^+ comma) 'in' expr colcom stmt
  143. blockStmt = 'block' symbol? colcom stmt
  144. staticStmt = 'static' colcom stmt
  145. deferStmt = 'defer' colcom stmt
  146. asmStmt = 'asm' pragma? (STR_LIT | RSTR_LIT | TRIPLE_STR_LIT)
  147. genericParam = symbol (comma symbol)* (colon expr)? ('=' optInd expr)?
  148. genericParamList = '[' optInd
  149. genericParam ^* (comma/semicolon) optPar ']'
  150. pattern = '{' stmt '}'
  151. indAndComment = (IND{>} COMMENT)? | COMMENT?
  152. routine = optInd identVis pattern? genericParamList?
  153. paramListColon pragma? ('=' COMMENT? stmt)? indAndComment
  154. commentStmt = COMMENT
  155. section(p) = COMMENT? p / (IND{>} (p / COMMENT)^+IND{=} DED)
  156. constant = identWithPragma (colon typedesc)? '=' optInd expr indAndComment
  157. enum = 'enum' optInd (symbol optInd ('=' optInd expr COMMENT?)? comma?)+
  158. objectWhen = 'when' expr colcom objectPart COMMENT?
  159. ('elif' expr colcom objectPart COMMENT?)*
  160. ('else' colcom objectPart COMMENT?)?
  161. objectBranch = 'of' exprList colcom objectPart
  162. objectBranches = objectBranch (IND{=} objectBranch)*
  163. (IND{=} 'elif' expr colcom objectPart)*
  164. (IND{=} 'else' colcom objectPart)?
  165. objectCase = 'case' identWithPragma ':' typeDesc ':'? COMMENT?
  166. (IND{>} objectBranches DED
  167. | IND{=} objectBranches)
  168. objectPart = IND{>} objectPart^+IND{=} DED
  169. / objectWhen / objectCase / 'nil' / 'discard' / declColonEquals
  170. object = 'object' pragma? ('of' typeDesc)? COMMENT? objectPart
  171. typeClassParam = ('var' | 'out')? symbol
  172. typeClass = typeClassParam ^* ',' (pragma)? ('of' typeDesc ^* ',')?
  173. &IND{>} stmt
  174. typeDef = identWithPragmaDot genericParamList? '=' optInd typeDefAux
  175. indAndComment?
  176. varTuple = '(' optInd identWithPragma ^+ comma optPar ')' '=' optInd expr
  177. colonBody = colcom stmt doBlocks?
  178. variable = (varTuple / identColonEquals) colonBody? indAndComment
  179. bindStmt = 'bind' optInd qualifiedIdent ^+ comma
  180. mixinStmt = 'mixin' optInd qualifiedIdent ^+ comma
  181. pragmaStmt = pragma (':' COMMENT? stmt)?
  182. simpleStmt = ((returnStmt | raiseStmt | yieldStmt | discardStmt | breakStmt
  183. | continueStmt | pragmaStmt | importStmt | exportStmt | fromStmt
  184. | includeStmt | commentStmt) / exprStmt) COMMENT?
  185. complexOrSimpleStmt = (ifStmt | whenStmt | whileStmt
  186. | tryStmt | forStmt
  187. | blockStmt | staticStmt | deferStmt | asmStmt
  188. | 'proc' routine
  189. | 'method' routine
  190. | 'iterator' routine
  191. | 'macro' routine
  192. | 'template' routine
  193. | 'converter' routine
  194. | 'type' section(typeDef)
  195. | 'const' section(constant)
  196. | ('let' | 'var' | 'using') section(variable)
  197. | bindStmt | mixinStmt)
  198. / simpleStmt
  199. stmt = (IND{>} complexOrSimpleStmt^+(IND{=} / ';') DED)
  200. / simpleStmt ^+ ';'