parser.odin 79 KB

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