parser.odin 84 KB

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