parser.odin 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928
  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. 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. 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. 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. 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. 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. 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 != nil ? label.end : 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 "complete":
  917. stmt := parse_stmt(p);
  918. switch s in &stmt.derived {
  919. case ast.Switch_Stmt: s.complete = true;
  920. case ast.Type_Switch_Stmt: s.complete = true;
  921. case: error(p, stmt.pos, "#complete 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. switch kind {
  957. case .Question:
  958. return 1;
  959. case .Ellipsis, .Range_Half:
  960. if !p.allow_range {
  961. return 0;
  962. }
  963. return 2;
  964. case .Cmp_Or:
  965. return 3;
  966. case .Cmp_And:
  967. return 4;
  968. case .Cmp_Eq, .Not_Eq,
  969. .Lt, .Gt,
  970. .Lt_Eq, .Gt_Eq:
  971. return 5;
  972. case .In, .Notin:
  973. if p.expr_level < 0 && !p.allow_in_expr {
  974. return 0;
  975. }
  976. fallthrough;
  977. case .Add, .Sub, .Or, .Xor:
  978. return 6;
  979. case .Mul, .Quo,
  980. .Mod, .Mod_Mod,
  981. .And, .And_Not,
  982. .Shl, .Shr:
  983. return 7;
  984. }
  985. return 0;
  986. }
  987. parse_type_or_ident :: proc(p: ^Parser) -> ^ast.Expr {
  988. prev_allow_type := p.allow_type;
  989. prev_expr_level := p.expr_level;
  990. defer {
  991. p.allow_type = prev_allow_type;
  992. p.expr_level = prev_expr_level;
  993. }
  994. p.allow_type = true;
  995. p.expr_level = -1;
  996. lhs := true;
  997. return parse_atom_expr(p, parse_operand(p, lhs), lhs);
  998. }
  999. parse_type :: proc(p: ^Parser) -> ^ast.Expr {
  1000. type := parse_type_or_ident(p);
  1001. if type == nil {
  1002. error(p, p.curr_tok.pos, "expected a type");
  1003. return ast.new(ast.Bad_Expr, p.curr_tok.pos, end_pos(p.curr_tok));
  1004. }
  1005. return type;
  1006. }
  1007. parse_body :: proc(p: ^Parser) -> ^ast.Block_Stmt {
  1008. prev_expr_level := p.expr_level;
  1009. defer p.expr_level = prev_expr_level;
  1010. p.expr_level = 0;
  1011. open := expect_token(p, .Open_Brace);
  1012. stmts := parse_stmt_list(p);
  1013. close := expect_token(p, .Close_Brace);
  1014. bs := ast.new(ast.Block_Stmt, open.pos, end_pos(close));
  1015. bs.open = open.pos;
  1016. bs.stmts = stmts;
  1017. bs.close = close.pos;
  1018. return bs;
  1019. }
  1020. convert_stmt_to_body :: proc(p: ^Parser, stmt: ^ast.Stmt) -> ^ast.Stmt {
  1021. switch s in stmt.derived {
  1022. case ast.Block_Stmt:
  1023. error(p, stmt.pos, "expected a normal statement rather than a block statement");
  1024. return stmt;
  1025. case ast.Empty_Stmt:
  1026. error(p, stmt.pos, "expected a non-empty statement");
  1027. }
  1028. bs := ast.new(ast.Block_Stmt, stmt.pos, stmt.end);
  1029. bs.open = stmt.pos;
  1030. bs.stmts = make([]^ast.Stmt, 1);
  1031. bs.stmts[0] = stmt;
  1032. bs.close = stmt.end;
  1033. return bs;
  1034. }
  1035. new_ast_field :: proc(names: []^ast.Expr, type: ^ast.Expr, default_value: ^ast.Expr) -> ^ast.Field {
  1036. pos, end: tokenizer.Pos;
  1037. if len(names) > 0 {
  1038. pos = names[0].pos;
  1039. if default_value != nil {
  1040. end = default_value.end;
  1041. } else if type != nil {
  1042. end = type.end;
  1043. } else {
  1044. end = names[len(names)-1].pos;
  1045. }
  1046. } else {
  1047. if type != nil {
  1048. pos = type.pos;
  1049. } else if default_value != nil {
  1050. pos = default_value.pos;
  1051. }
  1052. if default_value != nil {
  1053. end = default_value.end;
  1054. } else if type != nil {
  1055. end = type.end;
  1056. }
  1057. }
  1058. field := ast.new(ast.Field, pos, end);
  1059. field.names = names;
  1060. field.type = type;
  1061. field.default_value = default_value;
  1062. return field;
  1063. }
  1064. Field_Prefix :: enum {
  1065. Invalid,
  1066. Unknown,
  1067. Using,
  1068. No_Alias,
  1069. C_Vararg,
  1070. In,
  1071. Auto_Cast,
  1072. }
  1073. Field_Prefixes :: distinct bit_set[Field_Prefix];
  1074. Expr_And_Flags :: struct {
  1075. expr: ^ast.Expr,
  1076. flags: ast.Field_Flags,
  1077. }
  1078. convert_to_ident_list :: proc(p: ^Parser, list: []Expr_And_Flags, ignore_flags, allow_poly_names: bool) -> []^ast.Expr {
  1079. idents := make([dynamic]^ast.Expr, 0, len(list));
  1080. for ident, i in list {
  1081. if !ignore_flags {
  1082. if i != 0 {
  1083. error(p, ident.expr.pos, "illegal use of prefixes in parameter list");
  1084. }
  1085. }
  1086. id: ^ast.Expr = ident.expr;
  1087. switch n in ident.expr.derived {
  1088. case ast.Ident:
  1089. case ast.Bad_Expr:
  1090. case ast.Poly_Type:
  1091. if allow_poly_names {
  1092. if n.specialization == nil {
  1093. break;
  1094. } else {
  1095. error(p, ident.expr.pos, "expected a polymorphic identifier without an specialization");
  1096. }
  1097. } else {
  1098. error(p, ident.expr.pos, "expected a non-polymorphic identifier");
  1099. }
  1100. case:
  1101. error(p, ident.expr.pos, "expected an identifier");
  1102. id = ast.new(ast.Ident, ident.expr.pos, ident.expr.end);
  1103. }
  1104. append(&idents, id);
  1105. }
  1106. return idents[:];
  1107. }
  1108. is_token_field_prefix :: proc(p: ^Parser) -> Field_Prefix {
  1109. using Field_Prefix;
  1110. switch p.curr_tok.kind {
  1111. case .EOF:
  1112. return Invalid;
  1113. case .Using:
  1114. advance_token(p);
  1115. return Using;
  1116. case .In:
  1117. advance_token(p);
  1118. return In;
  1119. case .Auto_Cast:
  1120. advance_token(p);
  1121. return Auto_Cast;
  1122. case .Hash:
  1123. advance_token(p);
  1124. defer advance_token(p);
  1125. switch p.curr_tok.kind {
  1126. case .Ident:
  1127. switch p.curr_tok.text {
  1128. case "no_alias":
  1129. return No_Alias;
  1130. case "c_vararg":
  1131. return C_Vararg;
  1132. }
  1133. }
  1134. return Unknown;
  1135. }
  1136. return Invalid;
  1137. }
  1138. parse_field_prefixes :: proc(p: ^Parser) -> ast.Field_Flags {
  1139. counts: [len(Field_Prefix)]int;
  1140. for {
  1141. kind := is_token_field_prefix(p);
  1142. if kind == Field_Prefix.Invalid {
  1143. break;
  1144. }
  1145. if kind == Field_Prefix.Unknown {
  1146. error(p, p.curr_tok.pos, "unknown prefix kind '#%s'", p.curr_tok.text);
  1147. continue;
  1148. }
  1149. counts[kind] += 1;
  1150. }
  1151. flags: ast.Field_Flags;
  1152. for kind in Field_Prefix {
  1153. count := counts[kind];
  1154. using Field_Prefix;
  1155. #complete switch kind {
  1156. case Invalid, Unknown: // Ignore
  1157. case Using:
  1158. if count > 1 do error(p, p.curr_tok.pos, "multiple 'using' in this field list");
  1159. if count > 0 do flags |= {ast.Field_Flag.Using};
  1160. case No_Alias:
  1161. if count > 1 do error(p, p.curr_tok.pos, "multiple '#no_alias' in this field list");
  1162. if count > 0 do flags |= {ast.Field_Flag.No_Alias};
  1163. case C_Vararg:
  1164. if count > 1 do error(p, p.curr_tok.pos, "multiple '#c_vararg' in this field list");
  1165. if count > 0 do flags |= {ast.Field_Flag.C_Vararg};
  1166. case In:
  1167. if count > 1 do error(p, p.curr_tok.pos, "multiple 'in' in this field list");
  1168. if count > 0 do flags |= {ast.Field_Flag.In};
  1169. case Auto_Cast:
  1170. if count > 1 do error(p, p.curr_tok.pos, "multiple 'auto_cast' in this field list");
  1171. if count > 0 do flags |= {ast.Field_Flag.Auto_Cast};
  1172. }
  1173. }
  1174. return flags;
  1175. }
  1176. check_field_flag_prefixes :: proc(p: ^Parser, name_count: int, allowed_flags, set_flags: ast.Field_Flags) -> (flags: ast.Field_Flags) {
  1177. flags = set_flags;
  1178. if name_count > 1 && ast.Field_Flag.Using in flags {
  1179. error(p, p.curr_tok.pos, "cannot apply 'using' to more than one of the same type");
  1180. flags &~= {ast.Field_Flag.Using};
  1181. }
  1182. for flag in ast.Field_Flag {
  1183. if flag notin allowed_flags && flag in flags {
  1184. #complete switch flag {
  1185. case .Using:
  1186. error(p, p.curr_tok.pos, "'using' is not allowed within this field list");
  1187. case .No_Alias:
  1188. error(p, p.curr_tok.pos, "'#no_alias' is not allowed within this field list");
  1189. case .C_Vararg:
  1190. error(p, p.curr_tok.pos, "'#c_vararg' is not allowed within this field list");
  1191. case .Auto_Cast:
  1192. error(p, p.curr_tok.pos, "'auto_cast' is not allowed within this field list");
  1193. case .In:
  1194. error(p, p.curr_tok.pos, "'in' is not allowed within this field list");
  1195. case .Tags, .Ellipsis, .Results, .Default_Parameters, .Typeid_Token:
  1196. panic("Impossible prefixes");
  1197. }
  1198. flags &~= {flag};
  1199. }
  1200. }
  1201. if ast.Field_Flag.Using in allowed_flags && ast.Field_Flag.Using in flags {
  1202. flags &~= {ast.Field_Flag.Using};
  1203. }
  1204. return flags;
  1205. }
  1206. parse_var_type :: proc(p: ^Parser, flags: ast.Field_Flags) -> ^ast.Expr {
  1207. if ast.Field_Flag.Ellipsis in flags && p.curr_tok.kind == .Ellipsis {
  1208. tok := advance_token(p);
  1209. type := parse_type_or_ident(p);
  1210. if type == nil {
  1211. error(p, tok.pos, "variadic field missing type after '..'");
  1212. type = ast.new(ast.Bad_Expr, tok.pos, end_pos(tok));
  1213. }
  1214. e := ast.new(ast.Ellipsis, type.pos, type.end);
  1215. e.expr = type;
  1216. return e;
  1217. }
  1218. type: ^ast.Expr;
  1219. if ast.Field_Flag.Typeid_Token in flags && p.curr_tok.kind == .Typeid {
  1220. tok := expect_token(p, .Typeid);
  1221. specialization: ^ast.Expr;
  1222. end := tok.pos;
  1223. if allow_token(p, .Quo) {
  1224. specialization = parse_type(p);
  1225. end = specialization.end;
  1226. }
  1227. ti := ast.new(ast.Typeid_Type, tok.pos, end);
  1228. ti.tok = tok.kind;
  1229. ti.specialization = specialization;
  1230. type = ti;
  1231. } else {
  1232. type = parse_type(p);
  1233. }
  1234. return type;
  1235. }
  1236. check_procedure_name_list :: proc(p: ^Parser, names: []^ast.Expr) -> bool {
  1237. if len(names) == 0 {
  1238. return false;
  1239. }
  1240. _, first_is_polymorphic := names[0].derived.(ast.Poly_Type);
  1241. any_polymorphic_names := first_is_polymorphic;
  1242. for i := 1; i < len(names); i += 1 {
  1243. name := names[i];
  1244. if first_is_polymorphic {
  1245. if _, ok := name.derived.(ast.Poly_Type); ok {
  1246. any_polymorphic_names = true;
  1247. } else {
  1248. error(p, name.pos, "mixture of polymorphic and non-polymorphic identifiers");
  1249. return any_polymorphic_names;
  1250. }
  1251. } else {
  1252. if _, ok := name.derived.(ast.Poly_Type); ok {
  1253. any_polymorphic_names = true;
  1254. error(p, name.pos, "mixture of polymorphic and non-polymorphic identifiers");
  1255. return any_polymorphic_names;
  1256. } else {
  1257. // Okay
  1258. }
  1259. }
  1260. }
  1261. return any_polymorphic_names;
  1262. }
  1263. parse_ident_list :: proc(p: ^Parser, allow_poly_names: bool) -> []^ast.Expr {
  1264. list: [dynamic]^ast.Expr;
  1265. for {
  1266. if allow_poly_names && p.curr_tok.kind == .Dollar {
  1267. tok := expect_token(p, .Dollar);
  1268. ident := parse_ident(p);
  1269. if is_blank_ident(ident) {
  1270. error(p, ident.pos, "invalid polymorphic type definition with a blank identifier");
  1271. }
  1272. poly_name := ast.new(ast.Poly_Type, tok.pos, ident.end);
  1273. poly_name.type = ident;
  1274. append(&list, poly_name);
  1275. } else {
  1276. ident := parse_ident(p);
  1277. append(&list, ident);
  1278. }
  1279. if p.curr_tok.kind != .Comma ||
  1280. p.curr_tok.kind == .EOF {
  1281. break;
  1282. }
  1283. advance_token(p);
  1284. }
  1285. return list[:];
  1286. }
  1287. parse_field_list :: proc(p: ^Parser, follow: tokenizer.Token_Kind, allowed_flags: ast.Field_Flags) -> (field_list: ^ast.Field_List, total_name_count: int) {
  1288. handle_field :: proc(p: ^Parser,
  1289. seen_ellipsis: ^bool, fields: ^[dynamic]^ast.Field,
  1290. docs: ^ast.Comment_Group,
  1291. names: []^ast.Expr,
  1292. allowed_flags, set_flags: ast.Field_Flags
  1293. ) -> bool {
  1294. expect_field_separator :: proc(p: ^Parser, param: ^ast.Expr) -> bool {
  1295. tok := p.curr_tok;
  1296. if allow_token(p, .Comma) {
  1297. return true;
  1298. }
  1299. if allow_token(p, .Semicolon) {
  1300. error(p, tok.pos, "expected a comma, got a semicolon");
  1301. return true;
  1302. }
  1303. return false;
  1304. }
  1305. is_type_ellipsis :: proc(type: ^ast.Expr) -> bool {
  1306. if type == nil do return false;
  1307. _, ok := type.derived.(ast.Ellipsis);
  1308. return ok;
  1309. }
  1310. is_signature := (allowed_flags & ast.Field_Flags_Signature_Params) == ast.Field_Flags_Signature_Params;
  1311. any_polymorphic_names := check_procedure_name_list(p, names);
  1312. flags := check_field_flag_prefixes(p, len(names), allowed_flags, set_flags);
  1313. type: ^ast.Expr;
  1314. default_value: ^ast.Expr;
  1315. tag: tokenizer.Token;
  1316. expect_token_after(p, .Colon, "field list");
  1317. if p.curr_tok.kind != .Eq {
  1318. type = parse_var_type(p, allowed_flags);
  1319. tt := ast.unparen_expr(type);
  1320. if is_signature && !any_polymorphic_names {
  1321. if ti, ok := tt.derived.(ast.Typeid_Type); ok && ti.specialization != nil {
  1322. error(p, tt.pos, "specialization of typeid is not allowed without polymorphic names");
  1323. }
  1324. }
  1325. }
  1326. if allow_token(p, .Eq) {
  1327. default_value = parse_expr(p, false);
  1328. if ast.Field_Flag.Default_Parameters notin allowed_flags {
  1329. error(p, p.curr_tok.pos, "default parameters are only allowed for procedures");
  1330. default_value = nil;
  1331. }
  1332. }
  1333. if default_value != nil && len(names) > 1 {
  1334. error(p, p.curr_tok.pos, "default parameters can only be applied to single values");
  1335. }
  1336. if allowed_flags == ast.Field_Flags_Struct && default_value != nil {
  1337. error(p, default_value.pos, "default parameters are not allowed for structs");
  1338. default_value = nil;
  1339. }
  1340. if is_type_ellipsis(type) {
  1341. if seen_ellipsis^ do error(p, type.pos, "extra variadic parameter after ellipsis");
  1342. seen_ellipsis^ = true;
  1343. if len(names) != 1 {
  1344. error(p, type.pos, "variadic parameters can only have one field name");
  1345. }
  1346. } else if seen_ellipsis^ && default_value == nil {
  1347. error(p, p.curr_tok.pos, "extra parameter after ellipsis without a default value");
  1348. }
  1349. if type != nil && default_value == nil {
  1350. if p.curr_tok.kind == .String {
  1351. tag = expect_token(p, .String);
  1352. if .Tags notin allowed_flags {
  1353. error(p, tag.pos, "Field tags are only allowed within structures");
  1354. }
  1355. }
  1356. }
  1357. ok := expect_field_separator(p, type);
  1358. field := new_ast_field(names, type, default_value);
  1359. field.tag = tag;
  1360. field.docs = docs;
  1361. field.flags = flags;
  1362. field.comment = p.line_comment;
  1363. append(fields, field);
  1364. return ok;
  1365. }
  1366. start_tok := p.curr_tok;
  1367. docs := p.lead_comment;
  1368. fields: [dynamic]^ast.Field;
  1369. list: [dynamic]Expr_And_Flags;
  1370. defer delete(list);
  1371. seen_ellipsis := false;
  1372. allow_typeid_token := ast.Field_Flag.Typeid_Token in allowed_flags;
  1373. allow_poly_names := allow_typeid_token;
  1374. for p.curr_tok.kind != follow &&
  1375. p.curr_tok.kind != .Colon &&
  1376. p.curr_tok.kind != .EOF {
  1377. prefix_flags := parse_field_prefixes(p);
  1378. param := parse_var_type(p, allowed_flags & {ast.Field_Flag.Typeid_Token, ast.Field_Flag.Ellipsis});
  1379. if _, ok := param.derived.(ast.Ellipsis); ok {
  1380. if seen_ellipsis {
  1381. error(p, param.pos, "extra variadic parameter after ellipsis");
  1382. }
  1383. seen_ellipsis = true;
  1384. } else if seen_ellipsis {
  1385. error(p, param.pos, "extra parameter after ellipsis");
  1386. }
  1387. eaf := Expr_And_Flags{param, prefix_flags};
  1388. append(&list, eaf);
  1389. if !allow_token(p, .Comma) {
  1390. break;
  1391. }
  1392. }
  1393. if p.curr_tok.kind != .Colon {
  1394. for eaf in list {
  1395. type := eaf.expr;
  1396. tok: tokenizer.Token;
  1397. tok.pos = type.pos;
  1398. if ast.Field_Flag.Results notin allowed_flags {
  1399. tok.text = "_";
  1400. }
  1401. names := make([]^ast.Expr, 1);
  1402. names[0] = ast.new(ast.Ident, tok.pos, end_pos(tok));
  1403. names[0].derived.(ast.Ident).name = tok.text;
  1404. flags := check_field_flag_prefixes(p, len(list), allowed_flags, eaf.flags);
  1405. field := new_ast_field(names, type, nil);
  1406. field.docs = docs;
  1407. field.flags = flags;
  1408. field.comment = p.line_comment;
  1409. append(&fields, field);
  1410. }
  1411. } else {
  1412. names := convert_to_ident_list(p, list[:], true, allow_poly_names);
  1413. if len(names) == 0 {
  1414. error(p, p.curr_tok.pos, "empty field declaration");
  1415. }
  1416. set_flags: ast.Field_Flags;
  1417. if len(list) > 0 {
  1418. set_flags = list[0].flags;
  1419. }
  1420. total_name_count += len(names);
  1421. handle_field(p, &seen_ellipsis, &fields, docs, names, allowed_flags, set_flags);
  1422. for p.curr_tok.kind != follow && p.curr_tok.kind != .EOF {
  1423. docs = p.lead_comment;
  1424. set_flags = parse_field_prefixes(p);
  1425. names = parse_ident_list(p, allow_poly_names);
  1426. total_name_count += len(names);
  1427. ok := handle_field(p, &seen_ellipsis, &fields, docs, names, allowed_flags, set_flags);
  1428. if !ok {
  1429. break;
  1430. }
  1431. }
  1432. }
  1433. field_list = ast.new(ast.Field_List, start_tok.pos, p.curr_tok.pos);
  1434. field_list.list = fields[:];
  1435. return;
  1436. }
  1437. parse_results :: proc(p: ^Parser) -> (list: ^ast.Field_List, diverging: bool) {
  1438. if !allow_token(p, .Arrow_Right) {
  1439. return;
  1440. }
  1441. if allow_token(p, .Not) {
  1442. diverging = true;
  1443. return;
  1444. }
  1445. prev_level := p.expr_level;
  1446. defer p.expr_level = prev_level;
  1447. if p.curr_tok.kind != .Open_Paren {
  1448. type := parse_type(p);
  1449. field := new_ast_field(nil, type, nil);
  1450. list = ast.new(ast.Field_List, field.pos, field.end);
  1451. list.list = make([]^ast.Field, 1);
  1452. list.list[0] = field;
  1453. return;
  1454. }
  1455. expect_token(p, .Open_Paren);
  1456. list, _ = parse_field_list(p, .Close_Paren, ast.Field_Flags_Signature_Results);
  1457. expect_token_after(p, .Close_Paren, "parameter list");
  1458. return;
  1459. }
  1460. string_to_calling_convention :: proc(s: string) -> ast.Proc_Calling_Convention {
  1461. using ast.Proc_Calling_Convention;
  1462. if s[0] != '"' && s[0] != '`' {
  1463. return Invalid;
  1464. }
  1465. switch s[1:len(s)-1] {
  1466. case "odin":
  1467. return Odin;
  1468. case "contextless":
  1469. return Contextless;
  1470. case "cdecl", "c":
  1471. return C_Decl;
  1472. case "stdcall", "std":
  1473. return Std_Call;
  1474. case "fast", "fastcall":
  1475. return Fast_Call;
  1476. }
  1477. return Invalid;
  1478. }
  1479. parse_proc_tags :: proc(p: ^Parser) -> (tags: ast.Proc_Tags) {
  1480. for p.curr_tok.kind == .Hash {
  1481. _ = expect_token(p, .Hash);
  1482. ident := expect_token(p, .Ident);
  1483. switch ident.text {
  1484. case "bounds_check":
  1485. tags |= {.Bounds_Check};
  1486. case "no_bounds_check":
  1487. tags |= {.No_Bounds_Check};
  1488. case:
  1489. }
  1490. }
  1491. if .Bounds_Check in tags && .No_Bounds_Check in tags {
  1492. p.err(p.curr_tok.pos, "#bounds_check and #no_bounds_check applied to the same procedure type");
  1493. }
  1494. return;
  1495. }
  1496. parse_proc_type :: proc(p: ^Parser, tok: tokenizer.Token) -> ^ast.Proc_Type {
  1497. cc := ast.Proc_Calling_Convention.Invalid;
  1498. if p.curr_tok.kind == .String {
  1499. str := expect_token(p, .String);
  1500. cc = string_to_calling_convention(str.text);
  1501. if cc == ast.Proc_Calling_Convention.Invalid {
  1502. error(p, str.pos, "unknown calling convention '%s'", str.text);
  1503. }
  1504. }
  1505. if cc == ast.Proc_Calling_Convention.Invalid {
  1506. if p.in_foreign_block {
  1507. cc = ast.Proc_Calling_Convention.Foreign_Block_Default;
  1508. } else {
  1509. cc = ast.Proc_Calling_Convention.Odin;
  1510. }
  1511. }
  1512. expect_token(p, .Open_Paren);
  1513. params, _ := parse_field_list(p, .Close_Paren, ast.Field_Flags_Signature_Params);
  1514. expect_token(p, .Close_Paren);
  1515. results, diverging := parse_results(p);
  1516. is_generic := false;
  1517. loop: for param in params.list {
  1518. if param.type != nil {
  1519. if _, ok := param.type.derived.(ast.Poly_Type); ok {
  1520. is_generic = true;
  1521. break loop;
  1522. }
  1523. for name in param.names {
  1524. if _, ok := name.derived.(ast.Poly_Type); ok {
  1525. is_generic = true;
  1526. break loop;
  1527. }
  1528. }
  1529. }
  1530. }
  1531. end := end_pos(p.prev_tok);
  1532. pt := ast.new(ast.Proc_Type, tok.pos, end);
  1533. pt.tok = tok;
  1534. pt.calling_convention = cc;
  1535. pt.params = params;
  1536. pt.results = results;
  1537. pt.diverging = diverging;
  1538. pt.generic = is_generic;
  1539. return pt;
  1540. }
  1541. check_poly_params_for_type :: proc(p: ^Parser, poly_params: ^ast.Field_List, tok: tokenizer.Token) {
  1542. if poly_params == nil {
  1543. return;
  1544. }
  1545. for field in poly_params.list {
  1546. for name in field.names {
  1547. if name == nil do continue;
  1548. if _, ok := name.derived.(ast.Poly_Type); ok {
  1549. error(p, name.pos, "polymorphic names are not needed for %s parameters", tok.text);
  1550. return;
  1551. }
  1552. }
  1553. }
  1554. }
  1555. parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
  1556. switch p.curr_tok.kind {
  1557. case .Ident:
  1558. return parse_ident(p);
  1559. case .Undef:
  1560. tok := expect_token(p, .Undef);
  1561. undef := ast.new(ast.Undef, tok.pos, end_pos(tok));
  1562. undef.tok = tok.kind;
  1563. return undef;
  1564. case .Context:
  1565. tok := expect_token(p, .Context);
  1566. ctx := ast.new(ast.Implicit, tok.pos, end_pos(tok));
  1567. ctx.tok = tok;
  1568. return ctx;
  1569. case .Integer, .Float, .Imag,
  1570. .Rune, .String:
  1571. tok := advance_token(p);
  1572. bl := ast.new(ast.Basic_Lit, tok.pos, end_pos(tok));
  1573. bl.tok = tok;
  1574. return bl;
  1575. case .Size_Of, .Align_Of, .Offset_Of:
  1576. tok := advance_token(p);
  1577. expr := ast.new(ast.Implicit, tok.pos, end_pos(tok));
  1578. expr.tok = tok;
  1579. return parse_call_expr(p, expr);
  1580. case .Open_Brace:
  1581. if !lhs {
  1582. return parse_literal_value(p, nil);
  1583. }
  1584. case .Open_Paren:
  1585. open := expect_token(p, .Open_Paren);
  1586. p.expr_level += 1;
  1587. expr := parse_expr(p, false);
  1588. p.expr_level -= 1;
  1589. close := expect_token(p, .Close_Paren);
  1590. pe := ast.new(ast.Paren_Expr, open.pos, end_pos(close));
  1591. pe.open = open.pos;
  1592. pe.expr = expr;
  1593. pe.close = close.pos;
  1594. return pe;
  1595. case .Distinct:
  1596. tok := advance_token(p);
  1597. type := parse_type(p);
  1598. dt := ast.new(ast.Distinct_Type, tok.pos, type.end);
  1599. dt.tok = tok.kind;
  1600. dt.type = type;
  1601. return dt;
  1602. case .Opaque:
  1603. tok := advance_token(p);
  1604. type := parse_type(p);
  1605. ot := ast.new(ast.Opaque_Type, tok.pos, type.end);
  1606. ot.tok = tok.kind;
  1607. ot.type = type;
  1608. return ot;
  1609. case .Hash:
  1610. tok := expect_token(p, .Hash);
  1611. name := expect_token(p, .Ident);
  1612. switch name.text {
  1613. case "type":
  1614. type := parse_type(p);
  1615. hp := ast.new(ast.Helper_Type, tok.pos, type.end);
  1616. hp.tok = tok.kind;
  1617. hp.type = type;
  1618. return hp;
  1619. case "file", "line", "procedure", "caller_location":
  1620. bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(name));
  1621. bd.tok = tok;
  1622. bd.name = name.text;
  1623. return bd;
  1624. case "location", "load", "assert", "defined":
  1625. bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(name));
  1626. bd.tok = tok;
  1627. bd.name = name.text;
  1628. return parse_call_expr(p, bd);
  1629. case:
  1630. expr := parse_expr(p, lhs);
  1631. te := ast.new(ast.Tag_Expr, tok.pos, expr.pos);
  1632. te.op = tok;
  1633. te.name = name.text;
  1634. te.expr = expr;
  1635. return te;
  1636. }
  1637. case .Inline, .No_Inline:
  1638. tok := advance_token(p);
  1639. expr := parse_unary_expr(p, lhs);
  1640. pi := ast.Proc_Inlining.None;
  1641. switch tok.kind {
  1642. case .Inline:
  1643. pi = ast.Proc_Inlining.Inline;
  1644. case .No_Inline:
  1645. pi = ast.Proc_Inlining.No_Inline;
  1646. }
  1647. switch e in &ast.unparen_expr(expr).derived {
  1648. case ast.Proc_Lit:
  1649. if e.inlining != ast.Proc_Inlining.None && e.inlining != pi {
  1650. error(p, expr.pos, "both 'inline' and 'no_inline' cannot be applied to a procedure literal");
  1651. }
  1652. e.inlining = pi;
  1653. case ast.Call_Expr:
  1654. if e.inlining != ast.Proc_Inlining.None && e.inlining != pi {
  1655. error(p, expr.pos, "both 'inline' and 'no_inline' cannot be applied to a procedure call");
  1656. }
  1657. e.inlining = pi;
  1658. case:
  1659. error(p, tok.pos, "'%s' must be followed by a procedure literal or call", tok.text);
  1660. return ast.new(ast.Bad_Expr, tok.pos, expr.end);
  1661. }
  1662. return expr;
  1663. case .Proc:
  1664. tok := expect_token(p, .Proc);
  1665. if p.curr_tok.kind == .Open_Brace {
  1666. open := expect_token(p, .Open_Brace);
  1667. args: [dynamic]^ast.Expr;
  1668. for p.curr_tok.kind != .Close_Brace &&
  1669. p.curr_tok.kind != .EOF {
  1670. elem := parse_expr(p, false);
  1671. append(&args, elem);
  1672. if !allow_token(p, .Comma) {
  1673. break;
  1674. }
  1675. }
  1676. close := expect_token(p, .Close_Brace);
  1677. if len(args) == 0 {
  1678. error(p, tok.pos, "expected at least 1 argument in procedure group");
  1679. }
  1680. pg := ast.new(ast.Proc_Group, tok.pos, end_pos(close));
  1681. pg.tok = tok;
  1682. pg.open = open.pos;
  1683. pg.args = args[:];
  1684. pg.close = close.pos;
  1685. return pg;
  1686. }
  1687. type := parse_proc_type(p, tok);
  1688. where_token: tokenizer.Token;
  1689. where_clauses: []^ast.Expr;
  1690. if (p.curr_tok.kind == .Where) {
  1691. where_token = expect_token(p, .Where);
  1692. prev_level := p.expr_level;
  1693. p.expr_level = -1;
  1694. where_clauses = parse_rhs_expr_list(p);
  1695. p.expr_level = prev_level;
  1696. }
  1697. if p.allow_type && p.expr_level < 0 {
  1698. if where_token.kind != .Invalid {
  1699. error(p, where_token.pos, "'where' clauses are not allowed on procedure types");
  1700. }
  1701. return type;
  1702. }
  1703. body: ^ast.Stmt;
  1704. if allow_token(p, .Undef) {
  1705. // Okay
  1706. if where_token.kind != .Invalid {
  1707. error(p, where_token.pos, "'where' clauses are not allowed on procedure literals without a defined body (replaced with ---");
  1708. }
  1709. } else if p.curr_tok.kind == .Open_Brace {
  1710. prev_proc := p.curr_proc;
  1711. p.curr_proc = type;
  1712. body = parse_body(p);
  1713. p.curr_proc = prev_proc;
  1714. } else if allow_token(p, .Do) {
  1715. prev_proc := p.curr_proc;
  1716. p.curr_proc = type;
  1717. body = convert_stmt_to_body(p, parse_stmt(p));
  1718. p.curr_proc = prev_proc;
  1719. } else {
  1720. return type;
  1721. }
  1722. pl := ast.new(ast.Proc_Lit, tok.pos, end_pos(p.prev_tok));
  1723. pl.type = type;
  1724. pl.body = body;
  1725. pl.where_token = where_token;
  1726. pl.where_clauses = where_clauses;
  1727. return pl;
  1728. case .Dollar:
  1729. tok := advance_token(p);
  1730. type := parse_ident(p);
  1731. end := type.end;
  1732. specialization: ^ast.Expr;
  1733. if allow_token(p, .Quo) {
  1734. specialization = parse_type(p);
  1735. end = specialization.pos;
  1736. }
  1737. if is_blank_ident(type) {
  1738. error(p, type.pos, "invalid polymorphic type definition with a blank identifier");
  1739. }
  1740. pt := ast.new(ast.Poly_Type, tok.pos, end);
  1741. pt.dollar = tok.pos;
  1742. pt.type = type;
  1743. pt.specialization = specialization;
  1744. return pt;
  1745. case .Typeid:
  1746. tok := advance_token(p);
  1747. ti := ast.new(ast.Typeid_Type, tok.pos, end_pos(tok));
  1748. ti.tok = tok.kind;
  1749. ti.specialization = nil;
  1750. return ti;
  1751. case .Type_Of:
  1752. tok := advance_token(p);
  1753. i := ast.new(ast.Implicit, tok.pos, end_pos(tok));
  1754. i.tok = tok;
  1755. type: ^ast.Expr = parse_call_expr(p, i);
  1756. for p.curr_tok.kind == .Period {
  1757. period := advance_token(p);
  1758. field := parse_ident(p);
  1759. sel := ast.new(ast.Selector_Expr, period.pos, field.end);
  1760. sel.expr = type;
  1761. sel.field = field;
  1762. type = sel;
  1763. }
  1764. return type;
  1765. case .Pointer:
  1766. tok := expect_token(p, .Pointer);
  1767. elem := parse_type(p);
  1768. ptr := ast.new(ast.Pointer_Type, tok.pos, elem.end);
  1769. ptr.elem = elem;
  1770. return ptr;
  1771. case .Open_Bracket:
  1772. open := expect_token(p, .Open_Bracket);
  1773. count: ^ast.Expr;
  1774. if p.curr_tok.kind == .Question {
  1775. tok := expect_token(p, .Question);
  1776. q := ast.new(ast.Unary_Expr, tok.pos, end_pos(tok));
  1777. q.op = tok;
  1778. count = q;
  1779. } else if p.curr_tok.kind == .Dynamic {
  1780. tok := expect_token(p, .Dynamic);
  1781. close := expect_token(p, .Close_Bracket);
  1782. elem := parse_type(p);
  1783. da := ast.new(ast.Dynamic_Array_Type, open.pos, elem.end);
  1784. da.open = open.pos;
  1785. da.dynamic_pos = tok.pos;
  1786. da.close = close.pos;
  1787. da.elem = elem;
  1788. return da;
  1789. } else if p.curr_tok.kind != .Close_Bracket {
  1790. p.expr_level += 1;
  1791. count = parse_expr(p, false);
  1792. p.expr_level -= 1;
  1793. }
  1794. close := expect_token(p, .Close_Bracket);
  1795. elem := parse_type(p);
  1796. at := ast.new(ast.Array_Type, open.pos, elem.end);
  1797. at.open = open.pos;
  1798. at.len = count;
  1799. at.close = close.pos;
  1800. at.elem = elem;
  1801. return at;
  1802. case .Map:
  1803. tok := expect_token(p, .Map);
  1804. expect_token(p, .Open_Bracket);
  1805. key := parse_type(p);
  1806. expect_token(p, .Close_Bracket);
  1807. value := parse_type(p);
  1808. mt := ast.new(ast.Map_Type, tok.pos, value.end);
  1809. mt.tok_pos = tok.pos;
  1810. mt.key = key;
  1811. mt.value = value;
  1812. return mt;
  1813. case .Struct:
  1814. tok := expect_token(p, .Struct);
  1815. poly_params: ^ast.Field_List;
  1816. align: ^ast.Expr;
  1817. is_packed: bool;
  1818. is_raw_union: bool;
  1819. fields: ^ast.Field_List;
  1820. name_count: int;
  1821. if allow_token(p, .Open_Paren) {
  1822. param_count: int;
  1823. poly_params, param_count = parse_field_list(p, .Close_Paren, ast.Field_Flags_Record_Poly_Params);
  1824. if param_count == 0 {
  1825. error(p, poly_params.pos, "expected at least 1 polymorphic parameter");
  1826. poly_params = nil;
  1827. }
  1828. expect_token_after(p, .Close_Paren, "parameter list");
  1829. check_poly_params_for_type(p, poly_params, tok);
  1830. }
  1831. prev_level := p.expr_level;
  1832. p.expr_level = -1;
  1833. for allow_token(p, .Hash) {
  1834. tag := expect_token_after(p, .Ident, "#");
  1835. switch tag.text {
  1836. case "packed":
  1837. if is_packed do error(p, tag.pos, "duplicate struct tag '#%s'", tag.text);
  1838. is_packed = true;
  1839. case "align":
  1840. if align != nil do error(p, tag.pos, "duplicate struct tag '#%s'", tag.text);
  1841. align = parse_expr(p, true);
  1842. case "raw_union":
  1843. if is_raw_union do error(p, tag.pos, "duplicate struct tag '#%s'", tag.text);
  1844. is_raw_union = true;
  1845. case:
  1846. error(p, tag.pos, "invalid struct tag '#%s", tag.text);
  1847. }
  1848. }
  1849. p.expr_level = prev_level;
  1850. if is_raw_union && is_packed {
  1851. is_packed = false;
  1852. error(p, tok.pos, "'#raw_union' cannot also be '#packed");
  1853. }
  1854. where_token: tokenizer.Token;
  1855. where_clauses: []^ast.Expr;
  1856. if (p.curr_tok.kind == .Where) {
  1857. where_token = expect_token(p, .Where);
  1858. prev_level := p.expr_level;
  1859. p.expr_level = -1;
  1860. where_clauses = parse_rhs_expr_list(p);
  1861. p.expr_level = prev_level;
  1862. }
  1863. expect_token(p, .Open_Brace);
  1864. fields, name_count = parse_field_list(p, .Close_Brace, ast.Field_Flags_Struct);
  1865. close := expect_token(p, .Close_Brace);
  1866. st := ast.new(ast.Struct_Type, tok.pos, end_pos(close));
  1867. st.poly_params = poly_params;
  1868. st.align = align;
  1869. st.is_packed = is_packed;
  1870. st.is_raw_union = is_raw_union;
  1871. st.fields = fields;
  1872. st.name_count = name_count;
  1873. st.where_token = where_token;
  1874. st.where_clauses = where_clauses;
  1875. return st;
  1876. case .Union:
  1877. tok := expect_token(p, .Union);
  1878. poly_params: ^ast.Field_List;
  1879. align: ^ast.Expr;
  1880. if allow_token(p, .Open_Paren) {
  1881. param_count: int;
  1882. poly_params, param_count = parse_field_list(p, .Close_Paren, ast.Field_Flags_Record_Poly_Params);
  1883. if param_count == 0 {
  1884. error(p, poly_params.pos, "expected at least 1 polymorphic parameter");
  1885. poly_params = nil;
  1886. }
  1887. expect_token_after(p, .Close_Paren, "parameter list");
  1888. check_poly_params_for_type(p, poly_params, tok);
  1889. }
  1890. prev_level := p.expr_level;
  1891. p.expr_level = -1;
  1892. for allow_token(p, .Hash) {
  1893. tag := expect_token_after(p, .Ident, "#");
  1894. switch tag.text {
  1895. case "align":
  1896. if align != nil do error(p, tag.pos, "duplicate union tag '#%s'", tag.text);
  1897. align = parse_expr(p, true);
  1898. case:
  1899. error(p, tag.pos, "invalid union tag '#%s", tag.text);
  1900. }
  1901. }
  1902. p.expr_level = prev_level;
  1903. where_token: tokenizer.Token;
  1904. where_clauses: []^ast.Expr;
  1905. if (p.curr_tok.kind == .Where) {
  1906. where_token = expect_token(p, .Where);
  1907. prev_level := p.expr_level;
  1908. p.expr_level = -1;
  1909. where_clauses = parse_rhs_expr_list(p);
  1910. p.expr_level = prev_level;
  1911. }
  1912. variants: [dynamic]^ast.Expr;
  1913. expect_token_after(p, .Open_Brace, "union");
  1914. for p.curr_tok.kind != .Close_Brace && p.curr_tok.kind != .EOF {
  1915. type := parse_type(p);
  1916. if _, ok := type.derived.(ast.Bad_Expr); !ok {
  1917. append(&variants, type);
  1918. }
  1919. if !allow_token(p, .Comma) {
  1920. break;
  1921. }
  1922. }
  1923. close := expect_token(p, .Close_Brace);
  1924. ut := ast.new(ast.Union_Type, tok.pos, end_pos(close));
  1925. ut.poly_params = poly_params;
  1926. ut.variants = variants[:];
  1927. ut.align = align;
  1928. ut.where_token = where_token;
  1929. ut.where_clauses = where_clauses;
  1930. return ut;
  1931. case .Enum:
  1932. tok := expect_token(p, .Enum);
  1933. base_type: ^ast.Expr;
  1934. if p.curr_tok.kind != .Open_Brace {
  1935. base_type = parse_type(p);
  1936. }
  1937. open := expect_token(p, .Open_Brace);
  1938. fields := parse_elem_list(p);
  1939. close := expect_token(p, .Close_Brace);
  1940. et := ast.new(ast.Enum_Type, tok.pos, end_pos(close));
  1941. et.base_type = base_type;
  1942. et.open = open.pos;
  1943. et.fields = fields;
  1944. et.close = close.pos;
  1945. return et;
  1946. case .Bit_Field:
  1947. tok := expect_token(p, .Bit_Field);
  1948. fields: [dynamic]^ast.Field_Value;
  1949. align: ^ast.Expr;
  1950. prev_level := p.expr_level;
  1951. p.expr_level = -1;
  1952. for allow_token(p, .Hash) {
  1953. tag := expect_token_after(p, .Ident, "#");
  1954. switch tag.text {
  1955. case "align":
  1956. if align != nil {
  1957. error(p, tag.pos, "duplicate bit_field tag '#%s", tag.text);
  1958. }
  1959. align = parse_expr(p, true);
  1960. case:
  1961. error(p, tag.pos, "invalid bit_field tag '#%s", tag.text);
  1962. }
  1963. }
  1964. p.expr_level = prev_level;
  1965. open := expect_token_after(p, .Open_Brace, "bit_field");
  1966. for p.curr_tok.kind != .Close_Brace && p.curr_tok.kind != .EOF {
  1967. name := parse_ident(p);
  1968. colon := expect_token(p, .Colon);
  1969. value := parse_expr(p, true);
  1970. fv := ast.new(ast.Field_Value, name.pos, value.end);
  1971. fv.field = name;
  1972. fv.sep = colon.pos;
  1973. fv.value = value;
  1974. append(&fields, fv);
  1975. if !allow_token(p, .Comma) {
  1976. break;
  1977. }
  1978. }
  1979. close := expect_token(p, .Close_Brace);
  1980. bft := ast.new(ast.Bit_Field_Type, tok.pos, end_pos(close));
  1981. bft.tok_pos = tok.pos;
  1982. bft.open = open.pos;
  1983. bft.fields = fields[:];
  1984. bft.close = close.pos;
  1985. bft.align = align;
  1986. return bft;
  1987. case .Bit_Set:
  1988. tok := expect_token(p, .Bit_Set);
  1989. open := expect_token(p, .Open_Bracket);
  1990. elem, underlying: ^ast.Expr;
  1991. prev_allow_range := p.allow_range;
  1992. p.allow_range = true;
  1993. elem = parse_expr(p, false);
  1994. p.allow_range = prev_allow_range;
  1995. if allow_token(p, .Semicolon) {
  1996. underlying = parse_type(p);
  1997. }
  1998. close := expect_token(p, .Close_Bracket);
  1999. bst := ast.new(ast.Bit_Set_Type, tok.pos, end_pos(close));
  2000. bst.tok_pos = tok.pos;
  2001. bst.open = open.pos;
  2002. bst.elem = elem;
  2003. bst.underlying = underlying;
  2004. bst.close = close.pos;
  2005. return bst;
  2006. }
  2007. return nil;
  2008. }
  2009. is_literal_type :: proc(expr: ^ast.Expr) -> bool {
  2010. val := ast.unparen_expr(expr);
  2011. if val == nil {
  2012. return false;
  2013. }
  2014. switch _ in val.derived {
  2015. case ast.Bad_Expr,
  2016. ast.Ident,
  2017. ast.Selector_Expr,
  2018. ast.Array_Type,
  2019. ast.Struct_Type,
  2020. ast.Union_Type,
  2021. ast.Enum_Type,
  2022. ast.Dynamic_Array_Type,
  2023. ast.Map_Type,
  2024. ast.Bit_Field_Type,
  2025. ast.Bit_Set_Type,
  2026. ast.Call_Expr:
  2027. return true;
  2028. }
  2029. return false;
  2030. }
  2031. parse_value :: proc(p: ^Parser) -> ^ast.Expr {
  2032. if p.curr_tok.kind == .Open_Brace {
  2033. return parse_literal_value(p, nil);
  2034. }
  2035. prev_allow_range = p.allow_range;
  2036. defer p.allow_range = prev_allow_range;
  2037. p.allow_range = true;
  2038. return parse_expr(p, false);
  2039. }
  2040. parse_elem_list :: proc(p: ^Parser) -> []^ast.Expr {
  2041. elems: [dynamic]^ast.Expr;
  2042. for p.curr_tok.kind != .Close_Brace && p.curr_tok.kind != .EOF {
  2043. elem := parse_value(p);
  2044. if p.curr_tok.kind == .Eq {
  2045. eq := expect_token(p, .Eq);
  2046. value := parse_value(p);
  2047. fv := ast.new(ast.Field_Value, elem.pos, value.end);
  2048. fv.field = elem;
  2049. fv.sep = eq.pos;
  2050. fv.value = value;
  2051. elem = fv;
  2052. }
  2053. append(&elems, elem);
  2054. if !allow_token(p, .Comma) {
  2055. break;
  2056. }
  2057. }
  2058. return elems[:];
  2059. }
  2060. parse_literal_value :: proc(p: ^Parser, type: ^ast.Expr) -> ^ast.Comp_Lit {
  2061. elems: []^ast.Expr;
  2062. open := expect_token(p, .Open_Brace);
  2063. p.expr_level += 1;
  2064. if p.curr_tok.kind != .Close_Brace {
  2065. elems = parse_elem_list(p);
  2066. }
  2067. p.expr_level -= 1;
  2068. close := expect_token_after(p, .Close_Brace, "compound literal");
  2069. pos := type != nil ? type.pos : open.pos;
  2070. lit := ast.new(ast.Comp_Lit, pos, end_pos(close));
  2071. lit.type = type;
  2072. lit.open = open.pos;
  2073. lit.elems = elems;
  2074. lit.close = close.pos;
  2075. return lit;
  2076. }
  2077. parse_call_expr :: proc(p: ^Parser, operand: ^ast.Expr) -> ^ast.Call_Expr {
  2078. args: [dynamic]^ast.Expr;
  2079. ellipsis: tokenizer.Token;
  2080. p.expr_level += 1;
  2081. open := expect_token(p, .Open_Paren);
  2082. for p.curr_tok.kind != .Close_Paren &&
  2083. p.curr_tok.kind != .EOF &&
  2084. ellipsis.pos.line == 0 {
  2085. if p.curr_tok.kind == .Comma {
  2086. error(p, p.curr_tok.pos, "expected an expression not ,");
  2087. } else if p.curr_tok.kind == .Eq {
  2088. error(p, p.curr_tok.pos, "expected an expression not =");
  2089. }
  2090. prefix_ellipsis := false;
  2091. if p.curr_tok.kind == .Ellipsis {
  2092. prefix_ellipsis = true;
  2093. ellipsis = expect_token(p, .Ellipsis);
  2094. }
  2095. arg := parse_expr(p, false);
  2096. if p.curr_tok.kind == .Eq {
  2097. eq := expect_token(p, .Eq);
  2098. if prefix_ellipsis {
  2099. error(p, ellipsis.pos, "'..' must be applied to value rather than a field name");
  2100. }
  2101. value := parse_value(p);
  2102. fv := ast.new(ast.Field_Value, arg.pos, value.end);
  2103. fv.field = arg;
  2104. fv.sep = eq.pos;
  2105. fv.value = value;
  2106. arg = fv;
  2107. }
  2108. append(&args, arg);
  2109. if !allow_token(p, .Comma) {
  2110. break;
  2111. }
  2112. }
  2113. close := expect_token_after(p, .Close_Paren, "argument list");
  2114. p.expr_level -= 1;
  2115. ce := ast.new(ast.Call_Expr, operand.pos, end_pos(close));
  2116. ce.expr = operand;
  2117. ce.open = open.pos;
  2118. ce.args = args[:];
  2119. ce.ellipsis = ellipsis;
  2120. ce.close = close.pos;
  2121. return ce;
  2122. }
  2123. parse_atom_expr :: proc(p: ^Parser, value: ^ast.Expr, lhs: bool) -> (operand: ^ast.Expr) {
  2124. operand = value;
  2125. if operand == nil {
  2126. if p.allow_type do return nil;
  2127. error(p, p.curr_tok.pos, "expected an operand");
  2128. be := ast.new(ast.Bad_Expr, p.curr_tok.pos, end_pos(p.curr_tok));
  2129. advance_token(p);
  2130. operand = be;
  2131. }
  2132. loop := true;
  2133. is_lhs := lhs;
  2134. for loop {
  2135. switch p.curr_tok.kind {
  2136. case:
  2137. loop = false;
  2138. case .Open_Paren:
  2139. operand = parse_call_expr(p, operand);
  2140. case .Open_Bracket:
  2141. prev_allow_range := p.allow_range;
  2142. defer p.allow_range = prev_allow_range;
  2143. p.allow_range = false;
  2144. indicies: [2]^ast.Expr;
  2145. interval: tokenizer.Token;
  2146. is_slice_op := false;
  2147. p.expr_level += 1;
  2148. open := expect_token(p, .Open_Bracket);
  2149. switch p.curr_tok.kind {
  2150. case .Colon, .Ellipsis, .Range_Half:
  2151. // NOTE(bill): Do not err yet
  2152. break;
  2153. case:
  2154. indicies[0] = parse_expr(p, false);
  2155. }
  2156. switch p.curr_tok.kind {
  2157. case .Ellipsis, .Range_Half:
  2158. error(p, p.curr_tok.pos, "expected a colon, not a range");
  2159. fallthrough;
  2160. case .Colon:
  2161. interval = advance_token(p);
  2162. is_slice_op = true;
  2163. if (p.curr_tok.kind != .Close_Bracket && p.curr_tok.kind != .EOF) {
  2164. indicies[1] = parse_expr(p, false);
  2165. }
  2166. }
  2167. close := expect_token(p, .Close_Bracket);
  2168. p.expr_level -= 1;
  2169. if is_slice_op {
  2170. se := ast.new(ast.Slice_Expr, operand.pos, end_pos(close));
  2171. se.expr = operand;
  2172. se.open = open.pos;
  2173. se.low = indicies[0];
  2174. se.interval = interval;
  2175. se.high = indicies[1];
  2176. se.close = close.pos;
  2177. operand = se;
  2178. } else {
  2179. ie := ast.new(ast.Index_Expr, operand.pos, end_pos(close));
  2180. ie.expr = operand;
  2181. ie.open = open.pos;
  2182. ie.index = indicies[0];
  2183. ie.close = close.pos;
  2184. operand = ie;
  2185. }
  2186. case .Period:
  2187. tok := expect_token(p, .Period);
  2188. switch p.curr_tok.kind {
  2189. case .Ident:
  2190. field := parse_ident(p);
  2191. sel := ast.new(ast.Selector_Expr, operand.pos, field.end);
  2192. sel.expr = operand;
  2193. sel.field = field;
  2194. operand = sel;
  2195. case .Open_Paren:
  2196. open := expect_token(p, .Open_Paren);
  2197. type := parse_type(p);
  2198. close := expect_token(p, .Close_Paren);
  2199. ta := ast.new(ast.Type_Assertion, operand.pos, end_pos(close));
  2200. ta.expr = operand;
  2201. ta.open = open.pos;
  2202. ta.type = type;
  2203. ta.close = close.pos;
  2204. operand = ta;
  2205. case:
  2206. error(p, p.curr_tok.pos, "expected a selector");
  2207. advance_token(p);
  2208. operand = ast.new(ast.Bad_Expr, operand.pos, end_pos(tok));
  2209. }
  2210. case .Pointer:
  2211. op := expect_token(p, .Pointer);
  2212. deref := ast.new(ast.Deref_Expr, operand.pos, end_pos(op));
  2213. deref.expr = operand;
  2214. deref.op = op;
  2215. operand = deref;
  2216. case .Open_Brace:
  2217. if !is_lhs && is_literal_type(operand) && p.expr_level >= 0 {
  2218. operand = parse_literal_value(p, operand);
  2219. } else {
  2220. loop = false;
  2221. }
  2222. }
  2223. is_lhs = false;
  2224. }
  2225. return operand;
  2226. }
  2227. parse_expr :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
  2228. return parse_binary_expr(p, lhs, 0+1);
  2229. }
  2230. parse_unary_expr :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
  2231. switch p.curr_tok.kind {
  2232. case .Transmute, .Cast:
  2233. tok := advance_token(p);
  2234. open := expect_token(p, .Open_Paren);
  2235. type := parse_type(p);
  2236. close := expect_token(p, .Close_Paren);
  2237. expr := parse_unary_expr(p, lhs);
  2238. tc := ast.new(ast.Type_Cast, tok.pos, expr.end);
  2239. tc.tok = tok;
  2240. tc.open = open.pos;
  2241. tc.type = type;
  2242. tc.close = close.pos;
  2243. tc.expr = expr;
  2244. return tc;
  2245. case .Auto_Cast:
  2246. op := advance_token(p);
  2247. expr := parse_unary_expr(p, lhs);
  2248. ac := ast.new(ast.Auto_Cast, op.pos, expr.end);
  2249. ac.op = op;
  2250. ac.expr = expr;
  2251. return ac;
  2252. case .Add, .Sub,
  2253. .Not, .Xor,
  2254. .And:
  2255. op := advance_token(p);
  2256. expr := parse_unary_expr(p, lhs);
  2257. ue := ast.new(ast.Unary_Expr, op.pos, expr.end);
  2258. ue.op = op;
  2259. ue.expr = expr;
  2260. return ue;
  2261. case .Period:
  2262. op := advance_token(p);
  2263. field := parse_ident(p);
  2264. ise := ast.new(ast.Implicit_Selector_Expr, op.pos, field.end);
  2265. ise.field = field;
  2266. return ise;
  2267. }
  2268. return parse_atom_expr(p, parse_operand(p, lhs), lhs);
  2269. }
  2270. parse_binary_expr :: proc(p: ^Parser, lhs: bool, prec_in: int) -> ^ast.Expr {
  2271. expr := parse_unary_expr(p, lhs);
  2272. for prec := token_precedence(p, p.curr_tok.kind); prec >= prec_in; prec -= 1 {
  2273. for {
  2274. op := p.curr_tok;
  2275. op_prec := token_precedence(p, op.kind);
  2276. if op_prec != prec {
  2277. break;
  2278. }
  2279. expect_operator(p);
  2280. if op.kind == .Question {
  2281. cond := expr;
  2282. x := parse_expr(p, lhs);
  2283. colon := expect_token(p, .Colon);
  2284. y := parse_expr(p, lhs);
  2285. te := ast.new(ast.Ternary_Expr, expr.pos, end_pos(p.prev_tok));
  2286. te.cond = cond;
  2287. te.op1 = op;
  2288. te.x = x;
  2289. te.op2 = colon;
  2290. te.y = y;
  2291. expr = te;
  2292. } else {
  2293. right := parse_binary_expr(p, false, prec+1);
  2294. if right == nil {
  2295. error(p, op.pos, "expected expression on the right-hand side of the binary operator");
  2296. }
  2297. be := ast.new(ast.Binary_Expr, expr.pos, end_pos(p.prev_tok));
  2298. be.left = expr;
  2299. be.op = op;
  2300. be.right = right;
  2301. expr = be;
  2302. }
  2303. }
  2304. }
  2305. return expr;
  2306. }
  2307. parse_expr_list :: proc(p: ^Parser, lhs: bool) -> ([]^ast.Expr) {
  2308. list: [dynamic]^ast.Expr;
  2309. for {
  2310. expr := parse_expr(p, lhs);
  2311. append(&list, expr);
  2312. if p.curr_tok.kind != .Comma || p.curr_tok.kind == .EOF {
  2313. break;
  2314. }
  2315. advance_token(p);
  2316. }
  2317. return list[:];
  2318. }
  2319. parse_lhs_expr_list :: proc(p: ^Parser) -> []^ast.Expr {
  2320. return parse_expr_list(p, true);
  2321. }
  2322. parse_rhs_expr_list :: proc(p: ^Parser) -> []^ast.Expr {
  2323. return parse_expr_list(p, false);
  2324. }
  2325. parse_simple_stmt :: proc(p: ^Parser, flags: Stmt_Allow_Flags) -> ^ast.Stmt {
  2326. start_tok := p.curr_tok;
  2327. docs := p.lead_comment;
  2328. lhs := parse_lhs_expr_list(p);
  2329. op := p.curr_tok;
  2330. switch {
  2331. case tokenizer.is_assignment_operator(op.kind):
  2332. // if p.curr_proc == nil {
  2333. // error(p, p.curr_tok.pos, "simple statements are not allowed at the file scope");
  2334. // return ast.new(ast.Bad_Stmt, start_tok.pos, end_pos(p.curr_tok));
  2335. // }
  2336. advance_token(p);
  2337. rhs := parse_rhs_expr_list(p);
  2338. if len(rhs) == 0 {
  2339. error(p, p.curr_tok.pos, "no right-hand side in assignment statement");
  2340. return ast.new(ast.Bad_Stmt, start_tok.pos, end_pos(p.curr_tok));
  2341. }
  2342. stmt := ast.new(ast.Assign_Stmt, lhs[0].pos, rhs[len(rhs)-1].end);
  2343. stmt.lhs = lhs;
  2344. stmt.op = op;
  2345. stmt.rhs = rhs;
  2346. return stmt;
  2347. case op.kind == .In:
  2348. if .In in flags {
  2349. allow_token(p, .In);
  2350. prev_allow_range := p.allow_range;
  2351. p.allow_range = true;
  2352. expr := parse_expr(p, false);
  2353. p.allow_range = prev_allow_range;
  2354. rhs := make([]^ast.Expr, 1);
  2355. rhs[0] = expr;
  2356. stmt := ast.new(ast.Assign_Stmt, lhs[0].pos, rhs[len(rhs)-1].end);
  2357. stmt.lhs = lhs;
  2358. stmt.op = op;
  2359. stmt.rhs = rhs;
  2360. return stmt;
  2361. }
  2362. case op.kind == .Colon:
  2363. expect_token_after(p, .Colon, "identifier list");
  2364. if .Label in flags && len(lhs) == 1 {
  2365. switch p.curr_tok.kind {
  2366. case .Open_Brace, .If, .For, .Switch:
  2367. label := lhs[0];
  2368. stmt := parse_stmt(p);
  2369. if stmt != nil do switch n in &stmt.derived {
  2370. case ast.Block_Stmt: n.label = label;
  2371. case ast.If_Stmt: n.label = label;
  2372. case ast.For_Stmt: n.label = label;
  2373. case ast.Switch_Stmt: n.label = label;
  2374. case ast.Type_Switch_Stmt: n.label = label;
  2375. }
  2376. return stmt;
  2377. }
  2378. }
  2379. return parse_value_decl(p, lhs, docs);
  2380. }
  2381. if len(lhs) > 1 {
  2382. error(p, op.pos, "expected 1 expression, got %d", len(lhs));
  2383. return ast.new(ast.Bad_Stmt, start_tok.pos, end_pos(p.curr_tok));
  2384. }
  2385. es := ast.new(ast.Expr_Stmt, lhs[0].pos, lhs[0].end);
  2386. es.expr = lhs[0];
  2387. return es;
  2388. }
  2389. parse_value_decl :: proc(p: ^Parser, names: []^ast.Expr, docs: ^ast.Comment_Group) -> ^ast.Decl {
  2390. is_mutable := true;
  2391. values: []^ast.Expr;
  2392. type := parse_type_or_ident(p);
  2393. switch p.curr_tok.kind {
  2394. case .Eq, .Colon:
  2395. sep := advance_token(p);
  2396. is_mutable = sep.kind != .Colon;
  2397. values = parse_rhs_expr_list(p);
  2398. if len(values) > len(names) {
  2399. error(p, p.curr_tok.pos, "too many values on the right-hand side of the declaration");
  2400. } else if len(values) < len(names) && !is_mutable {
  2401. error(p, p.curr_tok.pos, "all constant declarations must be defined");
  2402. } else if len(values) == 0 {
  2403. error(p, p.curr_tok.pos, "expected an expression for this declaration");
  2404. }
  2405. }
  2406. if is_mutable {
  2407. if type == nil && len(values) == 0 {
  2408. error(p, p.curr_tok.pos, "missing variable type or initialization");
  2409. return ast.new(ast.Bad_Decl, names[0].pos, end_pos(p.curr_tok));
  2410. }
  2411. } else {
  2412. if type == nil && len(values) == 0 && len(names) > 0 {
  2413. error(p, p.curr_tok.pos, "missing constant value");
  2414. return ast.new(ast.Bad_Decl, names[0].pos, end_pos(p.curr_tok));
  2415. }
  2416. }
  2417. if p.expr_level >= 0 {
  2418. end: ^ast.Expr;
  2419. if !is_mutable && len(values) > 0 {
  2420. end = values[len(values)-1];
  2421. }
  2422. if p.curr_tok.kind == .Close_Brace &&
  2423. p.curr_tok.pos.line == p.prev_tok.pos.line {
  2424. } else {
  2425. expect_semicolon(p, end);
  2426. }
  2427. }
  2428. if p.curr_proc == nil {
  2429. if len(values) > 0 && len(names) != len(values) {
  2430. error(p, values[0].pos, "expected %d expressions on the right-hand side, got %d", len(names), len(values));
  2431. }
  2432. }
  2433. decl := ast.new(ast.Value_Decl, names[0].pos, end_pos(p.prev_tok));
  2434. decl.docs = docs;
  2435. decl.names = names;
  2436. decl.type = type;
  2437. decl.values = values;
  2438. decl.is_mutable = is_mutable;
  2439. return decl;
  2440. }
  2441. parse_import_decl :: proc(p: ^Parser, kind := Import_Decl_Kind.Standard) -> ^ast.Import_Decl {
  2442. docs := p.lead_comment;
  2443. tok := expect_token(p, .Import);
  2444. import_name: tokenizer.Token;
  2445. is_using := kind != Import_Decl_Kind.Standard;
  2446. switch p.curr_tok.kind {
  2447. case .Ident:
  2448. import_name = advance_token(p);
  2449. case:
  2450. import_name.pos = p.curr_tok.pos;
  2451. }
  2452. if !is_using && is_blank_ident(import_name) {
  2453. error(p, import_name.pos, "illegal import name: '_'");
  2454. }
  2455. path := expect_token_after(p, .String, "import");
  2456. decl := ast.new(ast.Import_Decl, tok.pos, end_pos(path));
  2457. decl.docs = docs;
  2458. decl.is_using = is_using;
  2459. decl.import_tok = tok;
  2460. decl.name = import_name;
  2461. decl.relpath = path;
  2462. decl.fullpath = path.text;
  2463. if p.curr_proc != nil {
  2464. error(p, decl.pos, "import declarations cannot be used within a procedure, it must be done at the file scope");
  2465. } else {
  2466. append(&p.file.imports, decl);
  2467. }
  2468. expect_semicolon(p, decl);
  2469. decl.comment = p.line_comment;
  2470. return decl;
  2471. }