parser.odin 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767
  1. package odin_parser
  2. import "core:odin/ast"
  3. import "core:odin/tokenizer"
  4. import "core:fmt"
  5. Warning_Handler :: #type proc(pos: tokenizer.Pos, fmt: string, args: ..any)
  6. Error_Handler :: #type proc(pos: tokenizer.Pos, fmt: string, args: ..any)
  7. Flag :: enum u32 {
  8. Optional_Semicolons,
  9. }
  10. Flags :: distinct bit_set[Flag; u32]
  11. Parser :: struct {
  12. file: ^ast.File,
  13. tok: tokenizer.Tokenizer,
  14. // If .Optional_Semicolons is true, semicolons are completely as statement terminators
  15. // different to .Insert_Semicolon in tok.flags
  16. flags: Flags,
  17. warn: Warning_Handler,
  18. err: Error_Handler,
  19. prev_tok: tokenizer.Token,
  20. curr_tok: tokenizer.Token,
  21. // >= 0: In Expression
  22. // < 0: In Control Clause
  23. // NOTE(bill): Used to prevent type literals in control clauses
  24. expr_level: int,
  25. allow_range: bool, // NOTE(bill): Ranges are only allowed in certain cases
  26. allow_in_expr: bool, // NOTE(bill): in expression are only allowed in certain cases
  27. in_foreign_block: bool,
  28. allow_type: bool,
  29. lead_comment: ^ast.Comment_Group,
  30. line_comment: ^ast.Comment_Group,
  31. curr_proc: ^ast.Node,
  32. error_count: int,
  33. fix_count: int,
  34. fix_prev_pos: tokenizer.Pos,
  35. peeking: bool,
  36. }
  37. MAX_FIX_COUNT :: 10
  38. Stmt_Allow_Flag :: enum {
  39. In,
  40. Label,
  41. }
  42. Stmt_Allow_Flags :: distinct bit_set[Stmt_Allow_Flag]
  43. Import_Decl_Kind :: enum {
  44. Standard,
  45. Using,
  46. }
  47. default_warning_handler :: proc(pos: tokenizer.Pos, msg: string, args: ..any) {
  48. fmt.eprintf("%s(%d:%d): Warning: ", pos.file, pos.line, pos.column)
  49. fmt.eprintf(msg, ..args)
  50. fmt.eprintf("\n")
  51. }
  52. default_error_handler :: proc(pos: tokenizer.Pos, msg: string, args: ..any) {
  53. fmt.eprintf("%s(%d:%d): ", pos.file, pos.line, pos.column)
  54. fmt.eprintf(msg, ..args)
  55. fmt.eprintf("\n")
  56. }
  57. warn :: proc(p: ^Parser, pos: tokenizer.Pos, msg: string, args: ..any) {
  58. if p.warn != nil {
  59. p.warn(pos, msg, ..args)
  60. }
  61. p.file.syntax_warning_count += 1
  62. }
  63. error :: proc(p: ^Parser, pos: tokenizer.Pos, msg: string, args: ..any) {
  64. if p.err != nil {
  65. p.err(pos, msg, ..args)
  66. }
  67. p.file.syntax_error_count += 1
  68. p.error_count += 1
  69. }
  70. end_pos :: proc(tok: tokenizer.Token) -> tokenizer.Pos {
  71. pos := tok.pos
  72. pos.offset += len(tok.text)
  73. if tok.kind == .Comment {
  74. if tok.text[:2] != "/*" {
  75. pos.column += len(tok.text)
  76. } else {
  77. for i := 0; i < len(tok.text); i += 1 {
  78. c := tok.text[i]
  79. if c == '\n' {
  80. pos.line += 1
  81. pos.column = 1
  82. } else {
  83. pos.column += 1
  84. }
  85. }
  86. }
  87. } else {
  88. pos.column += len(tok.text)
  89. }
  90. return pos
  91. }
  92. default_parser :: proc(flags := Flags{.Optional_Semicolons}) -> Parser {
  93. return Parser {
  94. flags = flags,
  95. err = default_error_handler,
  96. warn = default_warning_handler,
  97. }
  98. }
  99. is_package_name_reserved :: proc(name: string) -> bool {
  100. switch name {
  101. case "builtin", "intrinsics":
  102. return true
  103. }
  104. return false
  105. }
  106. parse_file :: proc(p: ^Parser, file: ^ast.File) -> bool {
  107. zero_parser: {
  108. p.prev_tok = {}
  109. p.curr_tok = {}
  110. p.expr_level = 0
  111. p.allow_range = false
  112. p.allow_in_expr = false
  113. p.in_foreign_block = false
  114. p.allow_type = false
  115. p.lead_comment = nil
  116. p.line_comment = nil
  117. }
  118. p.tok.flags += {.Insert_Semicolon}
  119. p.file = file
  120. tokenizer.init(&p.tok, file.src, file.fullpath, p.err)
  121. if p.tok.ch <= 0 {
  122. return true
  123. }
  124. advance_token(p)
  125. consume_comment_groups(p, p.prev_tok)
  126. docs := p.lead_comment
  127. invalid_pre_package_token: Maybe(tokenizer.Token)
  128. for p.curr_tok.kind != .Package && p.curr_tok.kind != .EOF {
  129. if p.curr_tok.kind == .Comment {
  130. consume_comment_groups(p, p.prev_tok)
  131. } else if p.curr_tok.kind == .File_Tag {
  132. append(&p.file.tags, p.curr_tok)
  133. advance_token(p)
  134. } else {
  135. if invalid_pre_package_token == nil {
  136. invalid_pre_package_token = p.curr_tok
  137. }
  138. advance_token(p)
  139. }
  140. }
  141. if p.curr_tok.kind != .Package {
  142. t := invalid_pre_package_token.? or_else p.curr_tok
  143. error(p, t.pos, "Expected a package declaration at the start of the file")
  144. return false
  145. }
  146. p.file.pkg_token = expect_token(p, .Package)
  147. if ippt, ok := invalid_pre_package_token.?; ok {
  148. error(p, ippt.pos, "Expected only comments or lines starting with '#+' before the package declaration")
  149. return false
  150. }
  151. pkg_name := expect_token_after(p, .Ident, "package")
  152. if pkg_name.kind == .Ident {
  153. switch name := pkg_name.text; {
  154. case is_blank_ident(name):
  155. error(p, pkg_name.pos, "invalid package name '_'")
  156. case is_package_name_reserved(name), file.pkg != nil && file.pkg.kind != .Runtime && name == "runtime":
  157. error(p, pkg_name.pos, "use of reserved package name '%s'", name)
  158. }
  159. }
  160. p.file.pkg_name = pkg_name.text
  161. pd := ast.new(ast.Package_Decl, pkg_name.pos, end_pos(p.prev_tok))
  162. pd.docs = docs
  163. pd.token = p.file.pkg_token
  164. pd.name = pkg_name.text
  165. pd.comment = p.line_comment
  166. p.file.pkg_decl = pd
  167. p.file.docs = docs
  168. expect_semicolon(p, pd)
  169. if p.file.syntax_error_count > 0 {
  170. return false
  171. }
  172. p.file.decls = make([dynamic]^ast.Stmt)
  173. for p.curr_tok.kind != .EOF {
  174. stmt := parse_stmt(p)
  175. if stmt != nil {
  176. if _, ok := stmt.derived.(^ast.Empty_Stmt); !ok {
  177. append(&p.file.decls, stmt)
  178. if es, es_ok := stmt.derived.(^ast.Expr_Stmt); es_ok && es.expr != nil {
  179. if _, pl_ok := es.expr.derived.(^ast.Proc_Lit); pl_ok {
  180. error(p, stmt.pos, "procedure literal evaluated but not used")
  181. }
  182. }
  183. }
  184. }
  185. }
  186. return true
  187. }
  188. peek_token_kind :: proc(p: ^Parser, kind: tokenizer.Token_Kind, lookahead := 0) -> (ok: bool) {
  189. prev_parser := p^
  190. p.peeking = true
  191. defer {
  192. p^ = prev_parser
  193. p.peeking = false
  194. }
  195. p.tok.err = nil
  196. for i := 0; i <= lookahead; i += 1 {
  197. advance_token(p)
  198. }
  199. ok = p.curr_tok.kind == kind
  200. return
  201. }
  202. peek_token :: proc(p: ^Parser, lookahead := 0) -> (tok: tokenizer.Token) {
  203. prev_parser := p^
  204. p.peeking = true
  205. defer {
  206. p^ = prev_parser
  207. p.peeking = false
  208. }
  209. p.tok.err = nil
  210. for i := 0; i <= lookahead; i += 1 {
  211. advance_token(p)
  212. }
  213. tok = p.curr_tok
  214. return
  215. }
  216. skip_possible_newline :: proc(p: ^Parser) -> bool {
  217. if tokenizer.is_newline(p.curr_tok) {
  218. advance_token(p)
  219. return true
  220. }
  221. return false
  222. }
  223. skip_possible_newline_for_literal :: proc(p: ^Parser) -> bool {
  224. if .Optional_Semicolons not_in p.flags {
  225. return false
  226. }
  227. curr_pos := p.curr_tok.pos
  228. if tokenizer.is_newline(p.curr_tok) {
  229. next := peek_token(p)
  230. if curr_pos.line+1 >= next.pos.line {
  231. #partial switch next.kind {
  232. case .Open_Brace, .Else, .Where:
  233. advance_token(p)
  234. return true
  235. }
  236. }
  237. }
  238. return false
  239. }
  240. next_token0 :: proc(p: ^Parser) -> bool {
  241. p.curr_tok = tokenizer.scan(&p.tok)
  242. if p.curr_tok.kind == .EOF {
  243. // error(p, p.curr_tok.pos, "token is EOF");
  244. return false
  245. }
  246. return true
  247. }
  248. consume_comment :: proc(p: ^Parser) -> (tok: tokenizer.Token, end_line: int) {
  249. tok = p.curr_tok
  250. assert(tok.kind == .Comment)
  251. end_line = tok.pos.line
  252. if tok.text[1] == '*' {
  253. for c in tok.text {
  254. if c == '\n' {
  255. end_line += 1
  256. }
  257. }
  258. }
  259. _ = next_token0(p)
  260. if p.curr_tok.pos.line > tok.pos.line {
  261. end_line += 1
  262. }
  263. return
  264. }
  265. consume_comment_group :: proc(p: ^Parser, n: int) -> (comments: ^ast.Comment_Group, end_line: int) {
  266. list: [dynamic]tokenizer.Token
  267. end_line = p.curr_tok.pos.line
  268. for p.curr_tok.kind == .Comment &&
  269. p.curr_tok.pos.line <= end_line+n {
  270. comment: tokenizer.Token
  271. comment, end_line = consume_comment(p)
  272. append(&list, comment)
  273. }
  274. if len(list) > 0 && !p.peeking {
  275. comments = ast.new(ast.Comment_Group, list[0].pos, end_pos(list[len(list)-1]))
  276. comments.list = list[:]
  277. append(&p.file.comments, comments)
  278. }
  279. return
  280. }
  281. consume_comment_groups :: proc(p: ^Parser, prev: tokenizer.Token) {
  282. if p.curr_tok.kind == .Comment {
  283. comment: ^ast.Comment_Group
  284. end_line := 0
  285. if p.curr_tok.pos.line == prev.pos.line {
  286. comment, end_line = consume_comment_group(p, 0)
  287. if p.curr_tok.pos.line != end_line || p.curr_tok.kind == .EOF {
  288. p.line_comment = comment
  289. }
  290. }
  291. end_line = -1
  292. for p.curr_tok.kind == .Comment {
  293. comment, end_line = consume_comment_group(p, 1)
  294. }
  295. if end_line+1 >= p.curr_tok.pos.line || end_line < 0 {
  296. p.lead_comment = comment
  297. }
  298. assert(p.curr_tok.kind != .Comment)
  299. }
  300. }
  301. advance_token :: proc(p: ^Parser) -> tokenizer.Token {
  302. p.lead_comment = nil
  303. p.line_comment = nil
  304. p.prev_tok = p.curr_tok
  305. prev := p.prev_tok
  306. if next_token0(p) {
  307. consume_comment_groups(p, prev)
  308. }
  309. return prev
  310. }
  311. expect_token :: proc(p: ^Parser, kind: tokenizer.Token_Kind) -> tokenizer.Token {
  312. prev := p.curr_tok
  313. if prev.kind != kind {
  314. e := tokenizer.to_string(kind)
  315. g := tokenizer.token_to_string(prev)
  316. error(p, prev.pos, "expected '%s', got '%s'", e, g)
  317. }
  318. advance_token(p)
  319. return prev
  320. }
  321. expect_token_after :: proc(p: ^Parser, kind: tokenizer.Token_Kind, msg: string) -> tokenizer.Token {
  322. prev := p.curr_tok
  323. if prev.kind != kind {
  324. e := tokenizer.to_string(kind)
  325. g := tokenizer.token_to_string(prev)
  326. error(p, prev.pos, "expected '%s' after %s, got '%s'", e, msg, g)
  327. }
  328. advance_token(p)
  329. return prev
  330. }
  331. expect_operator :: proc(p: ^Parser) -> tokenizer.Token {
  332. prev := p.curr_tok
  333. #partial switch prev.kind {
  334. case .If, .When, .Or_Else:
  335. // okay
  336. case:
  337. if !tokenizer.is_operator(prev.kind) {
  338. g := tokenizer.token_to_string(prev)
  339. error(p, prev.pos, "expected an operator, got '%s'", g)
  340. }
  341. }
  342. advance_token(p)
  343. return prev
  344. }
  345. allow_token :: proc(p: ^Parser, kind: tokenizer.Token_Kind) -> bool {
  346. if p.curr_tok.kind == kind {
  347. advance_token(p)
  348. return true
  349. }
  350. return false
  351. }
  352. end_of_line_pos :: proc(p: ^Parser, tok: tokenizer.Token) -> tokenizer.Pos {
  353. offset := clamp(tok.pos.offset, 0, len(p.tok.src)-1)
  354. s := p.tok.src[offset:]
  355. pos := tok.pos
  356. pos.column -= 1
  357. for len(s) != 0 && s[0] != 0 && s[0] != '\n' {
  358. s = s[1:]
  359. pos.column += 1
  360. }
  361. return pos
  362. }
  363. expect_closing_brace_of_field_list :: proc(p: ^Parser) -> tokenizer.Token {
  364. return expect_closing_token_of_field_list(p, .Close_Brace, "field list")
  365. }
  366. expect_closing_token_of_field_list :: proc(p: ^Parser, closing_kind: tokenizer.Token_Kind, msg: string) -> tokenizer.Token {
  367. token := p.curr_tok
  368. if allow_token(p, closing_kind) {
  369. return token
  370. }
  371. if allow_token(p, .Semicolon) && !tokenizer.is_newline(token) {
  372. str := tokenizer.token_to_string(token)
  373. error(p, end_of_line_pos(p, p.prev_tok), "expected a comma, got %s", str)
  374. }
  375. expect_closing := expect_token_after(p, closing_kind, msg)
  376. if expect_closing.kind != closing_kind {
  377. for p.curr_tok.kind != closing_kind && p.curr_tok.kind != .EOF && !is_non_inserted_semicolon(p.curr_tok) {
  378. advance_token(p)
  379. }
  380. return p.curr_tok
  381. }
  382. return expect_closing
  383. }
  384. expect_closing_parentheses_of_field_list :: proc(p: ^Parser) -> tokenizer.Token {
  385. token := p.curr_tok
  386. if allow_token(p, .Close_Paren) {
  387. return token
  388. }
  389. if allow_token(p, .Semicolon) && !tokenizer.is_newline(token) {
  390. str := tokenizer.token_to_string(token)
  391. error(p, end_of_line_pos(p, p.prev_tok), "expected a comma, got %s", str)
  392. }
  393. for p.curr_tok.kind != .Close_Paren && p.curr_tok.kind != .EOF && !is_non_inserted_semicolon(p.curr_tok) {
  394. advance_token(p)
  395. }
  396. return expect_token(p, .Close_Paren)
  397. }
  398. is_non_inserted_semicolon :: proc(tok: tokenizer.Token) -> bool {
  399. return tok.kind == .Semicolon && tok.text != "\n"
  400. }
  401. is_blank_ident :: proc{
  402. is_blank_ident_string,
  403. is_blank_ident_token,
  404. is_blank_ident_node,
  405. }
  406. is_blank_ident_string :: proc(str: string) -> bool {
  407. return str == "_"
  408. }
  409. is_blank_ident_token :: proc(tok: tokenizer.Token) -> bool {
  410. if tok.kind == .Ident {
  411. return is_blank_ident_string(tok.text)
  412. }
  413. return false
  414. }
  415. is_blank_ident_node :: proc(node: ^ast.Node) -> bool {
  416. if ident, ok := node.derived.(^ast.Ident); ok {
  417. return is_blank_ident(ident.name)
  418. }
  419. return true
  420. }
  421. fix_advance_to_next_stmt :: proc(p: ^Parser) {
  422. for {
  423. #partial switch t := p.curr_tok; t.kind {
  424. case .EOF, .Semicolon:
  425. return
  426. case .Package, .Foreign, .Import,
  427. .If, .For, .When, .Return, .Switch,
  428. .Defer, .Using,
  429. .Break, .Continue, .Fallthrough,
  430. .Hash:
  431. if t.pos == p.fix_prev_pos && p.fix_count < MAX_FIX_COUNT {
  432. p.fix_count += 1
  433. return
  434. }
  435. if t.pos.offset < p.fix_prev_pos.offset {
  436. p.fix_prev_pos = t.pos
  437. p.fix_count = 0
  438. return
  439. }
  440. }
  441. advance_token(p)
  442. }
  443. }
  444. is_semicolon_optional_for_node :: proc(p: ^Parser, node: ^ast.Node) -> bool {
  445. if node == nil {
  446. return false
  447. }
  448. if .Optional_Semicolons in p.flags {
  449. return true
  450. }
  451. #partial switch n in node.derived {
  452. case ^ast.Empty_Stmt, ^ast.Block_Stmt:
  453. return true
  454. case ^ast.If_Stmt, ^ast.When_Stmt,
  455. ^ast.For_Stmt, ^ast.Range_Stmt, ^ast.Inline_Range_Stmt,
  456. ^ast.Switch_Stmt, ^ast.Type_Switch_Stmt:
  457. return true
  458. case ^ast.Helper_Type:
  459. return is_semicolon_optional_for_node(p, n.type)
  460. case ^ast.Distinct_Type:
  461. return is_semicolon_optional_for_node(p, n.type)
  462. case ^ast.Pointer_Type:
  463. return is_semicolon_optional_for_node(p, n.elem)
  464. case ^ast.Struct_Type, ^ast.Union_Type, ^ast.Enum_Type, ^ast.Bit_Set_Type, ^ast.Bit_Field_Type:
  465. // Require semicolon within a procedure body
  466. return p.curr_proc == nil
  467. case ^ast.Proc_Lit:
  468. return true
  469. case ^ast.Package_Decl, ^ast.Import_Decl, ^ast.Foreign_Import_Decl:
  470. return true
  471. case ^ast.Foreign_Block_Decl:
  472. return is_semicolon_optional_for_node(p, n.body)
  473. case ^ast.Value_Decl:
  474. if n.is_mutable {
  475. return false
  476. }
  477. if len(n.values) > 0 {
  478. return is_semicolon_optional_for_node(p, n.values[len(n.values)-1])
  479. }
  480. }
  481. return false
  482. }
  483. expect_semicolon_newline_error :: proc(p: ^Parser, token: tokenizer.Token, s: ^ast.Node) {
  484. if .Optional_Semicolons not_in p.flags && .Insert_Semicolon in p.tok.flags && token.text == "\n" {
  485. #partial switch token.kind {
  486. case .Close_Brace:
  487. case .Close_Paren:
  488. case .Else:
  489. return
  490. }
  491. if is_semicolon_optional_for_node(p, s) {
  492. return
  493. }
  494. tok := token
  495. tok.pos.column -= 1
  496. error(p, tok.pos, "expected ';', got newline")
  497. }
  498. }
  499. expect_semicolon :: proc(p: ^Parser, node: ^ast.Node) -> bool {
  500. if allow_token(p, .Semicolon) {
  501. expect_semicolon_newline_error(p, p.prev_tok, node)
  502. return true
  503. }
  504. prev := p.prev_tok
  505. if prev.kind == .Semicolon {
  506. expect_semicolon_newline_error(p, p.prev_tok, node)
  507. return true
  508. }
  509. if p.curr_tok.kind == .EOF {
  510. return true
  511. }
  512. if node != nil {
  513. if .Insert_Semicolon in p.tok.flags {
  514. #partial switch p.curr_tok.kind {
  515. case .Close_Brace, .Close_Paren, .Else, .EOF:
  516. return true
  517. }
  518. if is_semicolon_optional_for_node(p, node) {
  519. return true
  520. }
  521. } else if prev.pos.line != p.curr_tok.pos.line {
  522. if is_semicolon_optional_for_node(p, node) {
  523. return true
  524. }
  525. } else {
  526. #partial switch p.curr_tok.kind {
  527. case .Close_Brace, .Close_Paren, .Else:
  528. return true
  529. case .EOF:
  530. if is_semicolon_optional_for_node(p, node) {
  531. return true
  532. }
  533. }
  534. }
  535. } else {
  536. if p.curr_tok.kind == .EOF {
  537. return true
  538. }
  539. }
  540. error(p, prev.pos, "expected ';', got %s", tokenizer.token_to_string(p.curr_tok))
  541. fix_advance_to_next_stmt(p)
  542. return false
  543. }
  544. new_blank_ident :: proc(p: ^Parser, pos: tokenizer.Pos) -> ^ast.Ident {
  545. tok: tokenizer.Token
  546. tok.pos = pos
  547. i := ast.new(ast.Ident, pos, end_pos(tok))
  548. i.name = "_"
  549. return i
  550. }
  551. parse_ident :: proc(p: ^Parser) -> ^ast.Ident {
  552. tok := p.curr_tok
  553. pos := tok.pos
  554. name := "_"
  555. if tok.kind == .Ident {
  556. name = tok.text
  557. advance_token(p)
  558. } else {
  559. expect_token(p, .Ident)
  560. }
  561. i := ast.new(ast.Ident, pos, end_pos(tok))
  562. i.name = name
  563. return i
  564. }
  565. parse_stmt_list :: proc(p: ^Parser) -> []^ast.Stmt {
  566. list: [dynamic]^ast.Stmt
  567. for p.curr_tok.kind != .Case &&
  568. p.curr_tok.kind != .Close_Brace &&
  569. p.curr_tok.kind != .EOF {
  570. stmt := parse_stmt(p)
  571. if stmt != nil {
  572. if _, ok := stmt.derived.(^ast.Empty_Stmt); !ok {
  573. append(&list, stmt)
  574. if es, es_ok := stmt.derived.(^ast.Expr_Stmt); es_ok && es.expr != nil {
  575. if _, pl_ok := es.expr.derived.(^ast.Proc_Lit); pl_ok {
  576. error(p, stmt.pos, "procedure literal evaluated but not used")
  577. }
  578. }
  579. }
  580. }
  581. }
  582. return list[:]
  583. }
  584. parse_block_stmt :: proc(p: ^Parser, is_when: bool) -> ^ast.Stmt {
  585. skip_possible_newline_for_literal(p)
  586. if !is_when && p.curr_proc == nil {
  587. error(p, p.curr_tok.pos, "you cannot use a block statement in the file scope")
  588. }
  589. return parse_body(p)
  590. }
  591. parse_when_stmt :: proc(p: ^Parser) -> ^ast.When_Stmt {
  592. tok := expect_token(p, .When)
  593. cond: ^ast.Expr
  594. body: ^ast.Stmt
  595. else_stmt: ^ast.Stmt
  596. prev_level := p.expr_level
  597. p.expr_level = -1
  598. prev_allow_in_expr := p.allow_in_expr
  599. p.allow_in_expr = true
  600. cond = parse_expr(p, false)
  601. p.allow_in_expr = prev_allow_in_expr
  602. p.expr_level = prev_level
  603. if cond == nil {
  604. error(p, p.curr_tok.pos, "expected a condition for when statement")
  605. }
  606. if allow_token(p, .Do) {
  607. body = convert_stmt_to_body(p, parse_stmt(p))
  608. if cond.pos.line != body.pos.line {
  609. error(p, body.pos, "the body of a 'do' must be on the same line as when statement")
  610. }
  611. } else {
  612. body = parse_block_stmt(p, true)
  613. }
  614. skip_possible_newline_for_literal(p)
  615. if p.curr_tok.kind == .Else {
  616. else_tok := expect_token(p, .Else)
  617. #partial switch p.curr_tok.kind {
  618. case .When:
  619. else_stmt = parse_when_stmt(p)
  620. case .Open_Brace:
  621. else_stmt = parse_block_stmt(p, true)
  622. case .Do:
  623. expect_token(p, .Do)
  624. else_stmt = convert_stmt_to_body(p, parse_stmt(p))
  625. if else_tok.pos.line != else_stmt.pos.line {
  626. error(p, else_stmt.pos, "the body of a 'do' must be on the same line as 'else'")
  627. }
  628. case:
  629. error(p, p.curr_tok.pos, "expected when statement block statement")
  630. else_stmt = ast.new(ast.Bad_Stmt, p.curr_tok.pos, end_pos(p.curr_tok))
  631. }
  632. }
  633. end := body.end
  634. if else_stmt != nil {
  635. end = else_stmt.end
  636. }
  637. when_stmt := ast.new(ast.When_Stmt, tok.pos, end)
  638. when_stmt.when_pos = tok.pos
  639. when_stmt.cond = cond
  640. when_stmt.body = body
  641. when_stmt.else_stmt = else_stmt
  642. return when_stmt
  643. }
  644. convert_stmt_to_expr :: proc(p: ^Parser, stmt: ^ast.Stmt, kind: string) -> ^ast.Expr {
  645. if stmt == nil {
  646. return nil
  647. }
  648. if es, ok := stmt.derived.(^ast.Expr_Stmt); ok {
  649. return es.expr
  650. }
  651. error(p, stmt.pos, "expected %s, found a simple statement", kind)
  652. return ast.new(ast.Bad_Expr, p.curr_tok.pos, end_pos(p.curr_tok))
  653. }
  654. parse_if_stmt :: proc(p: ^Parser) -> ^ast.If_Stmt {
  655. tok := expect_token(p, .If)
  656. init: ^ast.Stmt
  657. cond: ^ast.Expr
  658. body: ^ast.Stmt
  659. else_stmt: ^ast.Stmt
  660. prev_level := p.expr_level
  661. p.expr_level = -1
  662. prev_allow_in_expr := p.allow_in_expr
  663. p.allow_in_expr = true
  664. if allow_token(p, .Semicolon) {
  665. cond = parse_expr(p, false)
  666. } else {
  667. init = parse_simple_stmt(p, nil)
  668. if parse_control_statement_semicolon_separator(p) {
  669. cond = parse_expr(p, false)
  670. } else {
  671. cond = convert_stmt_to_expr(p, init, "boolean expression")
  672. init = nil
  673. }
  674. }
  675. p.expr_level = prev_level
  676. p.allow_in_expr = prev_allow_in_expr
  677. if cond == nil {
  678. error(p, p.curr_tok.pos, "expected a condition for if statement")
  679. }
  680. if allow_token(p, .Do) {
  681. body = convert_stmt_to_body(p, parse_stmt(p))
  682. if cond.pos.line != body.pos.line {
  683. error(p, body.pos, "the body of a 'do' must be on the same line as the if condition")
  684. }
  685. } else {
  686. body = parse_block_stmt(p, false)
  687. }
  688. else_tok := p.curr_tok.pos
  689. skip_possible_newline_for_literal(p)
  690. if p.curr_tok.kind == .Else {
  691. else_tok := expect_token(p, .Else)
  692. #partial switch p.curr_tok.kind {
  693. case .If:
  694. else_stmt = parse_if_stmt(p)
  695. case .Open_Brace:
  696. else_stmt = parse_block_stmt(p, false)
  697. case .Do:
  698. expect_token(p, .Do)
  699. else_stmt = convert_stmt_to_body(p, parse_stmt(p))
  700. if else_tok.pos.line != else_stmt.pos.line {
  701. error(p, body.pos, "the body of a 'do' must be on the same line as 'else'")
  702. }
  703. case:
  704. error(p, p.curr_tok.pos, "expected if statement block statement")
  705. else_stmt = ast.new(ast.Bad_Stmt, p.curr_tok.pos, end_pos(p.curr_tok))
  706. }
  707. }
  708. end: tokenizer.Pos
  709. if body != nil {
  710. end = body.end
  711. }
  712. if else_stmt != nil {
  713. end = else_stmt.end
  714. }
  715. if_stmt := ast.new(ast.If_Stmt, tok.pos, end)
  716. if_stmt.if_pos = tok.pos
  717. if_stmt.init = init
  718. if_stmt.cond = cond
  719. if_stmt.body = body
  720. if_stmt.else_stmt = else_stmt
  721. if_stmt.else_pos = else_tok
  722. return if_stmt
  723. }
  724. parse_control_statement_semicolon_separator :: proc(p: ^Parser) -> bool {
  725. tok := peek_token(p)
  726. if tok.kind != .Open_Brace {
  727. return allow_token(p, .Semicolon)
  728. }
  729. if p.curr_tok.text == ";" {
  730. return allow_token(p, .Semicolon)
  731. }
  732. return false
  733. }
  734. parse_for_stmt :: proc(p: ^Parser) -> ^ast.Stmt {
  735. if p.curr_proc == nil {
  736. error(p, p.curr_tok.pos, "you cannot use a for statement in the file scope")
  737. }
  738. tok := expect_token(p, .For)
  739. init: ^ast.Stmt
  740. cond: ^ast.Stmt
  741. post: ^ast.Stmt
  742. body: ^ast.Stmt
  743. is_range := false
  744. if p.curr_tok.kind != .Open_Brace && p.curr_tok.kind != .Do {
  745. prev_level := p.expr_level
  746. defer p.expr_level = prev_level
  747. p.expr_level = -1
  748. if p.curr_tok.kind == .In {
  749. in_tok := expect_token(p, .In)
  750. rhs: ^ast.Expr
  751. prev_allow_range := p.allow_range
  752. p.allow_range = true
  753. rhs = parse_expr(p, false)
  754. p.allow_range = prev_allow_range
  755. if allow_token(p, .Do) {
  756. body = convert_stmt_to_body(p, parse_stmt(p))
  757. if tok.pos.line != body.pos.line {
  758. error(p, body.pos, "the body of a 'do' must be on the same line as 'else'")
  759. }
  760. } else {
  761. body = parse_body(p)
  762. }
  763. range_stmt := ast.new(ast.Range_Stmt, tok.pos, body)
  764. range_stmt.for_pos = tok.pos
  765. range_stmt.in_pos = in_tok.pos
  766. range_stmt.expr = rhs
  767. range_stmt.body = body
  768. return range_stmt
  769. }
  770. if p.curr_tok.kind != .Semicolon {
  771. cond = parse_simple_stmt(p, {Stmt_Allow_Flag.In})
  772. if as, ok := cond.derived.(^ast.Assign_Stmt); ok && as.op.kind == .In {
  773. is_range = true
  774. }
  775. }
  776. if !is_range && parse_control_statement_semicolon_separator(p) {
  777. init = cond
  778. cond = nil
  779. if p.curr_tok.kind == .Open_Brace || p.curr_tok.kind == .Do {
  780. error(p, p.curr_tok.pos, "Expected ';', followed by a condition expression and post statement, got %s", tokenizer.tokens[p.curr_tok.kind])
  781. } else {
  782. if p.curr_tok.kind != .Semicolon {
  783. cond = parse_simple_stmt(p, nil)
  784. }
  785. if p.curr_tok.text != ";" {
  786. error(p, p.curr_tok.pos, "Expected ';', got %s", tokenizer.token_to_string(p.curr_tok))
  787. } else {
  788. expect_semicolon(p, nil)
  789. }
  790. if p.curr_tok.kind != .Open_Brace && p.curr_tok.kind != .Do {
  791. post = parse_simple_stmt(p, nil)
  792. }
  793. }
  794. }
  795. }
  796. if allow_token(p, .Do) {
  797. body = convert_stmt_to_body(p, parse_stmt(p))
  798. if tok.pos.line != body.pos.line {
  799. error(p, body.pos, "the body of a 'do' must be on the same line as the 'for' token")
  800. }
  801. } else {
  802. allow_token(p, .Semicolon)
  803. body = parse_body(p)
  804. }
  805. if is_range {
  806. assign_stmt := cond.derived.(^ast.Assign_Stmt)
  807. vals := assign_stmt.lhs[:]
  808. rhs: ^ast.Expr
  809. if len(assign_stmt.rhs) > 0 {
  810. rhs = assign_stmt.rhs[0]
  811. }
  812. range_stmt := ast.new(ast.Range_Stmt, tok.pos, body)
  813. range_stmt.for_pos = tok.pos
  814. range_stmt.vals = vals
  815. range_stmt.in_pos = assign_stmt.op.pos
  816. range_stmt.expr = rhs
  817. range_stmt.body = body
  818. return range_stmt
  819. }
  820. cond_expr := convert_stmt_to_expr(p, cond, "boolean expression")
  821. for_stmt := ast.new(ast.For_Stmt, tok.pos, body)
  822. for_stmt.for_pos = tok.pos
  823. for_stmt.init = init
  824. for_stmt.cond = cond_expr
  825. for_stmt.post = post
  826. for_stmt.body = body
  827. return for_stmt
  828. }
  829. parse_case_clause :: proc(p: ^Parser, is_type_switch: bool) -> ^ast.Case_Clause {
  830. tok := expect_token(p, .Case)
  831. list: []^ast.Expr
  832. if p.curr_tok.kind != .Colon {
  833. prev_allow_range, prev_allow_in_expr := p.allow_range, p.allow_in_expr
  834. defer p.allow_range, p.allow_in_expr = prev_allow_range, prev_allow_in_expr
  835. p.allow_range, p.allow_in_expr = !is_type_switch, !is_type_switch
  836. list = parse_rhs_expr_list(p)
  837. }
  838. terminator := expect_token(p, .Colon)
  839. stmts := parse_stmt_list(p)
  840. cc := ast.new(ast.Case_Clause, tok.pos, end_pos(p.prev_tok))
  841. cc.list = list
  842. cc.terminator = terminator
  843. cc.body = stmts
  844. cc.case_pos = tok.pos
  845. return cc
  846. }
  847. parse_switch_stmt :: proc(p: ^Parser) -> ^ast.Stmt {
  848. tok := expect_token(p, .Switch)
  849. init: ^ast.Stmt
  850. tag: ^ast.Stmt
  851. is_type_switch := false
  852. clauses: [dynamic]^ast.Stmt
  853. if p.curr_tok.kind != .Open_Brace {
  854. prev_level := p.expr_level
  855. defer p.expr_level = prev_level
  856. p.expr_level = -1
  857. if p.curr_tok.kind == .In {
  858. in_tok := expect_token(p, .In)
  859. is_type_switch = true
  860. lhs := make([]^ast.Expr, 1)
  861. rhs := make([]^ast.Expr, 1)
  862. lhs[0] = new_blank_ident(p, tok.pos)
  863. rhs[0] = parse_expr(p, true)
  864. as := ast.new(ast.Assign_Stmt, tok.pos, rhs[0])
  865. as.lhs = lhs
  866. as.op = in_tok
  867. as.rhs = rhs
  868. tag = as
  869. } else {
  870. tag = parse_simple_stmt(p, {Stmt_Allow_Flag.In})
  871. if as, ok := tag.derived.(^ast.Assign_Stmt); ok && as.op.kind == .In {
  872. is_type_switch = true
  873. } else if parse_control_statement_semicolon_separator(p) {
  874. init = tag
  875. tag = nil
  876. if p.curr_tok.kind != .Open_Brace {
  877. tag = parse_simple_stmt(p, nil)
  878. }
  879. }
  880. }
  881. }
  882. skip_possible_newline(p)
  883. open := expect_token(p, .Open_Brace)
  884. for p.curr_tok.kind == .Case {
  885. clause := parse_case_clause(p, is_type_switch)
  886. append(&clauses, clause)
  887. }
  888. close := expect_token(p, .Close_Brace)
  889. body := ast.new(ast.Block_Stmt, open.pos, end_pos(close))
  890. body.stmts = clauses[:]
  891. if is_type_switch {
  892. ts := ast.new(ast.Type_Switch_Stmt, tok.pos, body)
  893. ts.tag = tag
  894. ts.body = body
  895. ts.switch_pos = tok.pos
  896. return ts
  897. } else {
  898. cond := convert_stmt_to_expr(p, tag, "switch expression")
  899. ts := ast.new(ast.Switch_Stmt, tok.pos, body)
  900. ts.init = init
  901. ts.cond = cond
  902. ts.body = body
  903. ts.switch_pos = tok.pos
  904. return ts
  905. }
  906. }
  907. parse_attribute :: proc(p: ^Parser, tok: tokenizer.Token, open_kind, close_kind: tokenizer.Token_Kind, docs: ^ast.Comment_Group) -> ^ast.Stmt {
  908. elems: [dynamic]^ast.Expr
  909. open, close: tokenizer.Token
  910. if p.curr_tok.kind == .Ident {
  911. elem := parse_ident(p)
  912. append(&elems, elem)
  913. } else {
  914. open = expect_token(p, open_kind)
  915. p.expr_level += 1
  916. for p.curr_tok.kind != close_kind &&
  917. p.curr_tok.kind != .EOF {
  918. elem: ^ast.Expr
  919. elem = parse_ident(p)
  920. if p.curr_tok.kind == .Eq {
  921. eq := expect_token(p, .Eq)
  922. value := parse_value(p)
  923. fv := ast.new(ast.Field_Value, elem.pos, value)
  924. fv.field = elem
  925. fv.sep = eq.pos
  926. fv.value = value
  927. elem = fv
  928. }
  929. append(&elems, elem)
  930. allow_token(p, .Comma) or_break
  931. }
  932. p.expr_level -= 1
  933. close = expect_token_after(p, close_kind, "attribute")
  934. }
  935. attribute := ast.new(ast.Attribute, tok.pos, end_pos(close))
  936. attribute.tok = tok.kind
  937. attribute.open = open.pos
  938. attribute.elems = elems[:]
  939. attribute.close = close.pos
  940. skip_possible_newline(p)
  941. decl := parse_stmt(p)
  942. #partial switch d in decl.derived_stmt {
  943. case ^ast.Value_Decl:
  944. if d.docs == nil { d.docs = docs }
  945. append(&d.attributes, attribute)
  946. case ^ast.Foreign_Block_Decl:
  947. if d.docs == nil { d.docs = docs }
  948. append(&d.attributes, attribute)
  949. case ^ast.Foreign_Import_Decl:
  950. if d.docs == nil { d.docs = docs }
  951. append(&d.attributes, attribute)
  952. case ^ast.Import_Decl:
  953. if d.docs == nil { d.docs = docs }
  954. append(&d.attributes, attribute)
  955. case:
  956. error(p, decl.pos, "expected a value or foreign declaration after an attribute")
  957. free(attribute)
  958. delete(elems)
  959. }
  960. return decl
  961. }
  962. parse_foreign_block_decl :: proc(p: ^Parser) -> ^ast.Stmt {
  963. decl := parse_stmt(p)
  964. #partial switch _ in decl.derived_stmt {
  965. case ^ast.Empty_Stmt, ^ast.Bad_Stmt, ^ast.Bad_Decl:
  966. // Ignore
  967. return nil
  968. case ^ast.When_Stmt, ^ast.Value_Decl:
  969. return decl
  970. }
  971. error(p, decl.pos, "foreign blocks only allow procedure and variable declarations")
  972. return nil
  973. }
  974. parse_foreign_block :: proc(p: ^Parser, tok: tokenizer.Token) -> ^ast.Foreign_Block_Decl {
  975. docs := p.lead_comment
  976. foreign_library: ^ast.Expr
  977. #partial switch p.curr_tok.kind {
  978. case .Open_Brace:
  979. i := ast.new(ast.Ident, tok.pos, end_pos(tok))
  980. i.name = "_"
  981. foreign_library = i
  982. case:
  983. foreign_library = parse_ident(p)
  984. }
  985. decls: [dynamic]^ast.Stmt
  986. prev_in_foreign_block := p.in_foreign_block
  987. defer p.in_foreign_block = prev_in_foreign_block
  988. p.in_foreign_block = true
  989. skip_possible_newline_for_literal(p)
  990. open := expect_token(p, .Open_Brace)
  991. for p.curr_tok.kind != .Close_Brace && p.curr_tok.kind != .EOF {
  992. decl := parse_foreign_block_decl(p)
  993. if decl != nil {
  994. append(&decls, decl)
  995. }
  996. }
  997. close := expect_token(p, .Close_Brace)
  998. body := ast.new(ast.Block_Stmt, open.pos, end_pos(close))
  999. body.open = open.pos
  1000. body.stmts = decls[:]
  1001. body.close = close.pos
  1002. decl := ast.new(ast.Foreign_Block_Decl, tok.pos, body)
  1003. decl.docs = docs
  1004. decl.tok = tok
  1005. decl.foreign_library = foreign_library
  1006. decl.body = body
  1007. return decl
  1008. }
  1009. parse_foreign_decl :: proc(p: ^Parser) -> ^ast.Decl {
  1010. docs := p.lead_comment
  1011. tok := expect_token(p, .Foreign)
  1012. #partial switch p.curr_tok.kind {
  1013. case .Ident, .Open_Brace:
  1014. return parse_foreign_block(p, tok)
  1015. case .Import:
  1016. import_tok := expect_token(p, .Import)
  1017. name: ^ast.Ident
  1018. if p.curr_tok.kind == .Ident {
  1019. name = parse_ident(p)
  1020. }
  1021. if name != nil && is_blank_ident(name) {
  1022. error(p, name.pos, "illegal foreign import name: '_'")
  1023. }
  1024. fullpaths: [dynamic]^ast.Expr
  1025. if allow_token(p, .Open_Brace) {
  1026. for p.curr_tok.kind != .Close_Brace &&
  1027. p.curr_tok.kind != .EOF {
  1028. path := parse_expr(p, false)
  1029. append(&fullpaths, path)
  1030. allow_token(p, .Comma) or_break
  1031. }
  1032. expect_token(p, .Close_Brace)
  1033. } else {
  1034. path := expect_token(p, .String)
  1035. reserve(&fullpaths, 1)
  1036. bl := ast.new(ast.Basic_Lit, path.pos, end_pos(path))
  1037. bl.tok = path
  1038. append(&fullpaths, bl)
  1039. }
  1040. if len(fullpaths) == 0 {
  1041. error(p, import_tok.pos, "foreign import without any paths")
  1042. }
  1043. decl := ast.new(ast.Foreign_Import_Decl, tok.pos, end_pos(p.prev_tok))
  1044. decl.docs = docs
  1045. decl.foreign_tok = tok
  1046. decl.import_tok = import_tok
  1047. decl.name = name
  1048. decl.fullpaths = fullpaths[:]
  1049. expect_semicolon(p, decl)
  1050. decl.comment = p.line_comment
  1051. return decl
  1052. }
  1053. error(p, tok.pos, "invalid foreign declaration")
  1054. return ast.new(ast.Bad_Decl, tok.pos, end_pos(tok))
  1055. }
  1056. parse_unrolled_for_loop :: proc(p: ^Parser, inline_tok: tokenizer.Token) -> ^ast.Stmt {
  1057. for_tok := expect_token(p, .For)
  1058. val0, val1: ^ast.Expr
  1059. in_tok: tokenizer.Token
  1060. expr: ^ast.Expr
  1061. body: ^ast.Stmt
  1062. bad_stmt := false
  1063. if p.curr_tok.kind != .In {
  1064. idents := parse_ident_list(p, false)
  1065. switch len(idents) {
  1066. case 1:
  1067. val0 = idents[0]
  1068. case 2:
  1069. val0, val1 = idents[0], idents[1]
  1070. case:
  1071. error(p, for_tok.pos, "expected either 1 or 2 identifiers")
  1072. bad_stmt = true
  1073. }
  1074. }
  1075. in_tok = expect_token(p, .In)
  1076. prev_allow_range := p.allow_range
  1077. prev_level := p.expr_level
  1078. p.allow_range = true
  1079. p.expr_level = -1
  1080. expr = parse_expr(p, false)
  1081. p.expr_level = prev_level
  1082. p.allow_range = prev_allow_range
  1083. if allow_token(p, .Do) {
  1084. body = convert_stmt_to_body(p, parse_stmt(p))
  1085. if for_tok.pos.line != body.pos.line {
  1086. error(p, body.pos, "the body of a 'do' must be on the same line as the 'for' token")
  1087. }
  1088. } else {
  1089. body = parse_block_stmt(p, false)
  1090. }
  1091. if bad_stmt {
  1092. return ast.new(ast.Bad_Stmt, inline_tok.pos, end_pos(p.prev_tok))
  1093. }
  1094. range_stmt := ast.new(ast.Inline_Range_Stmt, inline_tok.pos, body)
  1095. range_stmt.inline_pos = inline_tok.pos
  1096. range_stmt.for_pos = for_tok.pos
  1097. range_stmt.val0 = val0
  1098. range_stmt.val1 = val1
  1099. range_stmt.in_pos = in_tok.pos
  1100. range_stmt.expr = expr
  1101. range_stmt.body = body
  1102. return range_stmt
  1103. }
  1104. parse_stmt :: proc(p: ^Parser) -> ^ast.Stmt {
  1105. #partial switch p.curr_tok.kind {
  1106. case .Inline:
  1107. if peek_token_kind(p, .For) {
  1108. inline_tok := expect_token(p, .Inline)
  1109. return parse_unrolled_for_loop(p, inline_tok)
  1110. }
  1111. fallthrough
  1112. // Operands
  1113. case .No_Inline,
  1114. .Context, // Also allows for 'context = '
  1115. .Proc,
  1116. .Ident,
  1117. .Integer, .Float, .Imag,
  1118. .Rune, .String,
  1119. .Open_Paren,
  1120. .Pointer,
  1121. .Asm, // Inline assembly
  1122. // Unary Expressions
  1123. .Add, .Sub, .Xor, .Not, .And:
  1124. s := parse_simple_stmt(p, {Stmt_Allow_Flag.Label})
  1125. expect_semicolon(p, s)
  1126. return s
  1127. case .Foreign: return parse_foreign_decl(p)
  1128. case .Import: return parse_import_decl(p)
  1129. case .If: return parse_if_stmt(p)
  1130. case .When: return parse_when_stmt(p)
  1131. case .For: return parse_for_stmt(p)
  1132. case .Switch: return parse_switch_stmt(p)
  1133. case .Defer:
  1134. tok := advance_token(p)
  1135. stmt := parse_stmt(p)
  1136. #partial switch s in stmt.derived_stmt {
  1137. case ^ast.Empty_Stmt:
  1138. error(p, s.pos, "empty statement after defer (e.g. ';')")
  1139. case ^ast.Defer_Stmt:
  1140. error(p, s.pos, "you cannot defer a defer statement")
  1141. stmt = s.stmt
  1142. case ^ast.Return_Stmt:
  1143. error(p, s.pos, "you cannot defer a return statement")
  1144. }
  1145. ds := ast.new(ast.Defer_Stmt, tok.pos, stmt)
  1146. ds.stmt = stmt
  1147. return ds
  1148. case .Return:
  1149. tok := advance_token(p)
  1150. if p.expr_level > 0 {
  1151. error(p, tok.pos, "you cannot use a return statement within an expression")
  1152. }
  1153. results: [dynamic]^ast.Expr
  1154. for p.curr_tok.kind != .Semicolon && p.curr_tok.kind != .Close_Brace {
  1155. result := parse_expr(p, false)
  1156. append(&results, result)
  1157. if p.curr_tok.kind != .Comma ||
  1158. p.curr_tok.kind == .EOF {
  1159. break
  1160. }
  1161. advance_token(p)
  1162. }
  1163. end := end_pos(tok)
  1164. if len(results) > 0 {
  1165. end = results[len(results)-1].end
  1166. }
  1167. rs := ast.new(ast.Return_Stmt, tok.pos, end)
  1168. rs.results = results[:]
  1169. expect_semicolon(p, rs)
  1170. return rs
  1171. case .Break, .Continue, .Fallthrough:
  1172. tok := advance_token(p)
  1173. label: ^ast.Ident
  1174. if tok.kind != .Fallthrough && p.curr_tok.kind == .Ident {
  1175. label = parse_ident(p)
  1176. }
  1177. s := ast.new(ast.Branch_Stmt, tok.pos, label)
  1178. s.tok = tok
  1179. s.label = label
  1180. expect_semicolon(p, s)
  1181. return s
  1182. case .Using:
  1183. docs := p.lead_comment
  1184. tok := expect_token(p, .Using)
  1185. if p.curr_tok.kind == .Import {
  1186. return parse_import_decl(p, Import_Decl_Kind.Using)
  1187. }
  1188. list := parse_lhs_expr_list(p)
  1189. if len(list) == 0 {
  1190. error(p, tok.pos, "illegal use of 'using' statement")
  1191. expect_semicolon(p, nil)
  1192. return ast.new(ast.Bad_Stmt, tok.pos, end_pos(p.prev_tok))
  1193. }
  1194. if p.curr_tok.kind != .Colon {
  1195. end := list[len(list)-1]
  1196. expect_semicolon(p, end)
  1197. us := ast.new(ast.Using_Stmt, tok.pos, end)
  1198. us.list = list
  1199. return us
  1200. }
  1201. expect_token_after(p, .Colon, "identifier list")
  1202. decl := parse_value_decl(p, list, docs)
  1203. if decl != nil {
  1204. #partial switch d in decl.derived_stmt {
  1205. case ^ast.Value_Decl:
  1206. d.is_using = true
  1207. return decl
  1208. }
  1209. }
  1210. error(p, tok.pos, "illegal use of 'using' statement")
  1211. return ast.new(ast.Bad_Stmt, tok.pos, end_pos(p.prev_tok))
  1212. case .At:
  1213. docs := p.lead_comment
  1214. tok := advance_token(p)
  1215. return parse_attribute(p, tok, .Open_Paren, .Close_Paren, docs)
  1216. case .Hash:
  1217. tok := expect_token(p, .Hash)
  1218. tag := expect_token(p, .Ident)
  1219. name := tag.text
  1220. switch name {
  1221. case "bounds_check", "no_bounds_check":
  1222. stmt := parse_stmt(p)
  1223. switch name {
  1224. case "bounds_check":
  1225. stmt.state_flags += {.Bounds_Check}
  1226. case "no_bounds_check":
  1227. stmt.state_flags += {.No_Bounds_Check}
  1228. }
  1229. return stmt
  1230. case "type_assert", "no_type_assert":
  1231. stmt := parse_stmt(p)
  1232. switch name {
  1233. case "type_assert":
  1234. stmt.state_flags += {.Type_Assert}
  1235. case "no_type_assert":
  1236. stmt.state_flags += {.No_Type_Assert}
  1237. }
  1238. return stmt
  1239. case "partial":
  1240. stmt := parse_stmt(p)
  1241. #partial switch s in stmt.derived_stmt {
  1242. case ^ast.Switch_Stmt: s.partial = true
  1243. case ^ast.Type_Switch_Stmt: s.partial = true
  1244. case: error(p, stmt.pos, "#partial can only be applied to a switch statement")
  1245. }
  1246. return stmt
  1247. case "assert", "panic":
  1248. bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(tag))
  1249. bd.tok = tok
  1250. bd.name = name
  1251. ce := parse_call_expr(p, bd)
  1252. es := ast.new(ast.Expr_Stmt, ce.pos, ce)
  1253. es.expr = ce
  1254. return es
  1255. case "force_inline", "force_no_inline":
  1256. expr := parse_inlining_operand(p, true, tag)
  1257. es := ast.new(ast.Expr_Stmt, expr.pos, expr)
  1258. es.expr = expr
  1259. return es
  1260. case "unroll":
  1261. return parse_unrolled_for_loop(p, tag)
  1262. case "reverse":
  1263. stmt := parse_stmt(p)
  1264. if range, is_range := stmt.derived.(^ast.Range_Stmt); is_range {
  1265. if range.reverse {
  1266. error(p, range.pos, "#reverse already applied to a 'for in' statement")
  1267. }
  1268. range.reverse = true
  1269. } else {
  1270. error(p, stmt.pos, "#reverse can only be applied to a 'for in' statement")
  1271. }
  1272. return stmt
  1273. case "include":
  1274. error(p, tag.pos, "#include is not a valid import declaration kind. Did you meant 'import'?")
  1275. return ast.new(ast.Bad_Stmt, tok.pos, end_pos(tag))
  1276. case:
  1277. stmt := parse_stmt(p)
  1278. end := stmt.pos if stmt != nil else end_pos(tok)
  1279. te := ast.new(ast.Tag_Stmt, tok.pos, end)
  1280. te.op = tok
  1281. te.name = name
  1282. te.stmt = stmt
  1283. fix_advance_to_next_stmt(p)
  1284. return te
  1285. }
  1286. case .Open_Brace:
  1287. return parse_block_stmt(p, false)
  1288. case .Semicolon:
  1289. tok := advance_token(p)
  1290. s := ast.new(ast.Empty_Stmt, tok.pos, end_pos(tok))
  1291. return s
  1292. }
  1293. #partial switch p.curr_tok.kind {
  1294. case .Else:
  1295. token := expect_token(p, .Else)
  1296. error(p, token.pos, "'else' unattached to an 'if' statement")
  1297. #partial switch p.curr_tok.kind {
  1298. case .If:
  1299. return parse_if_stmt(p)
  1300. case .When:
  1301. return parse_when_stmt(p)
  1302. case .Open_Brace:
  1303. return parse_block_stmt(p, true)
  1304. case .Do:
  1305. expect_token(p, .Do)
  1306. return convert_stmt_to_body(p, parse_stmt(p))
  1307. case:
  1308. fix_advance_to_next_stmt(p)
  1309. return ast.new(ast.Bad_Stmt, token.pos, end_pos(p.curr_tok))
  1310. }
  1311. }
  1312. tok := advance_token(p)
  1313. error(p, tok.pos, "expected a statement, got %s", tokenizer.token_to_string(tok))
  1314. fix_advance_to_next_stmt(p)
  1315. s := ast.new(ast.Bad_Stmt, tok.pos, end_pos(tok))
  1316. return s
  1317. }
  1318. token_precedence :: proc(p: ^Parser, kind: tokenizer.Token_Kind) -> int {
  1319. #partial switch kind {
  1320. case .Question, .If, .When, .Or_Else:
  1321. return 1
  1322. case .Ellipsis, .Range_Half, .Range_Full:
  1323. if !p.allow_range {
  1324. return 0
  1325. }
  1326. return 2
  1327. case .Cmp_Or:
  1328. return 3
  1329. case .Cmp_And:
  1330. return 4
  1331. case .Cmp_Eq, .Not_Eq,
  1332. .Lt, .Gt,
  1333. .Lt_Eq, .Gt_Eq:
  1334. return 5
  1335. case .In, .Not_In:
  1336. if p.expr_level < 0 && !p.allow_in_expr {
  1337. return 0
  1338. }
  1339. fallthrough
  1340. case .Add, .Sub, .Or, .Xor:
  1341. return 6
  1342. case .Mul, .Quo,
  1343. .Mod, .Mod_Mod,
  1344. .And, .And_Not,
  1345. .Shl, .Shr:
  1346. return 7
  1347. }
  1348. return 0
  1349. }
  1350. parse_type_or_ident :: proc(p: ^Parser) -> ^ast.Expr {
  1351. prev_allow_type := p.allow_type
  1352. prev_expr_level := p.expr_level
  1353. defer {
  1354. p.allow_type = prev_allow_type
  1355. p.expr_level = prev_expr_level
  1356. }
  1357. p.allow_type = true
  1358. p.expr_level = -1
  1359. lhs := true
  1360. return parse_atom_expr(p, parse_operand(p, lhs), lhs)
  1361. }
  1362. parse_type :: proc(p: ^Parser) -> ^ast.Expr {
  1363. type := parse_type_or_ident(p)
  1364. if type == nil {
  1365. error(p, p.curr_tok.pos, "expected a type")
  1366. return ast.new(ast.Bad_Expr, p.curr_tok.pos, end_pos(p.curr_tok))
  1367. }
  1368. return type
  1369. }
  1370. parse_body :: proc(p: ^Parser) -> ^ast.Block_Stmt {
  1371. prev_expr_level := p.expr_level
  1372. defer p.expr_level = prev_expr_level
  1373. p.expr_level = 0
  1374. open := expect_token(p, .Open_Brace)
  1375. stmts := parse_stmt_list(p)
  1376. close := expect_token(p, .Close_Brace)
  1377. bs := ast.new(ast.Block_Stmt, open.pos, end_pos(close))
  1378. bs.open = open.pos
  1379. bs.stmts = stmts
  1380. bs.close = close.pos
  1381. return bs
  1382. }
  1383. convert_stmt_to_body :: proc(p: ^Parser, stmt: ^ast.Stmt) -> ^ast.Stmt {
  1384. #partial switch s in stmt.derived_stmt {
  1385. case ^ast.Block_Stmt:
  1386. error(p, stmt.pos, "expected a normal statement rather than a block statement")
  1387. return stmt
  1388. case ^ast.Empty_Stmt:
  1389. error(p, stmt.pos, "expected a non-empty statement")
  1390. }
  1391. bs := ast.new(ast.Block_Stmt, stmt.pos, stmt)
  1392. bs.open = stmt.pos
  1393. bs.stmts = make([]^ast.Stmt, 1)
  1394. bs.stmts[0] = stmt
  1395. bs.close = stmt.end
  1396. bs.uses_do = true
  1397. return bs
  1398. }
  1399. new_ast_field :: proc(names: []^ast.Expr, type: ^ast.Expr, default_value: ^ast.Expr) -> ^ast.Field {
  1400. pos, end: tokenizer.Pos
  1401. if len(names) > 0 {
  1402. pos = names[0].pos
  1403. if default_value != nil {
  1404. end = default_value.end
  1405. } else if type != nil {
  1406. end = type.end
  1407. } else {
  1408. end = names[len(names)-1].pos
  1409. }
  1410. } else {
  1411. if type != nil {
  1412. pos = type.pos
  1413. } else if default_value != nil {
  1414. pos = default_value.pos
  1415. }
  1416. if default_value != nil {
  1417. end = default_value.end
  1418. } else if type != nil {
  1419. end = type.end
  1420. }
  1421. }
  1422. field := ast.new(ast.Field, pos, end)
  1423. field.names = names
  1424. field.type = type
  1425. field.default_value = default_value
  1426. return field
  1427. }
  1428. Expr_And_Flags :: struct {
  1429. expr: ^ast.Expr,
  1430. flags: ast.Field_Flags,
  1431. }
  1432. convert_to_ident_list :: proc(p: ^Parser, list: []Expr_And_Flags, ignore_flags, allow_poly_names: bool) -> []^ast.Expr {
  1433. idents := make([dynamic]^ast.Expr, 0, len(list))
  1434. for ident, i in list {
  1435. if !ignore_flags {
  1436. if i != 0 {
  1437. error(p, ident.expr.pos, "illegal use of prefixes in parameter list")
  1438. }
  1439. }
  1440. id: ^ast.Expr = ident.expr
  1441. #partial switch n in ident.expr.derived_expr {
  1442. case ^ast.Ident:
  1443. case ^ast.Bad_Expr:
  1444. case ^ast.Poly_Type:
  1445. if allow_poly_names {
  1446. if n.specialization == nil {
  1447. break
  1448. } else {
  1449. error(p, ident.expr.pos, "expected a polymorphic identifier without an specialization")
  1450. }
  1451. } else {
  1452. error(p, ident.expr.pos, "expected a non-polymorphic identifier")
  1453. }
  1454. case:
  1455. error(p, ident.expr.pos, "expected an identifier")
  1456. id = ast.new(ast.Ident, ident.expr.pos, ident.expr.end)
  1457. }
  1458. append(&idents, id)
  1459. }
  1460. return idents[:]
  1461. }
  1462. is_token_field_prefix :: proc(p: ^Parser) -> ast.Field_Flag {
  1463. #partial switch p.curr_tok.kind {
  1464. case .EOF:
  1465. return .Invalid
  1466. case .Using:
  1467. advance_token(p)
  1468. return .Using
  1469. case .Hash:
  1470. tok: tokenizer.Token
  1471. advance_token(p)
  1472. tok = p.curr_tok
  1473. advance_token(p)
  1474. if tok.kind == .Ident {
  1475. for kf in ast.field_hash_flag_strings {
  1476. if kf.key == tok.text {
  1477. return kf.flag
  1478. }
  1479. }
  1480. }
  1481. return .Unknown
  1482. }
  1483. return .Invalid
  1484. }
  1485. parse_field_prefixes :: proc(p: ^Parser) -> (flags: ast.Field_Flags) {
  1486. counts: [len(ast.Field_Flag)]int
  1487. for {
  1488. kind := is_token_field_prefix(p)
  1489. if kind == .Invalid {
  1490. break
  1491. }
  1492. if kind == .Unknown {
  1493. error(p, p.curr_tok.pos, "unknown prefix kind '#%s'", p.curr_tok.text)
  1494. continue
  1495. }
  1496. counts[kind] += 1
  1497. }
  1498. for kind in ast.Field_Flag {
  1499. count := counts[kind]
  1500. if kind == .Invalid || kind == .Unknown {
  1501. // Ignore
  1502. } else {
  1503. if count > 1 { error(p, p.curr_tok.pos, "multiple '%s' in this field list", ast.field_flag_strings[kind]) }
  1504. if count > 0 { flags += {kind} }
  1505. }
  1506. }
  1507. return
  1508. }
  1509. check_field_flag_prefixes :: proc(p: ^Parser, name_count: int, allowed_flags, set_flags: ast.Field_Flags) -> (flags: ast.Field_Flags) {
  1510. flags = set_flags
  1511. if name_count > 1 && .Using in flags {
  1512. error(p, p.curr_tok.pos, "cannot apply 'using' to more than one of the same type")
  1513. flags -= {.Using}
  1514. }
  1515. for flag in ast.Field_Flag {
  1516. if flag not_in allowed_flags && flag in flags {
  1517. #partial switch flag {
  1518. case .Unknown, .Invalid:
  1519. // ignore
  1520. case .Tags, .Ellipsis, .Results, .Default_Parameters, .Typeid_Token:
  1521. panic("Impossible prefixes")
  1522. case:
  1523. error(p, p.curr_tok.pos, "'%s' is not allowed within this field list", ast.field_flag_strings[flag])
  1524. }
  1525. flags -= {flag}
  1526. }
  1527. }
  1528. return flags
  1529. }
  1530. parse_var_type :: proc(p: ^Parser, flags: ast.Field_Flags) -> ^ast.Expr {
  1531. if .Ellipsis in flags && p.curr_tok.kind == .Ellipsis {
  1532. tok := advance_token(p)
  1533. type := parse_type_or_ident(p)
  1534. if type == nil {
  1535. error(p, tok.pos, "variadic field missing type after '..'")
  1536. type = ast.new(ast.Bad_Expr, tok.pos, end_pos(tok))
  1537. }
  1538. e := ast.new(ast.Ellipsis, type.pos, type)
  1539. e.tok = tok.kind
  1540. e.expr = type
  1541. return e
  1542. }
  1543. type: ^ast.Expr
  1544. if .Typeid_Token in flags && p.curr_tok.kind == .Typeid {
  1545. tok := expect_token(p, .Typeid)
  1546. specialization: ^ast.Expr
  1547. end := tok.pos
  1548. if allow_token(p, .Quo) {
  1549. specialization = parse_type(p)
  1550. end = specialization.end
  1551. }
  1552. ti := ast.new(ast.Typeid_Type, tok.pos, end)
  1553. ti.tok = tok.kind
  1554. ti.specialization = specialization
  1555. type = ti
  1556. } else {
  1557. type = parse_type(p)
  1558. }
  1559. return type
  1560. }
  1561. check_procedure_name_list :: proc(p: ^Parser, names: []^ast.Expr) -> bool {
  1562. if len(names) == 0 {
  1563. return false
  1564. }
  1565. _, first_is_polymorphic := names[0].derived.(^ast.Poly_Type)
  1566. any_polymorphic_names := first_is_polymorphic
  1567. for i := 1; i < len(names); i += 1 {
  1568. name := names[i]
  1569. if first_is_polymorphic {
  1570. if _, ok := name.derived.(^ast.Poly_Type); ok {
  1571. any_polymorphic_names = true
  1572. } else {
  1573. error(p, name.pos, "mixture of polymorphic and non-polymorphic identifiers")
  1574. return any_polymorphic_names
  1575. }
  1576. } else {
  1577. if _, ok := name.derived.(^ast.Poly_Type); ok {
  1578. any_polymorphic_names = true
  1579. error(p, name.pos, "mixture of polymorphic and non-polymorphic identifiers")
  1580. return any_polymorphic_names
  1581. } else {
  1582. // Okay
  1583. }
  1584. }
  1585. }
  1586. return any_polymorphic_names
  1587. }
  1588. parse_ident_list :: proc(p: ^Parser, allow_poly_names: bool) -> []^ast.Expr {
  1589. list: [dynamic]^ast.Expr
  1590. for {
  1591. if allow_poly_names && p.curr_tok.kind == .Dollar {
  1592. tok := expect_token(p, .Dollar)
  1593. ident := parse_ident(p)
  1594. if is_blank_ident(ident) {
  1595. error(p, ident.pos, "invalid polymorphic type definition with a blank identifier")
  1596. }
  1597. poly_name := ast.new(ast.Poly_Type, tok.pos, ident)
  1598. poly_name.type = ident
  1599. append(&list, poly_name)
  1600. } else {
  1601. ident := parse_ident(p)
  1602. append(&list, ident)
  1603. }
  1604. if p.curr_tok.kind != .Comma ||
  1605. p.curr_tok.kind == .EOF {
  1606. break
  1607. }
  1608. advance_token(p)
  1609. }
  1610. return list[:]
  1611. }
  1612. parse_field_list :: proc(p: ^Parser, follow: tokenizer.Token_Kind, allowed_flags: ast.Field_Flags) -> (field_list: ^ast.Field_List, total_name_count: int) {
  1613. handle_field :: proc(p: ^Parser,
  1614. seen_ellipsis: ^bool, fields: ^[dynamic]^ast.Field,
  1615. docs: ^ast.Comment_Group,
  1616. names: []^ast.Expr,
  1617. allowed_flags, set_flags: ast.Field_Flags,
  1618. ) -> bool {
  1619. expect_field_separator :: proc(p: ^Parser, param: ^ast.Expr) -> bool {
  1620. tok := p.curr_tok
  1621. if allow_token(p, .Comma) {
  1622. return true
  1623. }
  1624. if allow_token(p, .Semicolon) {
  1625. if !tokenizer.is_newline(tok) {
  1626. error(p, tok.pos, "expected a comma, got a semicolon")
  1627. }
  1628. return true
  1629. }
  1630. return false
  1631. }
  1632. is_type_ellipsis :: proc(type: ^ast.Expr) -> bool {
  1633. if type == nil {
  1634. return false
  1635. }
  1636. _, ok := type.derived.(^ast.Ellipsis)
  1637. return ok
  1638. }
  1639. is_signature := (allowed_flags & ast.Field_Flags_Signature_Params) == ast.Field_Flags_Signature_Params
  1640. any_polymorphic_names := check_procedure_name_list(p, names)
  1641. flags := check_field_flag_prefixes(p, len(names), allowed_flags, set_flags)
  1642. type: ^ast.Expr
  1643. default_value: ^ast.Expr
  1644. tag: tokenizer.Token
  1645. expect_token_after(p, .Colon, "field list")
  1646. if p.curr_tok.kind != .Eq {
  1647. type = parse_var_type(p, allowed_flags)
  1648. tt := ast.unparen_expr(type)
  1649. if is_signature && !any_polymorphic_names {
  1650. if ti, ok := tt.derived.(^ast.Typeid_Type); ok && ti.specialization != nil {
  1651. error(p, tt.pos, "specialization of typeid is not allowed without polymorphic names")
  1652. }
  1653. }
  1654. }
  1655. if allow_token(p, .Eq) {
  1656. default_value = parse_expr(p, false)
  1657. if .Default_Parameters not_in allowed_flags {
  1658. error(p, p.curr_tok.pos, "default parameters are only allowed for procedures")
  1659. default_value = nil
  1660. }
  1661. }
  1662. if default_value != nil && len(names) > 1 {
  1663. error(p, p.curr_tok.pos, "default parameters can only be applied to single values")
  1664. }
  1665. if allowed_flags == ast.Field_Flags_Struct && default_value != nil {
  1666. error(p, default_value.pos, "default parameters are not allowed for structs")
  1667. default_value = nil
  1668. }
  1669. if is_type_ellipsis(type) {
  1670. if seen_ellipsis^ {
  1671. error(p, type.pos, "extra variadic parameter after ellipsis")
  1672. }
  1673. seen_ellipsis^ = true
  1674. if len(names) != 1 {
  1675. error(p, type.pos, "variadic parameters can only have one field name")
  1676. }
  1677. } else if seen_ellipsis^ && default_value == nil {
  1678. error(p, p.curr_tok.pos, "extra parameter after ellipsis without a default value")
  1679. }
  1680. if type != nil && default_value == nil {
  1681. if p.curr_tok.kind == .String {
  1682. tag = expect_token(p, .String)
  1683. if .Tags not_in allowed_flags {
  1684. error(p, tag.pos, "Field tags are only allowed within structures")
  1685. }
  1686. }
  1687. }
  1688. ok := expect_field_separator(p, type)
  1689. field := new_ast_field(names, type, default_value)
  1690. field.tag = tag
  1691. field.docs = docs
  1692. field.flags = flags
  1693. field.comment = p.line_comment
  1694. append(fields, field)
  1695. return ok
  1696. }
  1697. start_tok := p.curr_tok
  1698. docs := p.lead_comment
  1699. fields: [dynamic]^ast.Field
  1700. list: [dynamic]Expr_And_Flags
  1701. defer delete(list)
  1702. seen_ellipsis := false
  1703. allow_typeid_token := .Typeid_Token in allowed_flags
  1704. allow_poly_names := allow_typeid_token
  1705. for p.curr_tok.kind != follow &&
  1706. p.curr_tok.kind != .Colon &&
  1707. p.curr_tok.kind != .EOF {
  1708. prefix_flags := parse_field_prefixes(p)
  1709. param := parse_var_type(p, allowed_flags & {.Typeid_Token, .Ellipsis})
  1710. if _, ok := param.derived.(^ast.Ellipsis); ok {
  1711. if seen_ellipsis {
  1712. error(p, param.pos, "extra variadic parameter after ellipsis")
  1713. }
  1714. seen_ellipsis = true
  1715. } else if seen_ellipsis {
  1716. error(p, param.pos, "extra parameter after ellipsis")
  1717. }
  1718. eaf := Expr_And_Flags{param, prefix_flags}
  1719. append(&list, eaf)
  1720. allow_token(p, .Comma) or_break
  1721. }
  1722. if p.curr_tok.kind != .Colon {
  1723. for eaf in list {
  1724. type := eaf.expr
  1725. tok: tokenizer.Token
  1726. tok.pos = type.pos
  1727. if .Results not_in allowed_flags {
  1728. tok.text = "_"
  1729. }
  1730. names := make([]^ast.Expr, 1)
  1731. names[0] = ast.new(ast.Ident, tok.pos, end_pos(tok))
  1732. #partial switch ident in names[0].derived_expr {
  1733. case ^ast.Ident:
  1734. ident.name = tok.text
  1735. case:
  1736. unreachable()
  1737. }
  1738. flags := check_field_flag_prefixes(p, len(list), allowed_flags, eaf.flags)
  1739. field := new_ast_field(names, type, nil)
  1740. field.docs = docs
  1741. field.flags = flags
  1742. field.comment = p.line_comment
  1743. append(&fields, field)
  1744. }
  1745. } else {
  1746. names := convert_to_ident_list(p, list[:], true, allow_poly_names)
  1747. if len(names) == 0 {
  1748. error(p, p.curr_tok.pos, "empty field declaration")
  1749. }
  1750. set_flags: ast.Field_Flags
  1751. if len(list) > 0 {
  1752. set_flags = list[0].flags
  1753. }
  1754. total_name_count += len(names)
  1755. handle_field(p, &seen_ellipsis, &fields, docs, names, allowed_flags, set_flags)
  1756. for p.curr_tok.kind != follow && p.curr_tok.kind != .EOF {
  1757. docs = p.lead_comment
  1758. set_flags = parse_field_prefixes(p)
  1759. names = parse_ident_list(p, allow_poly_names)
  1760. total_name_count += len(names)
  1761. handle_field(p, &seen_ellipsis, &fields, docs, names, allowed_flags, set_flags) or_break
  1762. }
  1763. }
  1764. field_list = ast.new(ast.Field_List, start_tok.pos, p.curr_tok.pos)
  1765. field_list.list = fields[:]
  1766. return
  1767. }
  1768. parse_results :: proc(p: ^Parser) -> (list: ^ast.Field_List, diverging: bool) {
  1769. if !allow_token(p, .Arrow_Right) {
  1770. return
  1771. }
  1772. if allow_token(p, .Not) {
  1773. diverging = true
  1774. return
  1775. }
  1776. prev_level := p.expr_level
  1777. defer p.expr_level = prev_level
  1778. if p.curr_tok.kind != .Open_Paren {
  1779. type := parse_type(p)
  1780. field := new_ast_field(nil, type, nil)
  1781. list = ast.new(ast.Field_List, field.pos, field.end)
  1782. list.list = make([]^ast.Field, 1)
  1783. list.list[0] = field
  1784. return
  1785. }
  1786. expect_token(p, .Open_Paren)
  1787. list, _ = parse_field_list(p, .Close_Paren, ast.Field_Flags_Signature_Results)
  1788. expect_token_after(p, .Close_Paren, "parameter list")
  1789. return
  1790. }
  1791. string_to_calling_convention :: proc(s: string) -> ast.Proc_Calling_Convention {
  1792. if s[0] != '"' && s[0] != '`' {
  1793. return nil
  1794. }
  1795. if len(s) == 2 {
  1796. return nil
  1797. }
  1798. return s
  1799. }
  1800. parse_proc_tags :: proc(p: ^Parser) -> (tags: ast.Proc_Tags) {
  1801. for p.curr_tok.kind == .Hash {
  1802. _ = expect_token(p, .Hash)
  1803. ident := expect_token(p, .Ident)
  1804. switch ident.text {
  1805. case "bounds_check": tags += {.Bounds_Check}
  1806. case "no_bounds_check": tags += {.No_Bounds_Check}
  1807. case "optional_ok": tags += {.Optional_Ok}
  1808. case "optional_allocator_error": tags += {.Optional_Allocator_Error}
  1809. case:
  1810. }
  1811. }
  1812. if .Bounds_Check in tags && .No_Bounds_Check in tags {
  1813. p.err(p.curr_tok.pos, "#bounds_check and #no_bounds_check applied to the same procedure type")
  1814. }
  1815. return
  1816. }
  1817. parse_proc_type :: proc(p: ^Parser, tok: tokenizer.Token) -> ^ast.Proc_Type {
  1818. cc: ast.Proc_Calling_Convention
  1819. if p.curr_tok.kind == .String {
  1820. str := expect_token(p, .String)
  1821. cc = string_to_calling_convention(str.text)
  1822. if cc == nil {
  1823. error(p, str.pos, "unknown calling convention '%s'", str.text)
  1824. }
  1825. }
  1826. if cc == nil && p.in_foreign_block {
  1827. cc = .Foreign_Block_Default
  1828. }
  1829. expect_token(p, .Open_Paren)
  1830. p.expr_level += 1
  1831. params, _ := parse_field_list(p, .Close_Paren, ast.Field_Flags_Signature_Params)
  1832. p.expr_level -= 1
  1833. expect_closing_parentheses_of_field_list(p)
  1834. results, diverging := parse_results(p)
  1835. is_generic := false
  1836. loop: for param in params.list {
  1837. if param.type != nil {
  1838. if _, ok := param.type.derived.(^ast.Poly_Type); ok {
  1839. is_generic = true
  1840. break loop
  1841. }
  1842. for name in param.names {
  1843. if _, ok := name.derived.(^ast.Poly_Type); ok {
  1844. is_generic = true
  1845. break loop
  1846. }
  1847. }
  1848. }
  1849. }
  1850. end := end_pos(p.prev_tok)
  1851. pt := ast.new(ast.Proc_Type, tok.pos, end)
  1852. pt.tok = tok
  1853. pt.calling_convention = cc
  1854. pt.params = params
  1855. pt.results = results
  1856. pt.diverging = diverging
  1857. pt.generic = is_generic
  1858. return pt
  1859. }
  1860. parse_inlining_operand :: proc(p: ^Parser, lhs: bool, tok: tokenizer.Token) -> ^ast.Expr {
  1861. expr := parse_unary_expr(p, lhs)
  1862. pi := ast.Proc_Inlining.None
  1863. #partial switch tok.kind {
  1864. case .Inline:
  1865. pi = .Inline
  1866. case .No_Inline:
  1867. pi = .No_Inline
  1868. case .Ident:
  1869. switch tok.text {
  1870. case "force_inline":
  1871. pi = .Inline
  1872. case "force_no_inline":
  1873. pi = .No_Inline
  1874. }
  1875. }
  1876. if expr != nil {
  1877. #partial switch e in ast.strip_or_return_expr(expr).derived_expr {
  1878. case ^ast.Proc_Lit:
  1879. if e.inlining != .None && e.inlining != pi {
  1880. error(p, expr.pos, "both 'inline' and 'no_inline' cannot be applied to a procedure literal")
  1881. }
  1882. e.inlining = pi
  1883. return expr
  1884. case ^ast.Call_Expr:
  1885. if e.inlining != .None && e.inlining != pi {
  1886. error(p, expr.pos, "both 'inline' and 'no_inline' cannot be applied to a procedure call")
  1887. }
  1888. e.inlining = pi
  1889. return expr
  1890. }
  1891. }
  1892. error(p, tok.pos, "'%s' must be followed by a procedure literal or call", tok.text)
  1893. return ast.new(ast.Bad_Expr, tok.pos, expr)
  1894. }
  1895. parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
  1896. #partial switch p.curr_tok.kind {
  1897. case .Ident:
  1898. return parse_ident(p)
  1899. case .Undef:
  1900. tok := expect_token(p, .Undef)
  1901. undef := ast.new(ast.Undef, tok.pos, end_pos(tok))
  1902. undef.tok = tok.kind
  1903. return undef
  1904. case .Context:
  1905. tok := expect_token(p, .Context)
  1906. ctx := ast.new(ast.Implicit, tok.pos, end_pos(tok))
  1907. ctx.tok = tok
  1908. return ctx
  1909. case .Integer, .Float, .Imag,
  1910. .Rune, .String:
  1911. tok := advance_token(p)
  1912. bl := ast.new(ast.Basic_Lit, tok.pos, end_pos(tok))
  1913. bl.tok = tok
  1914. return bl
  1915. case .Open_Brace:
  1916. if !lhs {
  1917. return parse_literal_value(p, nil)
  1918. }
  1919. case .Open_Paren:
  1920. open := expect_token(p, .Open_Paren)
  1921. p.expr_level += 1
  1922. expr := parse_expr(p, false)
  1923. p.expr_level -= 1
  1924. close := expect_token(p, .Close_Paren)
  1925. pe := ast.new(ast.Paren_Expr, open.pos, end_pos(close))
  1926. pe.open = open.pos
  1927. pe.expr = expr
  1928. pe.close = close.pos
  1929. return pe
  1930. case .Distinct:
  1931. tok := advance_token(p)
  1932. type := parse_type(p)
  1933. dt := ast.new(ast.Distinct_Type, tok.pos, type)
  1934. dt.tok = tok.kind
  1935. dt.type = type
  1936. return dt
  1937. case .Hash:
  1938. tok := expect_token(p, .Hash)
  1939. name := expect_token(p, .Ident)
  1940. switch name.text {
  1941. case "type":
  1942. type := parse_type(p)
  1943. hp := ast.new(ast.Helper_Type, tok.pos, type)
  1944. hp.tok = tok.kind
  1945. hp.type = type
  1946. return hp
  1947. case "file", "directory", "line", "procedure", "caller_location":
  1948. bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
  1949. bd.tok = tok
  1950. bd.name = name.text
  1951. return bd
  1952. case "caller_expression":
  1953. bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
  1954. bd.tok = tok
  1955. bd.name = name.text
  1956. if peek_token_kind(p, .Open_Paren) {
  1957. return parse_call_expr(p, bd)
  1958. }
  1959. return bd
  1960. case "location", "exists", "load", "load_directory", "load_hash", "hash", "assert", "panic", "defined", "config":
  1961. bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
  1962. bd.tok = tok
  1963. bd.name = name.text
  1964. return parse_call_expr(p, bd)
  1965. case "soa":
  1966. bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
  1967. bd.tok = tok
  1968. bd.name = name.text
  1969. original_type := parse_type(p)
  1970. type := ast.unparen_expr(original_type)
  1971. #partial switch t in type.derived_expr {
  1972. case ^ast.Array_Type: t.tag = bd
  1973. case ^ast.Dynamic_Array_Type: t.tag = bd
  1974. case ^ast.Pointer_Type: t.tag = bd
  1975. case:
  1976. error(p, original_type.pos, "expected an array or pointer type after #%s", name.text)
  1977. }
  1978. return original_type
  1979. case "simd":
  1980. bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
  1981. bd.tok = tok
  1982. bd.name = name.text
  1983. original_type := parse_type(p)
  1984. type := ast.unparen_expr(original_type)
  1985. #partial switch t in type.derived_expr {
  1986. case ^ast.Array_Type: t.tag = bd
  1987. case:
  1988. error(p, original_type.pos, "expected an array type after #%s", name.text)
  1989. }
  1990. return original_type
  1991. case "partial":
  1992. tag := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
  1993. tag.tok = tok
  1994. tag.name = name.text
  1995. original_expr := parse_expr(p, lhs)
  1996. expr := ast.unparen_expr(original_expr)
  1997. #partial switch t in expr.derived_expr {
  1998. case ^ast.Comp_Lit:
  1999. t.tag = tag
  2000. case ^ast.Array_Type:
  2001. t.tag = tag
  2002. error(p, tok.pos, "#%s has been replaced with #sparse for non-contiguous enumerated array types", name.text)
  2003. case:
  2004. error(p, tok.pos, "expected a compound literal after #%s", name.text)
  2005. }
  2006. return original_expr
  2007. case "sparse":
  2008. tag := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
  2009. tag.tok = tok
  2010. tag.name = name.text
  2011. original_type := parse_type(p)
  2012. type := ast.unparen_expr(original_type)
  2013. #partial switch t in type.derived_expr {
  2014. case ^ast.Array_Type:
  2015. t.tag = tag
  2016. case:
  2017. error(p, tok.pos, "expected an enumerated array type after #%s", name.text)
  2018. }
  2019. return original_type
  2020. case "bounds_check", "no_bounds_check":
  2021. operand := parse_expr(p, lhs)
  2022. switch name.text {
  2023. case "bounds_check":
  2024. operand.state_flags += {.Bounds_Check}
  2025. if .No_Bounds_Check in operand.state_flags {
  2026. error(p, name.pos, "#bounds_check and #no_bounds_check cannot be applied together")
  2027. }
  2028. case "no_bounds_check":
  2029. operand.state_flags += {.No_Bounds_Check}
  2030. if .Bounds_Check in operand.state_flags {
  2031. error(p, name.pos, "#bounds_check and #no_bounds_check cannot be applied together")
  2032. }
  2033. case: unimplemented()
  2034. }
  2035. return operand
  2036. case "relative":
  2037. tag := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
  2038. tag.tok = tok
  2039. tag.name = name.text
  2040. tag_call := parse_call_expr(p, tag)
  2041. type := parse_type(p)
  2042. rt := ast.new(ast.Relative_Type, tok.pos, type)
  2043. rt.tag = tag_call
  2044. rt.type = type
  2045. return rt
  2046. case "force_inline", "force_no_inline":
  2047. return parse_inlining_operand(p, lhs, name)
  2048. case:
  2049. expr := parse_expr(p, lhs)
  2050. end := expr.pos if expr != nil else end_pos(tok)
  2051. te := ast.new(ast.Tag_Expr, tok.pos, end)
  2052. te.op = tok
  2053. te.name = name.text
  2054. te.expr = expr
  2055. return te
  2056. }
  2057. case .Inline, .No_Inline:
  2058. tok := advance_token(p)
  2059. return parse_inlining_operand(p, lhs, tok)
  2060. case .Proc:
  2061. tok := expect_token(p, .Proc)
  2062. if p.curr_tok.kind == .Open_Brace {
  2063. open := expect_token(p, .Open_Brace)
  2064. args: [dynamic]^ast.Expr
  2065. for p.curr_tok.kind != .Close_Brace &&
  2066. p.curr_tok.kind != .EOF {
  2067. elem := parse_expr(p, false)
  2068. append(&args, elem)
  2069. allow_token(p, .Comma) or_break
  2070. }
  2071. close := expect_token(p, .Close_Brace)
  2072. if len(args) == 0 {
  2073. error(p, tok.pos, "expected at least 1 argument in procedure group")
  2074. }
  2075. pg := ast.new(ast.Proc_Group, tok.pos, end_pos(close))
  2076. pg.tok = tok
  2077. pg.open = open.pos
  2078. pg.args = args[:]
  2079. pg.close = close.pos
  2080. return pg
  2081. }
  2082. type := parse_proc_type(p, tok)
  2083. tags: ast.Proc_Tags
  2084. where_token: tokenizer.Token
  2085. where_clauses: []^ast.Expr
  2086. skip_possible_newline_for_literal(p)
  2087. if p.curr_tok.kind == .Where {
  2088. where_token = expect_token(p, .Where)
  2089. prev_level := p.expr_level
  2090. p.expr_level = -1
  2091. where_clauses = parse_rhs_expr_list(p)
  2092. p.expr_level = prev_level
  2093. }
  2094. tags = parse_proc_tags(p)
  2095. type.tags = tags
  2096. if p.allow_type && p.expr_level < 0 {
  2097. if where_token.kind != .Invalid {
  2098. error(p, where_token.pos, "'where' clauses are not allowed on procedure types")
  2099. }
  2100. return type
  2101. }
  2102. body: ^ast.Stmt
  2103. skip_possible_newline_for_literal(p)
  2104. if allow_token(p, .Undef) {
  2105. body = nil
  2106. if where_token.kind != .Invalid {
  2107. error(p, where_token.pos, "'where' clauses are not allowed on procedure literals without a defined body (replaced with ---")
  2108. }
  2109. } else if p.curr_tok.kind == .Open_Brace {
  2110. prev_proc := p.curr_proc
  2111. p.curr_proc = type
  2112. body = parse_body(p)
  2113. p.curr_proc = prev_proc
  2114. } else if allow_token(p, .Do) {
  2115. prev_proc := p.curr_proc
  2116. p.curr_proc = type
  2117. body = convert_stmt_to_body(p, parse_stmt(p))
  2118. p.curr_proc = prev_proc
  2119. if type.pos.line != body.pos.line {
  2120. error(p, body.pos, "the body of a 'do' must be on the same line as the signature")
  2121. }
  2122. } else {
  2123. return type
  2124. }
  2125. pl := ast.new(ast.Proc_Lit, tok.pos, end_pos(p.prev_tok))
  2126. pl.type = type
  2127. pl.body = body
  2128. pl.tags = tags
  2129. pl.where_token = where_token
  2130. pl.where_clauses = where_clauses
  2131. return pl
  2132. case .Dollar:
  2133. tok := advance_token(p)
  2134. type := parse_ident(p)
  2135. end := type.end
  2136. specialization: ^ast.Expr
  2137. if allow_token(p, .Quo) {
  2138. specialization = parse_type(p)
  2139. end = specialization.pos
  2140. }
  2141. if is_blank_ident(type) {
  2142. error(p, type.pos, "invalid polymorphic type definition with a blank identifier")
  2143. }
  2144. pt := ast.new(ast.Poly_Type, tok.pos, end)
  2145. pt.dollar = tok.pos
  2146. pt.type = type
  2147. pt.specialization = specialization
  2148. return pt
  2149. case .Typeid:
  2150. tok := advance_token(p)
  2151. ti := ast.new(ast.Typeid_Type, tok.pos, end_pos(tok))
  2152. ti.tok = tok.kind
  2153. ti.specialization = nil
  2154. return ti
  2155. case .Pointer:
  2156. tok := expect_token(p, .Pointer)
  2157. elem := parse_type(p)
  2158. ptr := ast.new(ast.Pointer_Type, tok.pos, elem)
  2159. ptr.pointer = tok.pos
  2160. ptr.elem = elem
  2161. return ptr
  2162. case .Open_Bracket:
  2163. open := expect_token(p, .Open_Bracket)
  2164. count: ^ast.Expr
  2165. #partial switch p.curr_tok.kind {
  2166. case .Pointer:
  2167. tok := expect_token(p, .Pointer)
  2168. close := expect_token(p, .Close_Bracket)
  2169. elem := parse_type(p)
  2170. t := ast.new(ast.Multi_Pointer_Type, open.pos, elem)
  2171. t.open = open.pos
  2172. t.pointer = tok.pos
  2173. t.close = close.pos
  2174. t.elem = elem
  2175. return t
  2176. case .Dynamic:
  2177. tok := expect_token(p, .Dynamic)
  2178. close := expect_token(p, .Close_Bracket)
  2179. elem := parse_type(p)
  2180. da := ast.new(ast.Dynamic_Array_Type, open.pos, elem)
  2181. da.open = open.pos
  2182. da.dynamic_pos = tok.pos
  2183. da.close = close.pos
  2184. da.elem = elem
  2185. return da
  2186. case .Question:
  2187. tok := expect_token(p, .Question)
  2188. q := ast.new(ast.Unary_Expr, tok.pos, end_pos(tok))
  2189. q.op = tok
  2190. count = q
  2191. case:
  2192. p.expr_level += 1
  2193. count = parse_expr(p, false)
  2194. p.expr_level -= 1
  2195. case .Close_Bracket:
  2196. // handle below
  2197. }
  2198. close := expect_token(p, .Close_Bracket)
  2199. elem := parse_type(p)
  2200. at := ast.new(ast.Array_Type, open.pos, elem)
  2201. at.open = open.pos
  2202. at.len = count
  2203. at.close = close.pos
  2204. at.elem = elem
  2205. return at
  2206. case .Map:
  2207. tok := expect_token(p, .Map)
  2208. expect_token(p, .Open_Bracket)
  2209. key := parse_type(p)
  2210. expect_token(p, .Close_Bracket)
  2211. value := parse_type(p)
  2212. mt := ast.new(ast.Map_Type, tok.pos, value)
  2213. mt.tok_pos = tok.pos
  2214. mt.key = key
  2215. mt.value = value
  2216. return mt
  2217. case .Struct:
  2218. tok := expect_token(p, .Struct)
  2219. poly_params: ^ast.Field_List
  2220. align: ^ast.Expr
  2221. min_field_align: ^ast.Expr
  2222. max_field_align: ^ast.Expr
  2223. is_packed: bool
  2224. is_raw_union: bool
  2225. is_no_copy: bool
  2226. fields: ^ast.Field_List
  2227. name_count: int
  2228. if allow_token(p, .Open_Paren) {
  2229. param_count: int
  2230. poly_params, param_count = parse_field_list(p, .Close_Paren, ast.Field_Flags_Record_Poly_Params)
  2231. if param_count == 0 {
  2232. error(p, poly_params.pos, "expected at least 1 polymorphic parameter")
  2233. poly_params = nil
  2234. }
  2235. expect_token_after(p, .Close_Paren, "parameter list")
  2236. }
  2237. prev_level := p.expr_level
  2238. p.expr_level = -1
  2239. for allow_token(p, .Hash) {
  2240. tag := expect_token_after(p, .Ident, "#")
  2241. switch tag.text {
  2242. case "packed":
  2243. if is_packed {
  2244. error(p, tag.pos, "duplicate struct tag '#%s'", tag.text)
  2245. }
  2246. is_packed = true
  2247. case "align":
  2248. if align != nil {
  2249. error(p, tag.pos, "duplicate struct tag '#%s'", tag.text)
  2250. }
  2251. align = parse_expr(p, true)
  2252. case "field_align":
  2253. if min_field_align != nil {
  2254. error(p, tag.pos, "duplicate struct tag '#%s'", tag.text)
  2255. }
  2256. warn(p, tag.pos, "#field_align has been deprecated in favour of #min_field_align")
  2257. min_field_align = parse_expr(p, true)
  2258. case "min_field_align":
  2259. if min_field_align != nil {
  2260. error(p, tag.pos, "duplicate struct tag '#%s'", tag.text)
  2261. }
  2262. min_field_align = parse_expr(p, true)
  2263. case "max_field_align":
  2264. if max_field_align != nil {
  2265. error(p, tag.pos, "duplicate struct tag '#%s'", tag.text)
  2266. }
  2267. max_field_align = parse_expr(p, true)
  2268. case "raw_union":
  2269. if is_raw_union {
  2270. error(p, tag.pos, "duplicate struct tag '#%s'", tag.text)
  2271. }
  2272. is_raw_union = true
  2273. case "no_copy":
  2274. if is_no_copy {
  2275. error(p, tag.pos, "duplicate struct tag '#%s'", tag.text)
  2276. }
  2277. is_no_copy = true
  2278. case:
  2279. error(p, tag.pos, "invalid struct tag '#%s", tag.text)
  2280. }
  2281. }
  2282. p.expr_level = prev_level
  2283. if is_raw_union && is_packed {
  2284. is_packed = false
  2285. error(p, tok.pos, "'#raw_union' cannot also be '#packed")
  2286. }
  2287. where_token: tokenizer.Token
  2288. where_clauses: []^ast.Expr
  2289. skip_possible_newline_for_literal(p)
  2290. if p.curr_tok.kind == .Where {
  2291. where_token = expect_token(p, .Where)
  2292. where_prev_level := p.expr_level
  2293. p.expr_level = -1
  2294. where_clauses = parse_rhs_expr_list(p)
  2295. p.expr_level = where_prev_level
  2296. }
  2297. skip_possible_newline_for_literal(p)
  2298. expect_token(p, .Open_Brace)
  2299. fields, name_count = parse_field_list(p, .Close_Brace, ast.Field_Flags_Struct)
  2300. close := expect_closing_brace_of_field_list(p)
  2301. st := ast.new(ast.Struct_Type, tok.pos, end_pos(close))
  2302. st.poly_params = poly_params
  2303. st.align = align
  2304. st.min_field_align = min_field_align
  2305. st.max_field_align = max_field_align
  2306. st.is_packed = is_packed
  2307. st.is_raw_union = is_raw_union
  2308. st.is_no_copy = is_no_copy
  2309. st.fields = fields
  2310. st.name_count = name_count
  2311. st.where_token = where_token
  2312. st.where_clauses = where_clauses
  2313. return st
  2314. case .Union:
  2315. tok := expect_token(p, .Union)
  2316. poly_params: ^ast.Field_List
  2317. align: ^ast.Expr
  2318. is_no_nil: bool
  2319. is_shared_nil: bool
  2320. if allow_token(p, .Open_Paren) {
  2321. param_count: int
  2322. poly_params, param_count = parse_field_list(p, .Close_Paren, ast.Field_Flags_Record_Poly_Params)
  2323. if param_count == 0 {
  2324. error(p, poly_params.pos, "expected at least 1 polymorphic parameter")
  2325. poly_params = nil
  2326. }
  2327. expect_token_after(p, .Close_Paren, "parameter list")
  2328. }
  2329. prev_level := p.expr_level
  2330. p.expr_level = -1
  2331. for allow_token(p, .Hash) {
  2332. tag := expect_token_after(p, .Ident, "#")
  2333. switch tag.text {
  2334. case "align":
  2335. if align != nil {
  2336. error(p, tag.pos, "duplicate union tag '#%s'", tag.text)
  2337. }
  2338. align = parse_expr(p, true)
  2339. case "maybe":
  2340. error(p, tag.pos, "#%s functionality has now been merged with standard 'union' functionality", tag.text)
  2341. case "no_nil":
  2342. if is_no_nil {
  2343. error(p, tag.pos, "duplicate union tag '#%s'", tag.text)
  2344. }
  2345. is_no_nil = true
  2346. case "shared_nil":
  2347. if is_shared_nil {
  2348. error(p, tag.pos, "duplicate union tag '#%s'", tag.text)
  2349. }
  2350. is_shared_nil = true
  2351. case:
  2352. error(p, tag.pos, "invalid union tag '#%s", tag.text)
  2353. }
  2354. }
  2355. p.expr_level = prev_level
  2356. if is_no_nil && is_shared_nil {
  2357. error(p, p.curr_tok.pos, "#shared_nil and #no_nil cannot be applied together")
  2358. }
  2359. union_kind := ast.Union_Type_Kind.Normal
  2360. switch {
  2361. case is_no_nil: union_kind = .no_nil
  2362. case is_shared_nil: union_kind = .shared_nil
  2363. }
  2364. where_token: tokenizer.Token
  2365. where_clauses: []^ast.Expr
  2366. skip_possible_newline_for_literal(p)
  2367. if p.curr_tok.kind == .Where {
  2368. where_token = expect_token(p, .Where)
  2369. where_prev_level := p.expr_level
  2370. p.expr_level = -1
  2371. where_clauses = parse_rhs_expr_list(p)
  2372. p.expr_level = where_prev_level
  2373. }
  2374. skip_possible_newline_for_literal(p)
  2375. expect_token_after(p, .Open_Brace, "union")
  2376. variants: [dynamic]^ast.Expr
  2377. for p.curr_tok.kind != .Close_Brace && p.curr_tok.kind != .EOF {
  2378. type := parse_type(p)
  2379. if _, ok := type.derived.(^ast.Bad_Expr); !ok {
  2380. append(&variants, type)
  2381. }
  2382. allow_token(p, .Comma) or_break
  2383. }
  2384. close := expect_closing_brace_of_field_list(p)
  2385. ut := ast.new(ast.Union_Type, tok.pos, end_pos(close))
  2386. ut.poly_params = poly_params
  2387. ut.variants = variants[:]
  2388. ut.align = align
  2389. ut.where_token = where_token
  2390. ut.where_clauses = where_clauses
  2391. ut.kind = union_kind
  2392. return ut
  2393. case .Enum:
  2394. tok := expect_token(p, .Enum)
  2395. base_type: ^ast.Expr
  2396. if p.curr_tok.kind != .Open_Brace {
  2397. base_type = parse_type(p)
  2398. }
  2399. skip_possible_newline_for_literal(p)
  2400. open := expect_token(p, .Open_Brace)
  2401. fields := parse_elem_list(p)
  2402. close := expect_closing_brace_of_field_list(p)
  2403. et := ast.new(ast.Enum_Type, tok.pos, end_pos(close))
  2404. et.base_type = base_type
  2405. et.open = open.pos
  2406. et.fields = fields
  2407. et.close = close.pos
  2408. return et
  2409. case .Bit_Set:
  2410. tok := expect_token(p, .Bit_Set)
  2411. open := expect_token(p, .Open_Bracket)
  2412. elem, underlying: ^ast.Expr
  2413. prev_allow_range := p.allow_range
  2414. p.allow_range = true
  2415. elem = parse_expr(p, false)
  2416. p.allow_range = prev_allow_range
  2417. if allow_token(p, .Semicolon) {
  2418. underlying = parse_type(p)
  2419. }
  2420. close := expect_token(p, .Close_Bracket)
  2421. bst := ast.new(ast.Bit_Set_Type, tok.pos, end_pos(close))
  2422. bst.tok_pos = tok.pos
  2423. bst.open = open.pos
  2424. bst.elem = elem
  2425. bst.underlying = underlying
  2426. bst.close = close.pos
  2427. return bst
  2428. case .Matrix:
  2429. tok := expect_token(p, .Matrix)
  2430. expect_token(p, .Open_Bracket)
  2431. row_count := parse_expr(p, false)
  2432. expect_token(p, .Comma)
  2433. column_count := parse_expr(p, false)
  2434. expect_token(p, .Close_Bracket)
  2435. elem := parse_type(p)
  2436. mt := ast.new(ast.Matrix_Type, tok.pos, elem)
  2437. mt.tok_pos = tok.pos
  2438. mt.row_count = row_count
  2439. mt.column_count = column_count
  2440. mt.elem = elem
  2441. return mt
  2442. case .Bit_Field:
  2443. tok := expect_token(p, .Bit_Field)
  2444. backing_type := parse_type_or_ident(p)
  2445. if backing_type == nil {
  2446. token := advance_token(p)
  2447. error(p, token.pos, "Expected a backing type for a 'bit_field'")
  2448. }
  2449. skip_possible_newline_for_literal(p)
  2450. open := expect_token_after(p, .Open_Brace, "bit_field")
  2451. fields: [dynamic]^ast.Bit_Field_Field
  2452. for p.curr_tok.kind != .Close_Brace && p.curr_tok.kind != .EOF {
  2453. name := parse_ident(p)
  2454. expect_token(p, .Colon)
  2455. type := parse_type(p)
  2456. expect_token(p, .Or)
  2457. bit_size := parse_expr(p, true)
  2458. tag: tokenizer.Token
  2459. if p.curr_tok.kind == .String {
  2460. tag = expect_token(p, .String)
  2461. }
  2462. field := ast.new(ast.Bit_Field_Field, name.pos, bit_size)
  2463. field.name = name
  2464. field.type = type
  2465. field.bit_size = bit_size
  2466. field.tag = tag
  2467. append(&fields, field)
  2468. allow_token(p, .Comma) or_break
  2469. }
  2470. close := expect_closing_brace_of_field_list(p)
  2471. bf := ast.new(ast.Bit_Field_Type, tok.pos, end_pos(close))
  2472. bf.tok_pos = tok.pos
  2473. bf.backing_type = backing_type
  2474. bf.open = open.pos
  2475. bf.fields = fields[:]
  2476. bf.close = close.pos
  2477. return bf
  2478. case .Asm:
  2479. tok := expect_token(p, .Asm)
  2480. param_types: [dynamic]^ast.Expr
  2481. return_type: ^ast.Expr
  2482. if allow_token(p, .Open_Paren) {
  2483. for p.curr_tok.kind != .Close_Paren && p.curr_tok.kind != .EOF {
  2484. t := parse_type(p)
  2485. append(&param_types, t)
  2486. if p.curr_tok.kind != .Comma ||
  2487. p.curr_tok.kind == .EOF {
  2488. break
  2489. }
  2490. advance_token(p)
  2491. }
  2492. expect_token(p, .Close_Paren)
  2493. if allow_token(p, .Arrow_Right) {
  2494. return_type = parse_type(p)
  2495. }
  2496. }
  2497. has_side_effects := false
  2498. is_align_stack := false
  2499. dialect := ast.Inline_Asm_Dialect.Default
  2500. for allow_token(p, .Hash) {
  2501. if p.curr_tok.kind == .Ident {
  2502. name := advance_token(p)
  2503. switch name.text {
  2504. case "side_effects":
  2505. if has_side_effects {
  2506. error(p, tok.pos, "duplicate directive on inline asm expression: '#side_effects'")
  2507. }
  2508. has_side_effects = true
  2509. case "align_stack":
  2510. if is_align_stack {
  2511. error(p, tok.pos, "duplicate directive on inline asm expression: '#align_stack'")
  2512. }
  2513. is_align_stack = true
  2514. case "att":
  2515. if dialect == .ATT {
  2516. error(p, tok.pos, "duplicate directive on inline asm expression: '#att'")
  2517. } else if dialect != .Default {
  2518. error(p, tok.pos, "conflicting asm dialects")
  2519. } else {
  2520. dialect = .ATT
  2521. }
  2522. case "intel":
  2523. if dialect == .Intel {
  2524. error(p, tok.pos, "duplicate directive on inline asm expression: '#intel'")
  2525. } else if dialect != .Default {
  2526. error(p, tok.pos, "conflicting asm dialects")
  2527. } else {
  2528. dialect = .Intel
  2529. }
  2530. }
  2531. } else {
  2532. error(p, p.curr_tok.pos, "expected an identifier after hash")
  2533. }
  2534. }
  2535. skip_possible_newline_for_literal(p)
  2536. open := expect_token(p, .Open_Brace)
  2537. asm_string := parse_expr(p, false)
  2538. expect_token(p, .Comma)
  2539. constraints_string := parse_expr(p, false)
  2540. allow_token(p, .Comma)
  2541. close := expect_closing_brace_of_field_list(p)
  2542. e := ast.new(ast.Inline_Asm_Expr, tok.pos, end_pos(close))
  2543. e.tok = tok
  2544. e.param_types = param_types[:]
  2545. e.return_type = return_type
  2546. e.constraints_string = constraints_string
  2547. e.has_side_effects = has_side_effects
  2548. e.is_align_stack = is_align_stack
  2549. e.dialect = dialect
  2550. e.open = open.pos
  2551. e.asm_string = asm_string
  2552. e.close = close.pos
  2553. return e
  2554. }
  2555. return nil
  2556. }
  2557. is_literal_type :: proc(expr: ^ast.Expr) -> bool {
  2558. val := ast.unparen_expr(expr)
  2559. if val == nil {
  2560. return false
  2561. }
  2562. #partial switch _ in val.derived_expr {
  2563. case ^ast.Bad_Expr,
  2564. ^ast.Ident,
  2565. ^ast.Selector_Expr,
  2566. ^ast.Array_Type,
  2567. ^ast.Struct_Type,
  2568. ^ast.Union_Type,
  2569. ^ast.Enum_Type,
  2570. ^ast.Dynamic_Array_Type,
  2571. ^ast.Map_Type,
  2572. ^ast.Bit_Set_Type,
  2573. ^ast.Matrix_Type,
  2574. ^ast.Call_Expr,
  2575. ^ast.Bit_Field_Type:
  2576. return true
  2577. }
  2578. return false
  2579. }
  2580. parse_value :: proc(p: ^Parser) -> ^ast.Expr {
  2581. if p.curr_tok.kind == .Open_Brace {
  2582. return parse_literal_value(p, nil)
  2583. }
  2584. prev_allow_range := p.allow_range
  2585. defer p.allow_range = prev_allow_range
  2586. p.allow_range = true
  2587. return parse_expr(p, false)
  2588. }
  2589. parse_elem_list :: proc(p: ^Parser) -> []^ast.Expr {
  2590. elems: [dynamic]^ast.Expr
  2591. for p.curr_tok.kind != .Close_Brace && p.curr_tok.kind != .EOF {
  2592. elem := parse_value(p)
  2593. if p.curr_tok.kind == .Eq {
  2594. eq := expect_token(p, .Eq)
  2595. value := parse_value(p)
  2596. fv := ast.new(ast.Field_Value, elem.pos, value)
  2597. fv.field = elem
  2598. fv.sep = eq.pos
  2599. fv.value = value
  2600. elem = fv
  2601. }
  2602. append(&elems, elem)
  2603. allow_token(p, .Comma) or_break
  2604. }
  2605. return elems[:]
  2606. }
  2607. parse_literal_value :: proc(p: ^Parser, type: ^ast.Expr) -> ^ast.Comp_Lit {
  2608. elems: []^ast.Expr
  2609. open := expect_token(p, .Open_Brace)
  2610. p.expr_level += 1
  2611. if p.curr_tok.kind != .Close_Brace {
  2612. elems = parse_elem_list(p)
  2613. }
  2614. p.expr_level -= 1
  2615. skip_possible_newline(p)
  2616. close := expect_closing_brace_of_field_list(p)
  2617. pos := type.pos if type != nil else open.pos
  2618. lit := ast.new(ast.Comp_Lit, pos, end_pos(close))
  2619. lit.type = type
  2620. lit.open = open.pos
  2621. lit.elems = elems
  2622. lit.close = close.pos
  2623. return lit
  2624. }
  2625. parse_call_expr :: proc(p: ^Parser, operand: ^ast.Expr) -> ^ast.Expr {
  2626. args: [dynamic]^ast.Expr
  2627. ellipsis: tokenizer.Token
  2628. p.expr_level += 1
  2629. open := expect_token(p, .Open_Paren)
  2630. seen_ellipsis := false
  2631. for p.curr_tok.kind != .Close_Paren &&
  2632. p.curr_tok.kind != .EOF {
  2633. if p.curr_tok.kind == .Comma {
  2634. error(p, p.curr_tok.pos, "expected an expression not ,")
  2635. } else if p.curr_tok.kind == .Eq {
  2636. error(p, p.curr_tok.pos, "expected an expression not =")
  2637. }
  2638. prefix_ellipsis := false
  2639. if p.curr_tok.kind == .Ellipsis {
  2640. prefix_ellipsis = true
  2641. ellipsis = expect_token(p, .Ellipsis)
  2642. }
  2643. arg := parse_expr(p, false)
  2644. if p.curr_tok.kind == .Eq {
  2645. eq := expect_token(p, .Eq)
  2646. if prefix_ellipsis {
  2647. error(p, ellipsis.pos, "'..' must be applied to value rather than a field name")
  2648. }
  2649. value := parse_value(p)
  2650. fv := ast.new(ast.Field_Value, arg.pos, value)
  2651. fv.field = arg
  2652. fv.sep = eq.pos
  2653. fv.value = value
  2654. arg = fv
  2655. } else if seen_ellipsis {
  2656. error(p, arg.pos, "Positional arguments are not allowed after '..'")
  2657. }
  2658. append(&args, arg)
  2659. if ellipsis.pos.line != 0 {
  2660. seen_ellipsis = true
  2661. }
  2662. allow_token(p, .Comma) or_break
  2663. }
  2664. close := expect_closing_token_of_field_list(p, .Close_Paren, "argument list")
  2665. p.expr_level -= 1
  2666. ce := ast.new(ast.Call_Expr, operand.pos, end_pos(close))
  2667. ce.expr = operand
  2668. ce.open = open.pos
  2669. ce.args = args[:]
  2670. ce.ellipsis = ellipsis
  2671. ce.close = close.pos
  2672. o := ast.unparen_expr(operand)
  2673. if se, ok := o.derived.(^ast.Selector_Expr); ok && se.op.kind == .Arrow_Right {
  2674. sce := ast.new(ast.Selector_Call_Expr, ce.pos, ce)
  2675. sce.expr = o
  2676. sce.call = ce
  2677. return sce
  2678. }
  2679. return ce
  2680. }
  2681. parse_atom_expr :: proc(p: ^Parser, value: ^ast.Expr, lhs: bool) -> (operand: ^ast.Expr) {
  2682. operand = value
  2683. if operand == nil {
  2684. if p.allow_type {
  2685. return nil
  2686. }
  2687. error(p, p.curr_tok.pos, "expected an operand")
  2688. fix_advance_to_next_stmt(p)
  2689. be := ast.new(ast.Bad_Expr, p.curr_tok.pos, end_pos(p.curr_tok))
  2690. operand = be
  2691. }
  2692. loop := true
  2693. is_lhs := lhs
  2694. for loop {
  2695. #partial switch p.curr_tok.kind {
  2696. case:
  2697. loop = false
  2698. case .Open_Paren:
  2699. operand = parse_call_expr(p, operand)
  2700. case .Open_Bracket:
  2701. prev_allow_range := p.allow_range
  2702. defer p.allow_range = prev_allow_range
  2703. p.allow_range = false
  2704. indices: [2]^ast.Expr
  2705. interval: tokenizer.Token
  2706. is_slice_op := false
  2707. p.expr_level += 1
  2708. open := expect_token(p, .Open_Bracket)
  2709. #partial switch p.curr_tok.kind {
  2710. case .Colon, .Ellipsis, .Range_Half, .Range_Full:
  2711. // NOTE(bill): Do not err yet
  2712. break
  2713. case:
  2714. indices[0] = parse_expr(p, false)
  2715. }
  2716. #partial switch p.curr_tok.kind {
  2717. case .Ellipsis, .Range_Half, .Range_Full:
  2718. error(p, p.curr_tok.pos, "expected a colon, not a range")
  2719. fallthrough
  2720. case .Colon, .Comma/*matrix index*/:
  2721. interval = advance_token(p)
  2722. is_slice_op = true
  2723. if p.curr_tok.kind != .Close_Bracket && p.curr_tok.kind != .EOF {
  2724. indices[1] = parse_expr(p, false)
  2725. }
  2726. }
  2727. close := expect_token(p, .Close_Bracket)
  2728. p.expr_level -= 1
  2729. if is_slice_op {
  2730. if interval.kind == .Comma {
  2731. if indices[0] == nil || indices[1] == nil {
  2732. error(p, p.curr_tok.pos, "matrix index expressions require both row and column indices")
  2733. }
  2734. se := ast.new(ast.Matrix_Index_Expr, operand.pos, end_pos(close))
  2735. se.expr = operand
  2736. se.open = open.pos
  2737. se.row_index = indices[0]
  2738. se.column_index = indices[1]
  2739. se.close = close.pos
  2740. operand = se
  2741. } else {
  2742. se := ast.new(ast.Slice_Expr, operand.pos, end_pos(close))
  2743. se.expr = operand
  2744. se.open = open.pos
  2745. se.low = indices[0]
  2746. se.interval = interval
  2747. se.high = indices[1]
  2748. se.close = close.pos
  2749. operand = se
  2750. }
  2751. } else {
  2752. ie := ast.new(ast.Index_Expr, operand.pos, end_pos(close))
  2753. ie.expr = operand
  2754. ie.open = open.pos
  2755. ie.index = indices[0]
  2756. ie.close = close.pos
  2757. operand = ie
  2758. }
  2759. case .Period:
  2760. tok := expect_token(p, .Period)
  2761. #partial switch p.curr_tok.kind {
  2762. case .Ident:
  2763. field := parse_ident(p)
  2764. sel := ast.new(ast.Selector_Expr, operand.pos, field)
  2765. sel.expr = operand
  2766. sel.op = tok
  2767. sel.field = field
  2768. operand = sel
  2769. case .Open_Paren:
  2770. open := expect_token(p, .Open_Paren)
  2771. type := parse_type(p)
  2772. close := expect_token(p, .Close_Paren)
  2773. ta := ast.new(ast.Type_Assertion, operand.pos, end_pos(close))
  2774. ta.expr = operand
  2775. ta.open = open.pos
  2776. ta.type = type
  2777. ta.close = close.pos
  2778. operand = ta
  2779. case .Question:
  2780. question := expect_token(p, .Question)
  2781. type := ast.new(ast.Unary_Expr, question.pos, end_pos(question))
  2782. type.op = question
  2783. type.expr = nil
  2784. ta := ast.new(ast.Type_Assertion, operand.pos, type)
  2785. ta.expr = operand
  2786. ta.type = type
  2787. operand = ta
  2788. case:
  2789. error(p, p.curr_tok.pos, "expected a selector")
  2790. advance_token(p)
  2791. operand = ast.new(ast.Bad_Expr, operand.pos, end_pos(tok))
  2792. }
  2793. case .Arrow_Right:
  2794. tok := expect_token(p, .Arrow_Right)
  2795. #partial switch p.curr_tok.kind {
  2796. case .Ident:
  2797. field := parse_ident(p)
  2798. sel := ast.new(ast.Selector_Expr, operand.pos, field)
  2799. sel.expr = operand
  2800. sel.op = tok
  2801. sel.field = field
  2802. operand = sel
  2803. case:
  2804. error(p, p.curr_tok.pos, "expected a selector")
  2805. advance_token(p)
  2806. operand = ast.new(ast.Bad_Expr, operand.pos, end_pos(tok))
  2807. }
  2808. case .Pointer:
  2809. op := expect_token(p, .Pointer)
  2810. deref := ast.new(ast.Deref_Expr, operand.pos, end_pos(op))
  2811. deref.expr = operand
  2812. deref.op = op
  2813. operand = deref
  2814. case .Or_Return:
  2815. token := expect_token(p, .Or_Return)
  2816. oe := ast.new(ast.Or_Return_Expr, operand.pos, end_pos(token))
  2817. oe.expr = operand
  2818. oe.token = token
  2819. operand = oe
  2820. case .Or_Break, .Or_Continue:
  2821. token := advance_token(p)
  2822. label: ^ast.Ident
  2823. end := end_pos(token)
  2824. if p.curr_tok.kind == .Ident {
  2825. end = end_pos(p.curr_tok)
  2826. label = parse_ident(p)
  2827. }
  2828. oe := ast.new(ast.Or_Branch_Expr, operand.pos, end)
  2829. oe.expr = operand
  2830. oe.token = token
  2831. oe.label = label
  2832. operand = oe
  2833. case .Open_Brace:
  2834. if !is_lhs && is_literal_type(operand) && p.expr_level >= 0 {
  2835. operand = parse_literal_value(p, operand)
  2836. } else {
  2837. loop = false
  2838. }
  2839. case .Increment, .Decrement:
  2840. if !lhs {
  2841. tok := advance_token(p)
  2842. error(p, tok.pos, "postfix '%s' operator is not supported", tok.text)
  2843. } else {
  2844. loop = false
  2845. }
  2846. }
  2847. is_lhs = false
  2848. }
  2849. return operand
  2850. }
  2851. parse_expr :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
  2852. return parse_binary_expr(p, lhs, 0+1)
  2853. }
  2854. parse_unary_expr :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
  2855. #partial switch p.curr_tok.kind {
  2856. case .Transmute, .Cast:
  2857. tok := advance_token(p)
  2858. open := expect_token(p, .Open_Paren)
  2859. type := parse_type(p)
  2860. close := expect_token(p, .Close_Paren)
  2861. expr := parse_unary_expr(p, lhs)
  2862. tc := ast.new(ast.Type_Cast, tok.pos, expr)
  2863. tc.tok = tok
  2864. tc.open = open.pos
  2865. tc.type = type
  2866. tc.close = close.pos
  2867. tc.expr = expr
  2868. return tc
  2869. case .Auto_Cast:
  2870. op := advance_token(p)
  2871. expr := parse_unary_expr(p, lhs)
  2872. ac := ast.new(ast.Auto_Cast, op.pos, expr)
  2873. ac.op = op
  2874. ac.expr = expr
  2875. return ac
  2876. case .Add, .Sub,
  2877. .Not, .Xor,
  2878. .And:
  2879. op := advance_token(p)
  2880. expr := parse_unary_expr(p, lhs)
  2881. ue := ast.new(ast.Unary_Expr, op.pos, expr)
  2882. ue.op = op
  2883. ue.expr = expr
  2884. return ue
  2885. case .Increment, .Decrement:
  2886. op := advance_token(p)
  2887. error(p, op.pos, "unary '%s' operator is not supported", op.text)
  2888. expr := parse_unary_expr(p, lhs)
  2889. ue := ast.new(ast.Unary_Expr, op.pos, expr)
  2890. ue.op = op
  2891. ue.expr = expr
  2892. return ue
  2893. case .Period:
  2894. op := advance_token(p)
  2895. field := parse_ident(p)
  2896. ise := ast.new(ast.Implicit_Selector_Expr, op.pos, field)
  2897. ise.field = field
  2898. return ise
  2899. }
  2900. return parse_atom_expr(p, parse_operand(p, lhs), lhs)
  2901. }
  2902. parse_binary_expr :: proc(p: ^Parser, lhs: bool, prec_in: int) -> ^ast.Expr {
  2903. start_pos := p.curr_tok.pos
  2904. expr := parse_unary_expr(p, lhs)
  2905. if expr == nil {
  2906. return ast.new(ast.Bad_Expr, start_pos, end_pos(p.prev_tok))
  2907. }
  2908. for prec := token_precedence(p, p.curr_tok.kind); prec >= prec_in; prec -= 1 {
  2909. loop: for {
  2910. op := p.curr_tok
  2911. op_prec := token_precedence(p, op.kind)
  2912. if op_prec != prec {
  2913. break loop
  2914. }
  2915. #partial switch op.kind {
  2916. case .If, .When:
  2917. if p.prev_tok.pos.line < op.pos.line {
  2918. // NOTE(bill): Check to see if the `if` or `when` is on the same line of the `lhs` condition
  2919. break loop
  2920. }
  2921. }
  2922. expect_operator(p)
  2923. #partial switch op.kind {
  2924. case .Question:
  2925. cond := expr
  2926. x := parse_expr(p, lhs)
  2927. colon := expect_token(p, .Colon)
  2928. y := parse_expr(p, lhs)
  2929. te := ast.new(ast.Ternary_If_Expr, expr.pos, end_pos(p.prev_tok))
  2930. te.cond = cond
  2931. te.op1 = op
  2932. te.x = x
  2933. te.op2 = colon
  2934. te.y = y
  2935. expr = te
  2936. case .If:
  2937. x := expr
  2938. cond := parse_expr(p, lhs)
  2939. else_tok := expect_token(p, .Else)
  2940. y := parse_expr(p, lhs)
  2941. te := ast.new(ast.Ternary_If_Expr, expr.pos, end_pos(p.prev_tok))
  2942. te.x = x
  2943. te.op1 = op
  2944. te.cond = cond
  2945. te.op2 = else_tok
  2946. te.y = y
  2947. expr = te
  2948. case .When:
  2949. x := expr
  2950. cond := parse_expr(p, lhs)
  2951. else_tok := expect_token(p, .Else)
  2952. y := parse_expr(p, lhs)
  2953. te := ast.new(ast.Ternary_When_Expr, expr.pos, end_pos(p.prev_tok))
  2954. te.x = x
  2955. te.op1 = op
  2956. te.cond = cond
  2957. te.op2 = else_tok
  2958. te.y = y
  2959. expr = te
  2960. case .Or_Else:
  2961. x := expr
  2962. y := parse_expr(p, lhs)
  2963. oe := ast.new(ast.Or_Else_Expr, expr.pos, end_pos(p.prev_tok))
  2964. oe.x = x
  2965. oe.token = op
  2966. oe.y = y
  2967. expr = oe
  2968. case:
  2969. right := parse_binary_expr(p, false, prec+1)
  2970. if right == nil {
  2971. error(p, op.pos, "expected expression on the right-hand side of the binary operator")
  2972. }
  2973. be := ast.new(ast.Binary_Expr, expr.pos, end_pos(p.prev_tok))
  2974. be.left = expr
  2975. be.op = op
  2976. be.right = right
  2977. expr = be
  2978. }
  2979. }
  2980. }
  2981. return expr
  2982. }
  2983. parse_expr_list :: proc(p: ^Parser, lhs: bool) -> ([]^ast.Expr) {
  2984. list: [dynamic]^ast.Expr
  2985. for {
  2986. expr := parse_expr(p, lhs)
  2987. append(&list, expr)
  2988. if p.curr_tok.kind != .Comma || p.curr_tok.kind == .EOF {
  2989. break
  2990. }
  2991. advance_token(p)
  2992. }
  2993. return list[:]
  2994. }
  2995. parse_lhs_expr_list :: proc(p: ^Parser) -> []^ast.Expr {
  2996. return parse_expr_list(p, true)
  2997. }
  2998. parse_rhs_expr_list :: proc(p: ^Parser) -> []^ast.Expr {
  2999. return parse_expr_list(p, false)
  3000. }
  3001. parse_simple_stmt :: proc(p: ^Parser, flags: Stmt_Allow_Flags) -> ^ast.Stmt {
  3002. start_tok := p.curr_tok
  3003. docs := p.lead_comment
  3004. lhs := parse_lhs_expr_list(p)
  3005. op := p.curr_tok
  3006. switch {
  3007. case tokenizer.is_assignment_operator(op.kind):
  3008. // if p.curr_proc == nil {
  3009. // error(p, p.curr_tok.pos, "simple statements are not allowed at the file scope");
  3010. // return ast.new(ast.Bad_Stmt, start_tok.pos, end_pos(p.curr_tok));
  3011. // }
  3012. advance_token(p)
  3013. rhs := parse_rhs_expr_list(p)
  3014. if len(rhs) == 0 {
  3015. error(p, p.curr_tok.pos, "no right-hand side in assignment statement")
  3016. return ast.new(ast.Bad_Stmt, start_tok.pos, end_pos(p.curr_tok))
  3017. }
  3018. stmt := ast.new(ast.Assign_Stmt, lhs[0].pos, rhs[len(rhs)-1])
  3019. stmt.lhs = lhs
  3020. stmt.op = op
  3021. stmt.rhs = rhs
  3022. return stmt
  3023. case op.kind == .In:
  3024. if .In in flags {
  3025. allow_token(p, .In)
  3026. prev_allow_range := p.allow_range
  3027. p.allow_range = true
  3028. expr := parse_expr(p, false)
  3029. p.allow_range = prev_allow_range
  3030. rhs := make([]^ast.Expr, 1)
  3031. rhs[0] = expr
  3032. stmt := ast.new(ast.Assign_Stmt, lhs[0].pos, rhs[len(rhs)-1])
  3033. stmt.lhs = lhs
  3034. stmt.op = op
  3035. stmt.rhs = rhs
  3036. return stmt
  3037. }
  3038. case op.kind == .Colon:
  3039. expect_token_after(p, .Colon, "identifier list")
  3040. if .Label in flags && len(lhs) == 1 {
  3041. is_partial := false
  3042. is_reverse := false
  3043. partial_token: tokenizer.Token
  3044. if p.curr_tok.kind == .Hash {
  3045. name := peek_token(p)
  3046. if name.kind == .Ident && name.text == "partial" &&
  3047. peek_token(p, 1).kind == .Switch {
  3048. partial_token = expect_token(p, .Hash)
  3049. expect_token(p, .Ident)
  3050. is_partial = true
  3051. } else if name.kind == .Ident && name.text == "reverse" &&
  3052. peek_token(p, 1).kind == .For {
  3053. partial_token = expect_token(p, .Hash)
  3054. expect_token(p, .Ident)
  3055. is_reverse = true
  3056. }
  3057. }
  3058. #partial switch p.curr_tok.kind {
  3059. case .Open_Brace, .If, .For, .Switch:
  3060. label := lhs[0]
  3061. stmt := parse_stmt(p)
  3062. if stmt != nil {
  3063. #partial switch n in stmt.derived_stmt {
  3064. case ^ast.Block_Stmt: n.label = label
  3065. case ^ast.If_Stmt: n.label = label
  3066. case ^ast.For_Stmt: n.label = label
  3067. case ^ast.Switch_Stmt: n.label = label
  3068. case ^ast.Type_Switch_Stmt: n.label = label
  3069. case ^ast.Range_Stmt: n.label = label
  3070. }
  3071. if is_partial {
  3072. #partial switch n in stmt.derived_stmt {
  3073. case ^ast.Switch_Stmt: n.partial = true
  3074. case ^ast.Type_Switch_Stmt: n.partial = true
  3075. case:
  3076. error(p, partial_token.pos, "incorrect use of directive, use '%s: #partial switch'", partial_token.text)
  3077. }
  3078. }
  3079. if is_reverse {
  3080. #partial switch n in stmt.derived_stmt {
  3081. case ^ast.Range_Stmt: n.reverse = true
  3082. case:
  3083. error(p, partial_token.pos, "incorrect use of directive, use '%s: #reverse for'", partial_token.text)
  3084. }
  3085. }
  3086. }
  3087. return stmt
  3088. }
  3089. }
  3090. return parse_value_decl(p, lhs, docs)
  3091. }
  3092. if len(lhs) > 1 {
  3093. error(p, op.pos, "expected 1 expression, got %d", len(lhs))
  3094. return ast.new(ast.Bad_Stmt, start_tok.pos, end_pos(p.curr_tok))
  3095. }
  3096. #partial switch op.kind {
  3097. case .Increment, .Decrement:
  3098. advance_token(p)
  3099. error(p, op.pos, "postfix '%s' statement is not supported", op.text)
  3100. }
  3101. es := ast.new(ast.Expr_Stmt, lhs[0].pos, lhs[0])
  3102. es.expr = lhs[0]
  3103. return es
  3104. }
  3105. parse_value_decl :: proc(p: ^Parser, names: []^ast.Expr, docs: ^ast.Comment_Group) -> ^ast.Decl {
  3106. is_mutable := true
  3107. values: []^ast.Expr
  3108. type := parse_type_or_ident(p)
  3109. #partial switch p.curr_tok.kind {
  3110. case .Eq, .Colon:
  3111. sep := advance_token(p)
  3112. is_mutable = sep.kind != .Colon
  3113. values = parse_rhs_expr_list(p)
  3114. if len(values) > len(names) {
  3115. error(p, p.curr_tok.pos, "too many values on the right-hand side of the declaration")
  3116. } else if len(values) < len(names) && !is_mutable {
  3117. error(p, p.curr_tok.pos, "all constant declarations must be defined")
  3118. } else if len(values) == 0 {
  3119. error(p, p.curr_tok.pos, "expected an expression for this declaration")
  3120. }
  3121. }
  3122. if is_mutable {
  3123. if type == nil && len(values) == 0 {
  3124. error(p, p.curr_tok.pos, "missing variable type or initialization")
  3125. return ast.new(ast.Bad_Decl, names[0].pos, end_pos(p.curr_tok))
  3126. }
  3127. } else {
  3128. if type == nil && len(values) == 0 && len(names) > 0 {
  3129. error(p, p.curr_tok.pos, "missing constant value")
  3130. return ast.new(ast.Bad_Decl, names[0].pos, end_pos(p.curr_tok))
  3131. }
  3132. }
  3133. end := p.prev_tok
  3134. if p.expr_level >= 0 {
  3135. end: ^ast.Expr
  3136. if !is_mutable && len(values) > 0 {
  3137. end = values[len(values)-1]
  3138. }
  3139. if p.curr_tok.kind == .Close_Brace &&
  3140. p.curr_tok.pos.line == p.prev_tok.pos.line {
  3141. } else {
  3142. expect_semicolon(p, end)
  3143. }
  3144. }
  3145. if p.curr_proc == nil {
  3146. if len(values) > 0 && len(names) != len(values) {
  3147. error(p, values[0].pos, "expected %d expressions on the right-hand side, got %d", len(names), len(values))
  3148. }
  3149. }
  3150. decl := ast.new(ast.Value_Decl, names[0].pos, end_pos(end))
  3151. decl.docs = docs
  3152. decl.names = names
  3153. decl.type = type
  3154. decl.values = values
  3155. decl.is_mutable = is_mutable
  3156. return decl
  3157. }
  3158. parse_import_decl :: proc(p: ^Parser, kind := Import_Decl_Kind.Standard) -> ^ast.Import_Decl {
  3159. docs := p.lead_comment
  3160. tok := expect_token(p, .Import)
  3161. import_name: tokenizer.Token
  3162. is_using := kind != Import_Decl_Kind.Standard
  3163. #partial switch p.curr_tok.kind {
  3164. case .Ident:
  3165. import_name = advance_token(p)
  3166. case:
  3167. import_name.pos = p.curr_tok.pos
  3168. }
  3169. if !is_using && is_blank_ident(import_name) {
  3170. error(p, import_name.pos, "illegal import name: '_'")
  3171. }
  3172. path := expect_token_after(p, .String, "import")
  3173. decl := ast.new(ast.Import_Decl, tok.pos, end_pos(path))
  3174. decl.docs = docs
  3175. decl.is_using = is_using
  3176. decl.import_tok = tok
  3177. decl.name = import_name
  3178. decl.relpath = path
  3179. decl.fullpath = path.text
  3180. if p.curr_proc != nil {
  3181. error(p, decl.pos, "import declarations cannot be used within a procedure, it must be done at the file scope")
  3182. } else {
  3183. append(&p.file.imports, decl)
  3184. }
  3185. expect_semicolon(p, decl)
  3186. decl.comment = p.line_comment
  3187. return decl
  3188. }