parser.odin 87 KB

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