More refactoring of parser to use the token children types directly instead of using a giant switch statement during build_match.

This commit is contained in:
Jonathan Turner 2010-08-15 02:04:35 +00:00
parent 7f037b26d4
commit 58c62f6333
4 changed files with 150 additions and 255 deletions

View File

@ -16,11 +16,15 @@ namespace chaiscript
/**
* Types of AST nodes available to the parser and eval
*/
class Token_Type { public: enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Inplace_Fun_Call, Arg_List, Variable, Equation, Var_Decl,
Comparison, Additive, Multiplicative, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String,
Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Map_Pair, Value_Range,
Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or,
Logical_And, Logical_Or}; };
class Token_Type {
public:
enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Inplace_Fun_Call, Arg_List, Variable, Equation, Var_Decl,
Comparison, Additive, Multiplicative, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String,
Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Map_Pair, Value_Range,
Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or,
Logical_And, Logical_Or
};
};
namespace
{
@ -59,7 +63,7 @@ namespace chaiscript
struct Token {
std::string text;
int identifier;
const char *filename;
char *filename;
File_Position start, end;
bool is_cached;
Boxed_Value cached_value;
@ -67,7 +71,7 @@ namespace chaiscript
std::vector<TokenPtr> children;
TokenPtr annotation;
Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Token(const std::string &token_text, int id, char *fname, int start_line, int start_col, int end_line, int end_col) :
text(token_text), identifier(id), filename(fname), is_cached(false) {
start.line = start_line;
@ -75,7 +79,7 @@ namespace chaiscript
end.line = end_line;
end.column = end_col;
}
Token(const std::string &token_text, int id, const char *fname) :
Token(const std::string &token_text, int id, char *fname) :
text(token_text), identifier(id), filename(fname), is_cached(false) { }
@ -93,278 +97,278 @@ namespace chaiscript
struct Error_Token : public Token {
public:
Error_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Error_Token(const std::string &token_text = "", int id = Token_Type::Error, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
};
struct Int_Token : public Token {
public:
Int_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Int_Token(const std::string &token_text = "", int id = Token_Type::Int, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Float_Token : public Token {
public:
Float_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Float_Token(const std::string &token_text = "", int id = Token_Type::Float, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Id_Token : public Token {
public:
Id_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Id_Token(const std::string &token_text = "", int id = Token_Type::Id, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Char_Token : public Token {
public:
Char_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Char_Token(const std::string &token_text = "", int id = Token_Type::Char, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
};
struct Str_Token : public Token {
public:
Str_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Str_Token(const std::string &token_text = "", int id = Token_Type::Str, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
};
struct Eol_Token : public Token {
public:
Eol_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Eol_Token(const std::string &token_text = "", int id = Token_Type::Eol, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
};
struct Fun_Call_Token : public Token {
public:
Fun_Call_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Fun_Call_Token(const std::string &token_text = "", int id = Token_Type::Fun_Call, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Inplace_Fun_Call_Token : public Token {
public:
Inplace_Fun_Call_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Inplace_Fun_Call_Token(const std::string &token_text = "", int id = Token_Type::Inplace_Fun_Call, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Arg_List_Token : public Token {
public:
Arg_List_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Arg_List_Token(const std::string &token_text = "", int id = Token_Type::Arg_List, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
};
struct Variable_Token : public Token {
public:
Variable_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Variable_Token(const std::string &token_text = "", int id = Token_Type::Variable, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
};
struct Equation_Token : public Token {
public:
Equation_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Equation_Token(const std::string &token_text = "", int id = Token_Type::Equation, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Var_Decl_Token : public Token {
public:
Var_Decl_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Var_Decl_Token(const std::string &token_text = "", int id = Token_Type::Var_Decl, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Comparison_Token : public Token {
public:
Comparison_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Comparison_Token(const std::string &token_text = "", int id = Token_Type::Comparison, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Additive_Token : public Token {
public:
Additive_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Additive_Token(const std::string &token_text = "", int id = Token_Type::Additive, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Multiplicative_Token : public Token {
public:
Multiplicative_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Multiplicative_Token(const std::string &token_text = "", int id = Token_Type::Multiplicative, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Array_Call_Token : public Token {
public:
Array_Call_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Array_Call_Token(const std::string &token_text = "", int id = Token_Type::Array_Call, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Dot_Access_Token : public Token {
public:
Dot_Access_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Dot_Access_Token(const std::string &token_text = "", int id = Token_Type::Dot_Access, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Quoted_String_Token : public Token {
public:
Quoted_String_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Quoted_String_Token(const std::string &token_text = "", int id = Token_Type::Quoted_String, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Single_Quoted_String_Token : public Token {
public:
Single_Quoted_String_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Single_Quoted_String_Token(const std::string &token_text = "", int id = Token_Type::Single_Quoted_String, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Lambda_Token : public Token {
public:
Lambda_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Lambda_Token(const std::string &token_text = "", int id = Token_Type::Lambda, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Block_Token : public Token {
public:
Block_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Block_Token(const std::string &token_text = "", int id = Token_Type::Block, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Def_Token : public Token {
public:
Def_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Def_Token(const std::string &token_text = "", int id = Token_Type::Def, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct While_Token : public Token {
public:
While_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
While_Token(const std::string &token_text = "", int id = Token_Type::While, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct If_Token : public Token {
public:
If_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
If_Token(const std::string &token_text = "", int id = Token_Type::If, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct For_Token : public Token {
public:
For_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
For_Token(const std::string &token_text = "", int id = Token_Type::For, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Inline_Array_Token : public Token {
public:
Inline_Array_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Inline_Array_Token(const std::string &token_text = "", int id = Token_Type::Inline_Array, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Inline_Map_Token : public Token {
public:
Inline_Map_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Inline_Map_Token(const std::string &token_text = "", int id = Token_Type::Inline_Map, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Return_Token : public Token {
public:
Return_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Return_Token(const std::string &token_text = "", int id = Token_Type::Return, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct File_Token : public Token {
public:
File_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
File_Token(const std::string &token_text = "", int id = Token_Type::File, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Prefix_Token : public Token {
public:
Prefix_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Prefix_Token(const std::string &token_text = "", int id = Token_Type::Prefix, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Break_Token : public Token {
public:
Break_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Break_Token(const std::string &token_text = "", int id = Token_Type::Break, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Map_Pair_Token : public Token {
public:
Map_Pair_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Map_Pair_Token(const std::string &token_text = "", int id = Token_Type::Map_Pair, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
};
struct Value_Range_Token : public Token {
public:
Value_Range_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Value_Range_Token(const std::string &token_text = "", int id = Token_Type::Value_Range, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
};
struct Inline_Range_Token : public Token {
public:
Inline_Range_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Inline_Range_Token(const std::string &token_text = "", int id = Token_Type::Inline_Range, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Annotation_Token : public Token {
public:
Annotation_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Annotation_Token(const std::string &token_text = "", int id = Token_Type::Annotation, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
};
struct Try_Token : public Token {
public:
Try_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Try_Token(const std::string &token_text = "", int id = Token_Type::Try, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Catch_Token : public Token {
public:
Catch_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Catch_Token(const std::string &token_text = "", int id = Token_Type::Catch, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
};
struct Finally_Token : public Token {
public:
Finally_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Finally_Token(const std::string &token_text = "", int id = Token_Type::Finally, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
};
struct Method_Token : public Token {
public:
Method_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Method_Token(const std::string &token_text = "", int id = Token_Type::Method, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Attr_Decl_Token : public Token {
public:
Attr_Decl_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Attr_Decl_Token(const std::string &token_text = "", int id = Token_Type::Attr_Decl, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Shift_Token : public Token {
public:
Shift_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Shift_Token(const std::string &token_text = "", int id = Token_Type::Shift, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Equality_Token : public Token {
public:
Equality_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Equality_Token(const std::string &token_text = "", int id = Token_Type::Equality, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Bitwise_And_Token : public Token {
public:
Bitwise_And_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Bitwise_And_Token(const std::string &token_text = "", int id = Token_Type::Bitwise_And, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Bitwise_Xor_Token : public Token {
public:
Bitwise_Xor_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Bitwise_Xor_Token(const std::string &token_text = "", int id = Token_Type::Bitwise_Xor, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Bitwise_Or_Token : public Token {
public:
Bitwise_Or_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Bitwise_Or_Token(const std::string &token_text = "", int id = Token_Type::Bitwise_Or, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Logical_And_Token : public Token {
public:
Logical_And_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Logical_And_Token(const std::string &token_text = "", int id = Token_Type::Logical_And, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};
struct Logical_Or_Token : public Token {
public:
Logical_Or_Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
Logical_Or_Token(const std::string &token_text = "", int id = Token_Type::Logical_Or, char *fname = NULL, int start_line = 0, int start_col = 0, int end_line = 0, int end_col = 0) :
Token(token_text, id, fname, start_line, start_col, end_line, end_col) { }
Boxed_Value eval(Dispatch_Engine &ss);
};

View File

@ -258,7 +258,7 @@ namespace chaiscript
#endif
loaded_files.insert(filename);
try {
if (parser.parse(input, loaded_files.find(filename)->c_str())) {
if (parser.parse(input, (char *)loaded_files.find(filename)->c_str())) {
#ifndef CHAISCRIPT_NO_THREADS
l.unlock();
#endif
@ -274,7 +274,7 @@ namespace chaiscript
#ifndef CHAISCRIPT_NO_THREADS
boost::shared_lock<boost::shared_mutex> l(mutex);
#endif
const char *fname = loaded_files.find("__EVAL__")->c_str();
char *fname = (char *)loaded_files.find("__EVAL__")->c_str();
#ifndef CHAISCRIPT_NO_THREADS
l.unlock();
#endif

View File

@ -24,7 +24,7 @@ namespace chaiscript
int line, col;
std::string multiline_comment_begin, multiline_comment_end;
std::string singleline_comment;
const char *filename;
char *filename;
std::vector<TokenPtr> match_stack;
std::vector<std::vector<std::string> > operator_matches;
@ -136,7 +136,7 @@ namespace chaiscript
/**
* Helper function that collects tokens from a starting position to the top of the stack into a new AST node
*/
void build_match(Token_Type::Type match_type, int match_start) {
void build_match(TokenPtr t, int match_start) {
int pos_line_start, pos_col_start, pos_line_stop, pos_col_stop;
int is_deep = false;
@ -155,165 +155,20 @@ namespace chaiscript
pos_col_stop = col;
}
Token *t;
switch (match_type) {
case(Token_Type::Error) :
t = new Error_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Int) :
t = new Int_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Float) :
t = new Float_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Id) :
t = new Id_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Char) :
t = new Char_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Str) :
t = new Str_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Eol) :
t = new Eol_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Fun_Call) :
t = new Fun_Call_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Inplace_Fun_Call) :
t = new Inplace_Fun_Call_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Arg_List) :
t = new Arg_List_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Variable) :
t = new Variable_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Equation) :
t = new Equation_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Var_Decl) :
t = new Var_Decl_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Comparison) :
t = new Comparison_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Additive) :
t = new Additive_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Multiplicative) :
t = new Multiplicative_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Array_Call) :
t = new Array_Call_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Dot_Access) :
t = new Dot_Access_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Quoted_String) :
t = new Quoted_String_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Single_Quoted_String) :
t = new Single_Quoted_String_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Lambda) :
t = new Lambda_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Block) :
t = new Block_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Def) :
t = new Def_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::While) :
t = new While_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::If) :
t = new If_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::For) :
t = new For_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Inline_Array) :
t = new Inline_Array_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Inline_Map) :
t = new Inline_Map_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Return) :
t = new Return_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::File) :
t = new File_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Prefix) :
t = new Prefix_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Break) :
t = new Break_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Map_Pair) :
t = new Map_Pair_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Value_Range) :
t = new Value_Range_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Inline_Range) :
t = new Inline_Range_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Annotation) :
t = new Annotation_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Try) :
t = new Try_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Catch) :
t = new Catch_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Finally) :
t = new Finally_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Method) :
t = new Method_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Attr_Decl) :
t = new Attr_Decl_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Shift) :
t = new Shift_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Equality) :
t = new Equality_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Bitwise_And) :
t = new Bitwise_And_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Bitwise_Xor) :
t = new Bitwise_Xor_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Bitwise_Or) :
t = new Bitwise_Or_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Logical_And) :
t = new Logical_And_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
case(Token_Type::Logical_Or) :
t = new Logical_Or_Token("", match_type, filename, pos_line_start, pos_col_start, pos_line_stop, pos_col_stop);
break;
}
TokenPtr tp(t);
t->filename = filename;
t->start.line = pos_line_start;
t->start.column = pos_col_start;
t->end.line = pos_line_stop;
t->end.column = pos_col_stop;
if (is_deep) {
t->children.assign(match_stack.begin() + (match_start), match_stack.end());
match_stack.erase(match_stack.begin() + (match_start), match_stack.end());
match_stack.push_back(tp);
match_stack.push_back(t);
}
else {
//todo: fix the fact that a successful match that captured no tokens doesn't have any real start position
match_stack.push_back(tp);
match_stack.push_back(t);
}
}
@ -748,7 +603,7 @@ namespace chaiscript
TokenPtr t(new Quoted_String_Token(match, Token_Type::Quoted_String, filename, prev_line, prev_col, line, col));
match_stack.push_back(t);
build_match(Token_Type::Additive, prev_stack_top);
build_match(TokenPtr(new Additive_Token()), prev_stack_top);
}
else {
TokenPtr t(new Quoted_String_Token(match, Token_Type::Quoted_String, filename, prev_line, prev_col, line, col));
@ -787,15 +642,15 @@ namespace chaiscript
TokenPtr t(new Quoted_String_Token(eval_match, Token_Type::Quoted_String, filename, prev_line, prev_col, line, col));
match_stack.push_back(t);
build_match(Token_Type::Arg_List, arg_stack_top);
build_match(TokenPtr(new Arg_List_Token()), arg_stack_top);
build_match(Token_Type::Inplace_Fun_Call, ev_stack_top);
build_match(TokenPtr(new Inplace_Fun_Call_Token()), ev_stack_top);
build_match(Token_Type::Arg_List, ev_stack_top);
build_match(TokenPtr(new Arg_List_Token()), ev_stack_top);
build_match(Token_Type::Fun_Call, tostr_stack_top);
build_match(TokenPtr(new Fun_Call_Token()), tostr_stack_top);
build_match(Token_Type::Additive, prev_stack_top);
build_match(TokenPtr(new Additive_Token()), prev_stack_top);
}
else {
throw Eval_Error("Unclosed in-string eval", File_Position(prev_line, prev_col), filename);
@ -848,7 +703,7 @@ namespace chaiscript
TokenPtr t(new Quoted_String_Token(match, Token_Type::Quoted_String, filename, prev_line, prev_col, line, col));
match_stack.push_back(t);
build_match(Token_Type::Additive, prev_stack_top);
build_match(TokenPtr(new Additive_Token()), prev_stack_top);
}
else {
TokenPtr t(new Quoted_String_Token(match, Token_Type::Quoted_String, filename, prev_line, prev_col, line, col));
@ -1206,7 +1061,7 @@ namespace chaiscript
}
} while (retval && Char(','));
}
build_match(Token_Type::Arg_List, prev_stack_top);
build_match(TokenPtr(new Arg_List_Token()), prev_stack_top);
}
return retval;
@ -1222,7 +1077,7 @@ namespace chaiscript
if (Value_Range()) {
retval = true;
build_match(Token_Type::Arg_List, prev_stack_top);
build_match(TokenPtr(new Arg_List_Token()), prev_stack_top);
}
else if (Map_Pair()) {
retval = true;
@ -1235,7 +1090,7 @@ namespace chaiscript
}
} while (retval && Char(','));
}
build_match(Token_Type::Arg_List, prev_stack_top);
build_match(TokenPtr(new Arg_List_Token()), prev_stack_top);
}
return retval;
@ -1266,7 +1121,7 @@ namespace chaiscript
throw Eval_Error("Incomplete anonymous function", File_Position(line, col), filename);
}
build_match(Token_Type::Lambda, prev_stack_top);
build_match(TokenPtr(new Lambda_Token()), prev_stack_top);
}
return retval;
@ -1327,10 +1182,10 @@ namespace chaiscript
}
if (is_method) {
build_match(Token_Type::Method, prev_stack_top);
build_match(TokenPtr(new Method_Token()), prev_stack_top);
}
else {
build_match(Token_Type::Def, prev_stack_top);
build_match(TokenPtr(new Def_Token()), prev_stack_top);
}
if (is_annotated) {
@ -1380,7 +1235,7 @@ namespace chaiscript
if (!Block()) {
throw Eval_Error("Incomplete 'catch' block", File_Position(line, col), filename);
}
build_match(Token_Type::Catch, catch_stack_top);
build_match(TokenPtr(new Catch_Token()), catch_stack_top);
has_matches = true;
}
}
@ -1393,10 +1248,10 @@ namespace chaiscript
if (!Block()) {
throw Eval_Error("Incomplete 'finally' block", File_Position(line, col), filename);
}
build_match(Token_Type::Finally, finally_stack_top);
build_match(TokenPtr(new Finally_Token()), finally_stack_top);
}
build_match(Token_Type::Try, prev_stack_top);
build_match(TokenPtr(new Try_Token()), prev_stack_top);
}
return retval;
@ -1460,7 +1315,7 @@ namespace chaiscript
}
}
build_match(Token_Type::If, prev_stack_top);
build_match(TokenPtr(new If_Token()), prev_stack_top);
}
return retval;
@ -1491,7 +1346,7 @@ namespace chaiscript
throw Eval_Error("Incomplete 'while' block", File_Position(line, col), filename);
}
build_match(Token_Type::While, prev_stack_top);
build_match(TokenPtr(new While_Token()), prev_stack_top);
}
return retval;
@ -1536,7 +1391,7 @@ namespace chaiscript
throw Eval_Error("Incomplete 'for' block", File_Position(line, col), filename);
}
build_match(Token_Type::For, prev_stack_top);
build_match(TokenPtr(new For_Token()), prev_stack_top);
}
return retval;
@ -1558,7 +1413,7 @@ namespace chaiscript
throw Eval_Error("Incomplete block", File_Position(line, col), filename);
}
build_match(Token_Type::Block, prev_stack_top);
build_match(TokenPtr(new Block_Token()), prev_stack_top);
}
return retval;
@ -1576,7 +1431,7 @@ namespace chaiscript
retval = true;
Operator();
build_match(Token_Type::Return, prev_stack_top);
build_match(TokenPtr(new Return_Token()), prev_stack_top);
}
return retval;
@ -1593,7 +1448,7 @@ namespace chaiscript
if (Keyword("break")) {
retval = true;
build_match(Token_Type::Break, prev_stack_top);
build_match(TokenPtr(new Break_Token()), prev_stack_top);
}
return retval;
@ -1622,7 +1477,7 @@ namespace chaiscript
throw Eval_Error("Incomplete function call", File_Position(line, col), filename);
}
build_match(Token_Type::Fun_Call, prev_stack_top);
build_match(TokenPtr(new Fun_Call_Token()), prev_stack_top);
}
else if (Char('[')) {
has_more = true;
@ -1631,7 +1486,7 @@ namespace chaiscript
throw Eval_Error("Incomplete array access", File_Position(line, col), filename);
}
build_match(Token_Type::Array_Call, prev_stack_top);
build_match(TokenPtr(new Array_Call_Token()), prev_stack_top);
}
}
}
@ -1654,7 +1509,7 @@ namespace chaiscript
throw Eval_Error("Incomplete variable declaration", File_Position(line, col), filename);
}
build_match(Token_Type::Var_Decl, prev_stack_top);
build_match(TokenPtr(new Var_Decl_Token()), prev_stack_top);
}
else if (Keyword("attr")) {
retval = true;
@ -1670,7 +1525,7 @@ namespace chaiscript
}
build_match(Token_Type::Attr_Decl, prev_stack_top);
build_match(TokenPtr(new Attr_Decl_Token()), prev_stack_top);
}
return retval;
@ -1710,17 +1565,17 @@ namespace chaiscript
}
if ((prev_stack_top != match_stack.size()) && (match_stack.back()->children.size() > 0)) {
if (match_stack.back()->children[0]->identifier == Token_Type::Value_Range) {
build_match(Token_Type::Inline_Range, prev_stack_top);
build_match(TokenPtr(new Inline_Range_Token()), prev_stack_top);
}
else if (match_stack.back()->children[0]->identifier == Token_Type::Map_Pair) {
build_match(Token_Type::Inline_Map, prev_stack_top);
build_match(TokenPtr(new Inline_Map_Token()), prev_stack_top);
}
else {
build_match(Token_Type::Inline_Array, prev_stack_top);
build_match(TokenPtr(new Inline_Array_Token()), prev_stack_top);
}
}
else {
build_match(Token_Type::Inline_Array, prev_stack_top);
build_match(TokenPtr(new Inline_Array_Token()), prev_stack_top);
}
}
@ -1742,7 +1597,7 @@ namespace chaiscript
throw Eval_Error("Incomplete '++' expression", File_Position(line, col), filename);
}
build_match(Token_Type::Prefix, prev_stack_top);
build_match(TokenPtr(new Prefix_Token()), prev_stack_top);
}
else if (Symbol("--", true)) {
retval = true;
@ -1751,7 +1606,7 @@ namespace chaiscript
throw Eval_Error("Incomplete '--' expression", File_Position(line, col), filename);
}
build_match(Token_Type::Prefix, prev_stack_top);
build_match(TokenPtr(new Prefix_Token()), prev_stack_top);
}
else if (Char('-', true)) {
retval = true;
@ -1760,7 +1615,7 @@ namespace chaiscript
throw Eval_Error("Incomplete unary '-' expression", File_Position(line, col), filename);
}
build_match(Token_Type::Prefix, prev_stack_top);
build_match(TokenPtr(new Prefix_Token()), prev_stack_top);
}
else if (Char('+', true)) {
retval = true;
@ -1769,7 +1624,7 @@ namespace chaiscript
throw Eval_Error("Incomplete unary '+' expression", File_Position(line, col), filename);
}
build_match(Token_Type::Prefix, prev_stack_top);
build_match(TokenPtr(new Prefix_Token()), prev_stack_top);
}
else if (Char('!', true)) {
retval = true;
@ -1778,7 +1633,7 @@ namespace chaiscript
throw Eval_Error("Incomplete '!' expression", File_Position(line, col), filename);
}
build_match(Token_Type::Prefix, prev_stack_top);
build_match(TokenPtr(new Prefix_Token()), prev_stack_top);
}
else if (Char('~', true)) {
retval = true;
@ -1787,7 +1642,7 @@ namespace chaiscript
throw Eval_Error("Incomplete '~' expression", File_Position(line, col), filename);
}
build_match(Token_Type::Prefix, prev_stack_top);
build_match(TokenPtr(new Prefix_Token()), prev_stack_top);
}
return retval;
@ -1831,7 +1686,43 @@ namespace chaiscript
}
} while (Operator_Helper(precedence));
build_match(operators[precedence], prev_stack_top);
switch (operators[precedence]) {
case(Token_Type::Comparison) :
build_match(TokenPtr(new Comparison_Token()), prev_stack_top);
break;
case(Token_Type::Dot_Access) :
build_match(TokenPtr(new Dot_Access_Token()), prev_stack_top);
break;
case(Token_Type::Additive) :
build_match(TokenPtr(new Additive_Token()), prev_stack_top);
break;
case(Token_Type::Multiplicative) :
build_match(TokenPtr(new Multiplicative_Token()), prev_stack_top);
break;
case(Token_Type::Shift) :
build_match(TokenPtr(new Shift_Token()), prev_stack_top);
break;
case(Token_Type::Equality) :
build_match(TokenPtr(new Equality_Token()), prev_stack_top);
break;
case(Token_Type::Bitwise_And) :
build_match(TokenPtr(new Bitwise_And_Token()), prev_stack_top);
break;
case(Token_Type::Bitwise_Xor) :
build_match(TokenPtr(new Bitwise_Xor_Token()), prev_stack_top);
break;
case(Token_Type::Bitwise_Or) :
build_match(TokenPtr(new Bitwise_Or_Token()), prev_stack_top);
break;
case(Token_Type::Logical_And) :
build_match(TokenPtr(new Logical_And_Token()), prev_stack_top);
break;
case(Token_Type::Logical_Or) :
build_match(TokenPtr(new Logical_Or_Token()), prev_stack_top);
break;
default:
throw Eval_Error("Internal error: unhandled token", File_Position(line, col), filename);
}
}
}
}
@ -1859,7 +1750,7 @@ namespace chaiscript
}
} while (retval && Symbol(":"));
build_match(Token_Type::Map_Pair, prev_stack_top);
build_match(TokenPtr(new Map_Pair_Token()), prev_stack_top);
}
}
@ -1883,7 +1774,7 @@ namespace chaiscript
throw Eval_Error("Incomplete value range", File_Position(line, col), filename);
}
build_match(Token_Type::Value_Range, prev_stack_top);
build_match(TokenPtr(new Value_Range_Token()), prev_stack_top);
}
else {
input_pos = prev_pos;
@ -1915,7 +1806,7 @@ namespace chaiscript
throw Eval_Error("Incomplete equation", File_Position(line, col), filename);
}
build_match(Token_Type::Equation, prev_stack_top);
build_match(TokenPtr(new Equation_Token()), prev_stack_top);
}
}
@ -2020,7 +1911,7 @@ namespace chaiscript
/**
* Parses the given input string, tagging parsed tokens with the given filename.
*/
bool parse(std::string input, const char *fname) {
bool parse(std::string input, char *fname) {
input_pos = input.begin();
input_end = input.end();
line = 1; col = 1;
@ -2037,7 +1928,7 @@ namespace chaiscript
throw Eval_Error("Unparsed input", File_Position(line, col), fname);
}
else {
build_match(Token_Type::File, 0);
build_match(TokenPtr(new File_Token()), 0);
return true;
}
}

View File

@ -143,7 +143,7 @@ int main(int argc, char *argv[]) {
std::cout << ee.what();
if (ee.call_stack.size() > 0) {
std::cout << "during evaluation at (" << ee.call_stack[0]->filename << " " << ee.call_stack[0]->start.line << ", " << ee.call_stack[0]->start.column << ")";
for (int j = 1; j < ee.call_stack.size(); ++j) {
for (unsigned int j = 1; j < ee.call_stack.size(); ++j) {
std::cout << std::endl;
std::cout << " > " << ee.call_stack[j]->filename << " (" << ee.call_stack[j]->start.line << ", " << ee.call_stack[j]->start.column << ")";
}