summaryrefslogtreecommitdiff
path: root/parser.c
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-08-01 20:21:37 +1200
committerDavid Phillips <david@sighup.nz>2018-08-01 20:29:38 +1200
commit328e34077a2017d4017f907ac69c7ed5d6c160d4 (patch)
tree1795fd0d10293320dae0854b5b1d34a59e8fdbec /parser.c
parent70c76b708308c72956e83163b2819be69f725a7e (diff)
downloadhence-328e34077a2017d4017f907ac69c7ed5d6c160d4.tar.xz
Trim leading whitespace from contextual error, detect EOF
Side-note that the token location tuple maintains the correct column number to include the whitespace in the source file. Side-side note: tabs are counted as one column but many editors will count them as whatever the tabstop/tabwidth is.
Diffstat (limited to 'parser.c')
-rw-r--r--parser.c30
1 files changed, 24 insertions, 6 deletions
diff --git a/parser.c b/parser.c
index 618df1a..f16af6c 100644
--- a/parser.c
+++ b/parser.c
@@ -14,9 +14,16 @@ static FILE* fd;
static struct token *cursor;
static const char *filename;
-#define emit(...) fprintf(stderr, "%s at (%zd,%zd): ", filename, cursor->loc.line, cursor->loc.column);\
- fprintf(stderr, __VA_ARGS__);\
- indicate_file_area(fd, cursor->loc.line, cursor->loc.column, cursor->span)
+/* FIXME change to varadic fuction */
+#define emit(...) \
+ if (cursor) { \
+ fprintf(stderr, "%s at (%zd,%zd): ", filename, cursor->loc.line, cursor->loc.column); \
+ fprintf(stderr, __VA_ARGS__); \
+ indicate_file_area(fd, cursor->loc.line, cursor->loc.column - cursor->loc.leading_whitespace_len, cursor->span); \
+ } else { \
+ fprintf(stderr, "%s: ", filename); \
+ fprintf(stderr, __VA_ARGS__); \
+ }
//static struct op_lookup uop_handlers[] = {
@@ -42,9 +49,13 @@ expect(enum TOKEN_TYPE e) {
char *expected_desc = "(internal error)";
char *observed_desc = "(internal error)";
- if (cursor->type != e) {
+ if (!cursor || cursor->type != e) {
expected_desc = get_token_description(e);
- observed_desc = get_token_description(cursor->type);
+ if (cursor) {
+ observed_desc = get_token_description(cursor->type);
+ } else {
+ observed_desc = "end of file";
+ }
emit("Error: Expected %s, got %s\n", expected_desc, observed_desc);
return 1;
}
@@ -54,7 +65,9 @@ expect(enum TOKEN_TYPE e) {
static void
kerchunk() {
- cursor = cursor->next;
+ if (cursor) {
+ cursor = cursor->next;
+ }
}
int
@@ -64,6 +77,11 @@ parse(const char *fname, FILE *f, struct token *t) {
filename = fname;
cursor = t;
+ /* Eat leading EOL tokens */
+ while (cursor && cursor->type == TOK_EOL) {
+ kerchunk();
+ }
+
if (expect(TOK_MODULE)) {
return 1;
}