parser.odin 85 KB

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