0efbdc192644 — Danielle Hutzley 3 years ago
Implement comments temporarily in the lexer until the preprocessor is added
4 files changed, 48 insertions(+), 19 deletions(-)

M src/laal_lexer.c
M src/laal_parser.c
M src/laal_writer.c
M test/interface.c
M src/laal_lexer.c +44 -16
@@ 23,6 23,18 @@ struct laal_tokens_t
   struct laal_tokens_node_t *head, *tail;
 };
 
+static void
+position_consider_char (laal_position_t *pos, char c, uintptr_t delta)
+{
+  if (c == '\n')
+    {
+      pos->col = 0;
+      pos->row++;
+    }
+  else
+    pos->col += delta;
+}
+
 static bool
 is_whitespace (char c)
 {

          
@@ 152,14 164,7 @@ laal_lexer_lex (laal_tokens_t output, co
             laal_try (lex_token, output, pos, (cursor - pos) / sizeof (char),
                       loc);
 
-          if (*cursor == '\n')
-            {
-              loc->col = 0;
-              loc->row++;
-            }
-          else
-            loc->col += 1 + cursor - pos;
-
+          position_consider_char (loc, *cursor, 1 + cursor - pos);
           cursor++;
           goto __laal_lexer_reset;
         }

          
@@ 197,14 202,7 @@ laal_lexer_lex (laal_tokens_t output, co
                 if (++cursor == NULL)
                   return LAAL_UNEXPECTED_EOF_ERROR;
 
-                if (*cursor == '\n')
-                  {
-                    new_loc.row++;
-                    new_loc.col = 0;
-                  }
-                else
-                  new_loc.col++;
-
+                position_consider_char (&new_loc, *cursor, 1);
                 if (!is_escape && *cursor == delim)
                   break;
 

          
@@ 217,6 215,36 @@ laal_lexer_lex (laal_tokens_t output, co
 
             goto __laal_lexer_reset;
           }
+        // TODO: until the preprocessor is implemented, I need a way to
+        // handle comments. This should be moved into the preprocessor
+        case '@':
+          if (pos != cursor)
+            laal_try (lex_token, output, pos, cursor - pos, loc);
+
+          uint8_t depth = 1;
+          while (depth != 0)
+            {
+              cursor++;
+              if (cursor == NULL || cursor + 1 == NULL)
+                return LAAL_UNEXPECTED_EOF_ERROR;
+
+              if (cursor[0] == '@')
+                {
+                  if (cursor[1] == '<')
+                    {
+                      cursor++;
+                      depth++;
+                    }
+                  else if (cursor[1] == '>')
+                    {
+                      cursor++;
+                      depth--;
+                    }
+                }
+              position_consider_char (loc, *cursor, 1);
+            }
+          cursor++;
+          goto __laal_lexer_reset;
         }
       cursor++;
       continue;

          
M src/laal_parser.c +1 -2
@@ 178,8 178,7 @@ parse_call_args (laal_tokens_t tokens, l
   while (laal_tokens_get (tokens)->type != closing_delimiter)
     {
       laal_value_t arg = NULL;
-      if (closing_delimiter == TOKEN_RPAREN
-          && laal_tokens_get (tokens)->type == TOKEN_IDENTIFIER
+      if (laal_tokens_get (tokens)->type == TOKEN_IDENTIFIER
           && laal_tokens_peek (tokens) != NULL
           && laal_tokens_peek (tokens)->type != TOKEN_LPAREN)
         {

          
M src/laal_writer.c +1 -0
@@ 158,6 158,7 @@ write_value (laal_value_t value, FILE *o
   assert (0);
 }
 
+// TODO: implement some form of pretty-printed output option
 laal_error_t
 laal_toplevel_write (laal_value_t value, FILE *output)
 {

          
M test/interface.c +2 -1
@@ 82,7 82,8 @@ END_TEST;
 TEST ("Write File")
 {
   const char *programme
-      = "greeting := format \"Hello, ${who}!\" {who := \"world\"};";
+      = "@<A simple greeting@>\n"
+        "greeting := format \"Hello, ${who}!\" {who := \"world\"};";
   laal_value_t res_initial = NULL;
   laal_error_t parse_initial_err = laal_parse_string (programme, &res_initial);
   if (parse_initial_err != LAAL_SUCCESS)