parser.nim 75 KB

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