Add langkit namespace

This commit is contained in:
Jason Turner 2009-06-13 22:19:30 +00:00
parent ce52cae45c
commit 416242286a
5 changed files with 509 additions and 495 deletions

View File

@ -10,8 +10,8 @@
template <typename Eval_Engine> template <typename Eval_Engine>
class ChaiScript_System { class ChaiScript_System {
Lexer lexer; langkit::Lexer lexer;
Rule parser; langkit::Rule parser;
Eval_Engine engine; Eval_Engine engine;
public: public:
@ -30,10 +30,10 @@ public:
val = Cast_Helper<std::string &>()(vals[0]); val = Cast_Helper<std::string &>()(vals[0]);
} }
catch (std::exception &e) { catch (std::exception &e) {
throw EvalError("Can not evaluate string: " + val, TokenPtr()); throw EvalError("Can not evaluate string: " + val, langkit::TokenPtr());
} }
catch (EvalError &ee) { catch (EvalError &ee) {
throw EvalError("Can not evaluate string: " + val + " reason: " + ee.reason, TokenPtr()); throw EvalError("Can not evaluate string: " + val + " reason: " + ee.reason, langkit::TokenPtr());
} }
return evaluate_string(val); return evaluate_string(val);
} }
@ -57,7 +57,8 @@ public:
return ret_val; return ret_val;
} }
void debug_print(TokenPtr token, std::string prepend) { void debug_print(langkit::TokenPtr token, std::string prepend) {
using namespace langkit;
std::cout << prepend << "Token: " << token->text << "(" << tokentype_to_string(token->identifier) << ") @ " << token->filename std::cout << prepend << "Token: " << token->text << "(" << tokentype_to_string(token->identifier) << ") @ " << token->filename
<< ": (" << token->start.line << ", " << token->start.column << ") to (" << ": (" << token->start.line << ", " << token->start.column << ") to ("
<< token->end.line << ", " << token->end.column << ") " << std::endl; << token->end.line << ", " << token->end.column << ") " << std::endl;
@ -67,13 +68,15 @@ public:
} }
} }
void debug_print(std::vector<TokenPtr> &tokens) { void debug_print(std::vector<langkit::TokenPtr> &tokens) {
using namespace langkit;
for (unsigned int i = 0; i < tokens.size(); ++i) { for (unsigned int i = 0; i < tokens.size(); ++i) {
debug_print(tokens[i], ""); debug_print(tokens[i], "");
} }
} }
Lexer build_lexer() { langkit::Lexer build_lexer() {
using namespace langkit;
Lexer lexer; Lexer lexer;
lexer.set_skip(Pattern("[ \\t]+", TokenType::Whitespace)); lexer.set_skip(Pattern("[ \\t]+", TokenType::Whitespace));
lexer.set_line_sep(Pattern("\\n|\\r\\n", TokenType::Carriage_Return)); lexer.set_line_sep(Pattern("\\n|\\r\\n", TokenType::Carriage_Return));
@ -98,7 +101,8 @@ public:
return lexer; return lexer;
} }
Rule build_parser_rules() { langkit::Rule build_parser_rules() {
using namespace langkit;
Rule params; Rule params;
Rule block(TokenType::Scoped_Block); Rule block(TokenType::Scoped_Block);
Rule fundef(TokenType::Function_Def); Rule fundef(TokenType::Function_Def);
@ -184,7 +188,8 @@ public:
} }
Eval_Engine build_eval_system(Lexer &lexer, Rule &parser) { Eval_Engine build_eval_system(langkit::Lexer &lexer, langkit::Rule &parser) {
using namespace langkit;
Eval_Engine ss; Eval_Engine ss;
Bootstrap::bootstrap(ss); Bootstrap::bootstrap(ss);
bootstrap_vector<std::vector<Boxed_Value> >(ss, "Vector"); bootstrap_vector<std::vector<Boxed_Value> >(ss, "Vector");
@ -197,7 +202,8 @@ public:
return ss; return ss;
} }
TokenPtr parse(Rule &rule, std::vector<TokenPtr> &tokens, const char *filename) { langkit::TokenPtr parse(langkit::Rule &rule, std::vector<langkit::TokenPtr> &tokens, const char *filename) {
using namespace langkit;
Token_Iterator iter = tokens.begin(), end = tokens.end(); Token_Iterator iter = tokens.begin(), end = tokens.end();
TokenPtr parent(new Token("Root", TokenType::File, filename)); TokenPtr parent(new Token("Root", TokenType::File, filename));
@ -215,6 +221,7 @@ public:
} }
Boxed_Value evaluate_string(const std::string &input, const char *filename = "__EVAL__") { Boxed_Value evaluate_string(const std::string &input, const char *filename = "__EVAL__") {
using namespace langkit;
std::vector<TokenPtr> tokens = lexer.lex(input, filename); std::vector<TokenPtr> tokens = lexer.lex(input, filename);
Boxed_Value value; Boxed_Value value;

View File

@ -8,33 +8,33 @@
struct ParserError { struct ParserError {
std::string reason; std::string reason;
TokenPtr location; langkit::TokenPtr location;
ParserError(const std::string &why, const TokenPtr where) : reason(why), location(where){ } ParserError(const std::string &why, const langkit::TokenPtr where) : reason(why), location(where){ }
}; };
struct EvalError { struct EvalError {
std::string reason; std::string reason;
TokenPtr location; langkit::TokenPtr location;
EvalError(const std::string &why, const TokenPtr where) : reason(why), location(where) { } EvalError(const std::string &why, const langkit::TokenPtr where) : reason(why), location(where) { }
}; };
struct ReturnValue { struct ReturnValue {
Boxed_Value retval; Boxed_Value retval;
TokenPtr location; langkit::TokenPtr location;
ReturnValue(const Boxed_Value &return_value, const TokenPtr where) : retval(return_value), location(where) { } ReturnValue(const Boxed_Value &return_value, const langkit::TokenPtr where) : retval(return_value), location(where) { }
}; };
struct BreakLoop { struct BreakLoop {
TokenPtr location; langkit::TokenPtr location;
BreakLoop(const TokenPtr where) : location(where) { } BreakLoop(const langkit::TokenPtr where) : location(where) { }
}; };
template <typename Eval_System> template <typename Eval_System>
const Boxed_Value eval_function (Eval_System &ss, TokenPtr node, const std::vector<std::string> &param_names, const std::vector<Boxed_Value> &vals) { const Boxed_Value eval_function (Eval_System &ss, langkit::TokenPtr node, const std::vector<std::string> &param_names, const std::vector<Boxed_Value> &vals) {
ss.new_scope(); ss.new_scope();
for (unsigned int i = 0; i < param_names.size(); ++i) { for (unsigned int i = 0; i < param_names.size(); ++i) {
@ -47,7 +47,7 @@ const Boxed_Value eval_function (Eval_System &ss, TokenPtr node, const std::vect
} }
template <typename Eval_System> template <typename Eval_System>
Boxed_Value eval_token(Eval_System &ss, TokenPtr node) { Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
Boxed_Value retval; Boxed_Value retval;
unsigned int i, j; unsigned int i, j;

View File

@ -8,6 +8,8 @@
#include <tr1/memory> #include <tr1/memory>
#include <string> #include <string>
namespace langkit
{
struct File_Position { struct File_Position {
int line; int line;
int column; int column;
@ -200,5 +202,5 @@ struct Lexer {
} }
}; };
}
#endif /* LANGKIT_LEXER_HPP_ */ #endif /* LANGKIT_LEXER_HPP_ */

View File

@ -8,6 +8,8 @@
#include "langkit_lexer.hpp" #include "langkit_lexer.hpp"
namespace langkit
{
struct RuleImpl; struct RuleImpl;
typedef std::vector<TokenPtr>::iterator Token_Iterator; typedef std::vector<TokenPtr>::iterator Token_Iterator;
@ -442,5 +444,6 @@ typedef Rule_Builder<Token_Iterator, Rule, &Wrap_Rule<Token_Iterator, Rule> > Wr
typedef Rule_Builder<Token_Iterator, Rule, &Ignore_Rule<Token_Iterator, Rule> > Ign; typedef Rule_Builder<Token_Iterator, Rule, &Ignore_Rule<Token_Iterator, Rule> > Ign;
typedef Rule_Builder<Token_Iterator, int, &Id_Rule<Token_Iterator> > Id; typedef Rule_Builder<Token_Iterator, int, &Id_Rule<Token_Iterator> > Id;
typedef Rule_Builder<Token_Iterator, const std::string&, &String_Rule<Token_Iterator> > Str; typedef Rule_Builder<Token_Iterator, const std::string&, &String_Rule<Token_Iterator> > Str;
}
#endif /* LANGKIT_PARSER_HPP_ */ #endif /* LANGKIT_PARSER_HPP_ */

View File

@ -24,7 +24,7 @@ const char *tokentype_to_string(int tokentype) {
return token_types[tokentype]; return token_types[tokentype];
} }
void debug_print(TokenPtr token, std::string prepend) { void debug_print(langkit::TokenPtr token, std::string prepend) {
std::cout << prepend << "Token: " << token->text << "(" << tokentype_to_string(token->identifier) << ") @ " << token->filename std::cout << prepend << "Token: " << token->text << "(" << tokentype_to_string(token->identifier) << ") @ " << token->filename
<< ": (" << token->start.line << ", " << token->start.column << ") to (" << ": (" << token->start.line << ", " << token->start.column << ") to ("
<< token->end.line << ", " << token->end.column << ") " << std::endl; << token->end.line << ", " << token->end.column << ") " << std::endl;
@ -34,7 +34,7 @@ void debug_print(TokenPtr token, std::string prepend) {
} }
} }
void debug_print(std::vector<TokenPtr> &tokens) { void debug_print(std::vector<langkit::TokenPtr> &tokens) {
for (unsigned int i = 0; i < tokens.size(); ++i) { for (unsigned int i = 0; i < tokens.size(); ++i) {
debug_print(tokens[i], ""); debug_print(tokens[i], "");
} }
@ -59,7 +59,8 @@ std::string load_file(const char *filename) {
return ret_val; return ret_val;
} }
void parse(std::vector<TokenPtr> &tokens, const char *filename) { void parse(std::vector<langkit::TokenPtr> &tokens, const char *filename) {
using namespace langkit;
/* /*
Rule lhs; Rule lhs;
@ -161,6 +162,7 @@ void parse(std::vector<TokenPtr> &tokens, const char *filename) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
using namespace langkit;
std::string input; std::string input;
Lexer lexer; Lexer lexer;