parser.odin 85 KB

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