Skip to content
Snippets Groups Projects
Commit 707ffa6a authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Overload ASTNode::string() and ASTNode::string_view() functions

This allows to retrieve associate string for nodes whose content has been
removed while rewriting the AST. For instance expressions such as 'x*x' can be
written.

Without this change, calls to 'string()' or 'string_view()' could produce
segmentation faults when reporting syntax errors... Now these calls can never
produce errors.
parent d0ce7b8b
No related branches found
No related tags found
1 merge request!37Feature/language
...@@ -16,13 +16,69 @@ using namespace TAO_PEGTL_NAMESPACE; ...@@ -16,13 +16,69 @@ using namespace TAO_PEGTL_NAMESPACE;
class SymbolTable; class SymbolTable;
struct ASTNode : public parse_tree::basic_node<ASTNode> class ASTNode : public parse_tree::basic_node<ASTNode>
{ {
private:
PUGS_INLINE
decltype(m_end)
_getEnd() const
{
if (not this->has_content()) {
if (this->children.size() > 0) {
return this->children[children.size() - 1]->_getEnd();
}
}
return m_end;
}
PUGS_INLINE
decltype(m_begin)
_getBegin() const
{
if (not this->has_content()) {
if (this->children.size() > 0) {
return this->children[0]->_getBegin();
}
}
return m_begin;
}
public:
std::shared_ptr<SymbolTable> m_symbol_table; std::shared_ptr<SymbolTable> m_symbol_table;
std::unique_ptr<INodeProcessor> m_node_processor; std::unique_ptr<INodeProcessor> m_node_processor;
ASTNodeDataType m_data_type{ASTNodeDataType::undefined_t}; ASTNodeDataType m_data_type{ASTNodeDataType::undefined_t};
[[nodiscard]] PUGS_INLINE std::string
string() const
{
if (this->has_content()) {
return this->parse_tree::basic_node<ASTNode>::string();
} else {
auto end = this->_getEnd();
auto begin = this->_getBegin();
if (end.data != nullptr) {
return std::string{begin.data, end.data};
}
return {"<optimized out>"};
}
}
[[nodiscard]] PUGS_INLINE std::string_view
string_view() const
{
if (this->has_content()) {
return this->parse_tree::basic_node<ASTNode>::string_view();
} else {
auto end = this->_getEnd();
auto begin = this->_getBegin();
if (end.data != nullptr) {
return std::string_view(begin.data, end.data - begin.data);
}
return {"<optimized out>"};
}
}
PUGS_INLINE PUGS_INLINE
std::string std::string
name() const noexcept name() const noexcept
......
...@@ -379,6 +379,19 @@ let f : R*Z -> B, (x,z) -> (3, x); ...@@ -379,6 +379,19 @@ let f : R*Z -> B, (x,z) -> (3, x);
REQUIRE_THROWS_WITH(ASTNodeDataTypeBuilder{*ast}, REQUIRE_THROWS_WITH(ASTNodeDataTypeBuilder{*ast},
"note: number of image spaces (1) B differs from number of expressions (2) (3, x)"); "note: number of image spaces (1) B differs from number of expressions (2) (3, x)");
} }
SECTION("wrong image size 2")
{
std::string_view data = R"(
let f : R -> R*R, x -> x*x*x;
)";
string_input input{data, "test.pgs"};
auto ast = ASTBuilder::build(input);
ASTSymbolTableBuilder{*ast};
REQUIRE_THROWS_WITH(ASTNodeDataTypeBuilder{*ast},
"note: number of image spaces (2) R*R differs from number of expressions (1) x*x*x");
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment