Skip to content

Commit

Permalink
Add line number to S-expression parser
Browse files Browse the repository at this point in the history
Summary:
Add m_line_no private data member inside s_expr_istream
Add 1 to m_line_no whenever \\n is found
Attach On line <m_line_no>: prefix to the error message
Add line numbers to the test

Reviewed By: justinjhendrick, arnaudvenet

Differential Revision: D8719762

fbshipit-source-id: 0f80391a36254c24278eb60a8844c68fa1305006
  • Loading branch information
Lan Nguyen authored and facebook-github-bot committed Jul 4, 2018
1 parent 71dc4c7 commit 6a5acc1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
11 changes: 9 additions & 2 deletions sparta/include/S_Expression.h
Expand Up @@ -225,7 +225,7 @@ class s_expr_istream final {
s_expr_istream& operator=(const s_expr_istream&) = delete;

s_expr_istream(std::istream& input)
: m_input(input), m_status(Status::Good), m_what("OK") {}
: m_input(input), m_line_number(1), m_status(Status::Good), m_what("OK") {}

s_expr_istream& operator>>(s_expr& expr);

Expand Down Expand Up @@ -259,6 +259,7 @@ class s_expr_istream final {

std::stack<s_expr> m_stack;
std::istream& m_input;
size_t m_line_number;
Status m_status;
std::string m_what;
};
Expand Down Expand Up @@ -803,6 +804,7 @@ inline s_expr_istream& s_expr_istream::operator>>(s_expr& expr) {
}
case ';': {
m_input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
++m_line_number;
break;
}
default: {
Expand Down Expand Up @@ -834,6 +836,9 @@ inline void s_expr_istream::skip_white_spaces() {
for (;;) {
char c = m_input.peek();
if (m_input.good() && std::isspace(c)) {
if (c == '\n') {
++m_line_number;
}
m_input.get();
} else {
return;
Expand All @@ -844,7 +849,9 @@ inline void s_expr_istream::skip_white_spaces() {
inline void s_expr_istream::set_status(Status status,
const std::string& what_arg) {
m_status = status;
m_what = what_arg;
std::ostringstream ss;
ss << "On line " << m_line_number << ": " << what_arg;
m_what = ss.str();
}

inline s_patn::s_patn()
Expand Down
33 changes: 27 additions & 6 deletions sparta/test/S_ExpressionTest.cpp
Expand Up @@ -160,17 +160,38 @@ TEST(S_ExpressionTest, basicOperations) {

std::string error;
erroneous_parse("((a) b ()", 1, error);
EXPECT_EQ("Incomplete S-expression", error);
EXPECT_EQ("On line 1: Incomplete S-expression", error);
erroneous_parse("(\n(a)\nb\n()\n", 1, error);
EXPECT_EQ("On line 5: Incomplete S-expression", error);
erroneous_parse("((a) b c))", 2, error);
EXPECT_EQ("Extra ')' encountered", error);
EXPECT_EQ("On line 1: Extra ')' encountered", error);
erroneous_parse(R"(
(
(a)
b
c
))
)", 2, error);
EXPECT_EQ("On line 6: Extra ')' encountered", error);
erroneous_parse("(a b #9999999999999)", 1, error);
EXPECT_EQ("Error parsing int32_t literal", error);
EXPECT_EQ("On line 1: Error parsing int32_t literal", error);
erroneous_parse("(a b #-9999999999999)", 1, error);
EXPECT_EQ("Error parsing int32_t literal", error);
EXPECT_EQ("On line 1: Error parsing int32_t literal", error);
erroneous_parse("(a b \"abcdef)", 1, error);
EXPECT_EQ("Error parsing string literal", error);
EXPECT_EQ("On line 1: Error parsing string literal", error);
erroneous_parse("123, (a b c)", 2, error);
EXPECT_EQ("Unexpected character encountered: ','", error);
EXPECT_EQ("On line 1: Unexpected character encountered: ','", error);
erroneous_parse(R"(;Should only take 1 endline in an inline comment\n\n\n
(
(const-string "foo\n\bar")
123, (a b c)
)
)", 2, error);
EXPECT_EQ("On line 4: Unexpected character encountered: ','", error);
erroneous_parse(R"(;The error should be on line 2
(123, (a b c) ; End of line 2
)", 2, error);
EXPECT_EQ("On line 2: Unexpected character encountered: ','", error);
}

TEST(S_ExpressionTest, patternMatching) {
Expand Down

0 comments on commit 6a5acc1

Please sign in to comment.