Skip to content

Commit

Permalink
Replaced repeated code with a macro
Browse files Browse the repository at this point in the history
Add error handling in couple of places
  • Loading branch information
rep-movsd committed Aug 22, 2017
1 parent b5b5a15 commit 67b66ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
6 changes: 4 additions & 2 deletions debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ constexpr ErrLine ParseError(const char* /*unused*/)
return ErrLine{};
}

#define PARSE_ERR(x) errRow = cur_row(); errCol = cur_col(); err = x
#define PARSE_WARN(x) warns.push_back(Message(x, cur_row(), cur_col()))
// Set error message and location if not already set
#define PARSE_ERR(x) if(errRow == -1) {errRow = cur_row(); errCol = cur_col(); err = x;}

// Push warning message and location to list
#define PARSE_WARN(x) warns.push_back(Message(x, cur_row(), cur_col()))


#endif
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace std;
int main()
{
constexpr auto parser =
#include "test/large_template.spt"
#include "test/trunc.spt"

REPORT_ERRORS(parser);

Expand Down
50 changes: 29 additions & 21 deletions seephit.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct parser
// symEndTag represents the point at which the parsing should stop
constexpr void parse_html(int iParentId)
{
while(errRow ==-1 && parse_content(iParentId));
while(errRow == -1 && parse_content(iParentId));
}

void dump() const
Expand Down Expand Up @@ -262,7 +262,7 @@ struct parser
// NAME is a sequence of [a-z\-] and VALUE is "text", 'text' or {text}
constexpr bool parse_attrs(node_attrs &attrs)
{
if(errRow > -1) return false;
ON_ERR_RETURN false;

// Swallow any space
eat_space();
Expand Down Expand Up @@ -292,7 +292,7 @@ struct parser
}

check_eos();
if(errRow > -1) return false;
ON_ERR_RETURN false;

// Eat the close delim
pszText++;
Expand Down Expand Up @@ -416,7 +416,7 @@ struct parser
// Attempts to parse "</TAG>" given "TAG"
constexpr void parse_close_tag(const char_view &symExpected)
{
if(errRow > -1) return;
ON_ERR_RETURN;

eat_space();

Expand Down Expand Up @@ -452,7 +452,7 @@ struct parser
// Creates a node "@attr" under the given node and chains attributes under it if any
constexpr void append_attrs(cnode &node, node_attrs &attrs)
{
if(errRow > -1) return;
ON_ERR_RETURN;

// If there are any attributes, they become the first children of this node
if(attrs.size())
Expand Down Expand Up @@ -505,20 +505,8 @@ struct parser
return iCurrId;
}

// Parse text until a <, forbidding & and >, optionally trims whitespace on bothe ends
constexpr int parse_text(bool bTrim)
constexpr void check_template_braces(const char_view &text)
{
if(errRow > -1) return 0;

// make sure we have something
check_eos();
bool contentUnexpectedChars[256] = {false};
contentUnexpectedChars[int('>')] = true;
auto text = eat_until('<', contentUnexpectedChars);

// Make sure we have something left
check_eos();

// check if we have a '{{'
int nBrace = 0;
auto p = text.begin();
Expand Down Expand Up @@ -547,6 +535,24 @@ struct parser
{
PARSE_ERR(Error_Missing_close_brace_in_template);
}
}

// Parse text until a <, forbidding & and >, optionally trims whitespace on bothe ends
constexpr int parse_text(bool bTrim)
{
ON_ERR_RETURN 0;

// make sure we have something
check_eos();
bool contentUnexpectedChars[256] = {false};
contentUnexpectedChars[int('>')] = true;
auto text = eat_until('<', contentUnexpectedChars);

// Make sure we have something left
check_eos();

// Check if the braces are {{ matching }}
check_template_braces(text);

// Trim whitespace if needed
if(bTrim)
Expand All @@ -569,17 +575,19 @@ struct parser
{
// Parse either an open tag or text, get the new child nodes ID
int iChild = -1;
if(is_open_tag())
bool bIsOpenTag = is_open_tag();
ON_ERR_RETURN false;

if(bIsOpenTag)
{
if(errRow > -1) return false;
iChild = parse_tag();
}
else
{
if(errRow > -1) return false;
// Trim the text unless the parent node is a <pre>
iChild = parse_text(nodes[iParentId].tag != g_symPre);
}
ON_ERR_RETURN false;

// If it's not the topmost level
if(iParentId >= 0)
Expand Down

0 comments on commit 67b66ab

Please sign in to comment.