Cleaned up the formatting a bit. Switched parser over to using charBetween, which significantly improves

readability of the early parsing rules.
This commit is contained in:
Jonathan Turner 2010-08-06 11:17:53 +00:00
parent 21253043d1
commit 556e7ad916
4 changed files with 3401 additions and 3383 deletions

View File

@ -519,7 +519,8 @@ namespace chaiscript
* Prints the contents of an AST node, including its children, recursively * Prints the contents of an AST node, including its children, recursively
*/ */
void debug_print(TokenPtr t, std::string prepend = "") { void debug_print(TokenPtr t, std::string prepend = "") {
std::cout << prepend << "(" << token_type_to_string(t->identifier) << ") " << t->text << " : " << t->start.line << ", " << t->start.column << std::endl; std::cout << prepend << "(" << token_type_to_string(t->identifier) << ") "
<< t->text << " : " << t->start.line << ", " << t->start.column << std::endl;
for (unsigned int j = 0; j < t->children.size(); ++j) { for (unsigned int j = 0; j < t->children.size(); ++j) {
debug_print(t->children[j], prepend + " "); debug_print(t->children[j], prepend + " ");
} }

View File

@ -895,7 +895,8 @@ namespace chaiscript
try { try {
if (function_name == class_name) { if (function_name == class_name) {
ss.add(Proxy_Function ss.add(Proxy_Function
(new Dynamic_Object_Constructor(class_name, Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, (new Dynamic_Object_Constructor(class_name, Proxy_Function
(new Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>,
boost::ref(ss), node->children.back(), boost::ref(ss), node->children.back(),
param_names, _1), numparams, param_names, _1), numparams,
annotation, guard)))), function_name); annotation, guard)))), function_name);
@ -909,7 +910,8 @@ namespace chaiscript
// No biggie, the type name is just not known // No biggie, the type name is just not known
} }
ss.add(Proxy_Function ss.add(Proxy_Function
(new Dynamic_Object_Function(class_name, Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, (new Dynamic_Object_Function(class_name, Proxy_Function
(new Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>,
boost::ref(ss), node->children.back(), boost::ref(ss), node->children.back(),
param_names, _1), numparams, param_names, _1), numparams,
annotation, guard)), ti)), function_name); annotation, guard)), ti)), function_name);
@ -944,9 +946,9 @@ namespace chaiscript
numparams = 0; numparams = 0;
} }
return Boxed_Value(Proxy_Function( return Boxed_Value(Proxy_Function(new Dynamic_Proxy_Function
new Dynamic_Proxy_Function( (boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1),
boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), numparams))); numparams)));
} }
/** /**

View File

@ -151,6 +151,18 @@ namespace chaiscript
} }
} }
/**
* Does ranged char check
*/
inline bool charBetween(char start, char end) {
if ((*input_pos >= start) && (*input_pos <= end)) {
return true;
}
else {
return false;
}
}
/** /**
* Skips any multi-line or single-line comment * Skips any multi-line or single-line comment
*/ */
@ -214,17 +226,17 @@ namespace chaiscript
bool retval = false; bool retval = false;
std::string::iterator start = input_pos; std::string::iterator start = input_pos;
if ((input_pos != input_end) && (((*input_pos >= '0') && (*input_pos <= '9')) || (*input_pos == '.'))) { if ((input_pos != input_end) && (charBetween('0', '9') || (*input_pos == '.'))) {
while ((input_pos != input_end) && (*input_pos >= '0') && (*input_pos <= '9')) { while ((input_pos != input_end) && charBetween('0', '9')) {
++input_pos; ++input_pos;
++col; ++col;
} }
if ((input_pos != input_end) && (*input_pos == '.')) { if ((input_pos != input_end) && (*input_pos == '.')) {
++input_pos; ++input_pos;
++col; ++col;
if ((input_pos != input_end) && (*input_pos >= '0') && (*input_pos <= '9')) { if ((input_pos != input_end) && charBetween('0', '9')) {
retval = true; retval = true;
while ((input_pos != input_end) && (*input_pos >= '0') && (*input_pos <= '9')) { while ((input_pos != input_end) && charBetween('0', '9')) {
++input_pos; ++input_pos;
++col; ++col;
} }
@ -250,13 +262,13 @@ namespace chaiscript
if ((input_pos != input_end) && ((*input_pos == 'x') || (*input_pos == 'X'))) { if ((input_pos != input_end) && ((*input_pos == 'x') || (*input_pos == 'X'))) {
++input_pos; ++input_pos;
++col; ++col;
if ((input_pos != input_end) && (((*input_pos >= '0') && (*input_pos <= '9')) || if ((input_pos != input_end) && (charBetween('0', '9') ||
((*input_pos >= 'a') && (*input_pos <= 'f')) || charBetween('a', 'f') ||
((*input_pos >= 'A') && (*input_pos <= 'F')))) { charBetween('A', 'F'))) {
retval = true; retval = true;
while ((input_pos != input_end) && (((*input_pos >= '0') && (*input_pos <= '9')) || while ((input_pos != input_end) && (charBetween('0', '9') ||
((*input_pos >= 'a') && (*input_pos <= 'f')) || charBetween('a', 'f') ||
((*input_pos >= 'A') && (*input_pos <= 'F')))) { charBetween('A', 'F'))) {
++input_pos; ++input_pos;
++col; ++col;
} }
@ -287,9 +299,9 @@ namespace chaiscript
if ((input_pos != input_end) && ((*input_pos == 'b') || (*input_pos == 'B'))) { if ((input_pos != input_end) && ((*input_pos == 'b') || (*input_pos == 'B'))) {
++input_pos; ++input_pos;
++col; ++col;
if ((input_pos != input_end) && ((*input_pos >= '0') && (*input_pos <= '1'))) { if ((input_pos != input_end) && charBetween('0', '1')) {
retval = true; retval = true;
while ((input_pos != input_end) && ((*input_pos >= '0') && (*input_pos <= '1'))) { while ((input_pos != input_end) && charBetween('0', '1')) {
++input_pos; ++input_pos;
++col; ++col;
} }
@ -321,7 +333,7 @@ namespace chaiscript
std::string::iterator start = input_pos; std::string::iterator start = input_pos;
int prev_col = col; int prev_col = col;
int prev_line = line; int prev_line = line;
if ((input_pos != input_end) && (((*input_pos >= '0') && (*input_pos <= '9')) || (*input_pos == '.')) ) { if ((input_pos != input_end) && (charBetween('0', '9') || (*input_pos == '.')) ) {
if (Hex_()) { if (Hex_()) {
std::string match(start, input_pos); std::string match(start, input_pos);
std::stringstream ss(match); std::stringstream ss(match);
@ -389,10 +401,10 @@ namespace chaiscript
*/ */
bool Id_() { bool Id_() {
bool retval = false; bool retval = false;
if ((input_pos != input_end) && (((*input_pos >= 'A') && (*input_pos <= 'Z')) || (*input_pos == '_') || ((*input_pos >= 'a') && (*input_pos <= 'z')))) { if ((input_pos != input_end) && (charBetween('A', 'Z') || (*input_pos == '_') || charBetween('a', 'z'))) {
retval = true; retval = true;
while ((input_pos != input_end) && (((*input_pos >= 'A') && (*input_pos <= 'Z')) || (*input_pos == '_') || ((*input_pos >= 'a') && (*input_pos <= 'z')) while ((input_pos != input_end) && (charBetween('A', 'Z') || (*input_pos == '_') ||
|| ((*input_pos >= '0') && (*input_pos <= '9')))) { charBetween('a', 'z') || charBetween('0', '9'))) {
++input_pos; ++input_pos;
++col; ++col;
} }
@ -843,8 +855,8 @@ namespace chaiscript
bool retval = Keyword_(s); bool retval = Keyword_(s);
if (retval) { if (retval) {
//todo: fix this. Hacky workaround for preventing substring matches //todo: fix this. Hacky workaround for preventing substring matches
if ((input_pos != input_end) && (((*input_pos >= 'A') && (*input_pos <= 'Z')) || (*input_pos == '_') || ((*input_pos >= 'a') && (*input_pos <= 'z')) if ((input_pos != input_end) && (charBetween('A', 'Z') || (*input_pos == '_') || charBetween('a', 'z')
|| ((*input_pos >= '0') && (*input_pos <= '9')))) { || charBetween('0', '9'))) {
input_pos = start; input_pos = start;
col = prev_col; col = prev_col;
line = prev_line; line = prev_line;
@ -862,8 +874,8 @@ namespace chaiscript
int prev_line = line; int prev_line = line;
if (Keyword_(s)) { if (Keyword_(s)) {
//todo: fix this. Hacky workaround for preventing substring matches //todo: fix this. Hacky workaround for preventing substring matches
if ((input_pos != input_end) && (((*input_pos >= 'A') && (*input_pos <= 'Z')) || (*input_pos == '_') || ((*input_pos >= 'a') && (*input_pos <= 'z')) if ((input_pos != input_end) && (charBetween('A', 'Z') || (*input_pos == '_') ||
|| ((*input_pos >= '0') && (*input_pos <= '9')))) { charBetween('a', 'z') || charBetween('0', '9'))) {
input_pos = start; input_pos = start;
col = prev_col; col = prev_col;
line = prev_line; line = prev_line;
@ -916,8 +928,10 @@ namespace chaiscript
bool retval = Symbol_(s); bool retval = Symbol_(s);
if (retval) { if (retval) {
//todo: fix this. Hacky workaround for preventing substring matches //todo: fix this. Hacky workaround for preventing substring matches
if ((input_pos != input_end) && (disallow_prevention == false) && ((*input_pos == '+') || (*input_pos == '-') || (*input_pos == '*') || (*input_pos == '/') if ((input_pos != input_end) && (disallow_prevention == false) &&
|| (*input_pos == '|') || (*input_pos == '&') || (*input_pos == '^') || (*input_pos == '=') || (*input_pos == '.') || (*input_pos == '<') || (*input_pos == '>'))) { ((*input_pos == '+') || (*input_pos == '-') || (*input_pos == '*') || (*input_pos == '/') ||
(*input_pos == '|') || (*input_pos == '&') || (*input_pos == '^') || (*input_pos == '=') ||
(*input_pos == '.') || (*input_pos == '<') || (*input_pos == '>'))) {
input_pos = start; input_pos = start;
col = prev_col; col = prev_col;
line = prev_line; line = prev_line;
@ -935,8 +949,10 @@ namespace chaiscript
int prev_line = line; int prev_line = line;
if (Symbol_(s)) { if (Symbol_(s)) {
//todo: fix this. Hacky workaround for preventing substring matches //todo: fix this. Hacky workaround for preventing substring matches
if ((input_pos != input_end) && (disallow_prevention == false) && ((*input_pos == '+') || (*input_pos == '-') || (*input_pos == '*') || (*input_pos == '/') if ((input_pos != input_end) && (disallow_prevention == false) &&
|| (*input_pos == '|') || (*input_pos == '&') || (*input_pos == '^') || (*input_pos == '=') || (*input_pos == '.') || (*input_pos == '<') || (*input_pos == '>'))) { ((*input_pos == '+') || (*input_pos == '-') || (*input_pos == '*') || (*input_pos == '/') ||
(*input_pos == '|') || (*input_pos == '&') || (*input_pos == '^') || (*input_pos == '=') ||
(*input_pos == '.') || (*input_pos == '<') || (*input_pos == '>'))) {
input_pos = start; input_pos = start;
col = prev_col; col = prev_col;
line = prev_line; line = prev_line;
@ -1858,5 +1874,4 @@ namespace chaiscript
}; };
} }
#endif /* CHAISCRIPT_PARSER_HPP_ */ #endif /* CHAISCRIPT_PARSER_HPP_ */