]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
added primitive line tracking to lexer
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 18 Jun 2018 17:34:57 +0000 (13:34 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 18 Jun 2018 17:34:57 +0000 (13:34 -0400)
source/lexer.l
source/main.c
source/parser.c
source/sclpl.h

index 2bb3501ef5bac1966372b5365afe644311ee218d..2a7eba6079a77d29911b972a8eed73ba4f5e5b78 100644 (file)
@@ -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 {
 }
 
 <<EOF>> { 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));
 }
index 69918a4dac6f05052829018afdcc1dfbfe07750d..8f4a4e24921939589a3185e507a1ee8409ccd8a3 100644 (file)
@@ -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;
 }
index 99b2b0b577e4620740deb61ae697c7e6bab99cd2..cf15f76064182d949a536caeeee7519e0f0fd2bd 100644 (file)
@@ -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);
 }
 
index 02c129a3c189e92f981d437768a324d6e9190a8a..903c45aa7cac69390a3137fedc54e8def6aaf4b0 100644 (file)
@@ -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);