parser.odin 72 KB

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