parser.odin 83 KB

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