From 6800761863dd45b6055768febc6ace6a20120dc7 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Sun, 19 Oct 2014 22:22:08 -0700 Subject: New ast module for for npc parsing Will eventually put most/all parsers there. --- src/io/line.hpp | 13 +++++++++++++ src/io/line_test.cpp | 16 +++++++++++++--- src/io/read.cpp | 2 ++ src/io/read_test.cpp | 6 ++++++ src/io/write.cpp | 2 ++ 5 files changed, 36 insertions(+), 3 deletions(-) (limited to 'src/io') diff --git a/src/io/line.hpp b/src/io/line.hpp index 8244c5e..5572e98 100644 --- a/src/io/line.hpp +++ b/src/io/line.hpp @@ -41,6 +41,9 @@ namespace io uint16_t line, column; AString message_str(ZString cat, ZString msg) const; + AString note_str(ZString msg) const { return message_str("note"_s, msg); } + AString warning_str(ZString msg) const { return message_str("warning"_s, msg); } + AString error_str(ZString msg) const { return message_str("error"_s, msg); } void message(ZString cat, ZString msg) const; void note(ZString msg) const { message("note"_s, msg); } void warning(ZString msg) const { message("warning"_s, msg); } @@ -64,12 +67,22 @@ namespace io LineChar begin, end; AString message_str(ZString cat, ZString msg) const; + AString note_str(ZString msg) const { return message_str("note"_s, msg); } + AString warning_str(ZString msg) const { return message_str("warning"_s, msg); } + AString error_str(ZString msg) const { return message_str("error"_s, msg); } void message(ZString cat, ZString msg) const; void note(ZString msg) const { message("note"_s, msg); } void warning(ZString msg) const { message("warning"_s, msg); } void error(ZString msg) const { message("error"_s, msg); } }; + template + struct Spanned + { + T data; + LineSpan span; + }; + class LineReader { protected: diff --git a/src/io/line_test.cpp b/src/io/line_test.cpp index edf60bd..46e7b57 100644 --- a/src/io/line_test.cpp +++ b/src/io/line_test.cpp @@ -23,6 +23,8 @@ #include "../strings/astring.hpp" #include "../strings/zstring.hpp" +#include "../tests/fdhack.hpp" + #include "../poison.hpp" @@ -57,6 +59,7 @@ TEST(io, line1) } TEST(io, line2) { + QuietFd q; io::LineReader lr(""_s, string_pipe("Hello\nWorld"_s)); io::Line hi; EXPECT_TRUE(lr.read_line(hi)); @@ -73,6 +76,7 @@ TEST(io, line2) } TEST(io, line3) { + QuietFd q; io::LineReader lr(""_s, string_pipe("Hello\rWorld"_s)); io::Line hi; EXPECT_TRUE(lr.read_line(hi)); @@ -89,6 +93,7 @@ TEST(io, line3) } TEST(io, line4) { + QuietFd q; io::LineReader lr(""_s, string_pipe("Hello\r\nWorld"_s)); io::Line hi; EXPECT_TRUE(lr.read_line(hi)); @@ -105,6 +110,7 @@ TEST(io, line4) } TEST(io, line5) { + QuietFd q; io::LineReader lr(""_s, string_pipe("Hello\n\rWorld"_s)); io::Line hi; EXPECT_TRUE(lr.read_line(hi)); @@ -175,6 +181,7 @@ TEST(io, linechar1) } TEST(io, linechar2) { + QuietFd q; io::LineCharReader lr(""_s, string_pipe("Hi\nWu"_s)); io::LineChar c; EXPECT_TRUE(lr.get(c)); @@ -223,6 +230,7 @@ TEST(io, linechar2) } TEST(io, linechar3) { + QuietFd q; io::LineCharReader lr(""_s, string_pipe("Hi\rWu"_s)); io::LineChar c; EXPECT_TRUE(lr.get(c)); @@ -271,6 +279,7 @@ TEST(io, linechar3) } TEST(io, linechar4) { + QuietFd q; io::LineCharReader lr(""_s, string_pipe("Hi\r\nWu"_s)); io::LineChar c; EXPECT_TRUE(lr.get(c)); @@ -319,6 +328,7 @@ TEST(io, linechar4) } TEST(io, linechar5) { + QuietFd q; io::LineCharReader lr(""_s, string_pipe("Hi\n\rWu"_s)); io::LineChar c; EXPECT_TRUE(lr.get(c)); @@ -402,17 +412,17 @@ TEST(io, linespan) } while (span.end.ch() != 'r'); - EXPECT_EQ(span.begin.message_str("note"_s, "foo"_s), + EXPECT_EQ(span.begin.note_str("foo"_s), ":1:5: note: foo\n" "Hello,\n" " ^\n"_s ); - EXPECT_EQ(span.end.message_str("warning"_s, "bar"_s), + EXPECT_EQ(span.end.warning_str("bar"_s), ":2:3: warning: bar\n" "World!\n" " ^\n"_s ); - EXPECT_EQ(span.message_str("error"_s, "qux"_s), + EXPECT_EQ(span.error_str("qux"_s), ":1:5: error: qux\n" "Hello,\n" " ^~ ...\n" diff --git a/src/io/read.cpp b/src/io/read.cpp index 3ae5246..f3ed293 100644 --- a/src/io/read.cpp +++ b/src/io/read.cpp @@ -36,6 +36,8 @@ namespace io { ReadFile::ReadFile(FD f) : fd(f), start(0), end(0) + // only for debug-sanity + , buf{} { } ReadFile::ReadFile(ZString name) diff --git a/src/io/read_test.cpp b/src/io/read_test.cpp index 8fe84b7..e655eb1 100644 --- a/src/io/read_test.cpp +++ b/src/io/read_test.cpp @@ -24,6 +24,8 @@ #include "../strings/zstring.hpp" #include "../strings/literal.hpp" +#include "../tests/fdhack.hpp" + #include "../poison.hpp" @@ -47,6 +49,7 @@ io::FD string_pipe(ZString sz) TEST(io, read1) { + QuietFd q; io::ReadFile rf(string_pipe("Hello"_s)); AString hi; EXPECT_TRUE(rf.getline(hi)); @@ -63,6 +66,7 @@ TEST(io, read2) } TEST(io, read3) { + QuietFd q; io::ReadFile rf(string_pipe("Hello\r"_s)); AString hi; EXPECT_TRUE(rf.getline(hi)); @@ -71,6 +75,7 @@ TEST(io, read3) } TEST(io, read4) { + QuietFd q; io::ReadFile rf(string_pipe("Hello\r\n"_s)); AString hi; EXPECT_TRUE(rf.getline(hi)); @@ -79,6 +84,7 @@ TEST(io, read4) } TEST(io, read5) { + QuietFd q; io::ReadFile rf(string_pipe("Hello\n\r"_s)); AString hi; EXPECT_TRUE(rf.getline(hi)); diff --git a/src/io/write.cpp b/src/io/write.cpp index 5359c7a..a98954b 100644 --- a/src/io/write.cpp +++ b/src/io/write.cpp @@ -37,6 +37,8 @@ namespace io { WriteFile::WriteFile(FD f, bool linebuffered) : fd(f), lb(linebuffered), buflen(0) + // only for debug-sanity + , buf{} {} WriteFile::WriteFile(ZString name, bool linebuffered) : fd(FD::open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)), lb(linebuffered), buflen(0) -- cgit v1.2.3-70-g09d2