From 5c9837048d9d976b389a17d1625c016f6fb38543 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 18 Jun 2018 13:34:57 -0400 Subject: [PATCH] added primitive line tracking to lexer --- source/lexer.l | 16 +++++++++------- source/main.c | 7 +++---- source/parser.c | 2 +- source/sclpl.h | 4 +--- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/source/lexer.l b/source/lexer.l index 2bb3501..2a7eba6 100644 --- a/source/lexer.l +++ b/source/lexer.l @@ -30,8 +30,10 @@ NOSPACE [^ \t\r\n] %% -[ \t\r\n] { /* whitespace is insignificant */ } -#.*[\r\n] { /* skip line comments */ } +"\r\n" { yylineno++; } +"\n" { yylineno++; } +#.*[\r\n] { yylineno++; } +[ \t] { /* whitespace is insignificant */ } "require" { return T_REQUIRES; } "provide" { return T_PROVIDES; } @@ -116,13 +118,13 @@ false { } <> { return T_END_FILE; } - . { return T_ERROR; } %% -void gettoken(Parser* ctx, Tok* tok) { - tok->type = yylex(); - if (tok->type != T_END_FILE) - memcpy(&(tok->value), &Value, sizeof(Value)); +void gettoken(Parser* ctx) { + ctx->tok.line = yylineno; + ctx->tok.type = yylex(); + if (ctx->tok.type != T_END_FILE) + memcpy(&(ctx->tok.value), &Value, sizeof(Value)); } diff --git a/source/main.c b/source/main.c index 69918a4..8f4a4e2 100644 --- a/source/main.c +++ b/source/main.c @@ -6,14 +6,13 @@ char* Artifact = "ast"; /* Driver Modes *****************************************************************************/ static int emit_tokens(void) { - Tok token = { 0 }; Parser ctx = { .input = stdin }; while (1) { - gettoken(&ctx, &token); - if (token.type == T_END_FILE) + gettoken(&ctx); + if (ctx.tok.type == T_END_FILE) break; else - pprint_token(stdout, &token, true); + pprint_token(stdout, &(ctx.tok), true); } return 0; } diff --git a/source/parser.c b/source/parser.c index 99b2b0b..cf15f76 100644 --- a/source/parser.c +++ b/source/parser.c @@ -24,7 +24,7 @@ static AST* token_to_tree(Parser* p, Tok* tok); *****************************************************************************/ static Tok* peek(Parser* p) { if (T_NONE == p->tok.type) - gettoken(p, &(p->tok)); + gettoken(p); return &(p->tok); } diff --git a/source/sclpl.h b/source/sclpl.h index 02c129a..903c45a 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -44,8 +44,6 @@ typedef enum { T_PACKAGE, T_REQUIRES, T_PROVIDES, T_LET, T_VAR, T_FUN, T_TYPE, T_STRUCT, T_UNION, T_ID, T_CHAR, T_INT, T_FLOAT, T_BOOL, T_STRING, -// T_LBRACE, T_RBRACE, T_LBRACK, T_RBRACK, T_LPAR, T_RPAR, T_COMMA, T_SQUOTE, -// T_DQUOTE, T_COLON, T_AMP, T_IF, T_ELSE, T_ASSIGN, T_SEMI, T_MUL } TokType; typedef struct { @@ -189,7 +187,7 @@ typedef struct { SymTable syms; } Parser; -void gettoken(Parser* ctx, Tok* tok); +void gettoken(Parser* ctx); AST* toplevel(Parser* p); void codegen_init(Parser* p); -- 2.54.0