diff options
| author | mryouse | 2023-05-28 02:26:42 +0000 |
|---|---|---|
| committer | mryouse | 2023-05-28 02:26:42 +0000 |
| commit | 9867aade2a94ab13aec70f74ccf37d6c38e66fd0 (patch) | |
| tree | 1e84e582ca698c2f3e138f4c0e5be16aed338905 | |
| parent | 15e2e506687f6e914095f2194c545204b49e3d98 (diff) | |
skip comments (better)
| -rw-r--r-- | parser.d | 34 |
1 files changed, 23 insertions, 11 deletions
@@ -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; |
