parser.odin 86 KB

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