parser.nim 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module implements the parser of the standard Nim syntax.
  10. # The parser strictly reflects the grammar ("doc/grammar.txt"); however
  11. # it uses several helper routines to keep the parser small. A special
  12. # efficient algorithm is used for the precedence levels. The parser here can
  13. # be seen as a refinement of the grammar, as it specifies how the AST is built
  14. # from the grammar and how comments belong to the AST.
  15. # In fact the grammar is generated from this file:
  16. when isMainModule:
  17. # Leave a note in grammar.txt that it is generated:
  18. #| # This file is generated by compiler/parser.nim.
  19. import pegs
  20. var outp = open("doc/grammar.txt", fmWrite)
  21. for line in lines("compiler/parser.nim"):
  22. if line =~ peg" \s* '#| ' {.*}":
  23. outp.write matches[0], "\L"
  24. outp.close
  25. import ".." / tools / grammar_nanny
  26. checkGrammarFile()
  27. import
  28. llstream, lexer, idents, strutils, ast, msgs, options, lineinfos,
  29. pathutils
  30. when defined(nimpretty):
  31. import layouter
  32. type
  33. Parser* = object # A Parser object represents a file that
  34. # is being parsed
  35. currInd: int # current indentation level
  36. firstTok: bool # Has the first token been read?
  37. hasProgress: bool # some while loop requires progress ensurance
  38. lex*: Lexer # The lexer that is used for parsing
  39. tok*: Token # The current token
  40. inPragma*: int # Pragma level
  41. inSemiStmtList*: int
  42. emptyNode: PNode
  43. when defined(nimpretty):
  44. em*: Emitter
  45. SymbolMode = enum
  46. smNormal, smAllowNil, smAfterDot
  47. PrimaryMode = enum
  48. pmNormal, pmTypeDesc, pmTypeDef, pmSkipSuffix
  49. proc parseAll*(p: var Parser): PNode
  50. proc closeParser*(p: var Parser)
  51. proc parseTopLevelStmt*(p: var Parser): PNode
  52. # helpers for the other parsers
  53. proc isOperator*(tok: Token): bool
  54. proc getTok*(p: var Parser)
  55. proc parMessage*(p: Parser, msg: TMsgKind, arg: string = "")
  56. proc skipComment*(p: var Parser, node: PNode)
  57. proc newNodeP*(kind: TNodeKind, p: Parser): PNode
  58. proc newIntNodeP*(kind: TNodeKind, intVal: BiggestInt, p: Parser): PNode
  59. proc newFloatNodeP*(kind: TNodeKind, floatVal: BiggestFloat, p: Parser): PNode
  60. proc newStrNodeP*(kind: TNodeKind, strVal: string, p: Parser): PNode
  61. proc newIdentNodeP*(ident: PIdent, p: Parser): PNode
  62. proc expectIdentOrKeyw*(p: Parser)
  63. proc expectIdent*(p: Parser)
  64. proc parLineInfo*(p: Parser): TLineInfo
  65. proc eat*(p: var Parser, tokType: TokType)
  66. proc skipInd*(p: var Parser)
  67. proc optPar*(p: var Parser)
  68. proc optInd*(p: var Parser, n: PNode)
  69. proc indAndComment*(p: var Parser, n: PNode)
  70. proc setBaseFlags*(n: PNode, base: NumericalBase)
  71. proc parseSymbol*(p: var Parser, mode = smNormal): PNode
  72. proc parseTry(p: var Parser; isExpr: bool): PNode
  73. proc parseCase(p: var Parser): PNode
  74. proc parseStmtPragma(p: var Parser): PNode
  75. proc parsePragma(p: var Parser): PNode
  76. proc postExprBlocks(p: var Parser, x: PNode): PNode
  77. proc parseExprStmt(p: var Parser): PNode
  78. proc parseBlock(p: var Parser): PNode
  79. proc primary(p: var Parser, mode: PrimaryMode): PNode
  80. proc simpleExprAux(p: var Parser, limit: int, mode: PrimaryMode): PNode
  81. # implementation
  82. template prettySection(body) =
  83. when defined(nimpretty): beginSection(p.em)
  84. body
  85. when defined(nimpretty): endSection(p.em)
  86. proc getTok(p: var Parser) =
  87. ## Get the next token from the parser's lexer, and store it in the parser's
  88. ## `tok` member.
  89. rawGetTok(p.lex, p.tok)
  90. p.hasProgress = true
  91. when defined(nimpretty):
  92. emitTok(p.em, p.lex, p.tok)
  93. # skip the additional tokens that nimpretty needs but the parser has no
  94. # interest in:
  95. while p.tok.tokType == tkComment:
  96. rawGetTok(p.lex, p.tok)
  97. emitTok(p.em, p.lex, p.tok)
  98. proc openParser*(p: var Parser, fileIdx: FileIndex, inputStream: PLLStream,
  99. cache: IdentCache; config: ConfigRef) =
  100. ## Open a parser, using the given arguments to set up its internal state.
  101. ##
  102. initToken(p.tok)
  103. openLexer(p.lex, fileIdx, inputStream, cache, config)
  104. when defined(nimpretty):
  105. openEmitter(p.em, cache, config, fileIdx)
  106. getTok(p) # read the first token
  107. p.firstTok = true
  108. p.emptyNode = newNode(nkEmpty)
  109. proc openParser*(p: var Parser, filename: AbsoluteFile, inputStream: PLLStream,
  110. cache: IdentCache; config: ConfigRef) =
  111. openParser(p, fileInfoIdx(config, filename), inputStream, cache, config)
  112. proc closeParser(p: var Parser) =
  113. ## Close a parser, freeing up its resources.
  114. closeLexer(p.lex)
  115. when defined(nimpretty):
  116. closeEmitter(p.em)
  117. proc parMessage(p: Parser, msg: TMsgKind, arg = "") =
  118. ## Produce and emit the parser message `arg` to output.
  119. lexMessageTok(p.lex, msg, p.tok, arg)
  120. proc parMessage(p: Parser, msg: string, tok: Token) =
  121. ## Produce and emit a parser message to output about the token `tok`
  122. parMessage(p, errGenerated, msg % prettyTok(tok))
  123. proc parMessage(p: Parser, arg: string) =
  124. ## Produce and emit the parser message `arg` to output.
  125. lexMessageTok(p.lex, errGenerated, p.tok, arg)
  126. template withInd(p, body: untyped) =
  127. let oldInd = p.currInd
  128. p.currInd = p.tok.indent
  129. body
  130. p.currInd = oldInd
  131. template newlineWasSplitting(p: var Parser) =
  132. when defined(nimpretty):
  133. layouter.newlineWasSplitting(p.em)
  134. template realInd(p): bool = p.tok.indent > p.currInd
  135. template sameInd(p): bool = p.tok.indent == p.currInd
  136. template sameOrNoInd(p): bool = p.tok.indent == p.currInd or p.tok.indent < 0
  137. proc validInd(p: var Parser): bool {.inline.} =
  138. result = p.tok.indent < 0 or p.tok.indent > p.currInd
  139. proc rawSkipComment(p: var Parser, node: PNode) =
  140. if p.tok.tokType == tkComment:
  141. if node != nil:
  142. when not defined(nimNoNilSeqs):
  143. if node.comment == nil: node.comment = ""
  144. when defined(nimpretty):
  145. if p.tok.commentOffsetB > p.tok.commentOffsetA:
  146. node.comment.add fileSection(p.lex.config, p.lex.fileIdx, p.tok.commentOffsetA, p.tok.commentOffsetB)
  147. else:
  148. node.comment.add p.tok.literal
  149. else:
  150. node.comment.add p.tok.literal
  151. else:
  152. parMessage(p, errInternal, "skipComment")
  153. getTok(p)
  154. proc skipComment(p: var Parser, node: PNode) =
  155. if p.tok.indent < 0: rawSkipComment(p, node)
  156. proc flexComment(p: var Parser, node: PNode) =
  157. if p.tok.indent < 0 or realInd(p): rawSkipComment(p, node)
  158. const
  159. errInvalidIndentation = "invalid indentation"
  160. errIdentifierExpected = "identifier expected, but got '$1'"
  161. errExprExpected = "expression expected, but found '$1'"
  162. proc skipInd(p: var Parser) =
  163. if p.tok.indent >= 0:
  164. if not realInd(p): parMessage(p, errInvalidIndentation)
  165. proc optPar(p: var Parser) =
  166. if p.tok.indent >= 0:
  167. if p.tok.indent < p.currInd: parMessage(p, errInvalidIndentation)
  168. proc optInd(p: var Parser, n: PNode) =
  169. skipComment(p, n)
  170. skipInd(p)
  171. proc getTokNoInd(p: var Parser) =
  172. getTok(p)
  173. if p.tok.indent >= 0: parMessage(p, errInvalidIndentation)
  174. proc expectIdentOrKeyw(p: Parser) =
  175. if p.tok.tokType != tkSymbol and not isKeyword(p.tok.tokType):
  176. lexMessage(p.lex, errGenerated, errIdentifierExpected % prettyTok(p.tok))
  177. proc expectIdent(p: Parser) =
  178. if p.tok.tokType != tkSymbol:
  179. lexMessage(p.lex, errGenerated, errIdentifierExpected % prettyTok(p.tok))
  180. proc eat(p: var Parser, tokType: TokType) =
  181. ## Move the parser to the next token if the current token is of type
  182. ## `tokType`, otherwise error.
  183. if p.tok.tokType == tokType:
  184. getTok(p)
  185. else:
  186. lexMessage(p.lex, errGenerated,
  187. "expected: '" & TokTypeToStr[tokType] & "', but got: '" & prettyTok(p.tok) & "'")
  188. proc parLineInfo(p: Parser): TLineInfo =
  189. ## Retrieve the line information associated with the parser's current state.
  190. result = getLineInfo(p.lex, p.tok)
  191. proc indAndComment(p: var Parser, n: PNode) =
  192. if p.tok.indent > p.currInd:
  193. if p.tok.tokType == tkComment: rawSkipComment(p, n)
  194. else: parMessage(p, errInvalidIndentation)
  195. else:
  196. skipComment(p, n)
  197. proc newNodeP(kind: TNodeKind, p: Parser): PNode =
  198. result = newNodeI(kind, parLineInfo(p))
  199. proc newIntNodeP(kind: TNodeKind, intVal: BiggestInt, p: Parser): PNode =
  200. result = newNodeP(kind, p)
  201. result.intVal = intVal
  202. proc newFloatNodeP(kind: TNodeKind, floatVal: BiggestFloat,
  203. p: Parser): PNode =
  204. result = newNodeP(kind, p)
  205. result.floatVal = floatVal
  206. proc newStrNodeP(kind: TNodeKind, strVal: string, p: Parser): PNode =
  207. result = newNodeP(kind, p)
  208. result.strVal = strVal
  209. proc newIdentNodeP(ident: PIdent, p: Parser): PNode =
  210. result = newNodeP(nkIdent, p)
  211. result.ident = ident
  212. proc parseExpr(p: var Parser): PNode
  213. proc parseStmt(p: var Parser): PNode
  214. proc parseTypeDesc(p: var Parser): PNode
  215. proc parseParamList(p: var Parser, retColon = true): PNode
  216. proc isSigilLike(tok: Token): bool {.inline.} =
  217. result = tok.tokType == tkOpr and tok.ident.s[0] == '@'
  218. proc isRightAssociative(tok: Token): bool {.inline.} =
  219. ## Determines whether the token is right assocative.
  220. result = tok.tokType == tkOpr and tok.ident.s[0] == '^'
  221. # or (tok.ident.s.len > 1 and tok.ident.s[^1] == '>')
  222. proc isOperator(tok: Token): bool =
  223. ## Determines if the given token is an operator type token.
  224. tok.tokType in {tkOpr, tkDiv, tkMod, tkShl, tkShr, tkIn, tkNotin, tkIs,
  225. tkIsnot, tkNot, tkOf, tkAs, tkFrom, tkDotDot, tkAnd,
  226. tkOr, tkXor}
  227. proc isUnary(tok: Token): bool =
  228. ## Check if the given token is a unary operator
  229. tok.tokType in {tkOpr, tkDotDot} and
  230. tok.strongSpaceB == 0 and
  231. tok.strongSpaceA > 0
  232. proc checkBinary(p: Parser) {.inline.} =
  233. ## Check if the current parser token is a binary operator.
  234. # we don't check '..' here as that's too annoying
  235. if p.tok.tokType == tkOpr:
  236. if p.tok.strongSpaceB > 0 and p.tok.strongSpaceA == 0:
  237. parMessage(p, warnInconsistentSpacing, prettyTok(p.tok))
  238. #| module = stmt ^* (';' / IND{=})
  239. #|
  240. #| comma = ',' COMMENT?
  241. #| semicolon = ';' COMMENT?
  242. #| colon = ':' COMMENT?
  243. #| colcom = ':' COMMENT?
  244. #|
  245. #| operator = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9
  246. #| | 'or' | 'xor' | 'and'
  247. #| | 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' | 'from'
  248. #| | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..'
  249. #|
  250. #| prefixOperator = operator
  251. #|
  252. #| optInd = COMMENT? IND?
  253. #| optPar = (IND{>} | IND{=})?
  254. #|
  255. #| simpleExpr = arrowExpr (OP0 optInd arrowExpr)* pragma?
  256. #| arrowExpr = assignExpr (OP1 optInd assignExpr)*
  257. #| assignExpr = orExpr (OP2 optInd orExpr)*
  258. #| orExpr = andExpr (OP3 optInd andExpr)*
  259. #| andExpr = cmpExpr (OP4 optInd cmpExpr)*
  260. #| cmpExpr = sliceExpr (OP5 optInd sliceExpr)*
  261. #| sliceExpr = ampExpr (OP6 optInd ampExpr)*
  262. #| ampExpr = plusExpr (OP7 optInd plusExpr)*
  263. #| plusExpr = mulExpr (OP8 optInd mulExpr)*
  264. #| mulExpr = dollarExpr (OP9 optInd dollarExpr)*
  265. #| dollarExpr = primary (OP10 optInd primary)*
  266. proc colcom(p: var Parser, n: PNode) =
  267. eat(p, tkColon)
  268. skipComment(p, n)
  269. const tkBuiltInMagics = {tkType, tkStatic, tkAddr}
  270. proc parseSymbol(p: var Parser, mode = smNormal): PNode =
  271. #| symbol = '`' (KEYW|IDENT|literal|(operator|'('|')'|'['|']'|'{'|'}'|'=')+)+ '`'
  272. #| | IDENT | KEYW
  273. case p.tok.tokType
  274. of tkSymbol:
  275. result = newIdentNodeP(p.tok.ident, p)
  276. getTok(p)
  277. of tokKeywordLow..tokKeywordHigh:
  278. if p.tok.tokType in tkBuiltInMagics or mode == smAfterDot:
  279. # for backwards compatibility these 2 are always valid:
  280. result = newIdentNodeP(p.tok.ident, p)
  281. getTok(p)
  282. elif p.tok.tokType == tkNil and mode == smAllowNil:
  283. result = newNodeP(nkNilLit, p)
  284. getTok(p)
  285. else:
  286. parMessage(p, errIdentifierExpected, p.tok)
  287. result = p.emptyNode
  288. of tkAccent:
  289. result = newNodeP(nkAccQuoted, p)
  290. getTok(p)
  291. # progress guaranteed
  292. while true:
  293. case p.tok.tokType
  294. of tkAccent:
  295. if result.len == 0:
  296. parMessage(p, errIdentifierExpected, p.tok)
  297. break
  298. of tkOpr, tkDot, tkDotDot, tkEquals, tkParLe..tkParDotRi:
  299. let lineinfo = parLineInfo(p)
  300. var accm = ""
  301. while p.tok.tokType in {tkOpr, tkDot, tkDotDot, tkEquals,
  302. tkParLe..tkParDotRi}:
  303. accm.add($p.tok)
  304. getTok(p)
  305. let node = newNodeI(nkIdent, lineinfo)
  306. node.ident = p.lex.cache.getIdent(accm)
  307. result.add(node)
  308. of tokKeywordLow..tokKeywordHigh, tkSymbol, tkIntLit..tkCharLit:
  309. result.add(newIdentNodeP(p.lex.cache.getIdent($p.tok), p))
  310. getTok(p)
  311. else:
  312. parMessage(p, errIdentifierExpected, p.tok)
  313. break
  314. eat(p, tkAccent)
  315. else:
  316. parMessage(p, errIdentifierExpected, p.tok)
  317. # BUGFIX: We must consume a token here to prevent endless loops!
  318. # But: this really sucks for idetools and keywords, so we don't do it
  319. # if it is a keyword:
  320. #if not isKeyword(p.tok.tokType): getTok(p)
  321. result = p.emptyNode
  322. proc colonOrEquals(p: var Parser, a: PNode): PNode =
  323. if p.tok.tokType == tkColon:
  324. result = newNodeP(nkExprColonExpr, p)
  325. getTok(p)
  326. newlineWasSplitting(p)
  327. #optInd(p, result)
  328. result.add(a)
  329. result.add(parseExpr(p))
  330. elif p.tok.tokType == tkEquals:
  331. result = newNodeP(nkExprEqExpr, p)
  332. getTok(p)
  333. #optInd(p, result)
  334. result.add(a)
  335. result.add(parseExpr(p))
  336. else:
  337. result = a
  338. proc exprColonEqExpr(p: var Parser): PNode =
  339. #| exprColonEqExpr = expr (':'|'=' expr)?
  340. var a = parseExpr(p)
  341. if p.tok.tokType == tkDo:
  342. result = postExprBlocks(p, a)
  343. else:
  344. result = colonOrEquals(p, a)
  345. proc exprList(p: var Parser, endTok: TokType, result: PNode) =
  346. #| exprList = expr ^+ comma
  347. when defined(nimpretty):
  348. inc p.em.doIndentMore
  349. getTok(p)
  350. optInd(p, result)
  351. # progress guaranteed
  352. while (p.tok.tokType != endTok) and (p.tok.tokType != tkEof):
  353. var a = parseExpr(p)
  354. result.add(a)
  355. if p.tok.tokType != tkComma: break
  356. getTok(p)
  357. optInd(p, a)
  358. when defined(nimpretty):
  359. dec p.em.doIndentMore
  360. proc exprColonEqExprListAux(p: var Parser, endTok: TokType, result: PNode) =
  361. assert(endTok in {tkCurlyRi, tkCurlyDotRi, tkBracketRi, tkParRi})
  362. getTok(p)
  363. flexComment(p, result)
  364. optPar(p)
  365. # progress guaranteed
  366. while p.tok.tokType != endTok and p.tok.tokType != tkEof:
  367. var a = exprColonEqExpr(p)
  368. result.add(a)
  369. if p.tok.tokType != tkComma: break
  370. getTok(p)
  371. # (1,) produces a tuple expression
  372. if endTok == tkParRi and p.tok.tokType == tkParRi and result.kind == nkPar:
  373. result.transitionSonsKind(nkTupleConstr)
  374. skipComment(p, a)
  375. optPar(p)
  376. eat(p, endTok)
  377. proc exprColonEqExprList(p: var Parser, kind: TNodeKind,
  378. endTok: TokType): PNode =
  379. #| exprColonEqExprList = exprColonEqExpr (comma exprColonEqExpr)* (comma)?
  380. result = newNodeP(kind, p)
  381. exprColonEqExprListAux(p, endTok, result)
  382. proc dotExpr(p: var Parser, a: PNode): PNode =
  383. #| dotExpr = expr '.' optInd (symbol | '[:' exprList ']')
  384. #| explicitGenericInstantiation = '[:' exprList ']' ( '(' exprColonEqExpr ')' )?
  385. var info = p.parLineInfo
  386. getTok(p)
  387. result = newNodeI(nkDotExpr, info)
  388. optInd(p, result)
  389. result.add(a)
  390. result.add(parseSymbol(p, smAfterDot))
  391. if p.tok.tokType == tkBracketLeColon and p.tok.strongSpaceA <= 0:
  392. var x = newNodeI(nkBracketExpr, p.parLineInfo)
  393. # rewrite 'x.y[:z]()' to 'y[z](x)'
  394. x.add result[1]
  395. exprList(p, tkBracketRi, x)
  396. eat(p, tkBracketRi)
  397. var y = newNodeI(nkCall, p.parLineInfo)
  398. y.add x
  399. y.add result[0]
  400. if p.tok.tokType == tkParLe and p.tok.strongSpaceA <= 0:
  401. exprColonEqExprListAux(p, tkParRi, y)
  402. result = y
  403. proc qualifiedIdent(p: var Parser): PNode =
  404. #| qualifiedIdent = symbol ('.' optInd symbol)?
  405. result = parseSymbol(p)
  406. if p.tok.tokType == tkDot: result = dotExpr(p, result)
  407. proc setOrTableConstr(p: var Parser): PNode =
  408. #| setOrTableConstr = '{' ((exprColonEqExpr comma)* | ':' ) '}'
  409. result = newNodeP(nkCurly, p)
  410. getTok(p) # skip '{'
  411. optInd(p, result)
  412. if p.tok.tokType == tkColon:
  413. getTok(p) # skip ':'
  414. result.transitionSonsKind(nkTableConstr)
  415. else:
  416. # progress guaranteed
  417. while p.tok.tokType notin {tkCurlyRi, tkEof}:
  418. var a = exprColonEqExpr(p)
  419. if a.kind == nkExprColonExpr: result.transitionSonsKind(nkTableConstr)
  420. result.add(a)
  421. if p.tok.tokType != tkComma: break
  422. getTok(p)
  423. skipComment(p, a)
  424. optPar(p)
  425. eat(p, tkCurlyRi) # skip '}'
  426. proc parseCast(p: var Parser): PNode =
  427. #| castExpr = 'cast' '[' optInd typeDesc optPar ']' '(' optInd expr optPar ')'
  428. result = newNodeP(nkCast, p)
  429. getTok(p)
  430. eat(p, tkBracketLe)
  431. optInd(p, result)
  432. result.add(parseTypeDesc(p))
  433. optPar(p)
  434. eat(p, tkBracketRi)
  435. eat(p, tkParLe)
  436. optInd(p, result)
  437. result.add(parseExpr(p))
  438. optPar(p)
  439. eat(p, tkParRi)
  440. proc setBaseFlags(n: PNode, base: NumericalBase) =
  441. case base
  442. of base10: discard
  443. of base2: incl(n.flags, nfBase2)
  444. of base8: incl(n.flags, nfBase8)
  445. of base16: incl(n.flags, nfBase16)
  446. proc parseGStrLit(p: var Parser, a: PNode): PNode =
  447. case p.tok.tokType
  448. of tkGStrLit:
  449. result = newNodeP(nkCallStrLit, p)
  450. result.add(a)
  451. result.add(newStrNodeP(nkRStrLit, p.tok.literal, p))
  452. getTok(p)
  453. of tkGTripleStrLit:
  454. result = newNodeP(nkCallStrLit, p)
  455. result.add(a)
  456. result.add(newStrNodeP(nkTripleStrLit, p.tok.literal, p))
  457. getTok(p)
  458. else:
  459. result = a
  460. proc complexOrSimpleStmt(p: var Parser): PNode
  461. proc simpleExpr(p: var Parser, mode = pmNormal): PNode
  462. proc parseIfExpr(p: var Parser, kind: TNodeKind): PNode
  463. proc semiStmtList(p: var Parser, result: PNode) =
  464. inc p.inSemiStmtList
  465. withInd(p):
  466. # Be lenient with the first stmt/expr
  467. result.add if p.tok.tokType == tkIf: parseIfExpr(p, nkIfStmt) else: complexOrSimpleStmt(p)
  468. while true:
  469. if p.tok.tokType == tkSemiColon:
  470. getTok(p)
  471. if p.tok.tokType == tkParRi:
  472. break
  473. elif not (sameInd(p) or realInd(p)):
  474. parMessage(p, errInvalidIndentation)
  475. result.add complexOrSimpleStmt(p)
  476. dec p.inSemiStmtList
  477. result.transitionSonsKind(nkStmtListExpr)
  478. proc parsePar(p: var Parser): PNode =
  479. #| parKeyw = 'discard' | 'include' | 'if' | 'while' | 'case' | 'try'
  480. #| | 'finally' | 'except' | 'for' | 'block' | 'const' | 'let'
  481. #| | 'when' | 'var' | 'mixin'
  482. #| par = '(' optInd
  483. #| ( &parKeyw (ifExpr \ complexOrSimpleStmt) ^+ ';'
  484. #| | ';' (ifExpr \ complexOrSimpleStmt) ^+ ';'
  485. #| | pragmaStmt
  486. #| | simpleExpr ( ('=' expr (';' (ifExpr \ complexOrSimpleStmt) ^+ ';' )? )
  487. #| | (':' expr (',' exprColonEqExpr ^+ ',' )? ) ) )
  488. #| optPar ')'
  489. #
  490. # unfortunately it's ambiguous: (expr: expr) vs (exprStmt); however a
  491. # leading ';' could be used to enforce a 'stmt' context ...
  492. result = newNodeP(nkPar, p)
  493. getTok(p)
  494. optInd(p, result)
  495. flexComment(p, result)
  496. if p.tok.tokType in {tkDiscard, tkInclude, tkIf, tkWhile, tkCase,
  497. tkTry, tkDefer, tkFinally, tkExcept, tkBlock,
  498. tkConst, tkLet, tkWhen, tkVar, tkFor,
  499. tkMixin}:
  500. # XXX 'bind' used to be an expression, so we exclude it here;
  501. # tests/reject/tbind2 fails otherwise.
  502. semiStmtList(p, result)
  503. elif p.tok.tokType == tkSemiColon:
  504. # '(;' enforces 'stmt' context:
  505. getTok(p)
  506. optInd(p, result)
  507. semiStmtList(p, result)
  508. elif p.tok.tokType == tkCurlyDotLe:
  509. result.add(parseStmtPragma(p))
  510. elif p.tok.tokType != tkParRi:
  511. var a = simpleExpr(p)
  512. if p.tok.tokType == tkDo:
  513. result = postExprBlocks(p, a)
  514. elif p.tok.tokType == tkEquals:
  515. # special case: allow assignments
  516. let asgn = newNodeP(nkAsgn, p)
  517. getTok(p)
  518. optInd(p, result)
  519. let b = parseExpr(p)
  520. asgn.add a
  521. asgn.add b
  522. result.add(asgn)
  523. if p.tok.tokType == tkSemiColon:
  524. semiStmtList(p, result)
  525. elif p.tok.tokType == tkSemiColon:
  526. # stmt context:
  527. result.add(a)
  528. semiStmtList(p, result)
  529. else:
  530. a = colonOrEquals(p, a)
  531. result.add(a)
  532. if p.tok.tokType == tkComma:
  533. getTok(p)
  534. skipComment(p, a)
  535. # (1,) produces a tuple expression:
  536. if p.tok.tokType == tkParRi:
  537. result.transitionSonsKind(nkTupleConstr)
  538. # progress guaranteed
  539. while p.tok.tokType != tkParRi and p.tok.tokType != tkEof:
  540. var a = exprColonEqExpr(p)
  541. result.add(a)
  542. if p.tok.tokType != tkComma: break
  543. getTok(p)
  544. skipComment(p, a)
  545. optPar(p)
  546. eat(p, tkParRi)
  547. proc identOrLiteral(p: var Parser, mode: PrimaryMode): PNode =
  548. #| literal = | INT_LIT | INT8_LIT | INT16_LIT | INT32_LIT | INT64_LIT
  549. #| | UINT_LIT | UINT8_LIT | UINT16_LIT | UINT32_LIT | UINT64_LIT
  550. #| | FLOAT_LIT | FLOAT32_LIT | FLOAT64_LIT
  551. #| | STR_LIT | RSTR_LIT | TRIPLESTR_LIT
  552. #| | CHAR_LIT
  553. #| | NIL
  554. #| generalizedLit = GENERALIZED_STR_LIT | GENERALIZED_TRIPLESTR_LIT
  555. #| identOrLiteral = generalizedLit | symbol | literal
  556. #| | par | arrayConstr | setOrTableConstr
  557. #| | castExpr
  558. #| tupleConstr = '(' optInd (exprColonEqExpr comma?)* optPar ')'
  559. #| arrayConstr = '[' optInd (exprColonEqExpr comma?)* optPar ']'
  560. case p.tok.tokType
  561. of tkSymbol, tkBuiltInMagics, tkOut:
  562. result = newIdentNodeP(p.tok.ident, p)
  563. getTok(p)
  564. result = parseGStrLit(p, result)
  565. of tkAccent:
  566. result = parseSymbol(p) # literals
  567. of tkIntLit:
  568. result = newIntNodeP(nkIntLit, p.tok.iNumber, p)
  569. setBaseFlags(result, p.tok.base)
  570. getTok(p)
  571. of tkInt8Lit:
  572. result = newIntNodeP(nkInt8Lit, p.tok.iNumber, p)
  573. setBaseFlags(result, p.tok.base)
  574. getTok(p)
  575. of tkInt16Lit:
  576. result = newIntNodeP(nkInt16Lit, p.tok.iNumber, p)
  577. setBaseFlags(result, p.tok.base)
  578. getTok(p)
  579. of tkInt32Lit:
  580. result = newIntNodeP(nkInt32Lit, p.tok.iNumber, p)
  581. setBaseFlags(result, p.tok.base)
  582. getTok(p)
  583. of tkInt64Lit:
  584. result = newIntNodeP(nkInt64Lit, p.tok.iNumber, p)
  585. setBaseFlags(result, p.tok.base)
  586. getTok(p)
  587. of tkUIntLit:
  588. result = newIntNodeP(nkUIntLit, p.tok.iNumber, p)
  589. setBaseFlags(result, p.tok.base)
  590. getTok(p)
  591. of tkUInt8Lit:
  592. result = newIntNodeP(nkUInt8Lit, p.tok.iNumber, p)
  593. setBaseFlags(result, p.tok.base)
  594. getTok(p)
  595. of tkUInt16Lit:
  596. result = newIntNodeP(nkUInt16Lit, p.tok.iNumber, p)
  597. setBaseFlags(result, p.tok.base)
  598. getTok(p)
  599. of tkUInt32Lit:
  600. result = newIntNodeP(nkUInt32Lit, p.tok.iNumber, p)
  601. setBaseFlags(result, p.tok.base)
  602. getTok(p)
  603. of tkUInt64Lit:
  604. result = newIntNodeP(nkUInt64Lit, p.tok.iNumber, p)
  605. setBaseFlags(result, p.tok.base)
  606. getTok(p)
  607. of tkFloatLit:
  608. result = newFloatNodeP(nkFloatLit, p.tok.fNumber, p)
  609. setBaseFlags(result, p.tok.base)
  610. getTok(p)
  611. of tkFloat32Lit:
  612. result = newFloatNodeP(nkFloat32Lit, p.tok.fNumber, p)
  613. setBaseFlags(result, p.tok.base)
  614. getTok(p)
  615. of tkFloat64Lit:
  616. result = newFloatNodeP(nkFloat64Lit, p.tok.fNumber, p)
  617. setBaseFlags(result, p.tok.base)
  618. getTok(p)
  619. of tkFloat128Lit:
  620. result = newFloatNodeP(nkFloat128Lit, p.tok.fNumber, p)
  621. setBaseFlags(result, p.tok.base)
  622. getTok(p)
  623. of tkStrLit:
  624. result = newStrNodeP(nkStrLit, p.tok.literal, p)
  625. getTok(p)
  626. of tkRStrLit:
  627. result = newStrNodeP(nkRStrLit, p.tok.literal, p)
  628. getTok(p)
  629. of tkTripleStrLit:
  630. result = newStrNodeP(nkTripleStrLit, p.tok.literal, p)
  631. getTok(p)
  632. of tkCharLit:
  633. result = newIntNodeP(nkCharLit, ord(p.tok.literal[0]), p)
  634. getTok(p)
  635. of tkNil:
  636. result = newNodeP(nkNilLit, p)
  637. getTok(p)
  638. of tkParLe:
  639. # () constructor
  640. if mode in {pmTypeDesc, pmTypeDef}:
  641. result = exprColonEqExprList(p, nkPar, tkParRi)
  642. else:
  643. result = parsePar(p)
  644. of tkCurlyLe:
  645. # {} constructor
  646. result = setOrTableConstr(p)
  647. of tkBracketLe:
  648. # [] constructor
  649. result = exprColonEqExprList(p, nkBracket, tkBracketRi)
  650. of tkCast:
  651. result = parseCast(p)
  652. else:
  653. parMessage(p, errExprExpected, p.tok)
  654. getTok(p) # we must consume a token here to prevent endless loops!
  655. result = p.emptyNode
  656. proc namedParams(p: var Parser, callee: PNode,
  657. kind: TNodeKind, endTok: TokType): PNode =
  658. let a = callee
  659. result = newNodeP(kind, p)
  660. result.add(a)
  661. # progress guaranteed
  662. exprColonEqExprListAux(p, endTok, result)
  663. proc commandParam(p: var Parser, isFirstParam: var bool; mode: PrimaryMode): PNode =
  664. if mode == pmTypeDesc:
  665. result = simpleExpr(p, mode)
  666. else:
  667. result = parseExpr(p)
  668. if p.tok.tokType == tkDo:
  669. result = postExprBlocks(p, result)
  670. elif p.tok.tokType == tkEquals and not isFirstParam:
  671. let lhs = result
  672. result = newNodeP(nkExprEqExpr, p)
  673. getTok(p)
  674. result.add(lhs)
  675. result.add(parseExpr(p))
  676. isFirstParam = false
  677. proc commandExpr(p: var Parser; r: PNode; mode: PrimaryMode): PNode =
  678. result = newNodeP(nkCommand, p)
  679. result.add(r)
  680. var isFirstParam = true
  681. # progress NOT guaranteed
  682. p.hasProgress = false
  683. result.add commandParam(p, isFirstParam, mode)
  684. proc primarySuffix(p: var Parser, r: PNode,
  685. baseIndent: int, mode: PrimaryMode): PNode =
  686. #| primarySuffix = '(' (exprColonEqExpr comma?)* ')'
  687. #| | '.' optInd symbol generalizedLit?
  688. #| | '[' optInd exprColonEqExprList optPar ']'
  689. #| | '{' optInd exprColonEqExprList optPar '}'
  690. #| | &( '`'|IDENT|literal|'cast'|'addr'|'type') expr # command syntax
  691. result = r
  692. # progress guaranteed
  693. while p.tok.indent < 0 or
  694. (p.tok.tokType == tkDot and p.tok.indent >= baseIndent):
  695. case p.tok.tokType
  696. of tkParLe:
  697. # progress guaranteed
  698. if p.tok.strongSpaceA > 0:
  699. # inside type sections, expressions such as `ref (int, bar)`
  700. # are parsed as a nkCommand with a single tuple argument (nkPar)
  701. if mode == pmTypeDef:
  702. result = newNodeP(nkCommand, p)
  703. result.add r
  704. result.add primary(p, pmNormal)
  705. else:
  706. result = commandExpr(p, result, mode)
  707. break
  708. result = namedParams(p, result, nkCall, tkParRi)
  709. if result.len > 1 and result[1].kind == nkExprColonExpr:
  710. result.transitionSonsKind(nkObjConstr)
  711. of tkDot:
  712. # progress guaranteed
  713. result = dotExpr(p, result)
  714. result = parseGStrLit(p, result)
  715. of tkBracketLe:
  716. # progress guaranteed
  717. if p.tok.strongSpaceA > 0:
  718. result = commandExpr(p, result, mode)
  719. break
  720. result = namedParams(p, result, nkBracketExpr, tkBracketRi)
  721. of tkCurlyLe:
  722. # progress guaranteed
  723. if p.tok.strongSpaceA > 0:
  724. result = commandExpr(p, result, mode)
  725. break
  726. result = namedParams(p, result, nkCurlyExpr, tkCurlyRi)
  727. of tkSymbol, tkAccent, tkIntLit..tkCharLit, tkNil, tkCast,
  728. tkOpr, tkDotDot, tkVar, tkOut, tkStatic, tkType, tkEnum, tkTuple,
  729. tkObject, tkProc:
  730. # XXX: In type sections we allow the free application of the
  731. # command syntax, with the exception of expressions such as
  732. # `foo ref` or `foo ptr`. Unfortunately, these two are also
  733. # used as infix operators for the memory regions feature and
  734. # the current parsing rules don't play well here.
  735. if p.inPragma == 0 and (isUnary(p.tok) or p.tok.tokType notin {tkOpr, tkDotDot}):
  736. # actually parsing {.push hints:off.} as {.push(hints:off).} is a sweet
  737. # solution, but pragmas.nim can't handle that
  738. result = commandExpr(p, result, mode)
  739. break
  740. else:
  741. break
  742. proc parseOperators(p: var Parser, headNode: PNode,
  743. limit: int, mode: PrimaryMode): PNode =
  744. result = headNode
  745. # expand while operators have priorities higher than 'limit'
  746. var opPrec = getPrecedence(p.tok)
  747. let modeB = if mode == pmTypeDef: pmTypeDesc else: mode
  748. # the operator itself must not start on a new line:
  749. # progress guaranteed
  750. while opPrec >= limit and p.tok.indent < 0 and not isUnary(p.tok):
  751. checkBinary(p)
  752. let leftAssoc = ord(not isRightAssociative(p.tok))
  753. var a = newNodeP(nkInfix, p)
  754. var opNode = newIdentNodeP(p.tok.ident, p) # skip operator:
  755. getTok(p)
  756. flexComment(p, a)
  757. optPar(p)
  758. # read sub-expression with higher priority:
  759. var b = simpleExprAux(p, opPrec + leftAssoc, modeB)
  760. a.add(opNode)
  761. a.add(result)
  762. a.add(b)
  763. result = a
  764. opPrec = getPrecedence(p.tok)
  765. proc simpleExprAux(p: var Parser, limit: int, mode: PrimaryMode): PNode =
  766. result = primary(p, mode)
  767. if p.tok.tokType == tkCurlyDotLe and (p.tok.indent < 0 or realInd(p)) and
  768. mode == pmNormal:
  769. var pragmaExp = newNodeP(nkPragmaExpr, p)
  770. pragmaExp.add result
  771. pragmaExp.add p.parsePragma
  772. result = pragmaExp
  773. result = parseOperators(p, result, limit, mode)
  774. proc simpleExpr(p: var Parser, mode = pmNormal): PNode =
  775. when defined(nimpretty):
  776. inc p.em.doIndentMore
  777. result = simpleExprAux(p, -1, mode)
  778. when defined(nimpretty):
  779. dec p.em.doIndentMore
  780. proc parsePragma(p: var Parser): PNode =
  781. #| pragma = '{.' optInd (exprColonEqExpr comma?)* optPar ('.}' | '}')
  782. result = newNodeP(nkPragma, p)
  783. inc p.inPragma
  784. when defined(nimpretty):
  785. inc p.em.doIndentMore
  786. inc p.em.keepIndents
  787. getTok(p)
  788. optInd(p, result)
  789. while p.tok.tokType notin {tkCurlyDotRi, tkCurlyRi, tkEof}:
  790. p.hasProgress = false
  791. var a = exprColonEqExpr(p)
  792. if not p.hasProgress: break
  793. result.add(a)
  794. if p.tok.tokType == tkComma:
  795. getTok(p)
  796. skipComment(p, a)
  797. optPar(p)
  798. if p.tok.tokType in {tkCurlyDotRi, tkCurlyRi}:
  799. when defined(nimpretty):
  800. if p.tok.tokType == tkCurlyRi: curlyRiWasPragma(p.em)
  801. getTok(p)
  802. else:
  803. parMessage(p, "expected '.}'")
  804. dec p.inPragma
  805. when defined(nimpretty):
  806. dec p.em.doIndentMore
  807. dec p.em.keepIndents
  808. proc identVis(p: var Parser; allowDot=false): PNode =
  809. #| identVis = symbol OPR? # postfix position
  810. #| identVisDot = symbol '.' optInd symbol OPR?
  811. var a = parseSymbol(p)
  812. if p.tok.tokType == tkOpr:
  813. when defined(nimpretty):
  814. starWasExportMarker(p.em)
  815. result = newNodeP(nkPostfix, p)
  816. result.add(newIdentNodeP(p.tok.ident, p))
  817. result.add(a)
  818. getTok(p)
  819. elif p.tok.tokType == tkDot and allowDot:
  820. result = dotExpr(p, a)
  821. else:
  822. result = a
  823. proc identWithPragma(p: var Parser; allowDot=false): PNode =
  824. #| identWithPragma = identVis pragma?
  825. #| identWithPragmaDot = identVisDot pragma?
  826. var a = identVis(p, allowDot)
  827. if p.tok.tokType == tkCurlyDotLe:
  828. result = newNodeP(nkPragmaExpr, p)
  829. result.add(a)
  830. result.add(parsePragma(p))
  831. else:
  832. result = a
  833. type
  834. DeclaredIdentFlag = enum
  835. withPragma, # identifier may have pragma
  836. withBothOptional # both ':' and '=' parts are optional
  837. withDot # allow 'var ident.ident = value'
  838. DeclaredIdentFlags = set[DeclaredIdentFlag]
  839. proc parseIdentColonEquals(p: var Parser, flags: DeclaredIdentFlags): PNode =
  840. #| declColonEquals = identWithPragma (comma identWithPragma)* comma?
  841. #| (':' optInd typeDesc)? ('=' optInd expr)?
  842. #| identColonEquals = IDENT (comma IDENT)* comma?
  843. #| (':' optInd typeDesc)? ('=' optInd expr)?)
  844. var a: PNode
  845. result = newNodeP(nkIdentDefs, p)
  846. # progress guaranteed
  847. while true:
  848. case p.tok.tokType
  849. of tkSymbol, tkAccent:
  850. if withPragma in flags: a = identWithPragma(p, allowDot=withDot in flags)
  851. else: a = parseSymbol(p)
  852. if a.kind == nkEmpty: return
  853. else: break
  854. result.add(a)
  855. if p.tok.tokType != tkComma: break
  856. getTok(p)
  857. optInd(p, a)
  858. if p.tok.tokType == tkColon:
  859. getTok(p)
  860. optInd(p, result)
  861. result.add(parseTypeDesc(p))
  862. else:
  863. result.add(newNodeP(nkEmpty, p))
  864. if p.tok.tokType != tkEquals and withBothOptional notin flags:
  865. parMessage(p, "':' or '=' expected, but got '$1'", p.tok)
  866. if p.tok.tokType == tkEquals:
  867. getTok(p)
  868. optInd(p, result)
  869. result.add(parseExpr(p))
  870. else:
  871. result.add(newNodeP(nkEmpty, p))
  872. proc parseTuple(p: var Parser, indentAllowed = false): PNode =
  873. #| inlTupleDecl = 'tuple'
  874. #| '[' optInd (identColonEquals (comma/semicolon)?)* optPar ']'
  875. #| extTupleDecl = 'tuple'
  876. #| COMMENT? (IND{>} identColonEquals (IND{=} identColonEquals)*)?
  877. #| tupleClass = 'tuple'
  878. result = newNodeP(nkTupleTy, p)
  879. getTok(p)
  880. if p.tok.tokType == tkBracketLe:
  881. getTok(p)
  882. optInd(p, result)
  883. # progress guaranteed
  884. while p.tok.tokType in {tkSymbol, tkAccent}:
  885. var a = parseIdentColonEquals(p, {})
  886. result.add(a)
  887. if p.tok.tokType notin {tkComma, tkSemiColon}: break
  888. when defined(nimpretty):
  889. commaWasSemicolon(p.em)
  890. getTok(p)
  891. skipComment(p, a)
  892. optPar(p)
  893. eat(p, tkBracketRi)
  894. elif indentAllowed:
  895. skipComment(p, result)
  896. if realInd(p):
  897. withInd(p):
  898. rawSkipComment(p, result)
  899. # progress guaranteed
  900. while true:
  901. case p.tok.tokType
  902. of tkSymbol, tkAccent:
  903. var a = parseIdentColonEquals(p, {})
  904. if p.tok.indent < 0 or p.tok.indent >= p.currInd:
  905. rawSkipComment(p, a)
  906. result.add(a)
  907. of tkEof: break
  908. else:
  909. parMessage(p, errIdentifierExpected, p.tok)
  910. break
  911. if not sameInd(p): break
  912. elif p.tok.tokType == tkParLe:
  913. parMessage(p, errGenerated, "the syntax for tuple types is 'tuple[...]', not 'tuple(...)'")
  914. else:
  915. result = newNodeP(nkTupleClassTy, p)
  916. proc parseParamList(p: var Parser, retColon = true): PNode =
  917. #| paramList = '(' declColonEquals ^* (comma/semicolon) ')'
  918. #| paramListArrow = paramList? ('->' optInd typeDesc)?
  919. #| paramListColon = paramList? (':' optInd typeDesc)?
  920. var a: PNode
  921. result = newNodeP(nkFormalParams, p)
  922. result.add(p.emptyNode) # return type
  923. when defined(nimpretty):
  924. inc p.em.doIndentMore
  925. inc p.em.keepIndents
  926. let hasParLe = p.tok.tokType == tkParLe and p.tok.indent < 0
  927. if hasParLe:
  928. getTok(p)
  929. optInd(p, result)
  930. # progress guaranteed
  931. while true:
  932. case p.tok.tokType
  933. of tkSymbol, tkAccent:
  934. a = parseIdentColonEquals(p, {withBothOptional, withPragma})
  935. of tkParRi:
  936. break
  937. of tkVar:
  938. parMessage(p, errGenerated, "the syntax is 'parameter: var T', not 'var parameter: T'")
  939. break
  940. else:
  941. parMessage(p, "expected closing ')'")
  942. break
  943. result.add(a)
  944. if p.tok.tokType notin {tkComma, tkSemiColon}: break
  945. when defined(nimpretty):
  946. commaWasSemicolon(p.em)
  947. getTok(p)
  948. skipComment(p, a)
  949. optPar(p)
  950. eat(p, tkParRi)
  951. let hasRet = if retColon: p.tok.tokType == tkColon
  952. else: p.tok.tokType == tkOpr and p.tok.ident.s == "->"
  953. if hasRet and p.tok.indent < 0:
  954. getTok(p)
  955. optInd(p, result)
  956. result[0] = parseTypeDesc(p)
  957. elif not retColon and not hasParLe:
  958. # Mark as "not there" in order to mark for deprecation in the semantic pass:
  959. result = p.emptyNode
  960. when defined(nimpretty):
  961. dec p.em.doIndentMore
  962. dec p.em.keepIndents
  963. proc optPragmas(p: var Parser): PNode =
  964. if p.tok.tokType == tkCurlyDotLe and (p.tok.indent < 0 or realInd(p)):
  965. result = parsePragma(p)
  966. else:
  967. result = p.emptyNode
  968. proc parseDoBlock(p: var Parser; info: TLineInfo): PNode =
  969. #| doBlock = 'do' paramListArrow pragma? colcom stmt
  970. let params = parseParamList(p, retColon=false)
  971. let pragmas = optPragmas(p)
  972. colcom(p, result)
  973. result = parseStmt(p)
  974. if params.kind != nkEmpty:
  975. result = newProcNode(nkDo, info,
  976. body = result, params = params, name = p.emptyNode, pattern = p.emptyNode,
  977. genericParams = p.emptyNode, pragmas = pragmas, exceptions = p.emptyNode)
  978. proc parseProcExpr(p: var Parser; isExpr: bool; kind: TNodeKind): PNode =
  979. #| procExpr = 'proc' paramListColon pragma? ('=' COMMENT? stmt)?
  980. # either a proc type or a anonymous proc
  981. let info = parLineInfo(p)
  982. getTok(p)
  983. let hasSignature = p.tok.tokType in {tkParLe, tkColon} and p.tok.indent < 0
  984. let params = parseParamList(p)
  985. let pragmas = optPragmas(p)
  986. if p.tok.tokType == tkEquals and isExpr:
  987. getTok(p)
  988. skipComment(p, result)
  989. result = newProcNode(kind, info, body = parseStmt(p),
  990. params = params, name = p.emptyNode, pattern = p.emptyNode,
  991. genericParams = p.emptyNode, pragmas = pragmas, exceptions = p.emptyNode)
  992. else:
  993. result = newNodeI(nkProcTy, info)
  994. if hasSignature:
  995. result.add(params)
  996. if kind == nkFuncDef:
  997. parMessage(p, "func keyword is not allowed in type descriptions, use proc with {.noSideEffect.} pragma instead")
  998. result.add(pragmas)
  999. proc isExprStart(p: Parser): bool =
  1000. case p.tok.tokType
  1001. of tkSymbol, tkAccent, tkOpr, tkNot, tkNil, tkCast, tkIf, tkFor,
  1002. tkProc, tkFunc, tkIterator, tkBind, tkBuiltInMagics,
  1003. tkParLe, tkBracketLe, tkCurlyLe, tkIntLit..tkCharLit, tkVar, tkRef, tkPtr,
  1004. tkTuple, tkObject, tkWhen, tkCase, tkOut:
  1005. result = true
  1006. else: result = false
  1007. proc parseSymbolList(p: var Parser, result: PNode) =
  1008. # progress guaranteed
  1009. while true:
  1010. var s = parseSymbol(p, smAllowNil)
  1011. if s.kind == nkEmpty: break
  1012. result.add(s)
  1013. if p.tok.tokType != tkComma: break
  1014. getTok(p)
  1015. optInd(p, s)
  1016. proc parseTypeDescKAux(p: var Parser, kind: TNodeKind,
  1017. mode: PrimaryMode): PNode =
  1018. #| distinct = 'distinct' optInd typeDesc
  1019. result = newNodeP(kind, p)
  1020. getTok(p)
  1021. if p.tok.indent != -1 and p.tok.indent <= p.currInd: return
  1022. optInd(p, result)
  1023. if not isOperator(p.tok) and isExprStart(p):
  1024. result.add(primary(p, mode))
  1025. if kind == nkDistinctTy and p.tok.tokType == tkSymbol:
  1026. # XXX document this feature!
  1027. var nodeKind: TNodeKind
  1028. if p.tok.ident.s == "with":
  1029. nodeKind = nkWith
  1030. elif p.tok.ident.s == "without":
  1031. nodeKind = nkWithout
  1032. else:
  1033. return result
  1034. getTok(p)
  1035. let list = newNodeP(nodeKind, p)
  1036. result.add list
  1037. parseSymbolList(p, list)
  1038. proc parseVarTuple(p: var Parser): PNode
  1039. proc parseFor(p: var Parser): PNode =
  1040. #| forStmt = 'for' (identWithPragma ^+ comma) 'in' expr colcom stmt
  1041. #| forExpr = forStmt
  1042. getTokNoInd(p)
  1043. result = newNodeP(nkForStmt, p)
  1044. if p.tok.tokType == tkParLe:
  1045. result.add(parseVarTuple(p))
  1046. else:
  1047. var a = identWithPragma(p)
  1048. result.add(a)
  1049. while p.tok.tokType == tkComma:
  1050. getTok(p)
  1051. optInd(p, a)
  1052. if p.tok.tokType == tkParLe:
  1053. result.add(parseVarTuple(p))
  1054. break
  1055. a = identWithPragma(p)
  1056. result.add(a)
  1057. eat(p, tkIn)
  1058. result.add(parseExpr(p))
  1059. colcom(p, result)
  1060. result.add(parseStmt(p))
  1061. template nimprettyDontTouch(body) =
  1062. when defined(nimpretty):
  1063. inc p.em.keepIndents
  1064. body
  1065. when defined(nimpretty):
  1066. dec p.em.keepIndents
  1067. proc parseExpr(p: var Parser): PNode =
  1068. #| expr = (blockExpr
  1069. #| | ifExpr
  1070. #| | whenExpr
  1071. #| | caseStmt
  1072. #| | forExpr
  1073. #| | tryExpr)
  1074. #| / simpleExpr
  1075. case p.tok.tokType
  1076. of tkBlock:
  1077. nimprettyDontTouch:
  1078. result = parseBlock(p)
  1079. of tkIf:
  1080. nimprettyDontTouch:
  1081. result = parseIfExpr(p, nkIfExpr)
  1082. of tkFor:
  1083. nimprettyDontTouch:
  1084. result = parseFor(p)
  1085. of tkWhen:
  1086. nimprettyDontTouch:
  1087. result = parseIfExpr(p, nkWhenExpr)
  1088. of tkCase:
  1089. # Currently we think nimpretty is good enough with case expressions,
  1090. # so it is allowed to touch them:
  1091. #nimprettyDontTouch:
  1092. result = parseCase(p)
  1093. of tkTry:
  1094. nimprettyDontTouch:
  1095. result = parseTry(p, isExpr=true)
  1096. else: result = simpleExpr(p)
  1097. proc parseEnum(p: var Parser): PNode
  1098. proc parseObject(p: var Parser): PNode
  1099. proc parseTypeClass(p: var Parser): PNode
  1100. proc primary(p: var Parser, mode: PrimaryMode): PNode =
  1101. #| typeKeyw = 'var' | 'out' | 'ref' | 'ptr' | 'shared' | 'tuple'
  1102. #| | 'proc' | 'iterator' | 'distinct' | 'object' | 'enum'
  1103. #| primary = typeKeyw optInd typeDesc
  1104. #| / prefixOperator* identOrLiteral primarySuffix*
  1105. #| / 'bind' primary
  1106. if isOperator(p.tok):
  1107. let isSigil = isSigilLike(p.tok)
  1108. result = newNodeP(nkPrefix, p)
  1109. var a = newIdentNodeP(p.tok.ident, p)
  1110. result.add(a)
  1111. getTok(p)
  1112. optInd(p, a)
  1113. if isSigil:
  1114. #XXX prefix operators
  1115. let baseInd = p.lex.currLineIndent
  1116. result.add(primary(p, pmSkipSuffix))
  1117. result = primarySuffix(p, result, baseInd, mode)
  1118. else:
  1119. result.add(primary(p, pmNormal))
  1120. return
  1121. case p.tok.tokType
  1122. of tkTuple: result = parseTuple(p, mode == pmTypeDef)
  1123. of tkProc: result = parseProcExpr(p, mode notin {pmTypeDesc, pmTypeDef}, nkLambda)
  1124. of tkFunc: result = parseProcExpr(p, mode notin {pmTypeDesc, pmTypeDef}, nkFuncDef)
  1125. of tkIterator:
  1126. result = parseProcExpr(p, mode notin {pmTypeDesc, pmTypeDef}, nkLambda)
  1127. if result.kind == nkLambda: result.transitionSonsKind(nkIteratorDef)
  1128. else: result.transitionSonsKind(nkIteratorTy)
  1129. of tkEnum:
  1130. if mode == pmTypeDef:
  1131. prettySection:
  1132. result = parseEnum(p)
  1133. else:
  1134. result = newNodeP(nkEnumTy, p)
  1135. getTok(p)
  1136. of tkObject:
  1137. if mode == pmTypeDef:
  1138. prettySection:
  1139. result = parseObject(p)
  1140. else:
  1141. result = newNodeP(nkObjectTy, p)
  1142. getTok(p)
  1143. of tkConcept:
  1144. if mode == pmTypeDef:
  1145. result = parseTypeClass(p)
  1146. else:
  1147. parMessage(p, "the 'concept' keyword is only valid in 'type' sections")
  1148. of tkBind:
  1149. result = newNodeP(nkBind, p)
  1150. getTok(p)
  1151. optInd(p, result)
  1152. result.add(primary(p, pmNormal))
  1153. of tkVar: result = parseTypeDescKAux(p, nkVarTy, mode)
  1154. of tkOut:
  1155. # I like this parser extension to be in 1.4 as it still might turn out
  1156. # useful in the long run.
  1157. result = parseTypeDescKAux(p, nkMutableTy, mode)
  1158. of tkRef: result = parseTypeDescKAux(p, nkRefTy, mode)
  1159. of tkPtr: result = parseTypeDescKAux(p, nkPtrTy, mode)
  1160. of tkDistinct: result = parseTypeDescKAux(p, nkDistinctTy, mode)
  1161. else:
  1162. let baseInd = p.lex.currLineIndent
  1163. result = identOrLiteral(p, mode)
  1164. if mode != pmSkipSuffix:
  1165. result = primarySuffix(p, result, baseInd, mode)
  1166. proc binaryNot(p: var Parser; a: PNode): PNode =
  1167. if p.tok.tokType == tkNot:
  1168. let notOpr = newIdentNodeP(p.tok.ident, p)
  1169. getTok(p)
  1170. optInd(p, notOpr)
  1171. let b = parseExpr(p)
  1172. result = newNodeP(nkInfix, p)
  1173. result.add notOpr
  1174. result.add a
  1175. result.add b
  1176. else:
  1177. result = a
  1178. proc parseTypeDesc(p: var Parser): PNode =
  1179. #| typeDesc = simpleExpr ('not' expr)?
  1180. newlineWasSplitting(p)
  1181. result = simpleExpr(p, pmTypeDesc)
  1182. result = binaryNot(p, result)
  1183. proc parseTypeDefAux(p: var Parser): PNode =
  1184. #| typeDefAux = simpleExpr ('not' expr)?
  1185. #| | 'concept' typeClass
  1186. result = simpleExpr(p, pmTypeDef)
  1187. result = binaryNot(p, result)
  1188. proc makeCall(n: PNode): PNode =
  1189. ## Creates a call if the given node isn't already a call.
  1190. if n.kind in nkCallKinds:
  1191. result = n
  1192. else:
  1193. result = newNodeI(nkCall, n.info)
  1194. result.add n
  1195. proc postExprBlocks(p: var Parser, x: PNode): PNode =
  1196. #| postExprBlocks = ':' stmt? ( IND{=} doBlock
  1197. #| | IND{=} 'of' exprList ':' stmt
  1198. #| | IND{=} 'elif' expr ':' stmt
  1199. #| | IND{=} 'except' exprList ':' stmt
  1200. #| | IND{=} 'else' ':' stmt )*
  1201. result = x
  1202. if p.tok.indent >= 0: return
  1203. var
  1204. openingParams = p.emptyNode
  1205. openingPragmas = p.emptyNode
  1206. if p.tok.tokType == tkDo:
  1207. getTok(p)
  1208. openingParams = parseParamList(p, retColon=false)
  1209. openingPragmas = optPragmas(p)
  1210. if p.tok.tokType == tkColon:
  1211. result = makeCall(result)
  1212. getTok(p)
  1213. skipComment(p, result)
  1214. if p.tok.tokType notin {tkOf, tkElif, tkElse, tkExcept}:
  1215. var stmtList = newNodeP(nkStmtList, p)
  1216. stmtList.add parseStmt(p)
  1217. # to keep backwards compatibility (see tests/vm/tstringnil)
  1218. if stmtList[0].kind == nkStmtList: stmtList = stmtList[0]
  1219. stmtList.flags.incl nfBlockArg
  1220. if openingParams.kind != nkEmpty:
  1221. result.add newProcNode(nkDo, stmtList.info, body = stmtList,
  1222. params = openingParams,
  1223. name = p.emptyNode, pattern = p.emptyNode,
  1224. genericParams = p.emptyNode,
  1225. pragmas = openingPragmas,
  1226. exceptions = p.emptyNode)
  1227. else:
  1228. result.add stmtList
  1229. while sameInd(p):
  1230. var nextBlock: PNode
  1231. let nextToken = p.tok.tokType
  1232. if nextToken == tkDo:
  1233. let info = parLineInfo(p)
  1234. getTok(p)
  1235. nextBlock = parseDoBlock(p, info)
  1236. else:
  1237. case nextToken
  1238. of tkOf:
  1239. nextBlock = newNodeP(nkOfBranch, p)
  1240. exprList(p, tkColon, nextBlock)
  1241. of tkElif:
  1242. nextBlock = newNodeP(nkElifBranch, p)
  1243. getTok(p)
  1244. optInd(p, nextBlock)
  1245. nextBlock.add parseExpr(p)
  1246. of tkExcept:
  1247. nextBlock = newNodeP(nkExceptBranch, p)
  1248. exprList(p, tkColon, nextBlock)
  1249. of tkElse:
  1250. nextBlock = newNodeP(nkElse, p)
  1251. getTok(p)
  1252. else: break
  1253. eat(p, tkColon)
  1254. nextBlock.add parseStmt(p)
  1255. nextBlock.flags.incl nfBlockArg
  1256. result.add nextBlock
  1257. if nextBlock.kind == nkElse: break
  1258. else:
  1259. if openingParams.kind != nkEmpty:
  1260. parMessage(p, "expected ':'")
  1261. proc parseExprStmt(p: var Parser): PNode =
  1262. #| exprStmt = simpleExpr
  1263. #| (( '=' optInd expr colonBody? )
  1264. #| / ( expr ^+ comma
  1265. #| postExprBlocks
  1266. #| ))?
  1267. var a = simpleExpr(p)
  1268. if p.tok.tokType == tkEquals:
  1269. result = newNodeP(nkAsgn, p)
  1270. getTok(p)
  1271. optInd(p, result)
  1272. var b = parseExpr(p)
  1273. b = postExprBlocks(p, b)
  1274. result.add(a)
  1275. result.add(b)
  1276. else:
  1277. # simpleExpr parsed 'p a' from 'p a, b'?
  1278. var isFirstParam = false
  1279. if p.tok.indent < 0 and p.tok.tokType == tkComma and a.kind == nkCommand:
  1280. result = a
  1281. while true:
  1282. getTok(p)
  1283. optInd(p, result)
  1284. result.add(commandParam(p, isFirstParam, pmNormal))
  1285. if p.tok.tokType != tkComma: break
  1286. elif p.tok.indent < 0 and isExprStart(p):
  1287. result = newTreeI(nkCommand, a.info, a)
  1288. while true:
  1289. result.add(commandParam(p, isFirstParam, pmNormal))
  1290. if p.tok.tokType != tkComma: break
  1291. getTok(p)
  1292. optInd(p, result)
  1293. else:
  1294. result = a
  1295. result = postExprBlocks(p, result)
  1296. proc parseModuleName(p: var Parser, kind: TNodeKind): PNode =
  1297. result = parseExpr(p)
  1298. when false:
  1299. # parseExpr already handles 'as' syntax ...
  1300. if p.tok.tokType == tkAs and kind == nkImportStmt:
  1301. let a = result
  1302. result = newNodeP(nkImportAs, p)
  1303. getTok(p)
  1304. result.add(a)
  1305. result.add(parseExpr(p))
  1306. proc parseImport(p: var Parser, kind: TNodeKind): PNode =
  1307. #| importStmt = 'import' optInd expr
  1308. #| ((comma expr)*
  1309. #| / 'except' optInd (expr ^+ comma))
  1310. #| exportStmt = 'export' optInd expr
  1311. #| ((comma expr)*
  1312. #| / 'except' optInd (expr ^+ comma))
  1313. result = newNodeP(kind, p)
  1314. getTok(p) # skip `import` or `export`
  1315. optInd(p, result)
  1316. var a = parseModuleName(p, kind)
  1317. result.add(a)
  1318. if p.tok.tokType in {tkComma, tkExcept}:
  1319. if p.tok.tokType == tkExcept:
  1320. result.transitionSonsKind(succ(kind))
  1321. getTok(p)
  1322. optInd(p, result)
  1323. while true:
  1324. # was: while p.tok.tokType notin {tkEof, tkSad, tkDed}:
  1325. p.hasProgress = false
  1326. a = parseModuleName(p, kind)
  1327. if a.kind == nkEmpty or not p.hasProgress: break
  1328. result.add(a)
  1329. if p.tok.tokType != tkComma: break
  1330. getTok(p)
  1331. optInd(p, a)
  1332. #expectNl(p)
  1333. proc parseIncludeStmt(p: var Parser): PNode =
  1334. #| includeStmt = 'include' optInd expr ^+ comma
  1335. result = newNodeP(nkIncludeStmt, p)
  1336. getTok(p) # skip `import` or `include`
  1337. optInd(p, result)
  1338. while true:
  1339. # was: while p.tok.tokType notin {tkEof, tkSad, tkDed}:
  1340. p.hasProgress = false
  1341. var a = parseExpr(p)
  1342. if a.kind == nkEmpty or not p.hasProgress: break
  1343. result.add(a)
  1344. if p.tok.tokType != tkComma: break
  1345. getTok(p)
  1346. optInd(p, a)
  1347. #expectNl(p)
  1348. proc parseFromStmt(p: var Parser): PNode =
  1349. #| fromStmt = 'from' expr 'import' optInd expr (comma expr)*
  1350. result = newNodeP(nkFromStmt, p)
  1351. getTok(p) # skip `from`
  1352. optInd(p, result)
  1353. var a = parseModuleName(p, nkImportStmt)
  1354. result.add(a) #optInd(p, a);
  1355. eat(p, tkImport)
  1356. optInd(p, result)
  1357. while true:
  1358. # p.tok.tokType notin {tkEof, tkSad, tkDed}:
  1359. p.hasProgress = false
  1360. a = parseExpr(p)
  1361. if a.kind == nkEmpty or not p.hasProgress: break
  1362. result.add(a)
  1363. if p.tok.tokType != tkComma: break
  1364. getTok(p)
  1365. optInd(p, a)
  1366. #expectNl(p)
  1367. proc parseReturnOrRaise(p: var Parser, kind: TNodeKind): PNode =
  1368. #| returnStmt = 'return' optInd expr?
  1369. #| raiseStmt = 'raise' optInd expr?
  1370. #| yieldStmt = 'yield' optInd expr?
  1371. #| discardStmt = 'discard' optInd expr?
  1372. #| breakStmt = 'break' optInd expr?
  1373. #| continueStmt = 'break' optInd expr?
  1374. result = newNodeP(kind, p)
  1375. getTok(p)
  1376. if p.tok.tokType == tkComment:
  1377. skipComment(p, result)
  1378. result.add(p.emptyNode)
  1379. elif p.tok.indent >= 0 and p.tok.indent <= p.currInd or not isExprStart(p):
  1380. # NL terminates:
  1381. result.add(p.emptyNode)
  1382. # nimpretty here!
  1383. else:
  1384. var e = parseExpr(p)
  1385. e = postExprBlocks(p, e)
  1386. result.add(e)
  1387. proc parseIfOrWhen(p: var Parser, kind: TNodeKind): PNode =
  1388. #| condStmt = expr colcom stmt COMMENT?
  1389. #| (IND{=} 'elif' expr colcom stmt)*
  1390. #| (IND{=} 'else' colcom stmt)?
  1391. #| ifStmt = 'if' condStmt
  1392. #| whenStmt = 'when' condStmt
  1393. result = newNodeP(kind, p)
  1394. while true:
  1395. getTok(p) # skip `if`, `when`, `elif`
  1396. var branch = newNodeP(nkElifBranch, p)
  1397. optInd(p, branch)
  1398. branch.add(parseExpr(p))
  1399. colcom(p, branch)
  1400. branch.add(parseStmt(p))
  1401. skipComment(p, branch)
  1402. result.add(branch)
  1403. if p.tok.tokType != tkElif or not sameOrNoInd(p): break
  1404. if p.tok.tokType == tkElse and sameOrNoInd(p):
  1405. var branch = newNodeP(nkElse, p)
  1406. eat(p, tkElse)
  1407. colcom(p, branch)
  1408. branch.add(parseStmt(p))
  1409. result.add(branch)
  1410. proc parseIfExpr(p: var Parser, kind: TNodeKind): PNode =
  1411. #| condExpr = expr colcom expr optInd
  1412. #| ('elif' expr colcom expr optInd)*
  1413. #| 'else' colcom expr
  1414. #| ifExpr = 'if' condExpr
  1415. #| whenExpr = 'when' condExpr
  1416. result = newNodeP(kind, p)
  1417. while true:
  1418. getTok(p) # skip `if`, `when`, `elif`
  1419. var branch = newNodeP(nkElifExpr, p)
  1420. optInd(p, branch)
  1421. branch.add(parseExpr(p))
  1422. colcom(p, branch)
  1423. branch.add(parseStmt(p))
  1424. skipComment(p, branch)
  1425. result.add(branch)
  1426. if p.tok.tokType != tkElif: break
  1427. if p.tok.tokType == tkElse:
  1428. var branch = newNodeP(nkElseExpr, p)
  1429. eat(p, tkElse)
  1430. colcom(p, branch)
  1431. branch.add(parseStmt(p))
  1432. result.add(branch)
  1433. proc parseWhile(p: var Parser): PNode =
  1434. #| whileStmt = 'while' expr colcom stmt
  1435. result = newNodeP(nkWhileStmt, p)
  1436. getTok(p)
  1437. optInd(p, result)
  1438. result.add(parseExpr(p))
  1439. colcom(p, result)
  1440. result.add(parseStmt(p))
  1441. proc parseCase(p: var Parser): PNode =
  1442. #| ofBranch = 'of' exprList colcom stmt
  1443. #| ofBranches = ofBranch (IND{=} ofBranch)*
  1444. #| (IND{=} 'elif' expr colcom stmt)*
  1445. #| (IND{=} 'else' colcom stmt)?
  1446. #| caseStmt = 'case' expr ':'? COMMENT?
  1447. #| (IND{>} ofBranches DED
  1448. #| | IND{=} ofBranches)
  1449. var
  1450. b: PNode
  1451. inElif = false
  1452. wasIndented = false
  1453. result = newNodeP(nkCaseStmt, p)
  1454. getTok(p)
  1455. result.add(parseExpr(p))
  1456. if p.tok.tokType == tkColon: getTok(p)
  1457. skipComment(p, result)
  1458. let oldInd = p.currInd
  1459. if realInd(p):
  1460. p.currInd = p.tok.indent
  1461. wasIndented = true
  1462. while sameInd(p):
  1463. case p.tok.tokType
  1464. of tkOf:
  1465. if inElif: break
  1466. b = newNodeP(nkOfBranch, p)
  1467. exprList(p, tkColon, b)
  1468. of tkElif:
  1469. inElif = true
  1470. b = newNodeP(nkElifBranch, p)
  1471. getTok(p)
  1472. optInd(p, b)
  1473. b.add(parseExpr(p))
  1474. of tkElse:
  1475. b = newNodeP(nkElse, p)
  1476. getTok(p)
  1477. else: break
  1478. colcom(p, b)
  1479. b.add(parseStmt(p))
  1480. result.add(b)
  1481. if b.kind == nkElse: break
  1482. if wasIndented:
  1483. p.currInd = oldInd
  1484. proc parseTry(p: var Parser; isExpr: bool): PNode =
  1485. #| tryStmt = 'try' colcom stmt &(IND{=}? 'except'|'finally')
  1486. #| (IND{=}? 'except' exprList colcom stmt)*
  1487. #| (IND{=}? 'finally' colcom stmt)?
  1488. #| tryExpr = 'try' colcom stmt &(optInd 'except'|'finally')
  1489. #| (optInd 'except' exprList colcom stmt)*
  1490. #| (optInd 'finally' colcom stmt)?
  1491. result = newNodeP(nkTryStmt, p)
  1492. getTok(p)
  1493. colcom(p, result)
  1494. result.add(parseStmt(p))
  1495. var b: PNode = nil
  1496. while sameOrNoInd(p) or isExpr:
  1497. case p.tok.tokType
  1498. of tkExcept:
  1499. b = newNodeP(nkExceptBranch, p)
  1500. exprList(p, tkColon, b)
  1501. of tkFinally:
  1502. b = newNodeP(nkFinally, p)
  1503. getTok(p)
  1504. else: break
  1505. colcom(p, b)
  1506. b.add(parseStmt(p))
  1507. result.add(b)
  1508. if b == nil: parMessage(p, "expected 'except'")
  1509. proc parseExceptBlock(p: var Parser, kind: TNodeKind): PNode =
  1510. #| exceptBlock = 'except' colcom stmt
  1511. result = newNodeP(kind, p)
  1512. getTok(p)
  1513. colcom(p, result)
  1514. result.add(parseStmt(p))
  1515. proc parseBlock(p: var Parser): PNode =
  1516. #| blockStmt = 'block' symbol? colcom stmt
  1517. #| blockExpr = 'block' symbol? colcom stmt
  1518. result = newNodeP(nkBlockStmt, p)
  1519. getTokNoInd(p)
  1520. if p.tok.tokType == tkColon: result.add(p.emptyNode)
  1521. else: result.add(parseSymbol(p))
  1522. colcom(p, result)
  1523. result.add(parseStmt(p))
  1524. proc parseStaticOrDefer(p: var Parser; k: TNodeKind): PNode =
  1525. #| staticStmt = 'static' colcom stmt
  1526. #| deferStmt = 'defer' colcom stmt
  1527. result = newNodeP(k, p)
  1528. getTok(p)
  1529. colcom(p, result)
  1530. result.add(parseStmt(p))
  1531. proc parseAsm(p: var Parser): PNode =
  1532. #| asmStmt = 'asm' pragma? (STR_LIT | RSTR_LIT | TRIPLESTR_LIT)
  1533. result = newNodeP(nkAsmStmt, p)
  1534. getTokNoInd(p)
  1535. if p.tok.tokType == tkCurlyDotLe: result.add(parsePragma(p))
  1536. else: result.add(p.emptyNode)
  1537. case p.tok.tokType
  1538. of tkStrLit: result.add(newStrNodeP(nkStrLit, p.tok.literal, p))
  1539. of tkRStrLit: result.add(newStrNodeP(nkRStrLit, p.tok.literal, p))
  1540. of tkTripleStrLit: result.add(newStrNodeP(nkTripleStrLit, p.tok.literal, p))
  1541. else:
  1542. parMessage(p, "the 'asm' statement takes a string literal")
  1543. result.add(p.emptyNode)
  1544. return
  1545. getTok(p)
  1546. proc parseGenericParam(p: var Parser): PNode =
  1547. #| genericParam = symbol (comma symbol)* (colon expr)? ('=' optInd expr)?
  1548. var a: PNode
  1549. result = newNodeP(nkIdentDefs, p)
  1550. # progress guaranteed
  1551. while true:
  1552. case p.tok.tokType
  1553. of tkIn, tkOut:
  1554. let x = p.lex.cache.getIdent(if p.tok.tokType == tkIn: "in" else: "out")
  1555. a = newNodeP(nkPrefix, p)
  1556. a.add newIdentNodeP(x, p)
  1557. getTok(p)
  1558. expectIdent(p)
  1559. a.add(parseSymbol(p))
  1560. of tkSymbol, tkAccent:
  1561. a = parseSymbol(p)
  1562. if a.kind == nkEmpty: return
  1563. else: break
  1564. result.add(a)
  1565. if p.tok.tokType != tkComma: break
  1566. getTok(p)
  1567. optInd(p, a)
  1568. if p.tok.tokType == tkColon:
  1569. getTok(p)
  1570. optInd(p, result)
  1571. result.add(parseExpr(p))
  1572. else:
  1573. result.add(p.emptyNode)
  1574. if p.tok.tokType == tkEquals:
  1575. getTok(p)
  1576. optInd(p, result)
  1577. result.add(parseExpr(p))
  1578. else:
  1579. result.add(p.emptyNode)
  1580. proc parseGenericParamList(p: var Parser): PNode =
  1581. #| genericParamList = '[' optInd
  1582. #| genericParam ^* (comma/semicolon) optPar ']'
  1583. result = newNodeP(nkGenericParams, p)
  1584. getTok(p)
  1585. optInd(p, result)
  1586. # progress guaranteed
  1587. while p.tok.tokType in {tkSymbol, tkAccent, tkIn, tkOut}:
  1588. var a = parseGenericParam(p)
  1589. result.add(a)
  1590. if p.tok.tokType notin {tkComma, tkSemiColon}: break
  1591. when defined(nimpretty):
  1592. commaWasSemicolon(p.em)
  1593. getTok(p)
  1594. skipComment(p, a)
  1595. optPar(p)
  1596. eat(p, tkBracketRi)
  1597. proc parsePattern(p: var Parser): PNode =
  1598. #| pattern = '{' stmt '}'
  1599. eat(p, tkCurlyLe)
  1600. result = parseStmt(p)
  1601. eat(p, tkCurlyRi)
  1602. proc parseRoutine(p: var Parser, kind: TNodeKind): PNode =
  1603. #| indAndComment = (IND{>} COMMENT)? | COMMENT?
  1604. #| routine = optInd identVis pattern? genericParamList?
  1605. #| paramListColon pragma? ('=' COMMENT? stmt)? indAndComment
  1606. result = newNodeP(kind, p)
  1607. getTok(p)
  1608. optInd(p, result)
  1609. result.add(identVis(p))
  1610. if p.tok.tokType == tkCurlyLe and p.validInd: result.add(p.parsePattern)
  1611. else: result.add(p.emptyNode)
  1612. if p.tok.tokType == tkBracketLe and p.validInd:
  1613. result.add(p.parseGenericParamList)
  1614. else:
  1615. result.add(p.emptyNode)
  1616. result.add(p.parseParamList)
  1617. if p.tok.tokType == tkCurlyDotLe and p.validInd: result.add(p.parsePragma)
  1618. else: result.add(p.emptyNode)
  1619. # empty exception tracking:
  1620. result.add(p.emptyNode)
  1621. if p.tok.tokType == tkEquals and p.validInd:
  1622. getTok(p)
  1623. skipComment(p, result)
  1624. result.add(parseStmt(p))
  1625. else:
  1626. result.add(p.emptyNode)
  1627. indAndComment(p, result)
  1628. proc newCommentStmt(p: var Parser): PNode =
  1629. #| commentStmt = COMMENT
  1630. result = newNodeP(nkCommentStmt, p)
  1631. result.comment = p.tok.literal
  1632. getTok(p)
  1633. proc parseSection(p: var Parser, kind: TNodeKind,
  1634. defparser: proc (p: var Parser): PNode {.nimcall.}): PNode =
  1635. #| section(RULE) = COMMENT? RULE / (IND{>} (RULE / COMMENT)^+IND{=} DED)
  1636. result = newNodeP(kind, p)
  1637. if kind != nkTypeSection: getTok(p)
  1638. skipComment(p, result)
  1639. if realInd(p):
  1640. withInd(p):
  1641. skipComment(p, result)
  1642. # progress guaranteed
  1643. while sameInd(p):
  1644. case p.tok.tokType
  1645. of tkSymbol, tkAccent, tkParLe:
  1646. var a = defparser(p)
  1647. skipComment(p, a)
  1648. result.add(a)
  1649. of tkComment:
  1650. var a = newCommentStmt(p)
  1651. result.add(a)
  1652. else:
  1653. parMessage(p, errIdentifierExpected, p.tok)
  1654. break
  1655. if result.len == 0: parMessage(p, errIdentifierExpected, p.tok)
  1656. elif p.tok.tokType in {tkSymbol, tkAccent, tkParLe} and p.tok.indent < 0:
  1657. # tkParLe is allowed for ``var (x, y) = ...`` tuple parsing
  1658. result.add(defparser(p))
  1659. else:
  1660. parMessage(p, errIdentifierExpected, p.tok)
  1661. proc parseEnum(p: var Parser): PNode =
  1662. #| enum = 'enum' optInd (symbol pragma? optInd ('=' optInd expr COMMENT?)? comma?)+
  1663. result = newNodeP(nkEnumTy, p)
  1664. getTok(p)
  1665. result.add(p.emptyNode)
  1666. optInd(p, result)
  1667. flexComment(p, result)
  1668. # progress guaranteed
  1669. while true:
  1670. var a = parseSymbol(p)
  1671. if a.kind == nkEmpty: return
  1672. var symPragma = a
  1673. var pragma: PNode
  1674. if p.tok.tokType == tkCurlyDotLe:
  1675. pragma = optPragmas(p)
  1676. symPragma = newNodeP(nkPragmaExpr, p)
  1677. symPragma.add(a)
  1678. symPragma.add(pragma)
  1679. # nimpretty support here
  1680. if p.tok.indent >= 0 and p.tok.indent <= p.currInd:
  1681. result.add(symPragma)
  1682. break
  1683. if p.tok.tokType == tkEquals and p.tok.indent < 0:
  1684. getTok(p)
  1685. optInd(p, symPragma)
  1686. var b = symPragma
  1687. symPragma = newNodeP(nkEnumFieldDef, p)
  1688. symPragma.add(b)
  1689. symPragma.add(parseExpr(p))
  1690. if p.tok.indent < 0 or p.tok.indent >= p.currInd:
  1691. rawSkipComment(p, symPragma)
  1692. if p.tok.tokType == tkComma and p.tok.indent < 0:
  1693. getTok(p)
  1694. rawSkipComment(p, symPragma)
  1695. else:
  1696. if p.tok.indent < 0 or p.tok.indent >= p.currInd:
  1697. rawSkipComment(p, symPragma)
  1698. result.add(symPragma)
  1699. if p.tok.indent >= 0 and p.tok.indent <= p.currInd or
  1700. p.tok.tokType == tkEof:
  1701. break
  1702. if result.len <= 1:
  1703. parMessage(p, errIdentifierExpected, p.tok)
  1704. proc parseObjectPart(p: var Parser): PNode
  1705. proc parseObjectWhen(p: var Parser): PNode =
  1706. #| objectWhen = 'when' expr colcom objectPart COMMENT?
  1707. #| ('elif' expr colcom objectPart COMMENT?)*
  1708. #| ('else' colcom objectPart COMMENT?)?
  1709. result = newNodeP(nkRecWhen, p)
  1710. # progress guaranteed
  1711. while sameInd(p):
  1712. getTok(p) # skip `when`, `elif`
  1713. var branch = newNodeP(nkElifBranch, p)
  1714. optInd(p, branch)
  1715. branch.add(parseExpr(p))
  1716. colcom(p, branch)
  1717. branch.add(parseObjectPart(p))
  1718. flexComment(p, branch)
  1719. result.add(branch)
  1720. if p.tok.tokType != tkElif: break
  1721. if p.tok.tokType == tkElse and sameInd(p):
  1722. var branch = newNodeP(nkElse, p)
  1723. eat(p, tkElse)
  1724. colcom(p, branch)
  1725. branch.add(parseObjectPart(p))
  1726. flexComment(p, branch)
  1727. result.add(branch)
  1728. proc parseObjectCase(p: var Parser): PNode =
  1729. #| objectBranch = 'of' exprList colcom objectPart
  1730. #| objectBranches = objectBranch (IND{=} objectBranch)*
  1731. #| (IND{=} 'elif' expr colcom objectPart)*
  1732. #| (IND{=} 'else' colcom objectPart)?
  1733. #| objectCase = 'case' identWithPragma ':' typeDesc ':'? COMMENT?
  1734. #| (IND{>} objectBranches DED
  1735. #| | IND{=} objectBranches)
  1736. result = newNodeP(nkRecCase, p)
  1737. getTokNoInd(p)
  1738. var a = newNodeP(nkIdentDefs, p)
  1739. a.add(identWithPragma(p))
  1740. eat(p, tkColon)
  1741. a.add(parseTypeDesc(p))
  1742. a.add(p.emptyNode)
  1743. result.add(a)
  1744. if p.tok.tokType == tkColon: getTok(p)
  1745. flexComment(p, result)
  1746. var wasIndented = false
  1747. let oldInd = p.currInd
  1748. if realInd(p):
  1749. p.currInd = p.tok.indent
  1750. wasIndented = true
  1751. # progress guaranteed
  1752. while sameInd(p):
  1753. var b: PNode
  1754. case p.tok.tokType
  1755. of tkOf:
  1756. b = newNodeP(nkOfBranch, p)
  1757. exprList(p, tkColon, b)
  1758. of tkElse:
  1759. b = newNodeP(nkElse, p)
  1760. getTok(p)
  1761. else: break
  1762. colcom(p, b)
  1763. var fields = parseObjectPart(p)
  1764. if fields.kind == nkEmpty:
  1765. parMessage(p, errIdentifierExpected, p.tok)
  1766. fields = newNodeP(nkNilLit, p) # don't break further semantic checking
  1767. b.add(fields)
  1768. result.add(b)
  1769. if b.kind == nkElse: break
  1770. if wasIndented:
  1771. p.currInd = oldInd
  1772. proc parseObjectPart(p: var Parser): PNode =
  1773. #| objectPart = IND{>} objectPart^+IND{=} DED
  1774. #| / objectWhen / objectCase / 'nil' / 'discard' / declColonEquals
  1775. if realInd(p):
  1776. result = newNodeP(nkRecList, p)
  1777. withInd(p):
  1778. rawSkipComment(p, result)
  1779. while sameInd(p):
  1780. case p.tok.tokType
  1781. of tkCase, tkWhen, tkSymbol, tkAccent, tkNil, tkDiscard:
  1782. result.add(parseObjectPart(p))
  1783. else:
  1784. parMessage(p, errIdentifierExpected, p.tok)
  1785. break
  1786. elif sameOrNoInd(p):
  1787. case p.tok.tokType
  1788. of tkWhen:
  1789. result = parseObjectWhen(p)
  1790. of tkCase:
  1791. result = parseObjectCase(p)
  1792. of tkSymbol, tkAccent:
  1793. result = parseIdentColonEquals(p, {withPragma})
  1794. if p.tok.indent < 0 or p.tok.indent >= p.currInd:
  1795. rawSkipComment(p, result)
  1796. of tkNil, tkDiscard:
  1797. result = newNodeP(nkNilLit, p)
  1798. getTok(p)
  1799. else:
  1800. result = p.emptyNode
  1801. else:
  1802. result = p.emptyNode
  1803. proc parseObject(p: var Parser): PNode =
  1804. #| object = 'object' pragma? ('of' typeDesc)? COMMENT? objectPart
  1805. result = newNodeP(nkObjectTy, p)
  1806. getTok(p)
  1807. if p.tok.tokType == tkCurlyDotLe and p.validInd:
  1808. # Deprecated since v0.20.0
  1809. parMessage(p, warnDeprecated, "type pragmas follow the type name; this form of writing pragmas is deprecated")
  1810. result.add(parsePragma(p))
  1811. else:
  1812. result.add(p.emptyNode)
  1813. if p.tok.tokType == tkOf and p.tok.indent < 0:
  1814. var a = newNodeP(nkOfInherit, p)
  1815. getTok(p)
  1816. a.add(parseTypeDesc(p))
  1817. result.add(a)
  1818. else:
  1819. result.add(p.emptyNode)
  1820. if p.tok.tokType == tkComment:
  1821. skipComment(p, result)
  1822. # an initial IND{>} HAS to follow:
  1823. if not realInd(p):
  1824. result.add(p.emptyNode)
  1825. else:
  1826. result.add(parseObjectPart(p))
  1827. proc parseTypeClassParam(p: var Parser): PNode =
  1828. let modifier = case p.tok.tokType
  1829. of tkOut, tkVar: nkVarTy
  1830. of tkPtr: nkPtrTy
  1831. of tkRef: nkRefTy
  1832. of tkStatic: nkStaticTy
  1833. of tkType: nkTypeOfExpr
  1834. else: nkEmpty
  1835. if modifier != nkEmpty:
  1836. result = newNodeP(modifier, p)
  1837. getTok(p)
  1838. result.add(p.parseSymbol)
  1839. else:
  1840. result = p.parseSymbol
  1841. proc parseTypeClass(p: var Parser): PNode =
  1842. #| typeClassParam = ('var' | 'out')? symbol
  1843. #| typeClass = typeClassParam ^* ',' (pragma)? ('of' typeDesc ^* ',')?
  1844. #| &IND{>} stmt
  1845. result = newNodeP(nkTypeClassTy, p)
  1846. getTok(p)
  1847. var args = newNodeP(nkArgList, p)
  1848. result.add(args)
  1849. args.add(p.parseTypeClassParam)
  1850. while p.tok.tokType == tkComma:
  1851. getTok(p)
  1852. args.add(p.parseTypeClassParam)
  1853. if p.tok.tokType == tkCurlyDotLe and p.validInd:
  1854. result.add(parsePragma(p))
  1855. else:
  1856. result.add(p.emptyNode)
  1857. if p.tok.tokType == tkOf and p.tok.indent < 0:
  1858. var a = newNodeP(nkOfInherit, p)
  1859. getTok(p)
  1860. # progress guaranteed
  1861. while true:
  1862. a.add(parseTypeDesc(p))
  1863. if p.tok.tokType != tkComma: break
  1864. getTok(p)
  1865. result.add(a)
  1866. else:
  1867. result.add(p.emptyNode)
  1868. if p.tok.tokType == tkComment:
  1869. skipComment(p, result)
  1870. # an initial IND{>} HAS to follow:
  1871. if not realInd(p):
  1872. result.add(p.emptyNode)
  1873. else:
  1874. result.add(parseStmt(p))
  1875. proc parseTypeDef(p: var Parser): PNode =
  1876. #|
  1877. #| typeDef = identWithPragmaDot genericParamList? '=' optInd typeDefAux
  1878. #| indAndComment? / identVisDot genericParamList? pragma '=' optInd typeDefAux
  1879. #| indAndComment?
  1880. result = newNodeP(nkTypeDef, p)
  1881. var identifier = identVis(p, allowDot=true)
  1882. var identPragma = identifier
  1883. var pragma: PNode
  1884. var genericParam: PNode
  1885. var noPragmaYet = true
  1886. if p.tok.tokType == tkCurlyDotLe:
  1887. pragma = optPragmas(p)
  1888. identPragma = newNodeP(nkPragmaExpr, p)
  1889. identPragma.add(identifier)
  1890. identPragma.add(pragma)
  1891. noPragmaYet = false
  1892. if p.tok.tokType == tkBracketLe and p.validInd:
  1893. if not noPragmaYet:
  1894. # Deprecated since v0.20.0
  1895. parMessage(p, warnDeprecated, "pragma before generic parameter list is deprecated")
  1896. genericParam = parseGenericParamList(p)
  1897. else:
  1898. genericParam = p.emptyNode
  1899. if noPragmaYet:
  1900. pragma = optPragmas(p)
  1901. if pragma.kind != nkEmpty:
  1902. identPragma = newNodeP(nkPragmaExpr, p)
  1903. identPragma.add(identifier)
  1904. identPragma.add(pragma)
  1905. elif p.tok.tokType == tkCurlyDotLe:
  1906. parMessage(p, errGenerated, "pragma already present")
  1907. result.add(identPragma)
  1908. result.add(genericParam)
  1909. if p.tok.tokType == tkEquals:
  1910. result.info = parLineInfo(p)
  1911. getTok(p)
  1912. optInd(p, result)
  1913. result.add(parseTypeDefAux(p))
  1914. else:
  1915. result.add(p.emptyNode)
  1916. indAndComment(p, result) # special extension!
  1917. proc parseVarTuple(p: var Parser): PNode =
  1918. #| varTuple = '(' optInd identWithPragma ^+ comma optPar ')' '=' optInd expr
  1919. result = newNodeP(nkVarTuple, p)
  1920. getTok(p) # skip '('
  1921. optInd(p, result)
  1922. # progress guaranteed
  1923. while p.tok.tokType in {tkSymbol, tkAccent}:
  1924. var a = identWithPragma(p, allowDot=true)
  1925. result.add(a)
  1926. if p.tok.tokType != tkComma: break
  1927. getTok(p)
  1928. skipComment(p, a)
  1929. result.add(p.emptyNode) # no type desc
  1930. optPar(p)
  1931. eat(p, tkParRi)
  1932. proc parseVariable(p: var Parser): PNode =
  1933. #| colonBody = colcom stmt postExprBlocks?
  1934. #| variable = (varTuple / identColonEquals) colonBody? indAndComment
  1935. if p.tok.tokType == tkParLe:
  1936. result = parseVarTuple(p)
  1937. eat(p, tkEquals)
  1938. optInd(p, result)
  1939. result.add(parseExpr(p))
  1940. else: result = parseIdentColonEquals(p, {withPragma, withDot})
  1941. result[^1] = postExprBlocks(p, result[^1])
  1942. indAndComment(p, result)
  1943. proc parseConstant(p: var Parser): PNode =
  1944. #| constant = (varTuple / identWithPragma) (colon typeDesc)? '=' optInd expr indAndComment
  1945. if p.tok.tokType == tkParLe: result = parseVarTuple(p)
  1946. else:
  1947. result = newNodeP(nkConstDef, p)
  1948. result.add(identWithPragma(p))
  1949. if p.tok.tokType == tkColon:
  1950. getTok(p)
  1951. optInd(p, result)
  1952. result.add(parseTypeDesc(p))
  1953. else:
  1954. result.add(p.emptyNode)
  1955. eat(p, tkEquals)
  1956. optInd(p, result)
  1957. #add(result, parseStmtListExpr(p))
  1958. result.add(parseExpr(p))
  1959. result[^1] = postExprBlocks(p, result[^1])
  1960. indAndComment(p, result)
  1961. proc parseBind(p: var Parser, k: TNodeKind): PNode =
  1962. #| bindStmt = 'bind' optInd qualifiedIdent ^+ comma
  1963. #| mixinStmt = 'mixin' optInd qualifiedIdent ^+ comma
  1964. result = newNodeP(k, p)
  1965. getTok(p)
  1966. optInd(p, result)
  1967. # progress guaranteed
  1968. while true:
  1969. var a = qualifiedIdent(p)
  1970. result.add(a)
  1971. if p.tok.tokType != tkComma: break
  1972. getTok(p)
  1973. optInd(p, a)
  1974. #expectNl(p)
  1975. proc parseStmtPragma(p: var Parser): PNode =
  1976. #| pragmaStmt = pragma (':' COMMENT? stmt)?
  1977. result = parsePragma(p)
  1978. if p.tok.tokType == tkColon and p.tok.indent < 0:
  1979. let a = result
  1980. result = newNodeI(nkPragmaBlock, a.info)
  1981. getTok(p)
  1982. skipComment(p, result)
  1983. result.add a
  1984. result.add parseStmt(p)
  1985. proc simpleStmt(p: var Parser): PNode =
  1986. #| simpleStmt = ((returnStmt | raiseStmt | yieldStmt | discardStmt | breakStmt
  1987. #| | continueStmt | pragmaStmt | importStmt | exportStmt | fromStmt
  1988. #| | includeStmt | commentStmt) / exprStmt) COMMENT?
  1989. #|
  1990. case p.tok.tokType
  1991. of tkReturn: result = parseReturnOrRaise(p, nkReturnStmt)
  1992. of tkRaise: result = parseReturnOrRaise(p, nkRaiseStmt)
  1993. of tkYield: result = parseReturnOrRaise(p, nkYieldStmt)
  1994. of tkDiscard: result = parseReturnOrRaise(p, nkDiscardStmt)
  1995. of tkBreak: result = parseReturnOrRaise(p, nkBreakStmt)
  1996. of tkContinue: result = parseReturnOrRaise(p, nkContinueStmt)
  1997. of tkCurlyDotLe: result = parseStmtPragma(p)
  1998. of tkImport: result = parseImport(p, nkImportStmt)
  1999. of tkExport: result = parseImport(p, nkExportStmt)
  2000. of tkFrom: result = parseFromStmt(p)
  2001. of tkInclude: result = parseIncludeStmt(p)
  2002. of tkComment: result = newCommentStmt(p)
  2003. else:
  2004. if isExprStart(p): result = parseExprStmt(p)
  2005. else: result = p.emptyNode
  2006. if result.kind notin {nkEmpty, nkCommentStmt}: skipComment(p, result)
  2007. proc complexOrSimpleStmt(p: var Parser): PNode =
  2008. #| complexOrSimpleStmt = (ifStmt | whenStmt | whileStmt
  2009. #| | tryStmt | forStmt
  2010. #| | blockStmt | staticStmt | deferStmt | asmStmt
  2011. #| | 'proc' routine
  2012. #| | 'method' routine
  2013. #| | 'func' routine
  2014. #| | 'iterator' routine
  2015. #| | 'macro' routine
  2016. #| | 'template' routine
  2017. #| | 'converter' routine
  2018. #| | 'type' section(typeDef)
  2019. #| | 'const' section(constant)
  2020. #| | ('let' | 'var' | 'using') section(variable)
  2021. #| | bindStmt | mixinStmt)
  2022. #| / simpleStmt
  2023. case p.tok.tokType
  2024. of tkIf: result = parseIfOrWhen(p, nkIfStmt)
  2025. of tkWhile: result = parseWhile(p)
  2026. of tkCase: result = parseCase(p)
  2027. of tkTry: result = parseTry(p, isExpr=false)
  2028. of tkFinally: result = parseExceptBlock(p, nkFinally)
  2029. of tkExcept: result = parseExceptBlock(p, nkExceptBranch)
  2030. of tkFor: result = parseFor(p)
  2031. of tkBlock: result = parseBlock(p)
  2032. of tkStatic: result = parseStaticOrDefer(p, nkStaticStmt)
  2033. of tkDefer: result = parseStaticOrDefer(p, nkDefer)
  2034. of tkAsm: result = parseAsm(p)
  2035. of tkProc: result = parseRoutine(p, nkProcDef)
  2036. of tkFunc: result = parseRoutine(p, nkFuncDef)
  2037. of tkMethod: result = parseRoutine(p, nkMethodDef)
  2038. of tkIterator: result = parseRoutine(p, nkIteratorDef)
  2039. of tkMacro: result = parseRoutine(p, nkMacroDef)
  2040. of tkTemplate: result = parseRoutine(p, nkTemplateDef)
  2041. of tkConverter: result = parseRoutine(p, nkConverterDef)
  2042. of tkType:
  2043. getTok(p)
  2044. if p.tok.tokType == tkParLe:
  2045. getTok(p)
  2046. result = newNodeP(nkTypeOfExpr, p)
  2047. result.add(primary(p, pmTypeDesc))
  2048. eat(p, tkParRi)
  2049. result = parseOperators(p, result, -1, pmNormal)
  2050. else:
  2051. result = parseSection(p, nkTypeSection, parseTypeDef)
  2052. of tkConst:
  2053. prettySection:
  2054. result = parseSection(p, nkConstSection, parseConstant)
  2055. of tkLet:
  2056. prettySection:
  2057. result = parseSection(p, nkLetSection, parseVariable)
  2058. of tkVar:
  2059. prettySection:
  2060. result = parseSection(p, nkVarSection, parseVariable)
  2061. of tkWhen: result = parseIfOrWhen(p, nkWhenStmt)
  2062. of tkBind: result = parseBind(p, nkBindStmt)
  2063. of tkMixin: result = parseBind(p, nkMixinStmt)
  2064. of tkUsing: result = parseSection(p, nkUsingStmt, parseVariable)
  2065. else: result = simpleStmt(p)
  2066. proc parseStmt(p: var Parser): PNode =
  2067. #| stmt = (IND{>} complexOrSimpleStmt^+(IND{=} / ';') DED)
  2068. #| / simpleStmt ^+ ';'
  2069. if p.tok.indent > p.currInd:
  2070. # nimpretty support here
  2071. result = newNodeP(nkStmtList, p)
  2072. withInd(p):
  2073. while true:
  2074. if p.tok.indent == p.currInd:
  2075. discard
  2076. elif p.tok.tokType == tkSemiColon:
  2077. getTok(p)
  2078. if p.tok.indent < 0 or p.tok.indent == p.currInd: discard
  2079. else: break
  2080. else:
  2081. if p.tok.indent > p.currInd and p.tok.tokType != tkDot:
  2082. parMessage(p, errInvalidIndentation)
  2083. break
  2084. if p.tok.tokType in {tkCurlyRi, tkParRi, tkCurlyDotRi, tkBracketRi}:
  2085. # XXX this ensures tnamedparamanonproc still compiles;
  2086. # deprecate this syntax later
  2087. break
  2088. p.hasProgress = false
  2089. if p.tok.tokType in {tkElse, tkElif}:
  2090. break # Allow this too, see tests/parser/tifexprs
  2091. result.add complexOrSimpleStmt(p)
  2092. if not p.hasProgress and p.tok.tokType == tkEof: break
  2093. else:
  2094. # the case statement is only needed for better error messages:
  2095. case p.tok.tokType
  2096. of tkIf, tkWhile, tkCase, tkTry, tkFor, tkBlock, tkAsm, tkProc, tkFunc,
  2097. tkIterator, tkMacro, tkType, tkConst, tkWhen, tkVar:
  2098. parMessage(p, "nestable statement requires indentation")
  2099. result = p.emptyNode
  2100. else:
  2101. if p.inSemiStmtList > 0:
  2102. result = simpleStmt(p)
  2103. if result.kind == nkEmpty: parMessage(p, errExprExpected, p.tok)
  2104. else:
  2105. result = newNodeP(nkStmtList, p)
  2106. while true:
  2107. if p.tok.indent >= 0:
  2108. parMessage(p, errInvalidIndentation)
  2109. p.hasProgress = false
  2110. let a = simpleStmt(p)
  2111. let err = not p.hasProgress
  2112. if a.kind == nkEmpty: parMessage(p, errExprExpected, p.tok)
  2113. result.add(a)
  2114. if p.tok.tokType != tkSemiColon: break
  2115. getTok(p)
  2116. if err and p.tok.tokType == tkEof: break
  2117. proc parseAll(p: var Parser): PNode =
  2118. ## Parses the rest of the input stream held by the parser into a PNode.
  2119. result = newNodeP(nkStmtList, p)
  2120. while p.tok.tokType != tkEof:
  2121. p.hasProgress = false
  2122. var a = complexOrSimpleStmt(p)
  2123. if a.kind != nkEmpty and p.hasProgress:
  2124. result.add(a)
  2125. else:
  2126. parMessage(p, errExprExpected, p.tok)
  2127. # bugfix: consume a token here to prevent an endless loop:
  2128. getTok(p)
  2129. if p.tok.indent != 0:
  2130. parMessage(p, errInvalidIndentation)
  2131. proc parseTopLevelStmt(p: var Parser): PNode =
  2132. ## Implements an iterator which, when called repeatedly, returns the next
  2133. ## top-level statement or emptyNode if end of stream.
  2134. result = p.emptyNode
  2135. # progress guaranteed
  2136. while true:
  2137. # nimpretty support here
  2138. if p.tok.indent != 0:
  2139. if p.firstTok and p.tok.indent < 0: discard
  2140. elif p.tok.tokType != tkSemiColon:
  2141. # special casing for better error messages:
  2142. if p.tok.tokType == tkOpr and p.tok.ident.s == "*":
  2143. parMessage(p, errGenerated,
  2144. "invalid indentation; an export marker '*' follows the declared identifier")
  2145. else:
  2146. parMessage(p, errInvalidIndentation)
  2147. p.firstTok = false
  2148. case p.tok.tokType
  2149. of tkSemiColon:
  2150. getTok(p)
  2151. if p.tok.indent <= 0: discard
  2152. else: parMessage(p, errInvalidIndentation)
  2153. p.firstTok = true
  2154. of tkEof: break
  2155. else:
  2156. result = complexOrSimpleStmt(p)
  2157. if result.kind == nkEmpty: parMessage(p, errExprExpected, p.tok)
  2158. break
  2159. proc parseString*(s: string; cache: IdentCache; config: ConfigRef;
  2160. filename: string = ""; line: int = 0;
  2161. errorHandler: ErrorHandler = nil): PNode =
  2162. ## Parses a string into an AST, returning the top node.
  2163. ## `filename` and `line`, although optional, provide info so that the
  2164. ## compiler can generate correct error messages referring to the original
  2165. ## source.
  2166. var stream = llStreamOpen(s)
  2167. stream.lineOffset = line
  2168. var parser: Parser
  2169. parser.lex.errorHandler = errorHandler
  2170. openParser(parser, AbsoluteFile filename, stream, cache, config)
  2171. result = parser.parseAll
  2172. closeParser(parser)