Fix Id Literals so that they are keyed off an Id search. This allows us to add operator overloading on the parse side.

This commit is contained in:
Jonathan Turner
2009-10-14 13:51:35 +00:00
parent c51d14fb13
commit 9827345213
3 changed files with 48 additions and 52 deletions

View File

@@ -278,7 +278,32 @@ namespace chaiscript
++col;
}
}
else if ((input_pos != input_end) && (*input_pos == '`')) {
retval = true;
++col;
++input_pos;
std::string::iterator start = input_pos;
while ((input_pos != input_end) && (*input_pos != '`')) {
if (Eol()) {
throw Eval_Error("Carriage return in identifier literal", File_Position(line, col), filename);
}
else {
++input_pos;
++col;
}
}
if (start == input_pos) {
throw Eval_Error("Missing contents of identifier literal", File_Position(line, col), filename);
}
else if (input_pos == input_end) {
throw Eval_Error("Incomplete identifier literal", File_Position(line, col), filename);
}
++col;
++input_pos;
}
return retval;
}
@@ -296,11 +321,20 @@ namespace chaiscript
int prev_col = col;
int prev_line = line;
if (Id_()) {
if (*start == '`') {
//Id Literal
std::string match(start+1, input_pos-1);
TokenPtr t(new Token(match, Token_Type::Id, filename, prev_line, prev_col, line, col));
match_stack.push_back(t);
return true;
}
else {
std::string match(start, input_pos);
TokenPtr t(new Token(match, Token_Type::Id, filename, prev_line, prev_col, line, col));
match_stack.push_back(t);
return true;
}
}
else {
return false;
}
@@ -1263,7 +1297,7 @@ namespace chaiscript
std::string::iterator prev_pos = input_pos;
unsigned int prev_stack_top = match_stack.size();
if (Id(true) || Id_Literal()) {
if (Id(true)) {
retval = true;
bool has_more = true;
@@ -1383,53 +1417,6 @@ namespace chaiscript
return retval;
}
/**
* Reads an identifier literal of the special form `<name>` from input
*/
bool Id_Literal() {
bool retval = false;
SkipWS();
if ((input_pos != input_end) && (*input_pos == '`')) {
retval = true;
int prev_col = col;
int prev_line = line;
++col;
++input_pos;
std::string::iterator start = input_pos;
while ((input_pos != input_end) && (*input_pos != '`')) {
if (Eol()) {
throw Eval_Error("Carriage return in identifier literal", File_Position(line, col), filename);
}
else {
++input_pos;
++col;
}
}
if (start == input_pos) {
throw Eval_Error("Missing contents of identifier literal", File_Position(line, col), filename);
}
else if (input_pos == input_end) {
throw Eval_Error("Incomplete identifier literal", File_Position(line, col), filename);
}
++col;
std::string match(start, input_pos);
TokenPtr t(new Token(match, Token_Type::Id, filename, prev_line, prev_col, line, col));
match_stack.push_back(t);
++input_pos;
}
return retval;
}
/**
* Reads a unary prefixed expression from input
*/

View File

@@ -0,0 +1,8 @@
def Bob::`+`(y) { this.x + y.x }
def Bob::Bob() { }
attr Bob::x
var b = Bob()
var c = Bob()
b.x = 4
c.x = 5
print(b+c)

View File

@@ -0,0 +1 @@
9