parser.odin 85 KB

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