parser.odin 83 KB

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