From 9867aade2a94ab13aec70f74ccf37d6c38e66fd0 Mon Sep 17 00:00:00 2001 From: mryouse Date: Sun, 28 May 2023 02:26:42 +0000 Subject: skip comments (better) --- parser.d | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/parser.d b/parser.d index 43c7faa..31a9ad4 100644 --- a/parser.d +++ b/parser.d @@ -402,11 +402,28 @@ class Parser { } } - void skipWhitespace() { - while (peekable() && canFind([' ', '\t', '\r', '\n'], peek())) { + void skipWhitespaceAndComment() { + bool inComment = false; + while (peekable()) { + // always increment the line if (peek() == '\n') { line++; } + + // if we find a semicolon, start a comment + if (peek() == ';') { + inComment = true; + // if we're looking at a comment and reach the end of the + // line, the comment is over + } else if (inComment && peek() == '\n') { + inComment = false; + // if we're NOT reading a comment, and the next character + // is NOT whitespace, we're done + } else if (!inComment && !canFind([' ', '\t', '\r', '\n'], peek())) { + break; + } + + // get the next character advance(); } } @@ -499,7 +516,7 @@ class Parser { char next; while(peekable()) { - skipWhitespace(); + skipWhitespaceAndComment(); next = peek(); if (next == ')') { break; @@ -535,7 +552,7 @@ class Parser { char next; while(peekable()) { - skipWhitespace(); + skipWhitespaceAndComment(); next = peek(); if (next == ')') { break; @@ -631,7 +648,7 @@ class Parser { Form parseCons() { - skipWhitespace(); + skipWhitespaceAndComment(); char next; Cons cons = new Cons(line); @@ -683,7 +700,7 @@ class Parser { } Form parseForm() { - skipWhitespace(); + skipWhitespaceAndComment(); if (!peekable()) { return new Eof(line); @@ -702,11 +719,6 @@ class Parser { case '(': advance(); // go past the open paren return parseCons(); - case ';': - while (peekable() && peek() != '\n') { - advance(); - } - return parseForm(); case ':': // skipping types for now, so consume/print/throw away char[] typ; -- cgit v1.2.3