parser.odin 89 KB

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