Skip to content

Commit

Permalink
Eliminate more use of error_node
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDTill committed Aug 3, 2023
1 parent 0e5e7d1 commit c7a404a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/forscape_parse_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,25 @@ bool ParseTree::empty() const noexcept {
}

const Typeset::Marker& ParseTree::getLeft(ParseNode pn) const noexcept {
assert(pn != PARSE_ERROR);
assert(isNode(pn));
return *reinterpret_cast<const Typeset::Marker*>(data.data()+pn+LEFT_MARKER_OFFSET);
}

void ParseTree::setLeft(ParseNode pn, const Typeset::Marker& m) noexcept {
assert(pn != PARSE_ERROR);
assert(isNode(pn));
*reinterpret_cast<Typeset::Marker*>(data.data()+pn+LEFT_MARKER_OFFSET) = m;
}

const Typeset::Marker& ParseTree::getRight(ParseNode pn) const noexcept {
assert(pn != PARSE_ERROR);
assert(isNode(pn));
return *reinterpret_cast<const Typeset::Marker*>(data.data()+pn+RIGHT_MARKER_OFFSET);
}

void ParseTree::setRight(ParseNode pn, const Typeset::Marker& m) noexcept {
assert(pn != PARSE_ERROR);
assert(isNode(pn));
*reinterpret_cast<Typeset::Marker*>(data.data()+pn+RIGHT_MARKER_OFFSET) = m;
}
Expand Down Expand Up @@ -232,6 +236,7 @@ template<typename T> ParseNode ParseTree::addNode(Op type, const Selection& sel,
}

template<typename T> ParseNode ParseTree::addNode(Op type, const T& children) alloc_except {
if(children[0] == PARSE_ERROR || children.back() == PARSE_ERROR) return PARSE_ERROR;
return addNode<T>(type, Selection(getLeft(children[0]), getRight(children.back())), children);
}

Expand All @@ -257,10 +262,12 @@ ParseNode ParseTree::addUnary(Op type, size_t child) alloc_except {
}

ParseNode ParseTree::addLeftUnary(Op type, const Typeset::Marker& left, ParseNode child) alloc_except {
if(child == PARSE_ERROR) return PARSE_ERROR;
return addNode<1>(type, Selection(left, getRight(child)), {child});
}

ParseNode ParseTree::addRightUnary(Op type, const Typeset::Marker& right, ParseNode child) alloc_except {
if(child == PARSE_ERROR) return PARSE_ERROR;
return addNode<1>(type, Selection(getLeft(child), right), {child});
}

Expand Down Expand Up @@ -392,8 +399,14 @@ ParseNode ParseTree::finishNary(Op type, const Selection& sel) alloc_except {

ParseNode ParseTree::finishNary(Op type) alloc_except {
assert(!nary_start.empty());
return finishNary(type, Selection(
getLeft(nary_construction_stack[nary_start.back()]), getRight(nary_construction_stack.back())));
ParseNode left = nary_construction_stack[nary_start.back()];
ParseNode right = nary_construction_stack.back();
if(left == PARSE_ERROR || right == PARSE_ERROR){
cancelNary();
return PARSE_ERROR;
}else{
return finishNary(type, Selection(getLeft(left), getRight(right)));
}
}

void ParseTree::cancelNary() noexcept {
Expand Down
20 changes: 18 additions & 2 deletions src/forscape_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void Parser::reset() noexcept {
index = 0;
parsing_dims = false;
loops = 0;
error_node = model->errors.empty() ? NONE: parse_tree.addTerminal(model->errors.back().code, model->errors.back().selection);
error_node = model->errors.empty() ? NONE : PARSE_ERROR;
}

void Parser::registerGrouping(const Typeset::Selection& sel) alloc_except {
Expand Down Expand Up @@ -874,7 +874,7 @@ ParseNode Parser::collectImplicitMult(ParseNode n) alloc_except {
for(;;){
if(!noErrors()){
parse_tree.cancelNary();
return error_node;
return PARSE_ERROR;
}

switch (currentType()) {
Expand Down Expand Up @@ -1664,6 +1664,10 @@ ParseNode Parser::binomial() alloc_except{
}

ParseNode Parser::superscript(ParseNode lhs) alloc_except{
if(lhs == PARSE_ERROR){
while(!match(ARGCLOSE)) advance();
return PARSE_ERROR;
}
Typeset::Marker left = parse_tree.getLeft(lhs);
Typeset::Marker right = rMark();
Typeset::Selection c(left, right);
Expand Down Expand Up @@ -1724,6 +1728,11 @@ ParseNode Parser::superscript(ParseNode lhs) alloc_except{
}

ParseNode Parser::subscript(ParseNode lhs, const Typeset::Marker& right) alloc_except{
if(lhs == PARSE_ERROR){
while(!match(ARGCLOSE)) advance();
return PARSE_ERROR;
}

Typeset::Marker left = parse_tree.getLeft(lhs);
Typeset::Selection selection(left, right);
advance();
Expand Down Expand Up @@ -1751,6 +1760,11 @@ ParseNode Parser::subscript(ParseNode lhs, const Typeset::Marker& right) alloc_e
}

ParseNode Parser::dualscript(ParseNode lhs) alloc_except{
if(lhs == PARSE_ERROR){
while(!match(ARGCLOSE)) advance();
return PARSE_ERROR;
}

Typeset::Marker left = parse_tree.getLeft(lhs);
Typeset::Marker right = rMark();
Typeset::Selection c(left, right);
Expand Down Expand Up @@ -2153,6 +2167,8 @@ void Parser::recover() noexcept{

#ifndef FORSCAPE_TYPESET_HEADLESS
void Parser::registerParseNodeRegion(ParseNode pn, size_t token_index) alloc_except {
if(pn == PARSE_ERROR) return;

assert(token_index < tokens.size());
assert(parse_tree.isLastAllocatedNode(pn));

Expand Down
2 changes: 2 additions & 0 deletions src/forscape_symbol_lexical_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ void SymbolLexicalPass::resolveBody(
const std::string& name,
#endif
ParseNode pn) alloc_except {
if(pn == PARSE_ERROR) return;

increaseLexicalDepth(SCOPE_NAME(name) parse_tree.getLeft(pn));
resolveStmt(pn);
decreaseLexicalDepth(parse_tree.getRight(pn));
Expand Down

0 comments on commit c7a404a

Please sign in to comment.