parser.cpp 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883
  1. struct AstNode;
  2. struct Scope;
  3. enum ParseFileError {
  4. ParseFile_None,
  5. ParseFile_WrongExtension,
  6. ParseFile_InvalidFile,
  7. ParseFile_EmptyFile,
  8. ParseFile_Permission,
  9. ParseFile_NotFound,
  10. ParseFile_InvalidToken,
  11. ParseFile_Count,
  12. };
  13. typedef gbArray(AstNode *) AstNodeArray;
  14. struct AstFile {
  15. gbArena arena;
  16. Tokenizer tokenizer;
  17. gbArray(Token) tokens;
  18. Token * cursor; // NOTE(bill): Current token, easy to peek forward and backwards if needed
  19. // >= 0: In Expression
  20. // < 0: In Control Clause
  21. // NOTE(bill): Used to prevent type literals in control clauses
  22. isize expr_level;
  23. AstNodeArray decls;
  24. b32 is_global_scope;
  25. AstNode *curr_proc;
  26. isize scope_level;
  27. Scope * scope; // NOTE(bill): Created in checker
  28. ErrorCollector error_collector;
  29. // TODO(bill): Error recovery
  30. // NOTE(bill): Error recovery
  31. #define PARSER_MAX_FIX_COUNT 6
  32. isize fix_count;
  33. TokenPos fix_prev_pos;
  34. };
  35. struct ImportedFile {
  36. String path;
  37. TokenPos pos; // #import
  38. };
  39. struct Parser {
  40. String init_fullpath;
  41. gbArray(AstFile) files;
  42. gbArray(ImportedFile) imports;
  43. gbArray(String) libraries;
  44. gbArray(String) system_libraries;
  45. isize load_index;
  46. isize total_token_count;
  47. };
  48. enum DeclKind {
  49. Declaration_Invalid,
  50. Declaration_Mutable,
  51. Declaration_Immutable,
  52. Declaration_Count,
  53. };
  54. enum ProcTag {
  55. ProcTag_bounds_check = GB_BIT(0),
  56. ProcTag_no_bounds_check = GB_BIT(1),
  57. ProcTag_foreign = GB_BIT(2),
  58. ProcTag_inline = GB_BIT(3),
  59. ProcTag_no_inline = GB_BIT(4),
  60. };
  61. enum VarDeclTag {
  62. VarDeclTag_thread_local = GB_BIT(0),
  63. };
  64. enum TypeFlag : u32 {
  65. TypeFlag_thread_local = GB_BIT(0),
  66. TypeFlag_volatile = GB_BIT(1),
  67. TypeFlag_atomic = GB_BIT(2),
  68. };
  69. enum StmtStateFlag : u32 {
  70. StmtStateFlag_bounds_check = GB_BIT(0),
  71. StmtStateFlag_no_bounds_check = GB_BIT(1),
  72. };
  73. enum CallExprKind {
  74. CallExpr_Prefix, // call(...)
  75. CallExpr_Postfix, // a'call
  76. CallExpr_Infix, // a ''call b
  77. };
  78. AstNodeArray make_ast_node_array(AstFile *f) {
  79. AstNodeArray a;
  80. gb_array_init(a, gb_arena_allocator(&f->arena));
  81. GB_ASSERT(a != NULL);
  82. return a;
  83. }
  84. #define AST_NODE_KINDS \
  85. AST_NODE_KIND(Invalid, "invalid node", struct{}) \
  86. AST_NODE_KIND(BasicLit, "basic literal", Token) \
  87. AST_NODE_KIND(Ident, "identifier", Token) \
  88. AST_NODE_KIND(Ellipsis, "ellipsis", struct { \
  89. Token token; \
  90. AstNode *expr; \
  91. }) \
  92. AST_NODE_KIND(ProcLit, "procedure literal", struct { \
  93. AstNode *type; \
  94. AstNode *body; \
  95. u64 tags; \
  96. }) \
  97. AST_NODE_KIND(CompoundLit, "compound literal", struct { \
  98. AstNode *type; \
  99. AstNodeArray elems; \
  100. Token open, close; \
  101. }) \
  102. AST_NODE_KIND(_ExprBegin, "", struct{}) \
  103. AST_NODE_KIND(BadExpr, "bad expression", struct { Token begin, end; }) \
  104. AST_NODE_KIND(TagExpr, "tag expression", struct { Token token, name; AstNode *expr; }) \
  105. AST_NODE_KIND(UnaryExpr, "unary expression", struct { Token op; AstNode *expr; }) \
  106. AST_NODE_KIND(BinaryExpr, "binary expression", struct { Token op; AstNode *left, *right; } ) \
  107. AST_NODE_KIND(ParenExpr, "parentheses expression", struct { AstNode *expr; Token open, close; }) \
  108. AST_NODE_KIND(SelectorExpr, "selector expression", struct { Token token; AstNode *expr, *selector; }) \
  109. AST_NODE_KIND(IndexExpr, "index expression", struct { AstNode *expr, *index; Token open, close; }) \
  110. AST_NODE_KIND(DerefExpr, "dereference expression", struct { Token op; AstNode *expr; }) \
  111. AST_NODE_KIND(CallExpr, "call expression", struct { \
  112. AstNode *proc; \
  113. gbArray(AstNode *) args; \
  114. Token open, close; \
  115. Token ellipsis; \
  116. CallExprKind kind; \
  117. }) \
  118. AST_NODE_KIND(SliceExpr, "slice expression", struct { \
  119. AstNode *expr; \
  120. Token open, close; \
  121. AstNode *low, *high, *max; \
  122. b32 triple_indexed; \
  123. }) \
  124. AST_NODE_KIND(FieldValue, "field value", struct { Token eq; AstNode *field, *value; }) \
  125. AST_NODE_KIND(_ExprEnd, "", struct{}) \
  126. AST_NODE_KIND(_StmtBegin, "", struct{}) \
  127. AST_NODE_KIND(BadStmt, "bad statement", struct { Token begin, end; }) \
  128. AST_NODE_KIND(EmptyStmt, "empty statement", struct { Token token; }) \
  129. AST_NODE_KIND(ExprStmt, "expression statement", struct { AstNode *expr; } ) \
  130. AST_NODE_KIND(IncDecStmt, "increment/decrement statement", struct { Token op; AstNode *expr; }) \
  131. AST_NODE_KIND(TagStmt, "tag statement", struct { \
  132. Token token; \
  133. Token name; \
  134. AstNode *stmt; \
  135. }) \
  136. AST_NODE_KIND(AssignStmt, "assign statement", struct { \
  137. Token op; \
  138. AstNodeArray lhs, rhs; \
  139. }) \
  140. AST_NODE_KIND(_ComplexStmtBegin, "", struct{}) \
  141. AST_NODE_KIND(BlockStmt, "block statement", struct { \
  142. AstNodeArray stmts; \
  143. Token open, close; \
  144. }) \
  145. AST_NODE_KIND(IfStmt, "if statement", struct { \
  146. Token token; \
  147. AstNode *init; \
  148. AstNode *cond; \
  149. AstNode *body; \
  150. AstNode *else_stmt; \
  151. }) \
  152. AST_NODE_KIND(ReturnStmt, "return statement", struct { \
  153. Token token; \
  154. AstNodeArray results; \
  155. }) \
  156. AST_NODE_KIND(ForStmt, "for statement", struct { \
  157. Token token; \
  158. AstNode *init, *cond, *post; \
  159. AstNode *body; \
  160. }) \
  161. AST_NODE_KIND(CaseClause, "case clause", struct { \
  162. Token token; \
  163. AstNodeArray list, stmts; \
  164. }) \
  165. AST_NODE_KIND(MatchStmt, "match statement", struct { \
  166. Token token; \
  167. AstNode *init, *tag; \
  168. AstNode *body; \
  169. }) \
  170. AST_NODE_KIND(TypeMatchStmt, "type match statement", struct { \
  171. Token token; \
  172. AstNode *tag, *var; \
  173. AstNode *body; \
  174. }) \
  175. AST_NODE_KIND(DeferStmt, "defer statement", struct { Token token; AstNode *stmt; }) \
  176. AST_NODE_KIND(BranchStmt, "branch statement", struct { Token token; }) \
  177. AST_NODE_KIND(UsingStmt, "using statement", struct { Token token; AstNode *node; }) \
  178. AST_NODE_KIND(AsmOperand, "assembly operand", struct { \
  179. Token string; \
  180. AstNode *operand; \
  181. }) \
  182. AST_NODE_KIND(AsmStmt, "assembly statement", struct { \
  183. Token token; \
  184. b32 is_volatile; \
  185. Token open, close; \
  186. Token code_string; \
  187. AstNode *output_list; \
  188. AstNode *input_list; \
  189. AstNode *clobber_list; \
  190. isize output_count, input_count, clobber_count; \
  191. }) \
  192. \
  193. AST_NODE_KIND(_ComplexStmtEnd, "", struct{}) \
  194. AST_NODE_KIND(_StmtEnd, "", struct{}) \
  195. AST_NODE_KIND(_DeclBegin, "", struct{}) \
  196. AST_NODE_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
  197. AST_NODE_KIND(VarDecl, "variable declaration", struct { \
  198. DeclKind kind; \
  199. u32 tags; \
  200. b32 is_using; \
  201. AstNodeArray names; \
  202. AstNode *type; \
  203. AstNodeArray values; \
  204. }) \
  205. AST_NODE_KIND(ProcDecl, "procedure declaration", struct { \
  206. AstNode *name; \
  207. AstNode *type; \
  208. AstNode *body; \
  209. u64 tags; \
  210. String foreign_name; \
  211. }) \
  212. AST_NODE_KIND(TypeDecl, "type declaration", struct { Token token; AstNode *name, *type; }) \
  213. AST_NODE_KIND(ImportDecl, "import declaration", struct { \
  214. Token token, relpath; \
  215. String fullpath; \
  216. Token import_name; \
  217. }) \
  218. AST_NODE_KIND(ForeignSystemLibrary, "foreign system library", struct { Token token, filepath; }) \
  219. AST_NODE_KIND(_DeclEnd, "", struct{}) \
  220. AST_NODE_KIND(_TypeBegin, "", struct{}) \
  221. AST_NODE_KIND(Field, "field", struct { \
  222. AstNodeArray names; \
  223. AstNode *type; \
  224. b32 is_using; \
  225. }) \
  226. AST_NODE_KIND(ProcType, "procedure type", struct { \
  227. Token token; \
  228. AstNodeArray params; \
  229. AstNodeArray results; \
  230. }) \
  231. AST_NODE_KIND(PointerType, "pointer type", struct { \
  232. Token token; \
  233. AstNode *type; \
  234. }) \
  235. AST_NODE_KIND(ArrayType, "array type", struct { \
  236. Token token; \
  237. AstNode *count; \
  238. AstNode *elem; \
  239. }) \
  240. AST_NODE_KIND(VectorType, "vector type", struct { \
  241. Token token; \
  242. AstNode *count; \
  243. AstNode *elem; \
  244. }) \
  245. AST_NODE_KIND(StructType, "struct type", struct { \
  246. Token token; \
  247. AstNodeArray decls; \
  248. isize decl_count; \
  249. b32 is_packed; \
  250. b32 is_ordered; \
  251. }) \
  252. AST_NODE_KIND(UnionType, "union type", struct { \
  253. Token token; \
  254. AstNodeArray decls; \
  255. isize decl_count; \
  256. }) \
  257. AST_NODE_KIND(RawUnionType, "raw union type", struct { \
  258. Token token; \
  259. AstNodeArray decls; \
  260. isize decl_count; \
  261. }) \
  262. AST_NODE_KIND(EnumType, "enum type", struct { \
  263. Token token; \
  264. AstNode *base_type; \
  265. AstNodeArray fields; \
  266. }) \
  267. AST_NODE_KIND(_TypeEnd, "", struct{}) \
  268. AST_NODE_KIND(Count, "", struct{})
  269. enum AstNodeKind {
  270. #define AST_NODE_KIND(_kind_name_, ...) GB_JOIN2(AstNode_, _kind_name_),
  271. AST_NODE_KINDS
  272. #undef AST_NODE_KIND
  273. };
  274. String const ast_node_strings[] = {
  275. #define AST_NODE_KIND(_kind_name_, name, ...) {cast(u8 *)name, gb_size_of(name)-1},
  276. AST_NODE_KINDS
  277. #undef AST_NODE_KIND
  278. };
  279. struct AstNode {
  280. AstNodeKind kind;
  281. // AstNode *prev, *next; // NOTE(bill): allow for Linked list
  282. u32 type_flags;
  283. u32 stmt_state_flags;
  284. union {
  285. #define AST_NODE_KIND(_kind_name_, name, ...) __VA_ARGS__ _kind_name_;
  286. AST_NODE_KINDS
  287. #undef AST_NODE_KIND
  288. };
  289. };
  290. #define ast_node(n_, Kind_, node_) auto *n_ = &(node_)->Kind_; GB_ASSERT((node_)->kind == GB_JOIN2(AstNode_, Kind_))
  291. #define case_ast_node(n_, Kind_, node_) case GB_JOIN2(AstNode_, Kind_): { ast_node(n_, Kind_, node_);
  292. #define case_end } break;
  293. gb_inline b32 is_ast_node_expr(AstNode *node) {
  294. return gb_is_between(node->kind, AstNode__ExprBegin+1, AstNode__ExprEnd-1);
  295. }
  296. gb_inline b32 is_ast_node_stmt(AstNode *node) {
  297. return gb_is_between(node->kind, AstNode__StmtBegin+1, AstNode__StmtEnd-1);
  298. }
  299. gb_inline b32 is_ast_node_complex_stmt(AstNode *node) {
  300. return gb_is_between(node->kind, AstNode__ComplexStmtBegin+1, AstNode__ComplexStmtEnd-1);
  301. }
  302. gb_inline b32 is_ast_node_decl(AstNode *node) {
  303. return gb_is_between(node->kind, AstNode__DeclBegin+1, AstNode__DeclEnd-1);
  304. }
  305. gb_inline b32 is_ast_node_type(AstNode *node) {
  306. return gb_is_between(node->kind, AstNode__TypeBegin+1, AstNode__TypeEnd-1);
  307. }
  308. Token ast_node_token(AstNode *node) {
  309. switch (node->kind) {
  310. case AstNode_BasicLit:
  311. return node->BasicLit;
  312. case AstNode_Ident:
  313. return node->Ident;
  314. case AstNode_ProcLit:
  315. return ast_node_token(node->ProcLit.type);
  316. case AstNode_CompoundLit:
  317. return ast_node_token(node->CompoundLit.type);
  318. case AstNode_TagExpr:
  319. return node->TagExpr.token;
  320. case AstNode_BadExpr:
  321. return node->BadExpr.begin;
  322. case AstNode_UnaryExpr:
  323. return node->UnaryExpr.op;
  324. case AstNode_BinaryExpr:
  325. return ast_node_token(node->BinaryExpr.left);
  326. case AstNode_ParenExpr:
  327. return node->ParenExpr.open;
  328. case AstNode_CallExpr:
  329. return ast_node_token(node->CallExpr.proc);
  330. case AstNode_SelectorExpr:
  331. return ast_node_token(node->SelectorExpr.selector);
  332. case AstNode_IndexExpr:
  333. return node->IndexExpr.open;
  334. case AstNode_SliceExpr:
  335. return node->SliceExpr.open;
  336. case AstNode_Ellipsis:
  337. return node->Ellipsis.token;
  338. case AstNode_FieldValue:
  339. return node->FieldValue.eq;
  340. case AstNode_DerefExpr:
  341. return node->DerefExpr.op;
  342. case AstNode_BadStmt:
  343. return node->BadStmt.begin;
  344. case AstNode_EmptyStmt:
  345. return node->EmptyStmt.token;
  346. case AstNode_ExprStmt:
  347. return ast_node_token(node->ExprStmt.expr);
  348. case AstNode_TagStmt:
  349. return node->TagStmt.token;
  350. case AstNode_IncDecStmt:
  351. return node->IncDecStmt.op;
  352. case AstNode_AssignStmt:
  353. return node->AssignStmt.op;
  354. case AstNode_BlockStmt:
  355. return node->BlockStmt.open;
  356. case AstNode_IfStmt:
  357. return node->IfStmt.token;
  358. case AstNode_ReturnStmt:
  359. return node->ReturnStmt.token;
  360. case AstNode_ForStmt:
  361. return node->ForStmt.token;
  362. case AstNode_MatchStmt:
  363. return node->MatchStmt.token;
  364. case AstNode_CaseClause:
  365. return node->CaseClause.token;
  366. case AstNode_DeferStmt:
  367. return node->DeferStmt.token;
  368. case AstNode_BranchStmt:
  369. return node->BranchStmt.token;
  370. case AstNode_UsingStmt:
  371. return node->UsingStmt.token;
  372. case AstNode_AsmStmt:
  373. return node->AsmStmt.token;
  374. case AstNode_BadDecl:
  375. return node->BadDecl.begin;
  376. case AstNode_VarDecl:
  377. return ast_node_token(node->VarDecl.names[0]);
  378. case AstNode_ProcDecl:
  379. return node->ProcDecl.name->Ident;
  380. case AstNode_TypeDecl:
  381. return node->TypeDecl.token;
  382. case AstNode_ImportDecl:
  383. return node->ImportDecl.token;
  384. case AstNode_ForeignSystemLibrary:
  385. return node->ForeignSystemLibrary.token;
  386. case AstNode_Field: {
  387. if (node->Field.names)
  388. return ast_node_token(node->Field.names[0]);
  389. else
  390. return ast_node_token(node->Field.type);
  391. }
  392. case AstNode_ProcType:
  393. return node->ProcType.token;
  394. case AstNode_PointerType:
  395. return node->PointerType.token;
  396. case AstNode_ArrayType:
  397. return node->ArrayType.token;
  398. case AstNode_VectorType:
  399. return node->VectorType.token;
  400. case AstNode_StructType:
  401. return node->StructType.token;
  402. case AstNode_UnionType:
  403. return node->UnionType.token;
  404. case AstNode_RawUnionType:
  405. return node->RawUnionType.token;
  406. case AstNode_EnumType:
  407. return node->EnumType.token;
  408. }
  409. return empty_token;
  410. }
  411. HashKey hash_token(Token t) {
  412. return hash_string(t.string);
  413. }
  414. #define ast_file_err(f, token, fmt, ...) ast_file_err_(f, __FUNCTION__, token, fmt, ##__VA_ARGS__)
  415. void ast_file_err_(AstFile *file, char *function, Token token, char *fmt, ...) {
  416. // NOTE(bill): Duplicate error, skip it
  417. if (!token_pos_are_equal(file->error_collector.prev, token.pos)) {
  418. va_list va;
  419. file->error_collector.prev = token.pos;
  420. #if 0
  421. gb_printf_err("%s()\n", function);
  422. #endif
  423. va_start(va, fmt);
  424. gb_printf_err("%.*s(%td:%td) Syntax error: %s\n",
  425. LIT(token.pos.file), token.pos.line, token.pos.column,
  426. gb_bprintf_va(fmt, va));
  427. va_end(va);
  428. }
  429. file->error_collector.count++;
  430. }
  431. // NOTE(bill): And this below is why is I/we need a new language! Discriminated unions are a pain in C/C++
  432. gb_inline AstNode *make_node(AstFile *f, AstNodeKind kind) {
  433. gbArena *arena = &f->arena;
  434. if (gb_arena_size_remaining(arena, GB_DEFAULT_MEMORY_ALIGNMENT) <= gb_size_of(AstNode)) {
  435. // NOTE(bill): If a syntax error is so bad, just quit!
  436. gb_exit(1);
  437. }
  438. AstNode *node = gb_alloc_item(gb_arena_allocator(arena), AstNode);
  439. node->kind = kind;
  440. return node;
  441. }
  442. gb_inline AstNode *make_bad_expr(AstFile *f, Token begin, Token end) {
  443. AstNode *result = make_node(f, AstNode_BadExpr);
  444. result->BadExpr.begin = begin;
  445. result->BadExpr.end = end;
  446. return result;
  447. }
  448. gb_inline AstNode *make_tag_expr(AstFile *f, Token token, Token name, AstNode *expr) {
  449. AstNode *result = make_node(f, AstNode_TagExpr);
  450. result->TagExpr.token = token;
  451. result->TagExpr.name = name;
  452. result->TagExpr.expr = expr;
  453. return result;
  454. }
  455. gb_inline AstNode *make_tag_stmt(AstFile *f, Token token, Token name, AstNode *stmt) {
  456. AstNode *result = make_node(f, AstNode_TagStmt);
  457. result->TagStmt.token = token;
  458. result->TagStmt.name = name;
  459. result->TagStmt.stmt = stmt;
  460. return result;
  461. }
  462. gb_inline AstNode *make_unary_expr(AstFile *f, Token op, AstNode *expr) {
  463. AstNode *result = make_node(f, AstNode_UnaryExpr);
  464. result->UnaryExpr.op = op;
  465. result->UnaryExpr.expr = expr;
  466. return result;
  467. }
  468. gb_inline AstNode *make_binary_expr(AstFile *f, Token op, AstNode *left, AstNode *right) {
  469. AstNode *result = make_node(f, AstNode_BinaryExpr);
  470. if (left == NULL) {
  471. ast_file_err(f, op, "No lhs expression for binary expression `%.*s`", LIT(op.string));
  472. left = make_bad_expr(f, op, op);
  473. }
  474. if (right == NULL) {
  475. ast_file_err(f, op, "No rhs expression for binary expression `%.*s`", LIT(op.string));
  476. right = make_bad_expr(f, op, op);
  477. }
  478. result->BinaryExpr.op = op;
  479. result->BinaryExpr.left = left;
  480. result->BinaryExpr.right = right;
  481. return result;
  482. }
  483. gb_inline AstNode *make_paren_expr(AstFile *f, AstNode *expr, Token open, Token close) {
  484. AstNode *result = make_node(f, AstNode_ParenExpr);
  485. result->ParenExpr.expr = expr;
  486. result->ParenExpr.open = open;
  487. result->ParenExpr.close = close;
  488. return result;
  489. }
  490. gb_inline AstNode *make_call_expr(AstFile *f, AstNode *proc, gbArray(AstNode *)args, Token open, Token close, Token ellipsis) {
  491. AstNode *result = make_node(f, AstNode_CallExpr);
  492. result->CallExpr.proc = proc;
  493. result->CallExpr.args = args;
  494. result->CallExpr.open = open;
  495. result->CallExpr.close = close;
  496. result->CallExpr.ellipsis = ellipsis;
  497. return result;
  498. }
  499. gb_inline AstNode *make_selector_expr(AstFile *f, Token token, AstNode *expr, AstNode *selector) {
  500. AstNode *result = make_node(f, AstNode_SelectorExpr);
  501. result->SelectorExpr.expr = expr;
  502. result->SelectorExpr.selector = selector;
  503. return result;
  504. }
  505. gb_inline AstNode *make_index_expr(AstFile *f, AstNode *expr, AstNode *index, Token open, Token close) {
  506. AstNode *result = make_node(f, AstNode_IndexExpr);
  507. result->IndexExpr.expr = expr;
  508. result->IndexExpr.index = index;
  509. result->IndexExpr.open = open;
  510. result->IndexExpr.close = close;
  511. return result;
  512. }
  513. gb_inline AstNode *make_slice_expr(AstFile *f, AstNode *expr, Token open, Token close, AstNode *low, AstNode *high, AstNode *max, b32 triple_indexed) {
  514. AstNode *result = make_node(f, AstNode_SliceExpr);
  515. result->SliceExpr.expr = expr;
  516. result->SliceExpr.open = open;
  517. result->SliceExpr.close = close;
  518. result->SliceExpr.low = low;
  519. result->SliceExpr.high = high;
  520. result->SliceExpr.max = max;
  521. result->SliceExpr.triple_indexed = triple_indexed;
  522. return result;
  523. }
  524. gb_inline AstNode *make_deref_expr(AstFile *f, AstNode *expr, Token op) {
  525. AstNode *result = make_node(f, AstNode_DerefExpr);
  526. result->DerefExpr.expr = expr;
  527. result->DerefExpr.op = op;
  528. return result;
  529. }
  530. gb_inline AstNode *make_basic_lit(AstFile *f, Token basic_lit) {
  531. AstNode *result = make_node(f, AstNode_BasicLit);
  532. result->BasicLit = basic_lit;
  533. return result;
  534. }
  535. gb_inline AstNode *make_ident(AstFile *f, Token token) {
  536. AstNode *result = make_node(f, AstNode_Ident);
  537. result->Ident = token;
  538. return result;
  539. }
  540. gb_inline AstNode *make_ellipsis(AstFile *f, Token token, AstNode *expr) {
  541. AstNode *result = make_node(f, AstNode_Ellipsis);
  542. result->Ellipsis.token = token;
  543. result->Ellipsis.expr = expr;
  544. return result;
  545. }
  546. gb_inline AstNode *make_proc_lit(AstFile *f, AstNode *type, AstNode *body, u64 tags) {
  547. AstNode *result = make_node(f, AstNode_ProcLit);
  548. result->ProcLit.type = type;
  549. result->ProcLit.body = body;
  550. result->ProcLit.tags = tags;
  551. return result;
  552. }
  553. gb_inline AstNode *make_field_value(AstFile *f, AstNode *field, AstNode *value, Token eq) {
  554. AstNode *result = make_node(f, AstNode_FieldValue);
  555. result->FieldValue.field = field;
  556. result->FieldValue.value = value;
  557. result->FieldValue.eq = eq;
  558. return result;
  559. }
  560. gb_inline AstNode *make_compound_lit(AstFile *f, AstNode *type, AstNodeArray elems, Token open, Token close) {
  561. AstNode *result = make_node(f, AstNode_CompoundLit);
  562. result->CompoundLit.type = type;
  563. result->CompoundLit.elems = elems;
  564. result->CompoundLit.open = open;
  565. result->CompoundLit.close = close;
  566. return result;
  567. }
  568. gb_inline AstNode *make_bad_stmt(AstFile *f, Token begin, Token end) {
  569. AstNode *result = make_node(f, AstNode_BadStmt);
  570. result->BadStmt.begin = begin;
  571. result->BadStmt.end = end;
  572. return result;
  573. }
  574. gb_inline AstNode *make_empty_stmt(AstFile *f, Token token) {
  575. AstNode *result = make_node(f, AstNode_EmptyStmt);
  576. result->EmptyStmt.token = token;
  577. return result;
  578. }
  579. gb_inline AstNode *make_expr_stmt(AstFile *f, AstNode *expr) {
  580. AstNode *result = make_node(f, AstNode_ExprStmt);
  581. result->ExprStmt.expr = expr;
  582. return result;
  583. }
  584. gb_inline AstNode *make_inc_dec_stmt(AstFile *f, Token op, AstNode *expr) {
  585. AstNode *result = make_node(f, AstNode_IncDecStmt);
  586. result->IncDecStmt.op = op;
  587. result->IncDecStmt.expr = expr;
  588. return result;
  589. }
  590. gb_inline AstNode *make_assign_stmt(AstFile *f, Token op, AstNodeArray lhs, AstNodeArray rhs) {
  591. AstNode *result = make_node(f, AstNode_AssignStmt);
  592. result->AssignStmt.op = op;
  593. result->AssignStmt.lhs = lhs;
  594. result->AssignStmt.rhs = rhs;
  595. return result;
  596. }
  597. gb_inline AstNode *make_block_stmt(AstFile *f, AstNodeArray stmts, Token open, Token close) {
  598. AstNode *result = make_node(f, AstNode_BlockStmt);
  599. result->BlockStmt.stmts = stmts;
  600. result->BlockStmt.open = open;
  601. result->BlockStmt.close = close;
  602. return result;
  603. }
  604. gb_inline AstNode *make_if_stmt(AstFile *f, Token token, AstNode *init, AstNode *cond, AstNode *body, AstNode *else_stmt) {
  605. AstNode *result = make_node(f, AstNode_IfStmt);
  606. result->IfStmt.token = token;
  607. result->IfStmt.init = init;
  608. result->IfStmt.cond = cond;
  609. result->IfStmt.body = body;
  610. result->IfStmt.else_stmt = else_stmt;
  611. return result;
  612. }
  613. gb_inline AstNode *make_return_stmt(AstFile *f, Token token, AstNodeArray results) {
  614. AstNode *result = make_node(f, AstNode_ReturnStmt);
  615. result->ReturnStmt.token = token;
  616. result->ReturnStmt.results = results;
  617. return result;
  618. }
  619. gb_inline AstNode *make_for_stmt(AstFile *f, Token token, AstNode *init, AstNode *cond, AstNode *post, AstNode *body) {
  620. AstNode *result = make_node(f, AstNode_ForStmt);
  621. result->ForStmt.token = token;
  622. result->ForStmt.init = init;
  623. result->ForStmt.cond = cond;
  624. result->ForStmt.post = post;
  625. result->ForStmt.body = body;
  626. return result;
  627. }
  628. gb_inline AstNode *make_match_stmt(AstFile *f, Token token, AstNode *init, AstNode *tag, AstNode *body) {
  629. AstNode *result = make_node(f, AstNode_MatchStmt);
  630. result->MatchStmt.token = token;
  631. result->MatchStmt.init = init;
  632. result->MatchStmt.tag = tag;
  633. result->MatchStmt.body = body;
  634. return result;
  635. }
  636. gb_inline AstNode *make_type_match_stmt(AstFile *f, Token token, AstNode *tag, AstNode *var, AstNode *body) {
  637. AstNode *result = make_node(f, AstNode_TypeMatchStmt);
  638. result->TypeMatchStmt.token = token;
  639. result->TypeMatchStmt.tag = tag;
  640. result->TypeMatchStmt.var = var;
  641. result->TypeMatchStmt.body = body;
  642. return result;
  643. }
  644. gb_inline AstNode *make_case_clause(AstFile *f, Token token, AstNodeArray list, AstNodeArray stmts) {
  645. AstNode *result = make_node(f, AstNode_CaseClause);
  646. result->CaseClause.token = token;
  647. result->CaseClause.list = list;
  648. result->CaseClause.stmts = stmts;
  649. return result;
  650. }
  651. gb_inline AstNode *make_defer_stmt(AstFile *f, Token token, AstNode *stmt) {
  652. AstNode *result = make_node(f, AstNode_DeferStmt);
  653. result->DeferStmt.token = token;
  654. result->DeferStmt.stmt = stmt;
  655. return result;
  656. }
  657. gb_inline AstNode *make_branch_stmt(AstFile *f, Token token) {
  658. AstNode *result = make_node(f, AstNode_BranchStmt);
  659. result->BranchStmt.token = token;
  660. return result;
  661. }
  662. gb_inline AstNode *make_using_stmt(AstFile *f, Token token, AstNode *node) {
  663. AstNode *result = make_node(f, AstNode_UsingStmt);
  664. result->UsingStmt.token = token;
  665. result->UsingStmt.node = node;
  666. return result;
  667. }
  668. gb_inline AstNode *make_asm_operand(AstFile *f, Token string, AstNode *operand) {
  669. AstNode *result = make_node(f, AstNode_AsmOperand);
  670. result->AsmOperand.string = string;
  671. result->AsmOperand.operand = operand;
  672. return result;
  673. }
  674. gb_inline AstNode *make_asm_stmt(AstFile *f, Token token, b32 is_volatile, Token open, Token close, Token code_string,
  675. AstNode *output_list, AstNode *input_list, AstNode *clobber_list,
  676. isize output_count, isize input_count, isize clobber_count) {
  677. AstNode *result = make_node(f, AstNode_AsmStmt);
  678. result->AsmStmt.token = token;
  679. result->AsmStmt.is_volatile = is_volatile;
  680. result->AsmStmt.open = open;
  681. result->AsmStmt.close = close;
  682. result->AsmStmt.code_string = code_string;
  683. result->AsmStmt.output_list = output_list;
  684. result->AsmStmt.input_list = input_list;
  685. result->AsmStmt.clobber_list = clobber_list;
  686. result->AsmStmt.output_count = output_count;
  687. result->AsmStmt.input_count = input_count;
  688. result->AsmStmt.clobber_count = clobber_count;
  689. return result;
  690. }
  691. gb_inline AstNode *make_bad_decl(AstFile *f, Token begin, Token end) {
  692. AstNode *result = make_node(f, AstNode_BadDecl);
  693. result->BadDecl.begin = begin;
  694. result->BadDecl.end = end;
  695. return result;
  696. }
  697. gb_inline AstNode *make_var_decl(AstFile *f, DeclKind kind, AstNodeArray names, AstNode *type, AstNodeArray values) {
  698. AstNode *result = make_node(f, AstNode_VarDecl);
  699. result->VarDecl.kind = kind;
  700. result->VarDecl.names = names;
  701. result->VarDecl.type = type;
  702. result->VarDecl.values = values;
  703. return result;
  704. }
  705. gb_inline AstNode *make_field(AstFile *f, AstNodeArray names, AstNode *type, b32 is_using) {
  706. AstNode *result = make_node(f, AstNode_Field);
  707. result->Field.names = names;
  708. result->Field.type = type;
  709. result->Field.is_using = is_using;
  710. return result;
  711. }
  712. gb_inline AstNode *make_proc_type(AstFile *f, Token token, AstNodeArray params, AstNodeArray results) {
  713. AstNode *result = make_node(f, AstNode_ProcType);
  714. result->ProcType.token = token;
  715. result->ProcType.params = params;
  716. result->ProcType.results = results;
  717. return result;
  718. }
  719. gb_inline AstNode *make_proc_decl(AstFile *f, AstNode *name, AstNode *proc_type, AstNode *body, u64 tags, String foreign_name) {
  720. AstNode *result = make_node(f, AstNode_ProcDecl);
  721. result->ProcDecl.name = name;
  722. result->ProcDecl.type = proc_type;
  723. result->ProcDecl.body = body;
  724. result->ProcDecl.tags = tags;
  725. result->ProcDecl.foreign_name = foreign_name;
  726. return result;
  727. }
  728. gb_inline AstNode *make_pointer_type(AstFile *f, Token token, AstNode *type) {
  729. AstNode *result = make_node(f, AstNode_PointerType);
  730. result->PointerType.token = token;
  731. result->PointerType.type = type;
  732. return result;
  733. }
  734. gb_inline AstNode *make_array_type(AstFile *f, Token token, AstNode *count, AstNode *elem) {
  735. AstNode *result = make_node(f, AstNode_ArrayType);
  736. result->ArrayType.token = token;
  737. result->ArrayType.count = count;
  738. result->ArrayType.elem = elem;
  739. return result;
  740. }
  741. gb_inline AstNode *make_vector_type(AstFile *f, Token token, AstNode *count, AstNode *elem) {
  742. AstNode *result = make_node(f, AstNode_VectorType);
  743. result->VectorType.token = token;
  744. result->VectorType.count = count;
  745. result->VectorType.elem = elem;
  746. return result;
  747. }
  748. gb_inline AstNode *make_struct_type(AstFile *f, Token token, AstNodeArray decls, isize decl_count, b32 is_packed, b32 is_ordered) {
  749. AstNode *result = make_node(f, AstNode_StructType);
  750. result->StructType.token = token;
  751. result->StructType.decls = decls;
  752. result->StructType.decl_count = decl_count;
  753. result->StructType.is_packed = is_packed;
  754. result->StructType.is_ordered = is_ordered;
  755. return result;
  756. }
  757. gb_inline AstNode *make_union_type(AstFile *f, Token token, AstNodeArray decls, isize decl_count) {
  758. AstNode *result = make_node(f, AstNode_UnionType);
  759. result->UnionType.token = token;
  760. result->UnionType.decls = decls;
  761. result->UnionType.decl_count = decl_count;
  762. return result;
  763. }
  764. gb_inline AstNode *make_raw_union_type(AstFile *f, Token token, AstNodeArray decls, isize decl_count) {
  765. AstNode *result = make_node(f, AstNode_RawUnionType);
  766. result->RawUnionType.token = token;
  767. result->RawUnionType.decls = decls;
  768. result->RawUnionType.decl_count = decl_count;
  769. return result;
  770. }
  771. gb_inline AstNode *make_enum_type(AstFile *f, Token token, AstNode *base_type, AstNodeArray fields) {
  772. AstNode *result = make_node(f, AstNode_EnumType);
  773. result->EnumType.token = token;
  774. result->EnumType.base_type = base_type;
  775. result->EnumType.fields = fields;
  776. return result;
  777. }
  778. gb_inline AstNode *make_type_decl(AstFile *f, Token token, AstNode *name, AstNode *type) {
  779. AstNode *result = make_node(f, AstNode_TypeDecl);
  780. result->TypeDecl.token = token;
  781. result->TypeDecl.name = name;
  782. result->TypeDecl.type = type;
  783. return result;
  784. }
  785. gb_inline AstNode *make_import_decl(AstFile *f, Token token, Token relpath, Token import_name) {
  786. AstNode *result = make_node(f, AstNode_ImportDecl);
  787. result->ImportDecl.token = token;
  788. result->ImportDecl.relpath = relpath;
  789. result->ImportDecl.import_name = import_name;
  790. return result;
  791. }
  792. gb_inline AstNode *make_foreign_system_library(AstFile *f, Token token, Token filepath) {
  793. AstNode *result = make_node(f, AstNode_ForeignSystemLibrary);
  794. result->ForeignSystemLibrary.token = token;
  795. result->ForeignSystemLibrary.filepath = filepath;
  796. return result;
  797. }
  798. gb_inline b32 next_token(AstFile *f) {
  799. if (f->cursor+1 < f->tokens + gb_array_count(f->tokens)) {
  800. f->cursor++;
  801. return true;
  802. } else {
  803. ast_file_err(f, f->cursor[0], "Token is EOF");
  804. return false;
  805. }
  806. }
  807. gb_inline Token expect_token(AstFile *f, TokenKind kind) {
  808. Token prev = f->cursor[0];
  809. if (prev.kind != kind) {
  810. ast_file_err(f, f->cursor[0], "Expected `%.*s`, got `%.*s`",
  811. LIT(token_strings[kind]),
  812. LIT(token_strings[prev.kind]));
  813. }
  814. next_token(f);
  815. return prev;
  816. }
  817. gb_inline Token expect_operator(AstFile *f) {
  818. Token prev = f->cursor[0];
  819. if (!gb_is_between(prev.kind, Token__OperatorBegin+1, Token__OperatorEnd-1)) {
  820. ast_file_err(f, f->cursor[0], "Expected an operator, got `%.*s`",
  821. LIT(token_strings[prev.kind]));
  822. }
  823. next_token(f);
  824. return prev;
  825. }
  826. gb_inline Token expect_keyword(AstFile *f) {
  827. Token prev = f->cursor[0];
  828. if (!gb_is_between(prev.kind, Token__KeywordBegin+1, Token__KeywordEnd-1)) {
  829. ast_file_err(f, f->cursor[0], "Expected a keyword, got `%.*s`",
  830. LIT(token_strings[prev.kind]));
  831. }
  832. next_token(f);
  833. return prev;
  834. }
  835. gb_inline b32 allow_token(AstFile *f, TokenKind kind) {
  836. Token prev = f->cursor[0];
  837. if (prev.kind == kind) {
  838. next_token(f);
  839. return true;
  840. }
  841. return false;
  842. }
  843. b32 is_blank_ident(String str) {
  844. if (str.len == 1) {
  845. return str.text[0] == '_';
  846. }
  847. return false;
  848. }
  849. void fix_advance_to_next_stmt(AstFile *f) {
  850. // TODO(bill): fix_advance_to_next_stmt
  851. #if 0
  852. for (;;) {
  853. Token t = f->cursor[0];
  854. switch (t.kind) {
  855. case Token_EOF:
  856. return;
  857. case Token_type:
  858. case Token_break:
  859. case Token_continue:
  860. case Token_fallthrough:
  861. case Token_if:
  862. case Token_for:
  863. case Token_defer:
  864. case Token_return:
  865. if (token_pos_are_equal(t.pos, f->fix_prev_pos) &&
  866. f->fix_count < PARSER_MAX_FIX_COUNT) {
  867. f->fix_count++;
  868. return;
  869. }
  870. if (token_pos_cmp(f->fix_prev_pos, t.pos) < 0) {
  871. f->fix_prev_pos = t.pos;
  872. f->fix_count = 0; // NOTE(bill): Reset
  873. return;
  874. }
  875. }
  876. next_token(f);
  877. }
  878. #endif
  879. }
  880. b32 expect_semicolon_after_stmt(AstFile *f, AstNode *s) {
  881. // if (s != NULL) {
  882. // switch (s->kind) {
  883. // case AstNode_ProcDecl:
  884. // return true;
  885. // case AstNode_TypeDecl: {
  886. // switch (s->TypeDecl.type->kind) {
  887. // case AstNode_StructType:
  888. // case AstNode_UnionType:
  889. // case AstNode_EnumType:
  890. // case AstNode_ProcType:
  891. // return true;
  892. // }
  893. // } break;
  894. // }
  895. // }
  896. if (!allow_token(f, Token_Semicolon)) {
  897. if (f->cursor[0].pos.line == f->cursor[-1].pos.line) {
  898. if (f->cursor[0].kind != Token_CloseBrace) {
  899. // CLEANUP(bill): Semicolon handling in parser
  900. ast_file_err(f, f->cursor[0],
  901. "Expected `;` after %.*s, got `%.*s`",
  902. LIT(ast_node_strings[s->kind]), LIT(token_strings[f->cursor[0].kind]));
  903. return false;
  904. }
  905. }
  906. }
  907. return true;
  908. }
  909. AstNode * parse_expr(AstFile *f, b32 lhs);
  910. AstNode * parse_proc_type(AstFile *f);
  911. AstNodeArray parse_stmt_list(AstFile *f);
  912. AstNode * parse_stmt(AstFile *f);
  913. AstNode * parse_body(AstFile *f);
  914. AstNode *parse_identifier(AstFile *f) {
  915. Token token = f->cursor[0];
  916. if (token.kind == Token_Identifier) {
  917. next_token(f);
  918. } else {
  919. token.string = make_string("_");
  920. expect_token(f, Token_Identifier);
  921. }
  922. return make_ident(f, token);
  923. }
  924. AstNode *parse_tag_expr(AstFile *f, AstNode *expression) {
  925. Token token = expect_token(f, Token_Hash);
  926. Token name = expect_token(f, Token_Identifier);
  927. return make_tag_expr(f, token, name, expression);
  928. }
  929. AstNode *parse_tag_stmt(AstFile *f, AstNode *statement) {
  930. Token token = expect_token(f, Token_Hash);
  931. Token name = expect_token(f, Token_Identifier);
  932. return make_tag_stmt(f, token, name, statement);
  933. }
  934. AstNode *unparen_expr(AstNode *node) {
  935. for (;;) {
  936. if (node->kind != AstNode_ParenExpr)
  937. return node;
  938. node = node->ParenExpr.expr;
  939. }
  940. }
  941. AstNode *parse_value(AstFile *f);
  942. AstNodeArray parse_element_list(AstFile *f) {
  943. AstNodeArray elems = make_ast_node_array(f);
  944. while (f->cursor[0].kind != Token_CloseBrace &&
  945. f->cursor[0].kind != Token_EOF) {
  946. AstNode *elem = parse_value(f);
  947. if (f->cursor[0].kind == Token_Eq) {
  948. Token eq = expect_token(f, Token_Eq);
  949. AstNode *value = parse_value(f);
  950. elem = make_field_value(f, elem, value, eq);
  951. }
  952. gb_array_append(elems, elem);
  953. if (f->cursor[0].kind != Token_Comma) {
  954. break;
  955. }
  956. next_token(f);
  957. }
  958. return elems;
  959. }
  960. AstNode *parse_literal_value(AstFile *f, AstNode *type) {
  961. AstNodeArray elems = NULL;
  962. Token open = expect_token(f, Token_OpenBrace);
  963. f->expr_level++;
  964. if (f->cursor[0].kind != Token_CloseBrace) {
  965. elems = parse_element_list(f);
  966. }
  967. f->expr_level--;
  968. Token close = expect_token(f, Token_CloseBrace);
  969. return make_compound_lit(f, type, elems, open, close);
  970. }
  971. AstNode *parse_value(AstFile *f) {
  972. if (f->cursor[0].kind == Token_OpenBrace)
  973. return parse_literal_value(f, NULL);
  974. AstNode *value = parse_expr(f, false);
  975. return value;
  976. }
  977. AstNode *parse_identifier_or_type(AstFile *f, u32 flags = 0);
  978. void check_proc_add_tag(AstFile *f, AstNode *tag_expr, u64 *tags, ProcTag tag, String tag_name) {
  979. if (*tags & tag) {
  980. ast_file_err(f, ast_node_token(tag_expr), "Procedure tag already used: %.*s", LIT(tag_name));
  981. }
  982. *tags |= tag;
  983. }
  984. b32 is_foreign_name_valid(String name) {
  985. // TODO(bill): is_foreign_name_valid
  986. if (name.len == 0)
  987. return false;
  988. isize offset = 0;
  989. while (offset < name.len) {
  990. Rune rune;
  991. isize remaining = name.len - offset;
  992. isize width = gb_utf8_decode(name.text+offset, remaining, &rune);
  993. if (rune == GB_RUNE_INVALID && width == 1) {
  994. return false;
  995. } else if (rune == GB_RUNE_BOM && remaining > 0) {
  996. return false;
  997. }
  998. if (offset == 0) {
  999. switch (rune) {
  1000. case '-':
  1001. case '$':
  1002. case '.':
  1003. case '_':
  1004. break;
  1005. default:
  1006. if (!gb_char_is_alpha(cast(char)rune))
  1007. return false;
  1008. break;
  1009. }
  1010. } else {
  1011. switch (rune) {
  1012. case '-':
  1013. case '$':
  1014. case '.':
  1015. case '_':
  1016. break;
  1017. default:
  1018. if (!gb_char_is_alphanumeric(cast(char)rune)) {
  1019. return false;
  1020. }
  1021. break;
  1022. }
  1023. }
  1024. offset += width;
  1025. }
  1026. return true;
  1027. }
  1028. void parse_proc_tags(AstFile *f, u64 *tags, String *foreign_name) {
  1029. // TODO(bill): Add this to procedure literals too
  1030. while (f->cursor[0].kind == Token_Hash) {
  1031. AstNode *tag_expr = parse_tag_expr(f, NULL);
  1032. ast_node(te, TagExpr, tag_expr);
  1033. String tag_name = te->name.string;
  1034. if (are_strings_equal(tag_name, make_string("foreign"))) {
  1035. check_proc_add_tag(f, tag_expr, tags, ProcTag_foreign, tag_name);
  1036. if (f->cursor[0].kind == Token_String) {
  1037. *foreign_name = f->cursor[0].string;
  1038. // TODO(bill): Check if valid string
  1039. if (!is_foreign_name_valid(*foreign_name)) {
  1040. ast_file_err(f, ast_node_token(tag_expr), "Invalid alternative foreign procedure name");
  1041. }
  1042. next_token(f);
  1043. }
  1044. } else if (are_strings_equal(tag_name, make_string("bounds_check"))) {
  1045. check_proc_add_tag(f, tag_expr, tags, ProcTag_bounds_check, tag_name);
  1046. } else if (are_strings_equal(tag_name, make_string("no_bounds_check"))) {
  1047. check_proc_add_tag(f, tag_expr, tags, ProcTag_no_bounds_check, tag_name);
  1048. } else if (are_strings_equal(tag_name, make_string("inline"))) {
  1049. check_proc_add_tag(f, tag_expr, tags, ProcTag_inline, tag_name);
  1050. } else if (are_strings_equal(tag_name, make_string("no_inline"))) {
  1051. check_proc_add_tag(f, tag_expr, tags, ProcTag_no_inline, tag_name);
  1052. // } else if (are_strings_equal(tag_name, make_string("no_context"))) {
  1053. // check_proc_add_tag(f, tag_expr, tags, ProcTag_no_context, tag_name);
  1054. } else {
  1055. ast_file_err(f, ast_node_token(tag_expr), "Unknown procedure tag");
  1056. }
  1057. }
  1058. if ((*tags & ProcTag_inline) && (*tags & ProcTag_no_inline)) {
  1059. ast_file_err(f, f->cursor[0], "You cannot apply both #inline and #no_inline to a procedure");
  1060. }
  1061. if ((*tags & ProcTag_bounds_check) && (*tags & ProcTag_no_bounds_check)) {
  1062. ast_file_err(f, f->cursor[0], "You cannot apply both #bounds_check and #no_bounds_check to a procedure");
  1063. }
  1064. if (((*tags & ProcTag_bounds_check) || (*tags & ProcTag_no_bounds_check)) && (*tags & ProcTag_foreign)) {
  1065. ast_file_err(f, f->cursor[0], "You cannot apply both #bounds_check or #no_bounds_check to a procedure without a body");
  1066. }
  1067. }
  1068. AstNode *parse_operand(AstFile *f, b32 lhs) {
  1069. AstNode *operand = NULL; // Operand
  1070. switch (f->cursor[0].kind) {
  1071. case Token_Identifier:
  1072. operand = parse_identifier(f);
  1073. if (!lhs) {
  1074. // TODO(bill): Handle?
  1075. }
  1076. return operand;
  1077. case Token_Integer:
  1078. case Token_Float:
  1079. case Token_String:
  1080. case Token_Rune:
  1081. operand = make_basic_lit(f, f->cursor[0]);
  1082. next_token(f);
  1083. return operand;
  1084. case Token_OpenParen: {
  1085. Token open, close;
  1086. // NOTE(bill): Skip the Paren Expression
  1087. open = expect_token(f, Token_OpenParen);
  1088. f->expr_level++;
  1089. operand = parse_expr(f, false);
  1090. f->expr_level--;
  1091. close = expect_token(f, Token_CloseParen);
  1092. return make_paren_expr(f, operand, open, close);
  1093. }
  1094. case Token_Hash: {
  1095. operand = parse_tag_expr(f, NULL);
  1096. String name = operand->TagExpr.name.string;
  1097. if (are_strings_equal(name, make_string("rune"))) {
  1098. if (f->cursor[0].kind == Token_String) {
  1099. Token *s = &f->cursor[0];
  1100. if (gb_utf8_strnlen(s->string.text, s->string.len) != 1) {
  1101. ast_file_err(f, *s, "Invalid rune literal %.*s", LIT(s->string));
  1102. }
  1103. s->kind = Token_Rune; // NOTE(bill): Change it
  1104. } else {
  1105. expect_token(f, Token_String);
  1106. }
  1107. operand = parse_operand(f, lhs);
  1108. } else {
  1109. operand->TagExpr.expr = parse_expr(f, false);
  1110. }
  1111. return operand;
  1112. }
  1113. // Parse Procedure Type or Literal
  1114. case Token_proc: {
  1115. AstNode *curr_proc = f->curr_proc;
  1116. AstNode *type = parse_proc_type(f);
  1117. f->curr_proc = type;
  1118. defer (f->curr_proc = curr_proc);
  1119. u64 tags = 0;
  1120. String foreign_name = {};
  1121. parse_proc_tags(f, &tags, &foreign_name);
  1122. if (tags & ProcTag_foreign) {
  1123. ast_file_err(f, f->cursor[0], "#foreign cannot be applied to procedure literals");
  1124. }
  1125. if (f->cursor[0].kind != Token_OpenBrace) {
  1126. return type;
  1127. } else {
  1128. AstNode *body;
  1129. f->expr_level++;
  1130. body = parse_body(f);
  1131. f->expr_level--;
  1132. return make_proc_lit(f, type, body, tags);
  1133. }
  1134. }
  1135. default: {
  1136. AstNode *type = parse_identifier_or_type(f);
  1137. if (type != NULL) {
  1138. // NOTE(bill): Sanity check as identifiers should be handled already
  1139. GB_ASSERT_MSG(type->kind != AstNode_Ident, "Type Cannot be identifier");
  1140. return type;
  1141. }
  1142. }
  1143. }
  1144. Token begin = f->cursor[0];
  1145. ast_file_err(f, begin, "Expected an operand");
  1146. fix_advance_to_next_stmt(f);
  1147. return make_bad_expr(f, begin, f->cursor[0]);
  1148. }
  1149. b32 is_literal_type(AstNode *node) {
  1150. switch (node->kind) {
  1151. case AstNode_BadExpr:
  1152. case AstNode_Ident:
  1153. case AstNode_SelectorExpr:
  1154. case AstNode_ArrayType:
  1155. case AstNode_VectorType:
  1156. case AstNode_StructType:
  1157. return true;
  1158. }
  1159. return false;
  1160. }
  1161. AstNode *parse_call_expr(AstFile *f, AstNode *operand) {
  1162. AstNodeArray args = make_ast_node_array(f);
  1163. Token open_paren, close_paren;
  1164. Token ellipsis = {};
  1165. f->expr_level++;
  1166. open_paren = expect_token(f, Token_OpenParen);
  1167. while (f->cursor[0].kind != Token_CloseParen &&
  1168. f->cursor[0].kind != Token_EOF &&
  1169. ellipsis.pos.line == 0) {
  1170. if (f->cursor[0].kind == Token_Comma)
  1171. ast_file_err(f, f->cursor[0], "Expected an expression not a ,");
  1172. if (f->cursor[0].kind == Token_Ellipsis) {
  1173. ellipsis = f->cursor[0];
  1174. next_token(f);
  1175. }
  1176. gb_array_append(args, parse_expr(f, false));
  1177. if (f->cursor[0].kind != Token_Comma) {
  1178. if (f->cursor[0].kind == Token_CloseParen)
  1179. break;
  1180. }
  1181. next_token(f);
  1182. }
  1183. f->expr_level--;
  1184. close_paren = expect_token(f, Token_CloseParen);
  1185. return make_call_expr(f, operand, args, open_paren, close_paren, ellipsis);
  1186. }
  1187. AstNode *parse_atom_expr(AstFile *f, b32 lhs) {
  1188. AstNode *operand = parse_operand(f, lhs);
  1189. b32 loop = true;
  1190. while (loop) {
  1191. switch (f->cursor[0].kind) {
  1192. case Token_Prime: {
  1193. Token op = expect_token(f, Token_Prime);
  1194. if (lhs) {
  1195. // TODO(bill): Handle this
  1196. }
  1197. AstNode *proc = parse_identifier(f);
  1198. gbArray(AstNode *) args;
  1199. gb_array_init_reserve(args, gb_arena_allocator(&f->arena), 1);
  1200. gb_array_append(args, operand);
  1201. operand = make_call_expr(f, proc, args, ast_node_token(operand), op, empty_token);
  1202. } break;
  1203. case Token_OpenParen: {
  1204. if (lhs) {
  1205. // TODO(bill): Handle this shit! Is this even allowed in this language?!
  1206. }
  1207. operand = parse_call_expr(f, operand);
  1208. } break;
  1209. case Token_Period: {
  1210. Token token = f->cursor[0];
  1211. next_token(f);
  1212. if (lhs) {
  1213. // TODO(bill): handle this
  1214. }
  1215. switch (f->cursor[0].kind) {
  1216. case Token_Identifier:
  1217. operand = make_selector_expr(f, token, operand, parse_identifier(f));
  1218. break;
  1219. default: {
  1220. ast_file_err(f, f->cursor[0], "Expected a selector");
  1221. next_token(f);
  1222. operand = make_selector_expr(f, f->cursor[0], operand, NULL);
  1223. } break;
  1224. }
  1225. } break;
  1226. case Token_OpenBracket: {
  1227. if (lhs) {
  1228. // TODO(bill): Handle this
  1229. }
  1230. Token open, close;
  1231. AstNode *indices[3] = {};
  1232. f->expr_level++;
  1233. open = expect_token(f, Token_OpenBracket);
  1234. if (f->cursor[0].kind != Token_Colon)
  1235. indices[0] = parse_expr(f, false);
  1236. isize colon_count = 0;
  1237. Token colons[2] = {};
  1238. while (f->cursor[0].kind == Token_Colon && colon_count < 2) {
  1239. colons[colon_count++] = f->cursor[0];
  1240. next_token(f);
  1241. if (f->cursor[0].kind != Token_Colon &&
  1242. f->cursor[0].kind != Token_CloseBracket &&
  1243. f->cursor[0].kind != Token_EOF) {
  1244. indices[colon_count] = parse_expr(f, false);
  1245. }
  1246. }
  1247. f->expr_level--;
  1248. close = expect_token(f, Token_CloseBracket);
  1249. if (colon_count == 0) {
  1250. operand = make_index_expr(f, operand, indices[0], open, close);
  1251. } else {
  1252. b32 triple_indexed = false;
  1253. if (colon_count == 2) {
  1254. triple_indexed = true;
  1255. if (indices[1] == NULL) {
  1256. ast_file_err(f, colons[0], "Second index is required in a triple indexed slice");
  1257. indices[1] = make_bad_expr(f, colons[0], colons[1]);
  1258. }
  1259. if (indices[2] == NULL) {
  1260. ast_file_err(f, colons[1], "Third index is required in a triple indexed slice");
  1261. indices[2] = make_bad_expr(f, colons[1], close);
  1262. }
  1263. }
  1264. operand = make_slice_expr(f, operand, open, close, indices[0], indices[1], indices[2], triple_indexed);
  1265. }
  1266. } break;
  1267. case Token_Pointer: // Deference
  1268. operand = make_deref_expr(f, operand, expect_token(f, Token_Pointer));
  1269. break;
  1270. case Token_OpenBrace: {
  1271. if (!lhs && is_literal_type(operand) && f->expr_level >= 0) {
  1272. operand = parse_literal_value(f, operand);
  1273. } else {
  1274. loop = false;
  1275. }
  1276. } break;
  1277. default:
  1278. loop = false;
  1279. break;
  1280. }
  1281. lhs = false; // NOTE(bill): 'tis not lhs anymore
  1282. }
  1283. return operand;
  1284. }
  1285. AstNode *parse_type(AstFile *f);
  1286. AstNode *parse_unary_expr(AstFile *f, b32 lhs) {
  1287. switch (f->cursor[0].kind) {
  1288. case Token_Pointer:
  1289. case Token_Add:
  1290. case Token_Sub:
  1291. case Token_Not:
  1292. case Token_Xor: {
  1293. AstNode *operand;
  1294. Token op = f->cursor[0];
  1295. next_token(f);
  1296. operand = parse_unary_expr(f, lhs);
  1297. return make_unary_expr(f, op, operand);
  1298. } break;
  1299. }
  1300. return parse_atom_expr(f, lhs);
  1301. }
  1302. AstNode *parse_binary_expr(AstFile *f, b32 lhs, i32 prec_in) {
  1303. AstNode *expression = parse_unary_expr(f, lhs);
  1304. for (i32 prec = token_precedence(f->cursor[0]); prec >= prec_in; prec--) {
  1305. for (;;) {
  1306. AstNode *right;
  1307. Token op = f->cursor[0];
  1308. i32 op_prec = token_precedence(op);
  1309. if (op_prec != prec)
  1310. break;
  1311. expect_operator(f); // NOTE(bill): error checks too
  1312. if (lhs) {
  1313. // TODO(bill): error checking
  1314. lhs = false;
  1315. }
  1316. switch (op.kind) {
  1317. case Token_DoublePrime: {
  1318. AstNode *proc = parse_identifier(f);
  1319. AstNode *right = parse_binary_expr(f, false, prec+1);
  1320. gbArray(AstNode *) args;
  1321. gb_array_init_reserve(args, gb_arena_allocator(&f->arena), 2);
  1322. gb_array_append(args, expression);
  1323. gb_array_append(args, right);
  1324. expression = make_call_expr(f, proc, args, op, ast_node_token(right), empty_token);
  1325. continue;
  1326. } break;
  1327. case Token_as:
  1328. case Token_transmute:
  1329. case Token_down_cast:
  1330. right = parse_type(f);
  1331. break;
  1332. default:
  1333. right = parse_binary_expr(f, false, prec+1);
  1334. if (!right) {
  1335. ast_file_err(f, op, "Expected expression on the right hand side of the binary operator");
  1336. }
  1337. break;
  1338. }
  1339. expression = make_binary_expr(f, op, expression, right);
  1340. }
  1341. }
  1342. return expression;
  1343. }
  1344. AstNode *parse_expr(AstFile *f, b32 lhs) {
  1345. return parse_binary_expr(f, lhs, 0+1);
  1346. }
  1347. AstNodeArray parse_expr_list(AstFile *f, b32 lhs) {
  1348. AstNodeArray list = make_ast_node_array(f);
  1349. do {
  1350. AstNode *e = parse_expr(f, lhs);
  1351. gb_array_append(list, e);
  1352. if (f->cursor[0].kind != Token_Comma ||
  1353. f->cursor[0].kind == Token_EOF) {
  1354. break;
  1355. }
  1356. next_token(f);
  1357. } while (true);
  1358. return list;
  1359. }
  1360. AstNodeArray parse_lhs_expr_list(AstFile *f) {
  1361. return parse_expr_list(f, true);
  1362. }
  1363. AstNodeArray parse_rhs_expr_list(AstFile *f) {
  1364. return parse_expr_list(f, false);
  1365. }
  1366. AstNode *parse_decl(AstFile *f, AstNodeArray names);
  1367. AstNode *parse_simple_stmt(AstFile *f) {
  1368. isize lhs_count = 0, rhs_count = 0;
  1369. AstNodeArray lhs = parse_lhs_expr_list(f);
  1370. AstNode *statement = NULL;
  1371. Token token = f->cursor[0];
  1372. switch (token.kind) {
  1373. case Token_Eq:
  1374. case Token_AddEq:
  1375. case Token_SubEq:
  1376. case Token_MulEq:
  1377. case Token_QuoEq:
  1378. case Token_ModEq:
  1379. case Token_AndEq:
  1380. case Token_OrEq:
  1381. case Token_XorEq:
  1382. case Token_ShlEq:
  1383. case Token_ShrEq:
  1384. case Token_AndNotEq:
  1385. case Token_CmpAndEq:
  1386. case Token_CmpOrEq:
  1387. {
  1388. if (f->curr_proc == NULL) {
  1389. ast_file_err(f, f->cursor[0], "You cannot use a simple statement in the file scope");
  1390. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1391. }
  1392. next_token(f);
  1393. AstNodeArray rhs = parse_rhs_expr_list(f);
  1394. if (gb_array_count(rhs) == 0) {
  1395. ast_file_err(f, token, "No right-hand side in assignment statement.");
  1396. return make_bad_stmt(f, token, f->cursor[0]);
  1397. }
  1398. return make_assign_stmt(f, token, lhs, rhs);
  1399. } break;
  1400. case Token_Colon: // Declare
  1401. return parse_decl(f, lhs);
  1402. }
  1403. if (lhs_count > 1) {
  1404. ast_file_err(f, token, "Expected 1 expression");
  1405. return make_bad_stmt(f, token, f->cursor[0]);
  1406. }
  1407. token = f->cursor[0];
  1408. switch (token.kind) {
  1409. case Token_Increment:
  1410. case Token_Decrement:
  1411. if (f->curr_proc == NULL) {
  1412. ast_file_err(f, f->cursor[0], "You cannot use a simple statement in the file scope");
  1413. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1414. }
  1415. statement = make_inc_dec_stmt(f, token, lhs[0]);
  1416. next_token(f);
  1417. return statement;
  1418. }
  1419. return make_expr_stmt(f, lhs[0]);
  1420. }
  1421. AstNode *parse_block_stmt(AstFile *f) {
  1422. if (f->curr_proc == NULL) {
  1423. ast_file_err(f, f->cursor[0], "You cannot use a block statement in the file scope");
  1424. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1425. }
  1426. AstNode *block_stmt = parse_body(f);
  1427. return block_stmt;
  1428. }
  1429. AstNode *convert_stmt_to_expr(AstFile *f, AstNode *statement, String kind) {
  1430. if (statement == NULL)
  1431. return NULL;
  1432. if (statement->kind == AstNode_ExprStmt)
  1433. return statement->ExprStmt.expr;
  1434. ast_file_err(f, f->cursor[0], "Expected `%.*s`, found a simple statement.", LIT(kind));
  1435. return make_bad_expr(f, f->cursor[0], f->cursor[1]);
  1436. }
  1437. AstNodeArray parse_identfier_list(AstFile *f) {
  1438. AstNodeArray list = make_ast_node_array(f);
  1439. do {
  1440. gb_array_append(list, parse_identifier(f));
  1441. if (f->cursor[0].kind != Token_Comma ||
  1442. f->cursor[0].kind == Token_EOF) {
  1443. break;
  1444. }
  1445. next_token(f);
  1446. } while (true);
  1447. return list;
  1448. }
  1449. AstNode *parse_type_attempt(AstFile *f) {
  1450. AstNode *type = parse_identifier_or_type(f);
  1451. if (type != NULL) {
  1452. // TODO(bill): Handle?
  1453. }
  1454. return type;
  1455. }
  1456. AstNode *parse_type(AstFile *f) {
  1457. AstNode *type = parse_type_attempt(f);
  1458. if (type == NULL) {
  1459. Token token = f->cursor[0];
  1460. ast_file_err(f, token, "Expected a type");
  1461. next_token(f);
  1462. return make_bad_expr(f, token, f->cursor[0]);
  1463. }
  1464. return type;
  1465. }
  1466. Token parse_procedure_signature(AstFile *f,
  1467. AstNodeArray *params, AstNodeArray *results);
  1468. AstNode *parse_proc_type(AstFile *f) {
  1469. AstNodeArray params = NULL;
  1470. AstNodeArray results = NULL;
  1471. Token proc_token = parse_procedure_signature(f, &params, &results);
  1472. return make_proc_type(f, proc_token, params, results);
  1473. }
  1474. AstNode *parse_field_decl(AstFile *f) {
  1475. b32 is_using = false;
  1476. if (allow_token(f, Token_using)) {
  1477. is_using = true;
  1478. }
  1479. AstNodeArray names = parse_lhs_expr_list(f);
  1480. if (gb_array_count(names) == 0) {
  1481. ast_file_err(f, f->cursor[0], "Empty field declaration");
  1482. }
  1483. if (gb_array_count(names) > 1 && is_using) {
  1484. ast_file_err(f, f->cursor[0], "Cannot apply `using` to more than one of the same type");
  1485. is_using = false;
  1486. }
  1487. expect_token(f, Token_Colon);
  1488. AstNode *type = NULL;
  1489. if (f->cursor[0].kind == Token_Ellipsis) {
  1490. Token ellipsis = f->cursor[0];
  1491. next_token(f);
  1492. type = parse_type_attempt(f);
  1493. if (type == NULL) {
  1494. ast_file_err(f, f->cursor[0], "variadic parameter is missing a type after `..`");
  1495. type = make_bad_expr(f, ellipsis, f->cursor[0]);
  1496. } else {
  1497. if (gb_array_count(names) > 1) {
  1498. ast_file_err(f, f->cursor[0], "mutliple variadic parameters, only `..`");
  1499. } else {
  1500. type = make_ellipsis(f, ellipsis, type);
  1501. }
  1502. }
  1503. } else {
  1504. type = parse_type_attempt(f);
  1505. }
  1506. if (type == NULL) {
  1507. ast_file_err(f, f->cursor[0], "Expected a type for this field declaration");
  1508. }
  1509. AstNode *field = make_field(f, names, type, is_using);
  1510. return field;
  1511. }
  1512. AstNodeArray parse_parameter_list(AstFile *f) {
  1513. AstNodeArray params = make_ast_node_array(f);
  1514. while (f->cursor[0].kind == Token_Identifier ||
  1515. f->cursor[0].kind == Token_using) {
  1516. AstNode *field = parse_field_decl(f);
  1517. gb_array_append(params, field);
  1518. if (f->cursor[0].kind != Token_Comma) {
  1519. break;
  1520. }
  1521. next_token(f);
  1522. }
  1523. return params;
  1524. }
  1525. AstNodeArray parse_struct_params(AstFile *f, isize *decl_count_, b32 using_allowed) {
  1526. AstNodeArray decls = make_ast_node_array(f);
  1527. isize decl_count = 0;
  1528. while (f->cursor[0].kind == Token_Identifier ||
  1529. f->cursor[0].kind == Token_using) {
  1530. b32 is_using = false;
  1531. if (allow_token(f, Token_using)) {
  1532. is_using = true;
  1533. }
  1534. AstNodeArray names = parse_lhs_expr_list(f);
  1535. if (gb_array_count(names) == 0) {
  1536. ast_file_err(f, f->cursor[0], "Empty field declaration");
  1537. }
  1538. if (!using_allowed && is_using) {
  1539. ast_file_err(f, f->cursor[0], "Cannot apply `using` to members of a union");
  1540. is_using = false;
  1541. }
  1542. if (gb_array_count(names) > 1 && is_using) {
  1543. ast_file_err(f, f->cursor[0], "Cannot apply `using` to more than one of the same type");
  1544. }
  1545. AstNode *decl = NULL;
  1546. if (f->cursor[0].kind == Token_Colon) {
  1547. decl = parse_decl(f, names);
  1548. if (decl->kind == AstNode_ProcDecl) {
  1549. ast_file_err(f, f->cursor[0], "Procedure declarations are not allowed within a structure");
  1550. decl = make_bad_decl(f, ast_node_token(names[0]), f->cursor[0]);
  1551. }
  1552. } else {
  1553. ast_file_err(f, f->cursor[0], "Illegal structure field");
  1554. decl = make_bad_decl(f, ast_node_token(names[0]), f->cursor[0]);
  1555. }
  1556. expect_semicolon_after_stmt(f, decl);
  1557. if (decl != NULL && is_ast_node_decl(decl)) {
  1558. gb_array_append(decls, decl);
  1559. if (decl->kind == AstNode_VarDecl) {
  1560. decl->VarDecl.is_using = is_using && using_allowed;
  1561. if (decl->VarDecl.kind == Declaration_Mutable) {
  1562. if (gb_array_count(decl->VarDecl.values) > 0) {
  1563. ast_file_err(f, f->cursor[0], "Default variable assignments within a structure will be ignored (at the moment)");
  1564. }
  1565. }
  1566. } else {
  1567. decl_count += 1;
  1568. }
  1569. }
  1570. }
  1571. if (decl_count_) *decl_count_ = decl_count;
  1572. return decls;
  1573. }
  1574. AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
  1575. switch (f->cursor[0].kind) {
  1576. case Token_volatile:
  1577. next_token(f);
  1578. return parse_identifier_or_type(f, flags | TypeFlag_volatile);
  1579. case Token_atomic:
  1580. next_token(f);
  1581. return parse_identifier_or_type(f, flags | TypeFlag_atomic);
  1582. case Token_Identifier: {
  1583. AstNode *e = parse_identifier(f);
  1584. while (f->cursor[0].kind == Token_Period) {
  1585. Token token = f->cursor[0];
  1586. next_token(f);
  1587. AstNode *sel = parse_identifier(f);
  1588. e = make_selector_expr(f, token, e, sel);
  1589. }
  1590. if (f->cursor[0].kind == Token_OpenParen) {
  1591. // HACK NOTE(bill): For type_of_val(expr)
  1592. e = parse_call_expr(f, e);
  1593. }
  1594. e->type_flags = flags;
  1595. return e;
  1596. }
  1597. case Token_Pointer: {
  1598. AstNode *e = make_pointer_type(f, expect_token(f, Token_Pointer), parse_type(f));
  1599. e->type_flags = flags;
  1600. return e;
  1601. }
  1602. case Token_OpenBracket: {
  1603. f->expr_level++;
  1604. Token token = expect_token(f, Token_OpenBracket);
  1605. AstNode *count_expr = NULL;
  1606. if (f->cursor[0].kind == Token_Ellipsis) {
  1607. count_expr = make_ellipsis(f, f->cursor[0], NULL);
  1608. next_token(f);
  1609. } else if (f->cursor[0].kind != Token_CloseBracket) {
  1610. count_expr = parse_expr(f, false);
  1611. }
  1612. expect_token(f, Token_CloseBracket);
  1613. f->expr_level--;
  1614. AstNode *e = make_array_type(f, token, count_expr, parse_type(f));
  1615. e->type_flags = flags;
  1616. return e;
  1617. }
  1618. case Token_OpenBrace: {
  1619. f->expr_level++;
  1620. Token token = expect_token(f, Token_OpenBrace);
  1621. AstNode *count_expr = parse_expr(f, false);
  1622. expect_token(f, Token_CloseBrace);
  1623. f->expr_level--;
  1624. AstNode *e = make_vector_type(f, token, count_expr, parse_type(f));
  1625. e->type_flags = flags;
  1626. return e;
  1627. }
  1628. case Token_struct: {
  1629. Token token = expect_token(f, Token_struct);
  1630. b32 is_packed = false;
  1631. b32 is_ordered = false;
  1632. while (allow_token(f, Token_Hash)) {
  1633. Token tag = expect_token(f, Token_Identifier);
  1634. if (are_strings_equal(tag.string, make_string("packed"))) {
  1635. is_packed = true;
  1636. } else if (are_strings_equal(tag.string, make_string("ordered"))) {
  1637. is_ordered = true;
  1638. } else {
  1639. ast_file_err(f, tag, "Expected a `#packed` or `#ordered` tag");
  1640. }
  1641. }
  1642. if (is_packed && is_ordered) {
  1643. ast_file_err(f, token, "`#ordered` is not needed with `#packed` which implies ordering");
  1644. }
  1645. Token open = expect_token(f, Token_OpenBrace);
  1646. isize decl_count = 0;
  1647. AstNodeArray decls = parse_struct_params(f, &decl_count, true);
  1648. Token close = expect_token(f, Token_CloseBrace);
  1649. AstNode *e = make_struct_type(f, token, decls, decl_count, is_packed, is_ordered);
  1650. e->type_flags = flags;
  1651. return e;
  1652. } break;
  1653. case Token_union: {
  1654. Token token = expect_token(f, Token_union);
  1655. Token open = expect_token(f, Token_OpenBrace);
  1656. isize decl_count = 0;
  1657. AstNodeArray decls = parse_struct_params(f, &decl_count, false);
  1658. Token close = expect_token(f, Token_CloseBrace);
  1659. AstNode *e = make_union_type(f, token, decls, decl_count);
  1660. e->type_flags = flags;
  1661. return e;
  1662. }
  1663. case Token_raw_union: {
  1664. Token token = expect_token(f, Token_raw_union);
  1665. Token open = expect_token(f, Token_OpenBrace);
  1666. isize decl_count = 0;
  1667. AstNodeArray decls = parse_struct_params(f, &decl_count, true);
  1668. Token close = expect_token(f, Token_CloseBrace);
  1669. AstNode *e = make_raw_union_type(f, token, decls, decl_count);
  1670. e->type_flags = flags;
  1671. return e;
  1672. }
  1673. case Token_enum: {
  1674. Token token = expect_token(f, Token_enum);
  1675. AstNode *base_type = NULL;
  1676. Token open, close;
  1677. if (f->cursor[0].kind != Token_OpenBrace) {
  1678. base_type = parse_type(f);
  1679. }
  1680. AstNodeArray fields = make_ast_node_array(f);
  1681. open = expect_token(f, Token_OpenBrace);
  1682. while (f->cursor[0].kind != Token_CloseBrace &&
  1683. f->cursor[0].kind != Token_EOF) {
  1684. AstNode *name = parse_identifier(f);
  1685. AstNode *value = NULL;
  1686. Token eq = empty_token;
  1687. if (f->cursor[0].kind == Token_Eq) {
  1688. eq = expect_token(f, Token_Eq);
  1689. value = parse_value(f);
  1690. }
  1691. AstNode *field = make_field_value(f, name, value, eq);
  1692. gb_array_append(fields, field);
  1693. if (f->cursor[0].kind != Token_Comma) {
  1694. break;
  1695. }
  1696. next_token(f);
  1697. }
  1698. close = expect_token(f, Token_CloseBrace);
  1699. AstNode *e = make_enum_type(f, token, base_type, fields);
  1700. e->type_flags = flags;
  1701. return e;
  1702. }
  1703. case Token_proc: {
  1704. AstNode *curr_proc = f->curr_proc;
  1705. AstNode *type = parse_proc_type(f);
  1706. f->curr_proc = type;
  1707. f->curr_proc = curr_proc;
  1708. return type;
  1709. }
  1710. case Token_OpenParen: {
  1711. // NOTE(bill): Skip the paren expression
  1712. AstNode *type;
  1713. Token open, close;
  1714. open = expect_token(f, Token_OpenParen);
  1715. type = parse_type(f);
  1716. close = expect_token(f, Token_CloseParen);
  1717. AstNode *e = make_paren_expr(f, type, open, close);
  1718. e->type_flags = flags;
  1719. return e;
  1720. }
  1721. // TODO(bill): Why is this even allowed? Is this a parsing error?
  1722. case Token_Colon:
  1723. break;
  1724. case Token_Eq:
  1725. if (f->cursor[-1].kind == Token_Colon)
  1726. break;
  1727. // fallthrough
  1728. default:
  1729. ast_file_err(f, f->cursor[0],
  1730. "Expected a type after `%.*s`, got `%.*s`", LIT(f->cursor[-1].string), LIT(f->cursor[0].string));
  1731. break;
  1732. }
  1733. return NULL;
  1734. }
  1735. AstNodeArray parse_results(AstFile *f) {
  1736. AstNodeArray results = make_ast_node_array(f);
  1737. if (allow_token(f, Token_ArrowRight)) {
  1738. if (f->cursor[0].kind == Token_OpenParen) {
  1739. expect_token(f, Token_OpenParen);
  1740. while (f->cursor[0].kind != Token_CloseParen &&
  1741. f->cursor[0].kind != Token_EOF) {
  1742. gb_array_append(results, parse_type(f));
  1743. if (f->cursor[0].kind != Token_Comma) {
  1744. break;
  1745. }
  1746. next_token(f);
  1747. }
  1748. expect_token(f, Token_CloseParen);
  1749. return results;
  1750. }
  1751. gb_array_append(results, parse_type(f));
  1752. return results;
  1753. }
  1754. return results;
  1755. }
  1756. Token parse_procedure_signature(AstFile *f,
  1757. AstNodeArray *params,
  1758. AstNodeArray *results) {
  1759. Token proc_token = expect_token(f, Token_proc);
  1760. expect_token(f, Token_OpenParen);
  1761. *params = parse_parameter_list(f);
  1762. expect_token(f, Token_CloseParen);
  1763. *results = parse_results(f);
  1764. return proc_token;
  1765. }
  1766. AstNode *parse_body(AstFile *f) {
  1767. AstNodeArray stmts = NULL;
  1768. Token open, close;
  1769. open = expect_token(f, Token_OpenBrace);
  1770. stmts = parse_stmt_list(f);
  1771. close = expect_token(f, Token_CloseBrace);
  1772. return make_block_stmt(f, stmts, open, close);
  1773. }
  1774. AstNode *parse_proc_decl(AstFile *f, Token proc_token, AstNode *name) {
  1775. AstNodeArray params = NULL;
  1776. AstNodeArray results = NULL;
  1777. parse_procedure_signature(f, &params, &results);
  1778. AstNode *proc_type = make_proc_type(f, proc_token, params, results);
  1779. AstNode *body = NULL;
  1780. u64 tags = 0;
  1781. String foreign_name = {};
  1782. parse_proc_tags(f, &tags, &foreign_name);
  1783. AstNode *curr_proc = f->curr_proc;
  1784. f->curr_proc = proc_type;
  1785. defer (f->curr_proc = curr_proc);
  1786. if (f->cursor[0].kind == Token_OpenBrace) {
  1787. if ((tags & ProcTag_foreign) != 0) {
  1788. ast_file_err(f, f->cursor[0], "A procedure tagged as `#foreign` cannot have a body");
  1789. }
  1790. body = parse_body(f);
  1791. }
  1792. return make_proc_decl(f, name, proc_type, body, tags, foreign_name);
  1793. }
  1794. AstNode *parse_decl(AstFile *f, AstNodeArray names) {
  1795. AstNodeArray values = NULL;
  1796. AstNode *type = NULL;
  1797. gb_for_array(i, names) {
  1798. AstNode *name = names[i];
  1799. if (name->kind == AstNode_Ident) {
  1800. String n = name->Ident.string;
  1801. // NOTE(bill): Check for reserved identifiers
  1802. if (are_strings_equal(n, make_string("context"))) {
  1803. ast_file_err(f, ast_node_token(name), "`context` is a reserved identifier");
  1804. break;
  1805. }
  1806. }
  1807. }
  1808. if (allow_token(f, Token_Colon)) {
  1809. if (!allow_token(f, Token_type)) {
  1810. type = parse_identifier_or_type(f);
  1811. }
  1812. } else if (f->cursor[0].kind != Token_Eq && f->cursor[0].kind != Token_Semicolon) {
  1813. ast_file_err(f, f->cursor[0], "Expected type separator `:` or `=`");
  1814. }
  1815. DeclKind declaration_kind = Declaration_Mutable;
  1816. if (f->cursor[0].kind == Token_Eq ||
  1817. f->cursor[0].kind == Token_Colon) {
  1818. if (f->cursor[0].kind == Token_Colon)
  1819. declaration_kind = Declaration_Immutable;
  1820. next_token(f);
  1821. if (f->cursor[0].kind == Token_type ||
  1822. f->cursor[0].kind == Token_struct ||
  1823. f->cursor[0].kind == Token_enum ||
  1824. f->cursor[0].kind == Token_union ||
  1825. f->cursor[0].kind == Token_raw_union) {
  1826. Token token = f->cursor[0];
  1827. if (token.kind == Token_type) {
  1828. next_token(f);
  1829. }
  1830. if (gb_array_count(names) != 1) {
  1831. ast_file_err(f, ast_node_token(names[0]), "You can only declare one type at a time");
  1832. return make_bad_decl(f, names[0]->Ident, token);
  1833. }
  1834. if (type != NULL) {
  1835. ast_file_err(f, f->cursor[-1], "Expected either `type` or nothing between : and :");
  1836. // NOTE(bill): Do not fail though
  1837. }
  1838. AstNode *type = parse_type(f);
  1839. return make_type_decl(f, token, names[0], type);
  1840. } else if (f->cursor[0].kind == Token_proc &&
  1841. declaration_kind == Declaration_Immutable) {
  1842. // NOTE(bill): Procedure declarations
  1843. Token proc_token = f->cursor[0];
  1844. AstNode *name = names[0];
  1845. if (gb_array_count(names) != 1) {
  1846. ast_file_err(f, proc_token, "You can only declare one procedure at a time");
  1847. return make_bad_decl(f, name->Ident, proc_token);
  1848. }
  1849. AstNode *proc_decl = parse_proc_decl(f, proc_token, name);
  1850. return proc_decl;
  1851. } else {
  1852. values = parse_rhs_expr_list(f);
  1853. if (gb_array_count(values) > gb_array_count(names)) {
  1854. ast_file_err(f, f->cursor[0], "Too many values on the right hand side of the declaration");
  1855. } else if (gb_array_count(values) < gb_array_count(names) &&
  1856. declaration_kind == Declaration_Immutable) {
  1857. ast_file_err(f, f->cursor[0], "All constant declarations must be defined");
  1858. } else if (gb_array_count(values) == 0) {
  1859. ast_file_err(f, f->cursor[0], "Expected an expression for this declaration");
  1860. }
  1861. }
  1862. }
  1863. if (declaration_kind == Declaration_Mutable) {
  1864. if (type == NULL && gb_array_count(values) == 0) {
  1865. ast_file_err(f, f->cursor[0], "Missing variable type or initialization");
  1866. return make_bad_decl(f, f->cursor[0], f->cursor[0]);
  1867. }
  1868. } else if (declaration_kind == Declaration_Immutable) {
  1869. if (type == NULL && gb_array_count(values) == 0 && gb_array_count(names) > 0) {
  1870. ast_file_err(f, f->cursor[0], "Missing constant value");
  1871. return make_bad_decl(f, f->cursor[0], f->cursor[0]);
  1872. }
  1873. } else {
  1874. Token begin = f->cursor[0];
  1875. ast_file_err(f, begin, "Unknown type of variable declaration");
  1876. fix_advance_to_next_stmt(f);
  1877. return make_bad_decl(f, begin, f->cursor[0]);
  1878. }
  1879. if (values == NULL) {
  1880. values = make_ast_node_array(f);
  1881. }
  1882. return make_var_decl(f, declaration_kind, names, type, values);
  1883. }
  1884. AstNode *parse_if_stmt(AstFile *f) {
  1885. if (f->curr_proc == NULL) {
  1886. ast_file_err(f, f->cursor[0], "You cannot use an if statement in the file scope");
  1887. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1888. }
  1889. Token token = expect_token(f, Token_if);
  1890. AstNode *init = NULL;
  1891. AstNode *cond = NULL;
  1892. AstNode *body = NULL;
  1893. AstNode *else_stmt = NULL;
  1894. isize prev_level = f->expr_level;
  1895. f->expr_level = -1;
  1896. if (allow_token(f, Token_Semicolon)) {
  1897. cond = parse_expr(f, false);
  1898. } else {
  1899. init = parse_simple_stmt(f);
  1900. if (allow_token(f, Token_Semicolon)) {
  1901. cond = parse_expr(f, false);
  1902. } else {
  1903. cond = convert_stmt_to_expr(f, init, make_string("boolean expression"));
  1904. init = NULL;
  1905. }
  1906. }
  1907. f->expr_level = prev_level;
  1908. if (cond == NULL) {
  1909. ast_file_err(f, f->cursor[0], "Expected condition for if statement");
  1910. }
  1911. body = parse_block_stmt(f);
  1912. if (allow_token(f, Token_else)) {
  1913. switch (f->cursor[0].kind) {
  1914. case Token_if:
  1915. else_stmt = parse_if_stmt(f);
  1916. break;
  1917. case Token_OpenBrace:
  1918. else_stmt = parse_block_stmt(f);
  1919. break;
  1920. default:
  1921. ast_file_err(f, f->cursor[0], "Expected if statement block statement");
  1922. else_stmt = make_bad_stmt(f, f->cursor[0], f->cursor[1]);
  1923. break;
  1924. }
  1925. }
  1926. return make_if_stmt(f, token, init, cond, body, else_stmt);
  1927. }
  1928. AstNode *parse_return_stmt(AstFile *f) {
  1929. if (f->curr_proc == NULL) {
  1930. ast_file_err(f, f->cursor[0], "You cannot use a return statement in the file scope");
  1931. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1932. }
  1933. Token token = expect_token(f, Token_return);
  1934. AstNodeArray results = make_ast_node_array(f);
  1935. if (f->cursor[0].kind != Token_Semicolon && f->cursor[0].kind != Token_CloseBrace &&
  1936. f->cursor[0].pos.line == token.pos.line) {
  1937. results = parse_rhs_expr_list(f);
  1938. }
  1939. if (f->cursor[0].kind != Token_CloseBrace) {
  1940. expect_semicolon_after_stmt(f, results ? results[0] : NULL);
  1941. }
  1942. return make_return_stmt(f, token, results);
  1943. }
  1944. AstNode *parse_for_stmt(AstFile *f) {
  1945. if (f->curr_proc == NULL) {
  1946. ast_file_err(f, f->cursor[0], "You cannot use a for statement in the file scope");
  1947. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1948. }
  1949. Token token = expect_token(f, Token_for);
  1950. AstNode *init = NULL;
  1951. AstNode *cond = NULL;
  1952. AstNode *end = NULL;
  1953. AstNode *body = NULL;
  1954. if (f->cursor[0].kind != Token_OpenBrace) {
  1955. isize prev_level = f->expr_level;
  1956. f->expr_level = -1;
  1957. if (f->cursor[0].kind != Token_Semicolon) {
  1958. cond = parse_simple_stmt(f);
  1959. if (is_ast_node_complex_stmt(cond)) {
  1960. ast_file_err(f, f->cursor[0],
  1961. "You are not allowed that type of statement in a for statement, it is too complex!");
  1962. }
  1963. }
  1964. if (allow_token(f, Token_Semicolon)) {
  1965. init = cond;
  1966. cond = NULL;
  1967. if (f->cursor[0].kind != Token_Semicolon) {
  1968. cond = parse_simple_stmt(f);
  1969. }
  1970. expect_token(f, Token_Semicolon);
  1971. if (f->cursor[0].kind != Token_OpenBrace) {
  1972. end = parse_simple_stmt(f);
  1973. }
  1974. }
  1975. f->expr_level = prev_level;
  1976. }
  1977. body = parse_block_stmt(f);
  1978. cond = convert_stmt_to_expr(f, cond, make_string("boolean expression"));
  1979. return make_for_stmt(f, token, init, cond, end, body);
  1980. }
  1981. AstNode *parse_case_clause(AstFile *f) {
  1982. Token token = f->cursor[0];
  1983. AstNodeArray list = make_ast_node_array(f);
  1984. if (allow_token(f, Token_case)) {
  1985. list = parse_rhs_expr_list(f);
  1986. } else {
  1987. expect_token(f, Token_default);
  1988. }
  1989. expect_token(f, Token_Colon); // TODO(bill): Is this the best syntax?
  1990. AstNodeArray stmts = parse_stmt_list(f);
  1991. return make_case_clause(f, token, list, stmts);
  1992. }
  1993. AstNode *parse_type_case_clause(AstFile *f) {
  1994. Token token = f->cursor[0];
  1995. AstNodeArray clause = make_ast_node_array(f);
  1996. if (allow_token(f, Token_case)) {
  1997. gb_array_append(clause, parse_expr(f, false));
  1998. } else {
  1999. expect_token(f, Token_default);
  2000. }
  2001. expect_token(f, Token_Colon); // TODO(bill): Is this the best syntax?
  2002. AstNodeArray stmts = parse_stmt_list(f);
  2003. return make_case_clause(f, token, clause, stmts);
  2004. }
  2005. AstNode *parse_match_stmt(AstFile *f) {
  2006. if (f->curr_proc == NULL) {
  2007. ast_file_err(f, f->cursor[0], "You cannot use a match statement in the file scope");
  2008. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  2009. }
  2010. Token token = expect_token(f, Token_match);
  2011. AstNode *init = NULL;
  2012. AstNode *tag = NULL;
  2013. AstNode *body = NULL;
  2014. Token open, close;
  2015. if (allow_token(f, Token_type)) {
  2016. isize prev_level = f->expr_level;
  2017. f->expr_level = -1;
  2018. AstNode *var = parse_identifier(f);
  2019. expect_token(f, Token_Colon);
  2020. tag = parse_simple_stmt(f);
  2021. f->expr_level = prev_level;
  2022. open = expect_token(f, Token_OpenBrace);
  2023. AstNodeArray list = make_ast_node_array(f);
  2024. while (f->cursor[0].kind == Token_case ||
  2025. f->cursor[0].kind == Token_default) {
  2026. gb_array_append(list, parse_type_case_clause(f));
  2027. }
  2028. close = expect_token(f, Token_CloseBrace);
  2029. body = make_block_stmt(f, list, open, close);
  2030. tag = convert_stmt_to_expr(f, tag, make_string("type match expression"));
  2031. return make_type_match_stmt(f, token, tag, var, body);
  2032. } else {
  2033. if (f->cursor[0].kind != Token_OpenBrace) {
  2034. isize prev_level = f->expr_level;
  2035. f->expr_level = -1;
  2036. if (f->cursor[0].kind != Token_Semicolon) {
  2037. tag = parse_simple_stmt(f);
  2038. }
  2039. if (allow_token(f, Token_Semicolon)) {
  2040. init = tag;
  2041. tag = NULL;
  2042. if (f->cursor[0].kind != Token_OpenBrace) {
  2043. tag = parse_simple_stmt(f);
  2044. }
  2045. }
  2046. f->expr_level = prev_level;
  2047. }
  2048. open = expect_token(f, Token_OpenBrace);
  2049. AstNodeArray list = make_ast_node_array(f);
  2050. while (f->cursor[0].kind == Token_case ||
  2051. f->cursor[0].kind == Token_default) {
  2052. gb_array_append(list, parse_case_clause(f));
  2053. }
  2054. close = expect_token(f, Token_CloseBrace);
  2055. body = make_block_stmt(f, list, open, close);
  2056. tag = convert_stmt_to_expr(f, tag, make_string("match expression"));
  2057. return make_match_stmt(f, token, init, tag, body);
  2058. }
  2059. }
  2060. AstNode *parse_defer_stmt(AstFile *f) {
  2061. if (f->curr_proc == NULL) {
  2062. ast_file_err(f, f->cursor[0], "You cannot use a defer statement in the file scope");
  2063. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  2064. }
  2065. Token token = expect_token(f, Token_defer);
  2066. AstNode *statement = parse_stmt(f);
  2067. switch (statement->kind) {
  2068. case AstNode_EmptyStmt:
  2069. ast_file_err(f, token, "Empty statement after defer (e.g. `;`)");
  2070. break;
  2071. case AstNode_DeferStmt:
  2072. ast_file_err(f, token, "You cannot defer a defer statement");
  2073. break;
  2074. case AstNode_ReturnStmt:
  2075. ast_file_err(f, token, "You cannot a return statement");
  2076. break;
  2077. }
  2078. return make_defer_stmt(f, token, statement);
  2079. }
  2080. AstNode *parse_asm_stmt(AstFile *f) {
  2081. Token token = expect_token(f, Token_asm);
  2082. b32 is_volatile = false;
  2083. if (allow_token(f, Token_volatile)) {
  2084. is_volatile = true;
  2085. }
  2086. Token open, close, code_string;
  2087. open = expect_token(f, Token_OpenBrace);
  2088. code_string = expect_token(f, Token_String);
  2089. AstNode *output_list = NULL;
  2090. AstNode *input_list = NULL;
  2091. AstNode *clobber_list = NULL;
  2092. isize output_count = 0;
  2093. isize input_count = 0;
  2094. isize clobber_count = 0;
  2095. // TODO(bill): Finish asm statement and determine syntax
  2096. // if (f->cursor[0].kind != Token_CloseBrace) {
  2097. // expect_token(f, Token_Colon);
  2098. // }
  2099. close = expect_token(f, Token_CloseBrace);
  2100. return make_asm_stmt(f, token, is_volatile, open, close, code_string,
  2101. output_list, input_list, clobber_list,
  2102. output_count, input_count, clobber_count);
  2103. }
  2104. AstNode *parse_stmt(AstFile *f) {
  2105. AstNode *s = NULL;
  2106. Token token = f->cursor[0];
  2107. switch (token.kind) {
  2108. // Operands
  2109. case Token_Identifier:
  2110. case Token_Integer:
  2111. case Token_Float:
  2112. case Token_Rune:
  2113. case Token_String:
  2114. case Token_OpenParen:
  2115. case Token_proc:
  2116. // Unary Operators
  2117. case Token_Add:
  2118. case Token_Sub:
  2119. case Token_Xor:
  2120. case Token_Not:
  2121. s = parse_simple_stmt(f);
  2122. expect_semicolon_after_stmt(f, s);
  2123. return s;
  2124. // TODO(bill): other keywords
  2125. case Token_if: return parse_if_stmt(f);
  2126. case Token_return: return parse_return_stmt(f);
  2127. case Token_for: return parse_for_stmt(f);
  2128. case Token_match: return parse_match_stmt(f);
  2129. case Token_defer: return parse_defer_stmt(f);
  2130. case Token_asm: return parse_asm_stmt(f);
  2131. case Token_break:
  2132. case Token_continue:
  2133. case Token_fallthrough:
  2134. next_token(f);
  2135. s = make_branch_stmt(f, token);
  2136. expect_semicolon_after_stmt(f, s);
  2137. return s;
  2138. case Token_using: {
  2139. AstNode *node = NULL;
  2140. next_token(f);
  2141. node = parse_stmt(f);
  2142. b32 valid = false;
  2143. switch (node->kind) {
  2144. case AstNode_ExprStmt: {
  2145. AstNode *e = unparen_expr(node->ExprStmt.expr);
  2146. while (e->kind == AstNode_SelectorExpr) {
  2147. e = unparen_expr(e->SelectorExpr.selector);
  2148. }
  2149. if (e->kind == AstNode_Ident) {
  2150. valid = true;
  2151. }
  2152. } break;
  2153. case AstNode_VarDecl:
  2154. if (node->VarDecl.kind == Declaration_Mutable) {
  2155. valid = true;
  2156. }
  2157. break;
  2158. }
  2159. if (!valid) {
  2160. ast_file_err(f, token, "Illegal use of `using` statement.");
  2161. return make_bad_stmt(f, token, f->cursor[0]);
  2162. }
  2163. return make_using_stmt(f, token, node);
  2164. } break;
  2165. case Token_Hash: {
  2166. s = parse_tag_stmt(f, NULL);
  2167. String tag = s->TagStmt.name.string;
  2168. if (are_strings_equal(tag, make_string("global_scope"))) {
  2169. if (f->curr_proc == NULL) {
  2170. f->is_global_scope = true;
  2171. return make_empty_stmt(f, f->cursor[0]);
  2172. }
  2173. ast_file_err(f, token, "You cannot use #global_scope within a procedure. This must be done at the file scope.");
  2174. return make_bad_decl(f, token, f->cursor[0]);
  2175. } else if (are_strings_equal(tag, make_string("import"))) {
  2176. // TODO(bill): better error messages
  2177. Token file_path = expect_token(f, Token_String);
  2178. Token as = expect_token(f, Token_as);
  2179. Token import_name = expect_token(f, Token_Identifier);
  2180. if (f->curr_proc == NULL) {
  2181. return make_import_decl(f, s->TagStmt.token, file_path, import_name);
  2182. }
  2183. ast_file_err(f, token, "You cannot use #import within a procedure. This must be done at the file scope.");
  2184. return make_bad_decl(f, token, file_path);
  2185. } else if (are_strings_equal(tag, make_string("foreign_system_library"))) {
  2186. Token file_path = expect_token(f, Token_String);
  2187. if (f->curr_proc == NULL) {
  2188. return make_foreign_system_library(f, s->TagStmt.token, file_path);
  2189. }
  2190. ast_file_err(f, token, "You cannot use #foreign_system_library within a procedure. This must be done at the file scope.");
  2191. return make_bad_decl(f, token, file_path);
  2192. } else if (are_strings_equal(tag, make_string("thread_local"))) {
  2193. AstNode *var_decl = parse_simple_stmt(f);
  2194. if (var_decl->kind != AstNode_VarDecl ||
  2195. var_decl->VarDecl.kind != Declaration_Mutable) {
  2196. ast_file_err(f, token, "#thread_local may only be applied to variable declarations");
  2197. return make_bad_decl(f, token, ast_node_token(var_decl));
  2198. }
  2199. if (f->curr_proc != NULL) {
  2200. ast_file_err(f, token, "#thread_local is only allowed at the file scope.");
  2201. return make_bad_decl(f, token, ast_node_token(var_decl));
  2202. }
  2203. var_decl->VarDecl.tags |= VarDeclTag_thread_local;
  2204. return var_decl;
  2205. } else if (are_strings_equal(tag, make_string("bounds_check"))) {
  2206. s = parse_stmt(f);
  2207. s->stmt_state_flags |= StmtStateFlag_bounds_check;
  2208. if ((s->stmt_state_flags & StmtStateFlag_no_bounds_check) != 0) {
  2209. ast_file_err(f, token, "#bounds_check and #no_bounds_check cannot be applied together");
  2210. }
  2211. return s;
  2212. } else if (are_strings_equal(tag, make_string("no_bounds_check"))) {
  2213. s = parse_stmt(f);
  2214. s->stmt_state_flags |= StmtStateFlag_no_bounds_check;
  2215. if ((s->stmt_state_flags & StmtStateFlag_bounds_check) != 0) {
  2216. ast_file_err(f, token, "#bounds_check and #no_bounds_check cannot be applied together");
  2217. }
  2218. return s;
  2219. }
  2220. s->TagStmt.stmt = parse_stmt(f); // TODO(bill): Find out why this doesn't work as an argument
  2221. return s;
  2222. } break;
  2223. case Token_OpenBrace: return parse_block_stmt(f);
  2224. case Token_Semicolon:
  2225. s = make_empty_stmt(f, token);
  2226. next_token(f);
  2227. return s;
  2228. }
  2229. ast_file_err(f, token,
  2230. "Expected a statement, got `%.*s`",
  2231. LIT(token_strings[token.kind]));
  2232. fix_advance_to_next_stmt(f);
  2233. return make_bad_stmt(f, token, f->cursor[0]);
  2234. }
  2235. AstNodeArray parse_stmt_list(AstFile *f) {
  2236. AstNodeArray list = make_ast_node_array(f);
  2237. while (f->cursor[0].kind != Token_case &&
  2238. f->cursor[0].kind != Token_default &&
  2239. f->cursor[0].kind != Token_CloseBrace &&
  2240. f->cursor[0].kind != Token_EOF) {
  2241. AstNode *stmt = parse_stmt(f);
  2242. if (stmt && stmt->kind != AstNode_EmptyStmt) {
  2243. gb_array_append(list, stmt);
  2244. }
  2245. }
  2246. return list;
  2247. }
  2248. ParseFileError init_ast_file(AstFile *f, String fullpath) {
  2249. if (!string_has_extension(fullpath, make_string("odin"))) {
  2250. gb_printf_err("Only `.odin` files are allowed\n");
  2251. return ParseFile_WrongExtension;
  2252. }
  2253. TokenizerInitError err = init_tokenizer(&f->tokenizer, fullpath);
  2254. if (err == TokenizerInit_None) {
  2255. gb_array_init(f->tokens, gb_heap_allocator());
  2256. for (;;) {
  2257. Token token = tokenizer_get_token(&f->tokenizer);
  2258. if (token.kind == Token_Invalid)
  2259. return ParseFile_InvalidToken;
  2260. gb_array_append(f->tokens, token);
  2261. if (token.kind == Token_EOF)
  2262. break;
  2263. }
  2264. f->cursor = &f->tokens[0];
  2265. // NOTE(bill): Is this big enough or too small?
  2266. isize arena_size = gb_size_of(AstNode);
  2267. arena_size *= 2*gb_array_count(f->tokens);
  2268. gb_arena_init_from_allocator(&f->arena, gb_heap_allocator(), arena_size);
  2269. f->curr_proc = NULL;
  2270. return ParseFile_None;
  2271. }
  2272. switch (err) {
  2273. case TokenizerInit_NotExists:
  2274. return ParseFile_NotFound;
  2275. case TokenizerInit_Permission:
  2276. return ParseFile_Permission;
  2277. case TokenizerInit_Empty:
  2278. return ParseFile_EmptyFile;
  2279. }
  2280. return ParseFile_InvalidFile;
  2281. }
  2282. void destroy_ast_file(AstFile *f) {
  2283. gb_arena_free(&f->arena);
  2284. gb_array_free(f->tokens);
  2285. gb_free(gb_heap_allocator(), f->tokenizer.fullpath.text);
  2286. destroy_tokenizer(&f->tokenizer);
  2287. }
  2288. b32 init_parser(Parser *p) {
  2289. gb_array_init(p->files, gb_heap_allocator());
  2290. gb_array_init(p->imports, gb_heap_allocator());
  2291. gb_array_init(p->libraries, gb_heap_allocator());
  2292. gb_array_init(p->system_libraries, gb_heap_allocator());
  2293. return true;
  2294. }
  2295. void destroy_parser(Parser *p) {
  2296. // TODO(bill): Fix memory leak
  2297. gb_for_array(i, p->files) {
  2298. destroy_ast_file(&p->files[i]);
  2299. }
  2300. #if 1
  2301. gb_for_array(i, p->imports) {
  2302. // gb_free(gb_heap_allocator(), p->imports[i].text);
  2303. }
  2304. #endif
  2305. gb_array_free(p->files);
  2306. gb_array_free(p->imports);
  2307. gb_array_free(p->libraries);
  2308. gb_array_free(p->system_libraries);
  2309. }
  2310. // NOTE(bill): Returns true if it's added
  2311. b32 try_add_import_path(Parser *p, String path, TokenPos pos) {
  2312. gb_for_array(i, p->imports) {
  2313. String import = p->imports[i].path;
  2314. if (are_strings_equal(import, path)) {
  2315. return false;
  2316. }
  2317. }
  2318. ImportedFile item;
  2319. item.path = path;
  2320. item.pos = pos;
  2321. gb_array_append(p->imports, item);
  2322. return true;
  2323. }
  2324. // NOTE(bill): Returns true if it's added
  2325. b32 try_add_foreign_system_library_path(Parser *p, String import_file) {
  2326. gb_for_array(i, p->system_libraries) {
  2327. String import = p->system_libraries[i];
  2328. if (are_strings_equal(import, import_file)) {
  2329. return false;
  2330. }
  2331. }
  2332. gb_array_append(p->system_libraries, import_file);
  2333. return true;
  2334. }
  2335. gb_global Rune illegal_import_runes[] = {
  2336. '"', '\'', '`', ' ',
  2337. '\\', // NOTE(bill): Disallow windows style filepaths
  2338. '!', '$', '%', '^', '&', '*', '(', ')', '=', '+',
  2339. '[', ']', '{', '}',
  2340. ';', ':', '#',
  2341. '|', ',', '<', '>', '?',
  2342. };
  2343. b32 is_import_path_valid(String path) {
  2344. if (path.len > 0) {
  2345. u8 *start = path.text;
  2346. u8 *end = path.text + path.len;
  2347. u8 *curr = start;
  2348. Rune r = -1;
  2349. while (curr < end) {
  2350. isize width = 1;
  2351. r = curr[0];
  2352. if (r >= 0x80) {
  2353. width = gb_utf8_decode(curr, end-curr, &r);
  2354. if (r == GB_RUNE_INVALID && width == 1)
  2355. return false;
  2356. else if (r == GB_RUNE_BOM && curr-start > 0)
  2357. return false;
  2358. }
  2359. for (isize i = 0; i < gb_count_of(illegal_import_runes); i++) {
  2360. if (r == illegal_import_runes[i])
  2361. return false;
  2362. }
  2363. curr += width;
  2364. }
  2365. return true;
  2366. }
  2367. return false;
  2368. }
  2369. void parse_file(Parser *p, AstFile *f) {
  2370. String filepath = f->tokenizer.fullpath;
  2371. String base_dir = filepath;
  2372. for (isize i = filepath.len-1; i >= 0; i--) {
  2373. if (base_dir.text[i] == GB_PATH_SEPARATOR)
  2374. break;
  2375. base_dir.len--;
  2376. }
  2377. f->decls = parse_stmt_list(f);
  2378. gb_for_array(i, f->decls) {
  2379. AstNode *node = f->decls[i];
  2380. if (!is_ast_node_decl(node) &&
  2381. node->kind != AstNode_BadStmt &&
  2382. node->kind != AstNode_EmptyStmt) {
  2383. // NOTE(bill): Sanity check
  2384. ast_file_err(f, ast_node_token(node), "Only declarations are allowed at file scope");
  2385. } else {
  2386. if (node->kind == AstNode_ImportDecl) {
  2387. auto *id = &node->ImportDecl;
  2388. String file_str = id->relpath.string;
  2389. if (!is_import_path_valid(file_str)) {
  2390. ast_file_err(f, ast_node_token(node), "Invalid `load` path");
  2391. continue;
  2392. }
  2393. isize str_len = base_dir.len+file_str.len;
  2394. u8 *str = gb_alloc_array(gb_heap_allocator(), u8, str_len+1);
  2395. defer (gb_free(gb_heap_allocator(), str));
  2396. gb_memcopy(str, base_dir.text, base_dir.len);
  2397. gb_memcopy(str+base_dir.len, file_str.text, file_str.len);
  2398. str[str_len] = '\0';
  2399. // HACK(bill): memory leak
  2400. char *path_str = gb_path_get_full_name(gb_heap_allocator(), cast(char *)str);
  2401. String import_file = make_string(path_str);
  2402. id->fullpath = import_file;
  2403. if (!try_add_import_path(p, import_file, ast_node_token(node).pos)) {
  2404. // gb_free(gb_heap_allocator(), import_file.text);
  2405. }
  2406. } else if (node->kind == AstNode_ForeignSystemLibrary) {
  2407. auto *id = &node->ForeignSystemLibrary;
  2408. String file_str = id->filepath.string;
  2409. if (!is_import_path_valid(file_str)) {
  2410. ast_file_err(f, ast_node_token(node), "Invalid `foreign_system_library` path");
  2411. continue;
  2412. }
  2413. try_add_foreign_system_library_path(p, file_str);
  2414. }
  2415. }
  2416. }
  2417. }
  2418. ParseFileError parse_files(Parser *p, char *init_filename) {
  2419. char *fullpath_str = gb_path_get_full_name(gb_heap_allocator(), init_filename);
  2420. String init_fullpath = make_string(fullpath_str);
  2421. TokenPos init_pos = {};
  2422. ImportedFile init_imported_file = {init_fullpath, init_pos};
  2423. gb_array_append(p->imports, init_imported_file);
  2424. p->init_fullpath = init_fullpath;
  2425. gb_for_array(i, p->imports) {
  2426. String import_path = p->imports[i].path;
  2427. TokenPos pos = p->imports[i].pos;
  2428. AstFile file = {};
  2429. ParseFileError err = init_ast_file(&file, import_path);
  2430. if (err != ParseFile_None) {
  2431. if (pos.line != 0) {
  2432. gb_printf_err("%.*s(%td:%td) ", LIT(pos.file), pos.line, pos.column);
  2433. }
  2434. gb_printf_err("Failed to parse file: %.*s\n", LIT(import_path));
  2435. switch (err) {
  2436. case ParseFile_WrongExtension:
  2437. gb_printf_err("\tInvalid file extension\n");
  2438. break;
  2439. case ParseFile_InvalidFile:
  2440. gb_printf_err("\tInvalid file\n");
  2441. break;
  2442. case ParseFile_EmptyFile:
  2443. gb_printf_err("\tFile is empty\n");
  2444. break;
  2445. case ParseFile_Permission:
  2446. gb_printf_err("\tFile permissions problem\n");
  2447. break;
  2448. case ParseFile_NotFound:
  2449. gb_printf_err("\tFile cannot be found\n");
  2450. break;
  2451. case ParseFile_InvalidToken:
  2452. gb_printf_err("\tInvalid token found in file\n");
  2453. break;
  2454. }
  2455. return err;
  2456. }
  2457. parse_file(p, &file);
  2458. gb_array_append(p->files, file);
  2459. p->total_token_count += gb_array_count(file.tokens);
  2460. }
  2461. return ParseFile_None;
  2462. }