Fix parameter/field lists and #import #include syntax

This commit is contained in:
Ginger Bill
2017-01-02 00:26:28 +00:00
parent 9202bd1b06
commit ce89a1428e
17 changed files with 623 additions and 788 deletions
+8 -25
View File
@@ -1235,31 +1235,14 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
}
case_end;
case_ast_node(gd, GenericDecl, decl);
case_ast_node(id, ImportDecl, decl);
if (!parent_scope->is_file) {
// NOTE(bill): Within a procedure, variables must be in order
// NOTE(bill): _Should_ be caught by the parser
// TODO(bill): Better error handling if it isn't
continue;
}
for_array(iota, gd->specs) {
AstNode *spec = gd->specs.e[iota];
switch (spec->kind) {
case AstNode_BadDecl:
break;
case_ast_node(is, ImportSpec, spec);
if (!parent_scope->is_file) {
// NOTE(bill): _Should_ be caught by the parser
// TODO(bill): Better error handling if it isn't
continue;
}
DelayedDecl di = {parent_scope, spec};
array_add(&c->delayed_imports, di);
case_end;
default:
error(ast_node_token(spec), "Invalid specification in declaration: `%.*s`", LIT(ast_node_strings[spec->kind]));
break;
}
}
DelayedDecl di = {parent_scope, decl};
array_add(&c->delayed_imports, di);
case_end;
case_ast_node(fl, ForeignLibrary, decl);
if (!parent_scope->is_file) {
@@ -1283,8 +1266,8 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
void check_import_entities(Checker *c, MapScope *file_scopes) {
for_array(i, c->delayed_imports) {
Scope *parent_scope = c->delayed_imports.e[i].parent;
AstNode *spec = c->delayed_imports.e[i].decl;
ast_node(id, ImportSpec, spec);
AstNode *decl = c->delayed_imports.e[i].decl;
ast_node(id, ImportDecl, decl);
Token token = id->relpath;
HashKey key = hash_string(id->fullpath);
@@ -1342,7 +1325,7 @@ void check_import_entities(Checker *c, MapScope *file_scopes) {
}
// NOTE(bill): Do not add other imported entities
add_entity(c, parent_scope, NULL, e);
if (id->keyword == Token_import) { // `#import`ed entities don't get exported
if (id->is_import) { // `#import`ed entities don't get exported
HashKey key = hash_string(e->token.string);
map_entity_set(&parent_scope->implicit, key, e);
}
+1 -1
View File
@@ -283,7 +283,7 @@ void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init,
check_init_constant(c, e, &operand);
c->context.iota = (ExactValue){0};
if (e->type == t_invalid) {
if (operand.mode == Addressing_Invalid) {
error(e->token, "Illegal cyclic declaration");
}
}
+103 -239
View File
@@ -250,21 +250,6 @@ AST_NODE_KIND(_ComplexStmtBegin, "", i32) \
}) \
AST_NODE_KIND(_ComplexStmtEnd, "", i32) \
AST_NODE_KIND(_StmtEnd, "", i32) \
AST_NODE_KIND(_SpecBegin, "", i32) \
AST_NODE_KIND(TypeSpec, "type specification", struct { \
AstNode *name; \
AstNode *type; \
AstNode *note; \
}) \
AST_NODE_KIND(ImportSpec, "import specification", struct { \
TokenKind keyword; \
Token relpath; \
String fullpath; \
Token import_name; \
AstNode *cond; \
AstNode *note; \
}) \
AST_NODE_KIND(_SpecEnd, "", i32) \
AST_NODE_KIND(_DeclBegin, "", i32) \
AST_NODE_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
AST_NODE_KIND(GenericDecl, "declaration", struct { \
@@ -281,6 +266,15 @@ AST_NODE_KIND(_DeclBegin, "", i32) \
AstNodeArray values; \
u64 tags; \
}) \
AST_NODE_KIND(ImportDecl, "import declaration", struct { \
Token token; \
bool is_import; \
Token relpath; \
String fullpath; \
Token import_name; \
AstNode *cond; \
AstNode *note; \
}) \
AST_NODE_KIND(ForeignLibrary, "foreign library", struct { \
Token token, filepath; \
String base_dir; \
@@ -496,14 +490,11 @@ Token ast_node_token(AstNode *node) {
return node->GenericDecl.token;
case AstNode_ValueDecl:
return ast_node_token(node->ValueDecl.names.e[0]);
case AstNode_ImportDecl:
return node->ImportDecl.token;
case AstNode_ForeignLibrary:
return node->ForeignLibrary.token;
case AstNode_TypeSpec:
return ast_node_token(node->TypeSpec.name);
case AstNode_ImportSpec:
return node->ImportSpec.relpath;
case AstNode_Field: {
if (node->Field.names.count > 0) {
@@ -1081,20 +1072,14 @@ AstNode *make_value_decl(AstFile *f, bool is_var, AstNodeArray names, AstNode *t
return result;
}
AstNode *make_type_spec(AstFile *f, AstNode *name, AstNode *type) {
AstNode *result = make_node(f, AstNode_TypeSpec);
result->TypeSpec.name = name;
result->TypeSpec.type = type;
return result;
}
AstNode *make_import_spec(AstFile *f, TokenKind keyword, Token relpath, Token import_name, AstNode *cond) {
AstNode *result = make_node(f, AstNode_ImportSpec);
result->ImportSpec.keyword = keyword;
result->ImportSpec.relpath = relpath;
result->ImportSpec.import_name = import_name;
result->ImportSpec.cond = cond;
AstNode *make_import_decl(AstFile *f, Token token, bool is_import, Token relpath, Token import_name, AstNode *cond) {
AstNode *result = make_node(f, AstNode_ImportDecl);
result->ImportDecl.token = token;
result->ImportDecl.is_import = is_import;
result->ImportDecl.relpath = relpath;
result->ImportDecl.import_name = import_name;
result->ImportDecl.cond = cond;
return result;
}
@@ -1206,13 +1191,6 @@ void fix_advance_to_next_stmt(AstFile *f) {
case Token_Semicolon:
return;
// case Token_var:
// case Token_const:
case Token_type:
case Token_proc:
case Token_import:
case Token_include:
case Token_if:
case Token_when:
case Token_return:
@@ -1294,12 +1272,6 @@ bool is_semicolon_optional_for_node(AstFile *f, AstNode *s) {
}
}
break;
case AstNode_TypeSpec:
if (f->prev_token.kind == Token_CloseBrace) {
return true;
}
break;
}
return false;
@@ -1787,8 +1759,8 @@ AstNode *parse_operand(AstFile *f, bool lhs) {
if ((tags & ProcTag_foreign) != 0) {
return make_proc_lit(f, type, NULL, tags, foreign_name, link_name);
}
if ((tags & ProcTag_link_name) != 0) {
syntax_error(token, "A procedure typed cannot be tagged with `#link_name`");
if (tags != 0) {
syntax_error(token, "A procedure type cannot have tags");
}
return type;
@@ -1802,8 +1774,8 @@ AstNode *parse_operand(AstFile *f, bool lhs) {
if (type != NULL) {
// TODO(bill): Is this correct???
// NOTE(bill): Sanity check as identifiers should be handled already
// TokenPos pos = ast_node_token(type).pos;
// GB_ASSERT_MSG(type->kind != AstNode_Ident, "Type Cannot be identifier %.*s(%td:%td)", LIT(pos.file), pos.line, pos.column);
TokenPos pos = ast_node_token(type).pos;
GB_ASSERT_MSG(type->kind != AstNode_Ident, "Type cannot be identifier %.*s(%td:%td)", LIT(pos.file), pos.line, pos.column);
return type;
}
break;
@@ -2138,153 +2110,6 @@ AstNode *parse_type(AstFile *f) {
return type;
}
#define PARSE_SPEC_PROC(name) AstNode *(name)(AstFile *f, TokenKind keyword, isize index)
typedef PARSE_SPEC_PROC(*ParserSpecProc);
AstNode *parse_generic_decl(AstFile *f, TokenKind keyword, ParserSpecProc spec_proc) {
Token token = expect_token(f, keyword);
Token open = {0}, close = {0};
AstNodeArray specs = {0};
if (f->curr_token.kind == Token_OpenBrace) {
open = expect_token(f, Token_OpenBrace);
array_init(&specs, heap_allocator());
for (isize index = 0;
f->curr_token.kind != Token_CloseBrace &&
f->curr_token.kind != Token_EOF;
index++) {
AstNode *spec = spec_proc(f, keyword, index);
array_add(&specs, spec);
expect_semicolon(f, spec);
}
close = expect_token(f, Token_CloseBrace);
} else {
array_init_reserve(&specs, heap_allocator(), 1);
array_add(&specs, spec_proc(f, keyword, 0));
}
return make_generic_decl(f, token, open, close, specs, 0, false);
}
// PARSE_SPEC_PROC(parse_value_spec) {
// AstNodeArray names = parse_identfier_list(f);
// parse_check_name_list_for_reserves(f, names);
// AstNode *type = parse_type_attempt(f);
// AstNodeArray values = {0};
// if (allow_token(f, Token_Eq)) {
// values = parse_rhs_expr_list(f);
// }
// if (values.count > names.count) {
// syntax_error(f->curr_token, "Too many values on the right hand side of the declaration");
// }
// switch (keyword) {
// case Token_var:
// if (type == NULL && values.count == 0 && names.count > 0) {
// syntax_error(f->curr_token, "Missing type or initialization");
// return make_bad_decl(f, f->curr_token, f->curr_token);
// }
// break;
// case Token_const:
// if (values.count == 0 && (index == 0 || type != NULL)) {
// syntax_error(f->curr_token, "Missing constant value");
// return make_bad_decl(f, f->curr_token, f->curr_token);
// }
// break;
// }
// // TODO(bill): Fix this so it does not require it
// if (values.e == NULL) {
// values = make_ast_node_array(f);
// }
// return make_value_spec(f, keyword, names, type, values);
// }
PARSE_SPEC_PROC(parse_type_spec) {
AstNode *name = parse_identifier(f);
AstNode *type = parse_type(f);
return make_type_spec(f, name, type);
}
PARSE_SPEC_PROC(parse_import_spec) {
AstNode *cond = NULL;
Token import_name = {0};
switch (f->curr_token.kind) {
case Token_Period:
import_name = f->curr_token;
import_name.kind = Token_Ident;
next_token(f);
break;
case Token_Ident:
import_name = f->curr_token;
next_token(f);
break;
default:
import_name.pos = f->curr_token.pos;
break;
}
if (str_eq(import_name.string, str_lit("_"))) {
syntax_error(import_name, "Illegal import name: `_`");
}
Token file_path = expect_token_after(f, Token_String, "import");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
AstNode *spec = NULL;
if (f->curr_proc != NULL) {
syntax_error(import_name, "You cannot use `import` within a procedure. This must be done at the file scope");
spec = make_bad_decl(f, import_name, file_path);
} else {
spec = make_import_spec(f, Token_import, file_path, import_name, cond);
}
return spec;
}
PARSE_SPEC_PROC(parse_include_spec) {
AstNode *cond = NULL;
Token file_path = expect_token_after(f, Token_String, "include");
Token import_name = file_path;
import_name.string = str_lit(".");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
AstNode *spec = NULL;
if (f->curr_proc != NULL) {
syntax_error(import_name, "You cannot use `include` within a procedure. This must be done at the file scope");
spec = make_bad_decl(f, import_name, file_path);
} else {
spec = make_import_spec(f, Token_include, file_path, import_name, cond);
}
return spec;
}
AstNode *parse_decl(AstFile *f) {
switch (f->curr_token.kind) {
case Token_import:
return parse_generic_decl(f, f->curr_token.kind, parse_import_spec);
case Token_include:
return parse_generic_decl(f, f->curr_token.kind, parse_include_spec);
default: {
Token token = f->curr_token;
syntax_error(token, "Expected a declaration");
fix_advance_to_next_stmt(f);
return make_bad_decl(f, token, f->curr_token);
}
}
}
AstNode *parse_simple_stmt(AstFile *f) {
Token start_token = f->curr_token;
AstNodeArray lhs = parse_lhs_expr_list(f);
@@ -2458,7 +2283,7 @@ AstNodeArray parse_field_list(AstFile *f, isize *name_count_, bool allow_using,
name_count += names.count;
// expect_token_after(f, Token_Colon, "parameter list");
expect_token_after(f, Token_Colon, "parameter list");
AstNode *type = NULL;
if (ellipsis_ok && f->curr_token.kind == Token_Ellipsis) {
@@ -3101,16 +2926,6 @@ AstNode *parse_stmt(AstFile *f) {
expect_semicolon(f, s);
return s;
// case Token_var:
// case Token_const:
case Token_proc:
case Token_type:
case Token_import:
case Token_include:
s = parse_decl(f);
expect_semicolon(f, s);
return s;
case Token_if: return parse_if_stmt(f);
case Token_when: return parse_when_stmt(f);
case Token_for: return parse_for_stmt(f);
@@ -3183,7 +2998,63 @@ AstNode *parse_stmt(AstFile *f) {
Token hash_token = expect_token(f, Token_Hash);
Token name = expect_token(f, Token_Ident);
String tag = name.string;
if (str_eq(tag, str_lit("shared_global_scope"))) {
if (str_eq(tag, str_lit("import"))) {
AstNode *cond = NULL;
Token import_name = {0};
switch (f->curr_token.kind) {
case Token_Period:
import_name = f->curr_token;
import_name.kind = Token_Ident;
next_token(f);
break;
case Token_Ident:
import_name = f->curr_token;
next_token(f);
break;
default:
import_name.pos = f->curr_token.pos;
break;
}
if (str_eq(import_name.string, str_lit("_"))) {
syntax_error(import_name, "Illegal #import name: `_`");
}
Token file_path = expect_token_after(f, Token_String, "#import");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
AstNode *decl = NULL;
if (f->curr_proc != NULL) {
syntax_error(import_name, "You cannot use `#import` within a procedure. This must be done at the file scope");
decl = make_bad_decl(f, import_name, file_path);
} else {
decl = make_import_decl(f, hash_token, true, file_path, import_name, cond);
}
expect_semicolon(f, decl);
return decl;
} else if (str_eq(tag, str_lit("include"))) {
AstNode *cond = NULL;
Token file_path = expect_token_after(f, Token_String, "#include");
Token import_name = file_path;
import_name.string = str_lit(".");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
AstNode *decl = NULL;
if (f->curr_proc != NULL) {
syntax_error(import_name, "You cannot use `#include` within a procedure. This must be done at the file scope");
decl = make_bad_decl(f, import_name, file_path);
} else {
decl = make_import_decl(f, hash_token, false, file_path, import_name, cond);
}
expect_semicolon(f, decl);
return decl;
} else if (str_eq(tag, str_lit("shared_global_scope"))) {
if (f->curr_proc == NULL) {
f->is_global_scope = true;
s = make_empty_stmt(f, f->curr_token);
@@ -3461,41 +3332,34 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, AstNodeArray
node->kind != AstNode_EmptyStmt) {
// NOTE(bill): Sanity check
syntax_error_node(node, "Only declarations are allowed at file scope %.*s", LIT(ast_node_strings[node->kind]));
} else if (node->kind == AstNode_GenericDecl) {
AstNodeGenericDecl *gd = &node->GenericDecl;
for_array(spec_index, gd->specs) {
AstNode *spec = gd->specs.e[spec_index];
switch (spec->kind) {
case_ast_node(is, ImportSpec, spec);
String file_str = is->relpath.string;
} else if (node->kind == AstNode_ImportDecl) {
ast_node(id, ImportDecl, node);
String file_str = id->relpath.string;
if (!is_import_path_valid(file_str)) {
if (is->keyword == Token_include) {
syntax_error_node(node, "Invalid #include path: `%.*s`", LIT(file_str));
} else {
syntax_error_node(node, "Invalid #import path: `%.*s`", LIT(file_str));
}
// NOTE(bill): It's a naughty name
gd->specs.e[spec_index] = make_bad_decl(f, is->relpath, is->relpath);
continue;
}
if (!is_import_path_valid(file_str)) {
if (id->is_import) {
syntax_error_node(node, "Invalid import path: `%.*s`", LIT(file_str));
} else {
syntax_error_node(node, "Invalid include path: `%.*s`", LIT(file_str));
}
// NOTE(bill): It's a naughty name
decls.e[i] = make_bad_decl(f, id->relpath, id->relpath);
continue;
}
gbAllocator allocator = heap_allocator(); // TODO(bill): Change this allocator
gbAllocator allocator = heap_allocator(); // TODO(bill): Change this allocator
String rel_path = get_fullpath_relative(allocator, base_dir, file_str);
String import_file = rel_path;
if (!gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
String abs_path = get_fullpath_core(allocator, file_str);
if (gb_file_exists(cast(char *)abs_path.text)) {
import_file = abs_path;
}
}
is->fullpath = import_file;
try_add_import_path(p, import_file, file_str, ast_node_token(node).pos);
case_end;
String rel_path = get_fullpath_relative(allocator, base_dir, file_str);
String import_file = rel_path;
if (!gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
String abs_path = get_fullpath_core(allocator, file_str);
if (gb_file_exists(cast(char *)abs_path.text)) {
import_file = abs_path;
}
}
id->fullpath = import_file;
try_add_import_path(p, import_file, file_str, ast_node_token(node).pos);
} else if (node->kind == AstNode_ForeignLibrary) {
AstNodeForeignLibrary *fl = &node->ForeignLibrary;
String file_str = fl->filepath.string;
+2 -3
View File
@@ -86,8 +86,8 @@ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
TOKEN_KIND(Token_proc, "proc"), \
/* TOKEN_KIND(Token_var, "var"), */\
/* TOKEN_KIND(Token_const, "const"), */\
TOKEN_KIND(Token_import, "import"), \
TOKEN_KIND(Token_include, "include"), \
/* TOKEN_KIND(Token_import, "import"), */\
/* TOKEN_KIND(Token_include, "include"), */\
TOKEN_KIND(Token_macro, "macro"), \
TOKEN_KIND(Token_match, "match"), \
TOKEN_KIND(Token_break, "break"), \
@@ -824,7 +824,6 @@ Token tokenizer_get_token(Tokenizer *t) {
break;
case ';':
token.kind = Token_Semicolon;
token.string = str_lit(";");
break;
case ',':
token.kind = Token_Comma;