4
const ParseTree* ParseTree::
5
get_branch(const std::string& name) const
7
std::map<std::string,ParseTree>::const_iterator p=branches.find(name);
8
if(p==branches.end()) return 0;
9
else return &p->second;
12
bool ParseTree::remove_first_matching_branch( const std::string& name )
14
std::map<std::string,ParseTree>::iterator p=branches.find(name);
15
if(p==branches.end()) return false;
21
get_number(const std::string& name, double& result) const
23
std::map<std::string,double>::const_iterator p=numbers.find(name);
24
if(p==numbers.end()) return false;
30
get_int(const std::string& name, int& result) const
33
if(get_number(name, number)){
34
result=(int)std::floor(number);
41
get_string(const std::string& name, std::string& result) const
43
std::map<std::string,std::string>::const_iterator p=strings.find(name);
44
if(p==strings.end()) return false;
49
const Array1d* ParseTree::
50
get_vector(const std::string& name) const
52
std::map<std::string,Array1d>::const_iterator p=vectors.find(name);
53
if(p==vectors.end()) return 0;
54
else return &p->second;
58
get_vec2d(const std::string& name, Vec2d& v) const
60
std::map<std::string,Array1d>::const_iterator p=vectors.find(name);
61
if(p==vectors.end()) return false;
63
std::cerr<<"Error: looking for 2d vector ["<<name<<"] but got dimension "<<p->second.n<<std::endl;
72
get_vec3d(const std::string& name, Vec3d& v) const
74
std::map<std::string,Array1d>::const_iterator p=vectors.find(name);
75
if(p==vectors.end()) return false;
77
std::cerr<<"Error: looking for 3d vector ["<<name<<"] but got dimension "<<p->second.n<<std::endl;
88
bool parse_vector(Lexer& lexer, Array1d& v)
96
std::cerr<<"Parse error: looking for closing bracket but hit end-of-file"<<std::endl;
99
std::cerr<<"Lex error: cannot make sense of ["<<token.string_value<<"]"<<std::endl;
102
v.push_back(token.number_value);
104
case TOKEN_RIGHT_BRACKET:
107
std::cerr<<"Parse error: looking for a number or a closing bracket, got unexpected token "<<token<<std::endl;
113
bool recursive_parse(Lexer& lexer, ParseTree& tree, bool root_level)
122
std::cerr<<"Parse error: looking for closing parenthesis but hit end-of-file"<<std::endl;
127
std::cerr<<"Lex error: cannot make sense of ["<<token.string_value<<"]"<<std::endl;
129
case TOKEN_IDENTIFIER: case TOKEN_STRING:
130
name=token.string_value;
131
if(tree.branches.find(name)!=tree.branches.end()
132
|| tree.numbers.find(name)!=tree.numbers.end()
133
|| tree.strings.find(name)!=tree.strings.end()
134
|| tree.vectors.find(name)!=tree.vectors.end()){
135
std::cerr<<"Parse error: name ["<<name<<"] appears multiple times in record"<<std::endl;
139
case TOKEN_RIGHT_PAREN:
141
std::cerr<<"Parse error: hit closing parenthesis at root level"<<std::endl;
146
std::cerr<<"Parse error: looking for a name, got unexpected token "<<token<<std::endl;
149
// we've now got the name, let's read the value
153
std::cerr<<"Parse error: looking for a value for ["<<name<<"] but hit end-of-file"<<std::endl;
156
std::cerr<<"Lex error: cannot make sense of ["<<token.string_value<<"]"<<std::endl;
158
case TOKEN_IDENTIFIER: case TOKEN_STRING:
159
tree.strings.insert(std::make_pair(name, token.string_value));
162
tree.numbers.insert(std::make_pair(name, token.number_value));
164
case TOKEN_LEFT_PAREN:
167
if(!recursive_parse(lexer, subtree, false)) return false;
168
tree.branches.insert(std::make_pair(name, subtree));
171
case TOKEN_RIGHT_PAREN:
172
std::cerr<<"Parse error: looking for a value for ["<<name<<"] but hit closing parenthesis"<<std::endl;
174
case TOKEN_LEFT_BRACKET:
177
if(!parse_vector(lexer, v)) return false;
178
tree.vectors.insert(std::make_pair(name, v));
181
case TOKEN_RIGHT_BRACKET:
182
std::cerr<<"Parse error: looking for a value for ["<<name<<"] but hit closing bracket"<<std::endl;
188
} // unnamed namespace
191
bool parse_stream(std::istream& input, ParseTree& tree)
194
return recursive_parse(lexer, tree, true);