Skip to content

Commit

Permalink
Improved csg compatibility and error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
arnholm committed Aug 31, 2020
1 parent 4dc9441 commit e9e45b7
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 30 deletions.
2 changes: 1 addition & 1 deletion csg_parser/csg_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void csg_node::parse_params()

// extract value, parse it and assign it to parameter map
std::string value_str = par_value(params,ieq);
std::shared_ptr<csg_value> value = csg_value::parse(value_str);
std::shared_ptr<csg_value> value = csg_value::parse(value_str,m_line_no);
if(value.get()) m_par[name] = value;

// truncate the parameter list from left
Expand Down
2 changes: 1 addition & 1 deletion csg_parser/csg_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class csg_node {

private:
int m_level; // level of this node in tree (root has level -1)
size_t m_line_no; // csg file no
size_t m_line_no; // csg file line no
std::string m_func; // openscad function signature
par_map m_par; // parsed parameter list
std::vector<std::shared_ptr<csg_node>> m_children; // child nodes
Expand Down
19 changes: 16 additions & 3 deletions csg_parser/csg_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,27 @@ void csg_parser::init_func(const std::string& csg)
size_t level = 0;
size_t line_no = 1;

for(size_t i=0; i<csg.size(); i++) {
size_t i=0;
while(i<csg.size()) {

// get current character
char c = csg[i];
char c = csg[i++];

// Skip // comment line
while(c=='/' && csg[i]=='/') {
while(csg[++i]!='\n') c = csg[i];
c = csg[++i];
line_no++;
}

// update the tree level
if( c=='{') ++level;
else if( c=='}') --level;

if( c=='\n') line_no++;
if( c=='\n') {
line_no++;
token.clear();
}

// characters not contributing to token
if( c==' ' || c=='\t' || c=='\n' || c==';' || c=='{' || c=='}' || c=='#' || c=='%') continue;
Expand All @@ -63,11 +74,13 @@ void csg_parser::init_func(const std::string& csg)
func.push_back(func_data(token,std::make_pair(level,line_no)));
token.clear();
}

}

// build the tree
size_t index = 0;
m_root->build_tree(func,index);
// m_root->dump();
}

bool csg_parser::to_xcsg(cf_xmlTree& tree)
Expand Down
6 changes: 4 additions & 2 deletions csg_parser/csg_scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
#include <boost/lexical_cast.hpp>

csg_scalar::csg_scalar()
: csg_value(0)
{}

csg_scalar::csg_scalar(const std::string& value)
:m_value(value)
csg_scalar::csg_scalar(const std::string& value,size_t line_no)
: csg_value(line_no)
, m_value(value)
{}

csg_scalar::~csg_scalar()
Expand Down
2 changes: 1 addition & 1 deletion csg_parser/csg_scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class csg_scalar : public csg_value {
public:
csg_scalar();
csg_scalar(const std::string& value);
csg_scalar(const std::string& value,size_t line_no);
virtual ~csg_scalar();

size_t size() const { return 1; }
Expand Down
27 changes: 14 additions & 13 deletions csg_parser/csg_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,39 @@
#include "csg_vector.h"
#include <stdexcept>

csg_value::csg_value()
csg_value::csg_value(size_t line_no)
: m_line_no(line_no)
{}

csg_value::~csg_value()
{}

std::string csg_value::to_string() const
{
throw std::runtime_error("csg_value::to_string(), value is not a scalar");
throw std::runtime_error(".csg file line " + std::to_string(m_line_no) +", csg_value::to_string(), value is not a scalar");
}

bool csg_value::to_bool() const
{
throw std::runtime_error("csg_value::to_bool(), value is not a scalar");
throw std::runtime_error(".csg file line " + std::to_string(m_line_no) +", csg_value::to_bool(), value is not a scalar");
}

int csg_value::to_int() const
{
throw std::runtime_error("csg_value::to_int(), value is not a scalar");
throw std::runtime_error(".csg file line " + std::to_string(m_line_no) +", csg_value::to_int(), value is not a scalar");
}

double csg_value::to_double() const
{
throw std::runtime_error("csg_value::to_double(), value is not a scalar");
throw std::runtime_error(".csg file line " + std::to_string(m_line_no) +", csg_value::to_double(), value is not a scalar");
}

std::shared_ptr<csg_value> csg_value::get(size_t i) const
{
throw std::runtime_error("csg_value::get(), value is not a vector");
throw std::runtime_error(".csg file line " + std::to_string(m_line_no) +", csg_value::get(), value is not a vector");
}

std::shared_ptr<csg_value> csg_value::parse(const std::string& value_str)
std::shared_ptr<csg_value> csg_value::parse(const std::string& value_str, size_t line_no)
{
std::shared_ptr<csg_value> value;

Expand All @@ -68,18 +69,18 @@ std::shared_ptr<csg_value> csg_value::parse(const std::string& value_str)

// strip the [] pair from both sides
// so we end up with a string of comma separated values (which may be vectors)
return parse_vector(value_str.substr(i+1,i2-1));
return parse_vector(value_str.substr(i+1,i2-1),line_no);
}
else {
// found a scalar
return std::make_shared<csg_scalar>(value_str);
return std::make_shared<csg_scalar>(value_str,line_no);
}
}

return value;
}

std::shared_ptr<csg_value> csg_value::parse_vector(const std::string& values)
std::shared_ptr<csg_value> csg_value::parse_vector(const std::string& values, size_t line_no)
{
std::vector<std::shared_ptr<csg_value>> vec;
std::string token;
Expand All @@ -96,7 +97,7 @@ std::shared_ptr<csg_value> csg_value::parse_vector(const std::string& values)
// end of vector data
size_t len = token.size();
std::string vals = token.substr(1,len-2);
vec.push_back(parse_vector(vals));
vec.push_back(parse_vector(vals,line_no));
token = "";
icount = 0;

Expand All @@ -107,12 +108,12 @@ std::shared_ptr<csg_value> csg_value::parse_vector(const std::string& values)
// end of scalar data
size_t len = (c == ',')? token.size()-1 : token.size();
std::string val = token.substr(0,len);
vec.push_back(std::make_shared<csg_scalar>(val));
vec.push_back(std::make_shared<csg_scalar>(val,line_no));
token = "";
icount = 0;
}
}
return std::make_shared<csg_vector>(vec);
return std::make_shared<csg_vector>(vec,line_no);
}


11 changes: 8 additions & 3 deletions csg_parser/csg_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
// abstract parameter value for csg node
class csg_value {
public:
csg_value();
csg_value(size_t line_no);
virtual ~csg_value();

// convert a value string into a csg_value, possibly a scalar or (nested) vector
static std::shared_ptr<csg_value> parse(const std::string& value_str);
static std::shared_ptr<csg_value> parse_vector(const std::string& value_str);
static std::shared_ptr<csg_value> parse(const std::string& value_str, size_t line_no);
static std::shared_ptr<csg_value> parse_vector(const std::string& value_str, size_t line_no);

virtual bool is_vector() const { return false; }

Expand All @@ -42,6 +42,11 @@ class csg_value {
virtual bool to_bool() const;
virtual int to_int() const;
virtual double to_double() const;

size_t line_no() const { return m_line_no; }

private:
size_t m_line_no; // csg file line no
};

#endif // CSG_VALUE_H
11 changes: 7 additions & 4 deletions csg_parser/csg_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@
#include <stdexcept>

csg_vector::csg_vector()
: csg_value(0)
{}

csg_vector::csg_vector(const std::vector<std::string>& vec)
csg_vector::csg_vector(const std::vector<std::string>& vec,size_t line_no)
: csg_value(line_no)
{
for(size_t i=0; i<vec.size(); i++) {
m_vector.push_back(std::make_shared<csg_scalar>(vec[i]));
m_vector.push_back(std::make_shared<csg_scalar>(vec[i],line_no));
}
}

csg_vector::csg_vector(const std::vector<std::shared_ptr<csg_value>>& vec)
: m_vector(vec)
csg_vector::csg_vector(const std::vector<std::shared_ptr<csg_value>>& vec,size_t line_no)
: csg_value(line_no)
, m_vector(vec)
{}

csg_vector::~csg_vector()
Expand Down
4 changes: 2 additions & 2 deletions csg_parser/csg_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
class csg_vector : public csg_value {
public:
csg_vector();
csg_vector(const std::vector<std::string>& vec);
csg_vector(const std::vector<std::shared_ptr<csg_value>>& vec);
csg_vector(const std::vector<std::string>& vec,size_t line_no);
csg_vector(const std::vector<std::shared_ptr<csg_value>>& vec,size_t line_no);
virtual ~csg_vector();

virtual bool is_vector() const { return true; }
Expand Down

0 comments on commit e9e45b7

Please sign in to comment.