parser.nim 83 KB

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