Add hex value parsing support

This commit is contained in:
Jonathan Turner 2009-10-06 16:04:05 +00:00
parent edd274ccce
commit 46fd7e9a58

View File

@ -9,6 +9,7 @@
#include <exception> #include <exception>
#include <fstream> #include <fstream>
#include <sstream>
#include "chaiscript_prelude.hpp" #include "chaiscript_prelude.hpp"
@ -143,6 +144,10 @@ namespace chaiscript
*/ */
bool Float_() { bool Float_() {
bool retval = false; bool retval = false;
std::string::iterator start = input_pos;
int prev_col = col;
int prev_line = line;
if ((input_pos != input_end) && (((*input_pos >= '0') && (*input_pos <= '9')) || (*input_pos == '.'))) { if ((input_pos != input_end) && (((*input_pos >= '0') && (*input_pos <= '9')) || (*input_pos == '.'))) {
while ((input_pos != input_end) && (*input_pos >= '0') && (*input_pos <= '9')) { while ((input_pos != input_end) && (*input_pos >= '0') && (*input_pos <= '9')) {
++input_pos; ++input_pos;
@ -164,10 +169,47 @@ namespace chaiscript
} }
} }
} }
return retval;
}
/**
* Reads a floating point value from input, without skipping initial whitespace
*/
bool Hex_() {
bool retval = false;
if ((input_pos != input_end) && (*input_pos == '0')) {
++input_pos;
++col;
if ((input_pos != input_end) && ((*input_pos == 'x') || (*input_pos == 'X'))) {
++input_pos;
++col;
if ((input_pos != input_end) && (((*input_pos >= '0') && (*input_pos <= '9')) ||
((*input_pos >= 'a') && (*input_pos <= 'f')) ||
((*input_pos >= 'A') && (*input_pos <= 'F')))) {
retval = true;
while ((input_pos != input_end) && (((*input_pos >= '0') && (*input_pos <= '9')) ||
((*input_pos >= 'a') && (*input_pos <= 'f')) ||
((*input_pos >= 'A') && (*input_pos <= 'F')))) {
++input_pos;
++col;
}
}
else {
--input_pos;
--col;
}
}
else {
--input_pos;
--col;
}
}
return retval; return retval;
} }
/** /**
* Reads a number from the input, detecting if it's an integer or floating point * Reads a number from the input, detecting if it's an integer or floating point
*/ */
@ -175,13 +217,25 @@ namespace chaiscript
SkipWS(); SkipWS();
if (!capture) { if (!capture) {
return Float_(); return Hex_() || Float_();
} }
else { else {
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) && (((*input_pos >= '0') && (*input_pos <= '9')) || (*input_pos == '.')) ) {
if (Hex_()) {
std::string match(start, input_pos);
std::stringstream ss(match);
int temp_int;
ss >> std::hex >> temp_int;
std::ostringstream out_int;
out_int << temp_int;
TokenPtr t(new Token(out_int.str(), Token_Type::Int, filename, prev_line, prev_col, line, col));
match_stack.push_back(t);
return true;
}
if (Float_()) { if (Float_()) {
std::string match(start, input_pos); std::string match(start, input_pos);
TokenPtr t(new Token(match, Token_Type::Float, filename, prev_line, prev_col, line, col)); TokenPtr t(new Token(match, Token_Type::Float, filename, prev_line, prev_col, line, col));