parser.odin 84 KB

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