From 740367ec5c9d1dd98c2b7a59581c585a087f8b70 Mon Sep 17 00:00:00 2001
From: Calvin Rose <calsrose@gmail.com>
Date: Sat, 1 Jul 2017 12:47:57 -0400
Subject: [PATCH] Add # comments to parser

---
 core/parse.c       | 26 +++++++++++++++++++++++++-
 gsttests/basic.gst |  7 ++++---
 include/gst/gst.h  |  6 ++++++
 3 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/core/parse.c b/core/parse.c
index 2419aa3b..341aadb9 100644
--- a/core/parse.c
+++ b/core/parse.c
@@ -407,7 +407,29 @@ static int form_state(GstParser *p, uint8_t c) {
 /* Handle a character */
 void gst_parse_byte(GstParser *p, uint8_t c) {
     int done = 0;
-    ++p->index;
+    /* Update position in source */
+    if (c == '\n') {
+        p->line++;
+        p->index = 0;
+        p->comment = GST_PCOMMENT_EXPECTING;
+    } else {
+        ++p->index; 
+    }
+    /* Check comments */
+    switch (p->comment) {
+        case GST_PCOMMENT_NOT:
+            break;
+        case GST_PCOMMENT_EXPECTING:
+            if (c == '#') {
+                p->comment = GST_PCOMMENT_INSIDE;
+                return;
+            } else if (!is_whitespace(c)) {
+                p->comment = GST_PCOMMENT_NOT;
+            }
+            break;
+        case GST_PCOMMENT_INSIDE:
+            return;
+    }
     /* Dispatch character to state */
     while (!done) {
         GstParseState *top = parser_peek(p);
@@ -478,8 +500,10 @@ void gst_parser(GstParser *p, Gst *vm) {
     p->data = data;
     p->count = 0;
     p->index = 0;
+    p->line = 1;
     p->quoteCount = 0;
     p->error = NULL;
     p->status = GST_PARSER_ROOT;
     p->value.type = GST_NIL;
+    p->comment = GST_PCOMMENT_EXPECTING;
 }
\ No newline at end of file
diff --git a/gsttests/basic.gst b/gsttests/basic.gst
index d873b9c0..32973bfe 100644
--- a/gsttests/basic.gst
+++ b/gsttests/basic.gst
@@ -53,7 +53,8 @@
 
 (assert (= (struct 1 2 3 4 5 6 7 8) (struct 7 8 5 6 3 4 1 2)) "struct order does not matter")
 
-"Serialization tests"
+# Serialization tests
+
 (def scheck (fn [x]
     (def dat (serialize x))
     (def deser (deserialize dat))
@@ -80,7 +81,7 @@
 (assert (= athread-result "hello, world!") "thread error result")
 (assert (= (status athread) "error") "thread error status")
 
-"yield tests"
+# yield tests
 
 (def t (thread (fn [] (tran nil 1) (tran nil 2) 3)))
 
@@ -89,7 +90,7 @@
 (assert (= 3 (tran t)) "return from thread")
 (assert (= (status t) "dead") "finished thread is dead")
 
-"report"
+# report
 
 (print num-tests-passed "of" num-tests-run "tests passed")
 (exit! 0)
\ No newline at end of file
diff --git a/include/gst/gst.h b/include/gst/gst.h
index 6946c2ec..b1f0d5ad 100644
--- a/include/gst/gst.h
+++ b/include/gst/gst.h
@@ -360,6 +360,7 @@ struct GstParser {
     uint32_t count;
     uint32_t cap;
     uint32_t index;
+    uint32_t line;
     uint32_t quoteCount;
     enum {
         GST_PARSER_PENDING = 0,
@@ -367,6 +368,11 @@ struct GstParser {
         GST_PARSER_ERROR,
         GST_PARSER_ROOT
     } status;
+    enum {
+        GST_PCOMMENT_NOT,
+        GST_PCOMMENT_EXPECTING,
+        GST_PCOMMENT_INSIDE
+    } comment;
 };
 
 /* Compilation state */