parser.odin 85 KB

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