parser.odin 84 KB

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