From 59e358c5edd79d7dcafcef95a978fb36959d1da6 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 7 Mar 2009 03:25:15 +0000 Subject: Block comments. Wrap up cursor moving logic into a separate function so it's less crufty. git-svn-id: http://svn.drobilla.net/resp/tuplr@71 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- tuplr.cpp | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'tuplr.cpp') diff --git a/tuplr.cpp b/tuplr.cpp index c726cc6..49a3607 100644 --- a/tuplr.cpp +++ b/tuplr.cpp @@ -36,30 +36,38 @@ std::ostream& out = std::cout; * S-Expression Lexer :: text -> S-Expressions (SExp) * ***************************************************************************/ +inline int +readChar(Cursor& cur, istream& in) +{ + int ch = in.get(); + switch (ch) { + case '\n': ++cur.line; cur.col = 0; break; + default: ++cur.col; + } + return ch; +} + SExp -readExpression(Cursor& cur, std::istream& in) +readExpression(Cursor& cur, istream& in) { #define PUSH(s, t) { if (t != "") { s.top().list.push_back(SExp(loc, t)); t = ""; } } #define YIELD(s, t) { if (s.empty()) { return SExp(loc, t); } else PUSH(s, t) } stack stk; string tok; Cursor loc; // start of tok - while (char ch = in.get()) { - ++cur.col; - switch (ch) { + while (int c = readChar(cur, in)) { + switch (c) { case EOF: if (!stk.empty()) throw Error("unexpected end of file", cur); return SExp(cur); case ';': - while ((ch = in.get()) != '\n') {} - case '\n': - ++cur.line; cur.col = 0; - case ' ': case '\t': + while ((c = readChar(cur, in)) != '\n') {} + case '\n': case ' ': case '\t': if (tok != "") YIELD(stk, tok); break; case '"': loc = cur; - do { tok.push_back(ch); ++cur.col; } while ((ch = in.get()) != '"'); + do { tok.push_back(c); } while ((c = readChar(cur, in)) != '"'); YIELD(stk, tok + '"'); break; case '(': @@ -79,9 +87,14 @@ readExpression(Cursor& cur, std::istream& in) stk.top().list.push_back(l); } break; + case '#': + if (in.peek() == '|') { + while (!(readChar(cur, in) == '|' && readChar(cur, in) == '#')) {} + break; + } default: if (tok == "") loc = cur; - tok += ch; + tok += c; } } switch (stk.size()) { -- cgit v1.2.1