Add langkit namespace
This commit is contained in:
parent
ce52cae45c
commit
416242286a
@ -10,8 +10,8 @@
|
||||
|
||||
template <typename Eval_Engine>
|
||||
class ChaiScript_System {
|
||||
Lexer lexer;
|
||||
Rule parser;
|
||||
langkit::Lexer lexer;
|
||||
langkit::Rule parser;
|
||||
Eval_Engine engine;
|
||||
|
||||
public:
|
||||
@ -30,10 +30,10 @@ public:
|
||||
val = Cast_Helper<std::string &>()(vals[0]);
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
throw EvalError("Can not evaluate string: " + val, TokenPtr());
|
||||
throw EvalError("Can not evaluate string: " + val, langkit::TokenPtr());
|
||||
}
|
||||
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);
|
||||
}
|
||||
@ -57,7 +57,8 @@ public:
|
||||
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
|
||||
<< ": (" << token->start.line << ", " << token->start.column << ") to ("
|
||||
<< 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) {
|
||||
debug_print(tokens[i], "");
|
||||
}
|
||||
}
|
||||
|
||||
Lexer build_lexer() {
|
||||
langkit::Lexer build_lexer() {
|
||||
using namespace langkit;
|
||||
Lexer lexer;
|
||||
lexer.set_skip(Pattern("[ \\t]+", TokenType::Whitespace));
|
||||
lexer.set_line_sep(Pattern("\\n|\\r\\n", TokenType::Carriage_Return));
|
||||
@ -98,7 +101,8 @@ public:
|
||||
return lexer;
|
||||
}
|
||||
|
||||
Rule build_parser_rules() {
|
||||
langkit::Rule build_parser_rules() {
|
||||
using namespace langkit;
|
||||
Rule params;
|
||||
Rule block(TokenType::Scoped_Block);
|
||||
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;
|
||||
Bootstrap::bootstrap(ss);
|
||||
bootstrap_vector<std::vector<Boxed_Value> >(ss, "Vector");
|
||||
@ -197,7 +202,8 @@ public:
|
||||
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();
|
||||
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__") {
|
||||
using namespace langkit;
|
||||
std::vector<TokenPtr> tokens = lexer.lex(input, filename);
|
||||
Boxed_Value value;
|
||||
|
||||
|
@ -8,33 +8,33 @@
|
||||
|
||||
struct ParserError {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
TokenPtr location;
|
||||
langkit::TokenPtr location;
|
||||
|
||||
BreakLoop(const TokenPtr where) : location(where) { }
|
||||
BreakLoop(const langkit::TokenPtr where) : location(where) { }
|
||||
};
|
||||
|
||||
template <typename Eval_System>
|
||||
const Boxed_Value eval_function (Eval_System &ss, TokenPtr node, const std::vector<std::string> ¶m_names, const std::vector<Boxed_Value> &vals) {
|
||||
const Boxed_Value eval_function (Eval_System &ss, langkit::TokenPtr node, const std::vector<std::string> ¶m_names, const std::vector<Boxed_Value> &vals) {
|
||||
ss.new_scope();
|
||||
|
||||
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>
|
||||
Boxed_Value eval_token(Eval_System &ss, TokenPtr node) {
|
||||
Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
unsigned int i, j;
|
||||
|
||||
|
@ -8,7 +8,9 @@
|
||||
#include <tr1/memory>
|
||||
#include <string>
|
||||
|
||||
struct File_Position {
|
||||
namespace langkit
|
||||
{
|
||||
struct File_Position {
|
||||
int line;
|
||||
int column;
|
||||
|
||||
@ -16,19 +18,19 @@ struct File_Position {
|
||||
: line(file_line), column(file_column) { }
|
||||
|
||||
File_Position() : line(0), column(0) { }
|
||||
};
|
||||
};
|
||||
|
||||
struct Pattern {
|
||||
struct Pattern {
|
||||
boost::regex regex;
|
||||
int identifier;
|
||||
|
||||
Pattern() { }
|
||||
Pattern(const std::string ®exp, int id) : regex(regexp), identifier(id) { }
|
||||
};
|
||||
};
|
||||
|
||||
typedef std::tr1::shared_ptr<struct Token> TokenPtr;
|
||||
typedef std::tr1::shared_ptr<struct Token> TokenPtr;
|
||||
|
||||
struct Token {
|
||||
struct Token {
|
||||
std::string text;
|
||||
int identifier;
|
||||
const char *filename;
|
||||
@ -37,9 +39,9 @@ struct Token {
|
||||
std::vector<TokenPtr> children;
|
||||
|
||||
Token(const std::string &token_text, int id, const char *fname) : text(token_text), identifier(id), filename(fname) { }
|
||||
};
|
||||
};
|
||||
|
||||
struct Lexer {
|
||||
struct Lexer {
|
||||
std::vector<Pattern> lex_patterns;
|
||||
Pattern skip_pattern;
|
||||
Pattern command_sep_pattern;
|
||||
@ -198,7 +200,7 @@ struct Lexer {
|
||||
void set_singleline_comment(const Pattern &p) {
|
||||
singleline_comment_pattern = p;
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
#endif /* LANGKIT_LEXER_HPP_ */
|
||||
|
@ -8,13 +8,15 @@
|
||||
|
||||
#include "langkit_lexer.hpp"
|
||||
|
||||
struct RuleImpl;
|
||||
namespace langkit
|
||||
{
|
||||
struct RuleImpl;
|
||||
|
||||
typedef std::vector<TokenPtr>::iterator Token_Iterator;
|
||||
typedef boost::function<std::pair<Token_Iterator, bool>(Token_Iterator, Token_Iterator, TokenPtr, bool, int)> RuleFun;
|
||||
typedef std::tr1::shared_ptr<RuleImpl> RuleImplPtr;
|
||||
typedef std::vector<TokenPtr>::iterator Token_Iterator;
|
||||
typedef boost::function<std::pair<Token_Iterator, bool>(Token_Iterator, Token_Iterator, TokenPtr, bool, int)> RuleFun;
|
||||
typedef std::tr1::shared_ptr<RuleImpl> RuleImplPtr;
|
||||
|
||||
struct RuleImpl {
|
||||
struct RuleImpl {
|
||||
RuleFun rule;
|
||||
bool keep;
|
||||
int new_id;
|
||||
@ -27,12 +29,12 @@ struct RuleImpl {
|
||||
std::pair<Token_Iterator, bool> operator()(Token_Iterator iter, Token_Iterator end, TokenPtr parent) {
|
||||
return rule(iter, end, parent, keep, new_id);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
//struct Rule;
|
||||
//struct Rule;
|
||||
|
||||
template <typename T_Iter>
|
||||
std::pair<T_Iter, bool> String_Rule(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, const std::string &val) {
|
||||
template <typename T_Iter>
|
||||
std::pair<T_Iter, bool> String_Rule(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, const std::string &val) {
|
||||
if (iter != end) {
|
||||
if ((*iter)->text == val) {
|
||||
if (keep) {
|
||||
@ -43,10 +45,10 @@ std::pair<T_Iter, bool> String_Rule(T_Iter iter, T_Iter end, TokenPtr parent, bo
|
||||
}
|
||||
|
||||
return std::pair<T_Iter, bool>(iter, false);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T_Iter>
|
||||
std::pair<T_Iter, bool> Id_Rule(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, const int val) {
|
||||
template <typename T_Iter>
|
||||
std::pair<T_Iter, bool> Id_Rule(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, const int val) {
|
||||
if (iter != end) {
|
||||
if ((*iter)->identifier == val) {
|
||||
if (keep) {
|
||||
@ -57,10 +59,10 @@ std::pair<T_Iter, bool> Id_Rule(T_Iter iter, T_Iter end, TokenPtr parent, bool k
|
||||
}
|
||||
|
||||
return std::pair<T_Iter, bool>(iter, false);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Or_Rule(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, R_Type lhs, R_Type rhs) {
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Or_Rule(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, R_Type lhs, R_Type rhs) {
|
||||
T_Iter new_iter;
|
||||
unsigned int prev_size;
|
||||
TokenPtr prev_parent = parent;
|
||||
@ -120,10 +122,10 @@ std::pair<T_Iter, bool> Or_Rule(T_Iter iter, T_Iter end, TokenPtr parent, bool k
|
||||
}
|
||||
|
||||
return std::pair<T_Iter, bool>(iter, false);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<Token_Iterator, bool> And_Rule(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, R_Type lhs, R_Type rhs) {
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<Token_Iterator, bool> And_Rule(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, R_Type lhs, R_Type rhs) {
|
||||
T_Iter lhs_iter, rhs_iter;
|
||||
unsigned int prev_size;
|
||||
TokenPtr prev_parent = parent;
|
||||
@ -164,10 +166,10 @@ std::pair<Token_Iterator, bool> And_Rule(T_Iter iter, T_Iter end, TokenPtr paren
|
||||
}
|
||||
|
||||
return std::pair<T_Iter, bool>(iter, false);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Kleene_Rule
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Kleene_Rule
|
||||
(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, R_Type rule) {
|
||||
|
||||
TokenPtr prev_parent = parent;
|
||||
@ -203,10 +205,10 @@ std::pair<T_Iter, bool> Kleene_Rule
|
||||
else {
|
||||
return std::pair<T_Iter, bool>(iter, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Plus_Rule
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Plus_Rule
|
||||
(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, R_Type rule) {
|
||||
|
||||
unsigned int prev_size;
|
||||
@ -255,10 +257,10 @@ std::pair<T_Iter, bool> Plus_Rule
|
||||
}
|
||||
|
||||
return std::pair<T_Iter, bool>(iter, false);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Optional_Rule
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Optional_Rule
|
||||
(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, R_Type rule) {
|
||||
|
||||
TokenPtr prev_parent = parent;
|
||||
@ -294,10 +296,10 @@ std::pair<T_Iter, bool> Optional_Rule
|
||||
else {
|
||||
return std::pair<T_Iter, bool>(iter, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Epsilon_Rule
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Epsilon_Rule
|
||||
(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, R_Type rule) {
|
||||
|
||||
TokenPtr prev_parent = parent;
|
||||
@ -327,10 +329,10 @@ std::pair<T_Iter, bool> Epsilon_Rule
|
||||
}
|
||||
|
||||
return std::pair<T_Iter, bool>(iter, result.second);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Wrap_Rule
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Wrap_Rule
|
||||
(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, R_Type rule) {
|
||||
|
||||
TokenPtr prev_parent = parent;
|
||||
@ -360,18 +362,18 @@ std::pair<T_Iter, bool> Wrap_Rule
|
||||
}
|
||||
|
||||
return std::pair<T_Iter, bool>(result.first, result.second);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Ignore_Rule
|
||||
template <typename T_Iter, typename R_Type>
|
||||
std::pair<T_Iter, bool> Ignore_Rule
|
||||
(T_Iter iter, T_Iter end, TokenPtr parent, bool keep, int new_id, R_Type rule) {
|
||||
|
||||
rule.impl->keep = false;
|
||||
|
||||
return rule(iter, end, parent);
|
||||
}
|
||||
}
|
||||
|
||||
struct Rule {
|
||||
struct Rule {
|
||||
RuleImplPtr impl;
|
||||
|
||||
Rule() : impl(new RuleImpl()) {}
|
||||
@ -391,33 +393,33 @@ struct Rule {
|
||||
return *this;
|
||||
}
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
inline Rule operator>>(const Rule &lhs, const Rule &rhs) {
|
||||
inline Rule operator>>(const Rule &lhs, const Rule &rhs) {
|
||||
return Rule(boost::bind(And_Rule<Token_Iterator, Rule>, _1, _2, _3, _4, _5, lhs, rhs));
|
||||
}
|
||||
}
|
||||
|
||||
inline Rule operator|(const Rule &lhs, const Rule &rhs) {
|
||||
inline Rule operator|(const Rule &lhs, const Rule &rhs) {
|
||||
return Rule(boost::bind(Or_Rule<Token_Iterator, Rule>, _1, _2, _3, _4, _5, lhs, rhs));
|
||||
}
|
||||
}
|
||||
|
||||
inline Rule operator*(const Rule &operand) {
|
||||
inline Rule operator*(const Rule &operand) {
|
||||
return Rule(boost::bind(Kleene_Rule<Token_Iterator, Rule>, _1, _2, _3, _4, _5, operand));
|
||||
}
|
||||
}
|
||||
|
||||
inline Rule operator+(const Rule &operand) {
|
||||
inline Rule operator+(const Rule &operand) {
|
||||
return Rule(boost::bind(Plus_Rule<Token_Iterator, Rule>, _1, _2, _3, _4, _5, operand));
|
||||
}
|
||||
}
|
||||
|
||||
inline Rule operator~(const Rule &operand) {
|
||||
inline Rule operator~(const Rule &operand) {
|
||||
return Rule(boost::bind(Optional_Rule<Token_Iterator, Rule>, _1, _2, _3, _4, _5, operand));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename ItrType, typename ParamType,
|
||||
template<typename ItrType, typename ParamType,
|
||||
std::pair<ItrType,bool> (*Function)(ItrType, ItrType, TokenPtr, bool, int, ParamType)>
|
||||
struct Rule_Builder
|
||||
{
|
||||
struct Rule_Builder
|
||||
{
|
||||
Rule_Builder(ParamType p, bool t_keep = true)
|
||||
: m_p(p), m_keep(t_keep)
|
||||
{
|
||||
@ -434,13 +436,14 @@ struct Rule_Builder
|
||||
|
||||
ParamType m_p;
|
||||
bool m_keep;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
typedef Rule_Builder<Token_Iterator, Rule, &Epsilon_Rule<Token_Iterator, Rule> > Epsilon;
|
||||
typedef Rule_Builder<Token_Iterator, Rule, &Wrap_Rule<Token_Iterator, Rule> > Wrap;
|
||||
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, const std::string&, &String_Rule<Token_Iterator> > Str;
|
||||
typedef Rule_Builder<Token_Iterator, Rule, &Epsilon_Rule<Token_Iterator, Rule> > Epsilon;
|
||||
typedef Rule_Builder<Token_Iterator, Rule, &Wrap_Rule<Token_Iterator, Rule> > Wrap;
|
||||
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, const std::string&, &String_Rule<Token_Iterator> > Str;
|
||||
}
|
||||
|
||||
#endif /* LANGKIT_PARSER_HPP_ */
|
||||
|
@ -24,7 +24,7 @@ const char *tokentype_to_string(int 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
|
||||
<< ": (" << token->start.line << ", " << token->start.column << ") to ("
|
||||
<< 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) {
|
||||
debug_print(tokens[i], "");
|
||||
}
|
||||
@ -59,7 +59,8 @@ std::string load_file(const char *filename) {
|
||||
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;
|
||||
@ -161,6 +162,7 @@ void parse(std::vector<TokenPtr> &tokens, const char *filename) {
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
using namespace langkit;
|
||||
std::string input;
|
||||
|
||||
Lexer lexer;
|
||||
|
Loading…
x
Reference in New Issue
Block a user