parser.odin 89 KB

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