Renaming of helper function char_between. Adding help to check for hitting the end of input.

This commit is contained in:
Jonathan Turner
2010-08-07 02:18:51 +00:00
parent 556e7ad916
commit d838f7a6d4

View File

@@ -145,7 +145,7 @@ namespace chaiscript
match_stack.push_back(t); match_stack.push_back(t);
} }
else { else {
//todo: fix the fact that a successful match that captured no tokens does't have any real start position //todo: fix the fact that a successful match that captured no tokens doesn't have any real start position
TokenPtr t(new Token("", match_type, filename, line, col, line, col)); TokenPtr t(new Token("", match_type, filename, line, col, line, col));
match_stack.push_back(t); match_stack.push_back(t);
} }
@@ -154,7 +154,7 @@ namespace chaiscript
/** /**
* Does ranged char check * Does ranged char check
*/ */
inline bool charBetween(char start, char end) { inline bool char_between(char start, char end) {
if ((*input_pos >= start) && (*input_pos <= end)) { if ((*input_pos >= start) && (*input_pos <= end)) {
return true; return true;
} }
@@ -162,7 +162,14 @@ namespace chaiscript
return false; return false;
} }
} }
/**
* Check to see if there is more text parse
*/
inline bool has_more_input() {
return (input_pos != input_end);
}
/** /**
* Skips any multi-line or single-line comment * Skips any multi-line or single-line comment
*/ */
@@ -203,7 +210,7 @@ namespace chaiscript
*/ */
bool SkipWS() { bool SkipWS() {
bool retval = false; bool retval = false;
while (input_pos != input_end) { while (has_more_input()) {
if ((*input_pos == ' ') || (*input_pos == '\t')) { if ((*input_pos == ' ') || (*input_pos == '\t')) {
++input_pos; ++input_pos;
++col; ++col;
@@ -226,17 +233,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) && (charBetween('0', '9') || (*input_pos == '.'))) { if (has_more_input() && (char_between('0', '9') || (*input_pos == '.'))) {
while ((input_pos != input_end) && charBetween('0', '9')) { while (has_more_input() && char_between('0', '9')) {
++input_pos; ++input_pos;
++col; ++col;
} }
if ((input_pos != input_end) && (*input_pos == '.')) { if (has_more_input() && (*input_pos == '.')) {
++input_pos; ++input_pos;
++col; ++col;
if ((input_pos != input_end) && charBetween('0', '9')) { if (has_more_input() && char_between('0', '9')) {
retval = true; retval = true;
while ((input_pos != input_end) && charBetween('0', '9')) { while (has_more_input() && char_between('0', '9')) {
++input_pos; ++input_pos;
++col; ++col;
} }
@@ -255,20 +262,20 @@ namespace chaiscript
*/ */
bool Hex_() { bool Hex_() {
bool retval = false; bool retval = false;
if ((input_pos != input_end) && (*input_pos == '0')) { if (has_more_input() && (*input_pos == '0')) {
++input_pos; ++input_pos;
++col; ++col;
if ((input_pos != input_end) && ((*input_pos == 'x') || (*input_pos == 'X'))) { if (has_more_input() && ((*input_pos == 'x') || (*input_pos == 'X'))) {
++input_pos; ++input_pos;
++col; ++col;
if ((input_pos != input_end) && (charBetween('0', '9') || if (has_more_input() && (char_between('0', '9') ||
charBetween('a', 'f') || char_between('a', 'f') ||
charBetween('A', 'F'))) { char_between('A', 'F'))) {
retval = true; retval = true;
while ((input_pos != input_end) && (charBetween('0', '9') || while (has_more_input() && (char_between('0', '9') ||
charBetween('a', 'f') || char_between('a', 'f') ||
charBetween('A', 'F'))) { char_between('A', 'F'))) {
++input_pos; ++input_pos;
++col; ++col;
} }
@@ -292,16 +299,16 @@ namespace chaiscript
*/ */
bool Binary_() { bool Binary_() {
bool retval = false; bool retval = false;
if ((input_pos != input_end) && (*input_pos == '0')) { if (has_more_input() && (*input_pos == '0')) {
++input_pos; ++input_pos;
++col; ++col;
if ((input_pos != input_end) && ((*input_pos == 'b') || (*input_pos == 'B'))) { if (has_more_input() && ((*input_pos == 'b') || (*input_pos == 'B'))) {
++input_pos; ++input_pos;
++col; ++col;
if ((input_pos != input_end) && charBetween('0', '1')) { if (has_more_input() && char_between('0', '1')) {
retval = true; retval = true;
while ((input_pos != input_end) && charBetween('0', '1')) { while (has_more_input() && char_between('0', '1')) {
++input_pos; ++input_pos;
++col; ++col;
} }
@@ -333,7 +340,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) && (charBetween('0', '9') || (*input_pos == '.')) ) { if (has_more_input() && (char_between('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);
@@ -401,21 +408,21 @@ namespace chaiscript
*/ */
bool Id_() { bool Id_() {
bool retval = false; bool retval = false;
if ((input_pos != input_end) && (charBetween('A', 'Z') || (*input_pos == '_') || charBetween('a', 'z'))) { if (has_more_input() && (char_between('A', 'Z') || (*input_pos == '_') || char_between('a', 'z'))) {
retval = true; retval = true;
while ((input_pos != input_end) && (charBetween('A', 'Z') || (*input_pos == '_') || while (has_more_input() && (char_between('A', 'Z') || (*input_pos == '_') ||
charBetween('a', 'z') || charBetween('0', '9'))) { char_between('a', 'z') || char_between('0', '9'))) {
++input_pos; ++input_pos;
++col; ++col;
} }
} }
else if ((input_pos != input_end) && (*input_pos == '`')) { else if (has_more_input() && (*input_pos == '`')) {
retval = true; retval = true;
++col; ++col;
++input_pos; ++input_pos;
std::string::iterator start = input_pos; std::string::iterator start = input_pos;
while ((input_pos != input_end) && (*input_pos != '`')) { while (has_more_input() && (*input_pos != '`')) {
if (Eol()) { if (Eol()) {
throw Eval_Error("Carriage return in identifier literal", File_Position(line, col), filename); throw Eval_Error("Carriage return in identifier literal", File_Position(line, col), filename);
} }
@@ -509,13 +516,13 @@ namespace chaiscript
bool Quoted_String_() { bool Quoted_String_() {
bool retval = false; bool retval = false;
char prev_char = 0; char prev_char = 0;
if ((input_pos != input_end) && (*input_pos == '\"')) { if (has_more_input() && (*input_pos == '\"')) {
retval = true; retval = true;
prev_char = *input_pos; prev_char = *input_pos;
++input_pos; ++input_pos;
++col; ++col;
while ((input_pos != input_end) && ((*input_pos != '\"') || ((*input_pos == '\"') && (prev_char == '\\')))) { while (has_more_input() && ((*input_pos != '\"') || ((*input_pos == '\"') && (prev_char == '\\')))) {
if (!Eol_()) { if (!Eol_()) {
if (prev_char == '\\') { if (prev_char == '\\') {
prev_char = 0; prev_char = 0;
@@ -528,7 +535,7 @@ namespace chaiscript
} }
} }
if (input_pos != input_end) { if (has_more_input()) {
++input_pos; ++input_pos;
++col; ++col;
} }
@@ -695,13 +702,13 @@ namespace chaiscript
bool Single_Quoted_String_() { bool Single_Quoted_String_() {
bool retval = false; bool retval = false;
char prev_char = 0; char prev_char = 0;
if ((input_pos != input_end) && (*input_pos == '\'')) { if (has_more_input() && (*input_pos == '\'')) {
retval = true; retval = true;
prev_char = *input_pos; prev_char = *input_pos;
++input_pos; ++input_pos;
++col; ++col;
while ((input_pos != input_end) && ((*input_pos != '\'') || ((*input_pos == '\'') && (prev_char == '\\')))) { while (has_more_input() && ((*input_pos != '\'') || ((*input_pos == '\'') && (prev_char == '\\')))) {
if (!Eol_()) { if (!Eol_()) {
if (prev_char == '\\') { if (prev_char == '\\') {
prev_char = 0; prev_char = 0;
@@ -785,7 +792,7 @@ namespace chaiscript
*/ */
bool Char_(char c) { bool Char_(char c) {
bool retval = false; bool retval = false;
if ((input_pos != input_end) && (*input_pos == c)) { if (has_more_input() && (*input_pos == c)) {
++input_pos; ++input_pos;
++col; ++col;
retval = true; retval = true;
@@ -855,8 +862,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) && (charBetween('A', 'Z') || (*input_pos == '_') || charBetween('a', 'z') if (has_more_input() && (char_between('A', 'Z') || (*input_pos == '_') ||
|| charBetween('0', '9'))) { char_between('a', 'z') || char_between('0', '9'))) {
input_pos = start; input_pos = start;
col = prev_col; col = prev_col;
line = prev_line; line = prev_line;
@@ -874,8 +881,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) && (charBetween('A', 'Z') || (*input_pos == '_') || if (has_more_input() && (char_between('A', 'Z') || (*input_pos == '_') ||
charBetween('a', 'z') || charBetween('0', '9'))) { char_between('a', 'z') || char_between('0', '9'))) {
input_pos = start; input_pos = start;
col = prev_col; col = prev_col;
line = prev_line; line = prev_line;
@@ -928,7 +935,7 @@ 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) && if (has_more_input() && (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 == '>'))) { (*input_pos == '.') || (*input_pos == '<') || (*input_pos == '>'))) {
@@ -949,7 +956,7 @@ 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) && if (has_more_input() && (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 == '>'))) { (*input_pos == '.') || (*input_pos == '<') || (*input_pos == '>'))) {
@@ -977,12 +984,12 @@ namespace chaiscript
bool Eol_() { bool Eol_() {
bool retval = false; bool retval = false;
if ((input_pos != input_end) && (Symbol_("\r\n") || Char_('\n'))) { if (has_more_input() && (Symbol_("\r\n") || Char_('\n'))) {
retval = true; retval = true;
++line; ++line;
col = 1; col = 1;
} }
else if ((input_pos != input_end) && Char_(';')) { else if (has_more_input() && Char_(';')) {
retval = true; retval = true;
} }