2008-08-31 17:00:39 +00:00
|
|
|
#ifndef MIRC_PARSER_H
|
|
|
|
#define MIRC_PARSER_H
|
|
|
|
|
2008-08-31 09:08:24 +00:00
|
|
|
/*
|
|
|
|
Mercenary
|
|
|
|
|
|
|
|
mIRC Scripting Language Parser
|
|
|
|
*/
|
|
|
|
|
|
|
|
//#define BOOST_SPIRIT_DEBUG
|
2009-04-23 15:29:37 +00:00
|
|
|
#include <string.h>
|
2008-08-31 09:08:24 +00:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/function.hpp>
|
|
|
|
#include <boost/spirit/core.hpp>
|
|
|
|
#include <boost/spirit/utility/confix.hpp>
|
|
|
|
#include <boost/spirit/dynamic/if.hpp>
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QStack>
|
2008-08-31 17:00:39 +00:00
|
|
|
|
2008-08-31 09:50:57 +00:00
|
|
|
class MIRCScriptManager;
|
2008-08-31 09:08:24 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace boost;
|
|
|
|
using namespace boost::spirit;
|
|
|
|
|
|
|
|
typedef QMap<QString, QString> mirc_variables;
|
|
|
|
|
|
|
|
struct mirc_alias {
|
|
|
|
mirc_alias(bool _global = true) {
|
|
|
|
global = _global;
|
|
|
|
}
|
|
|
|
bool global;
|
|
|
|
QString code;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef QMap<QString, mirc_alias> mirc_aliases;
|
|
|
|
|
|
|
|
enum mirc_engine_stage {
|
|
|
|
PARSE,
|
2009-04-22 14:46:41 +00:00
|
|
|
EXECUTE,
|
|
|
|
TERMINATED
|
2008-08-31 09:08:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class mirc_script_engine : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
private:
|
2008-08-31 09:50:57 +00:00
|
|
|
MIRCScriptManager *manager;
|
2008-08-31 09:08:24 +00:00
|
|
|
mirc_aliases::iterator current_alias;
|
|
|
|
mirc_variables::iterator current_variable;
|
2009-04-22 14:46:41 +00:00
|
|
|
QStringList current_value;
|
2008-08-31 09:08:24 +00:00
|
|
|
QStack<QStringList> stack;
|
2009-04-22 18:24:39 +00:00
|
|
|
QString _alias;
|
2009-04-17 02:45:22 +00:00
|
|
|
|
2008-08-31 09:08:24 +00:00
|
|
|
public:
|
|
|
|
mirc_engine_stage stage;
|
|
|
|
mirc_alias script;
|
|
|
|
mirc_aliases aliases;
|
|
|
|
mirc_variables vars;
|
|
|
|
|
2008-08-31 17:00:39 +00:00
|
|
|
mirc_script_engine(MIRCScriptManager *m);
|
2008-08-31 09:08:24 +00:00
|
|
|
|
2008-08-31 17:00:39 +00:00
|
|
|
void handle_alias_definition(char const* str, char const* end);
|
|
|
|
void handle_alias_definition_local(char const* str, char const* end);
|
|
|
|
void close_alias(char const*, char const*);
|
|
|
|
void store_code(char const* str, char const* end);
|
2009-04-22 18:24:39 +00:00
|
|
|
void set_alias(char const* str, char const* end);
|
2008-08-31 17:00:39 +00:00
|
|
|
void call_alias(char const*, char const*);
|
|
|
|
void return_alias(char const*, char const*);
|
|
|
|
void declare_variable(char const* str, char const* end);
|
|
|
|
void assign_variable(char const* str, char const* end);
|
|
|
|
void fetch_variable(char const*, char const*);
|
2009-04-22 14:46:41 +00:00
|
|
|
void append_value(char const* str, char const *end);
|
2008-08-31 17:00:39 +00:00
|
|
|
void append_expression(char const* str, char const *end);
|
2009-04-17 02:45:22 +00:00
|
|
|
void clear_stack(char const*, char const*);
|
2008-08-31 09:08:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mirc_script : public grammar<mirc_script> {
|
|
|
|
mirc_script_engine *actions;
|
|
|
|
|
|
|
|
mirc_script( mirc_script_engine *_actions ) {
|
|
|
|
actions = _actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ScannerT>
|
|
|
|
struct definition {
|
|
|
|
rule<ScannerT> script,
|
|
|
|
space,
|
2009-04-22 14:46:41 +00:00
|
|
|
nospace,
|
2008-08-31 09:08:24 +00:00
|
|
|
identifier,
|
|
|
|
string,
|
2009-04-22 14:46:41 +00:00
|
|
|
value,
|
2008-08-31 09:08:24 +00:00
|
|
|
expression,
|
|
|
|
expression_group,
|
|
|
|
parameters,
|
|
|
|
variable,
|
|
|
|
assignment,
|
|
|
|
alias_action,
|
|
|
|
alias_function,
|
|
|
|
alias_definition,
|
|
|
|
code_line,
|
|
|
|
code_block,
|
|
|
|
comment;
|
|
|
|
|
|
|
|
definition(mirc_script const &self) {
|
|
|
|
|
|
|
|
typedef function< void(const char*, const char*) > s_action;
|
|
|
|
s_action a_def ( bind( &mirc_script_engine::handle_alias_definition, self.actions, _1, _2 ) );
|
|
|
|
s_action l_def ( bind( &mirc_script_engine::handle_alias_definition_local, self.actions, _1, _2 ) );
|
|
|
|
s_action a_close ( bind( &mirc_script_engine::close_alias, self.actions, _1, _2 ) );
|
2009-04-22 18:24:39 +00:00
|
|
|
s_action a_set ( bind( &mirc_script_engine::set_alias, self.actions, _1, _2 ) );
|
2008-08-31 17:00:39 +00:00
|
|
|
s_action a_call ( bind( &mirc_script_engine::call_alias, self.actions, _1, _2 ) );
|
|
|
|
s_action a_return ( bind( &mirc_script_engine::return_alias, self.actions, _1, _2 ) );
|
2008-08-31 09:08:24 +00:00
|
|
|
s_action v_def ( bind( &mirc_script_engine::declare_variable, self.actions, _1, _2 ) );
|
|
|
|
s_action v_assign ( bind( &mirc_script_engine::assign_variable, self.actions, _1, _2 ) );
|
2008-08-31 17:00:39 +00:00
|
|
|
s_action v_fetch ( bind( &mirc_script_engine::fetch_variable, self.actions, _1, _2 ) );
|
2009-04-22 14:46:41 +00:00
|
|
|
s_action v_append ( bind( &mirc_script_engine::append_value, self.actions, _1, _2 ) );
|
2008-08-31 09:08:24 +00:00
|
|
|
s_action e_append ( bind( &mirc_script_engine::append_expression, self.actions, _1, _2 ) );
|
|
|
|
s_action s_code ( bind( &mirc_script_engine::store_code, self.actions, _1, _2 ) );
|
2009-04-17 02:45:22 +00:00
|
|
|
s_action c_stack ( bind( &mirc_script_engine::clear_stack, self.actions, _1, _2 ) );
|
2008-08-31 09:08:24 +00:00
|
|
|
|
|
|
|
script
|
|
|
|
= *(
|
|
|
|
alias_definition[a_close]
|
|
|
|
| code_block
|
|
|
|
) >> end_p
|
|
|
|
;
|
|
|
|
space
|
|
|
|
= ( blank_p
|
|
|
|
| str_p("$&") >> *blank_p >> eol_p
|
|
|
|
)
|
|
|
|
;
|
2009-04-22 14:46:41 +00:00
|
|
|
nospace
|
|
|
|
= +space >> str_p("$+") >> +space
|
|
|
|
;
|
2008-08-31 09:08:24 +00:00
|
|
|
identifier
|
|
|
|
= alpha_p >> *alnum_p
|
|
|
|
;
|
|
|
|
string
|
2009-04-22 14:46:41 +00:00
|
|
|
= (+(graph_p - ch_p(',') - ch_p('(') - ch_p(')')))
|
|
|
|
;
|
|
|
|
value
|
|
|
|
= ( variable[v_append][v_fetch]
|
2009-04-22 18:24:39 +00:00
|
|
|
| alias_function[v_append][a_return]
|
2009-04-22 14:46:41 +00:00
|
|
|
| string[v_append]
|
2008-08-31 17:00:39 +00:00
|
|
|
)
|
2008-08-31 09:08:24 +00:00
|
|
|
;
|
|
|
|
expression
|
2009-04-22 14:46:41 +00:00
|
|
|
= (*nospace >> value)[e_append]
|
|
|
|
>> *(+space >> (+(*nospace >> value))[e_append])
|
2008-08-31 09:08:24 +00:00
|
|
|
;
|
|
|
|
expression_group
|
2009-04-22 18:24:39 +00:00
|
|
|
= (*nospace >> *(*(ch_p(',') | ch_p('(') | ch_p(')'))[v_append] >> value))[e_append]
|
|
|
|
>> *(+space >> (+(*nospace >> *(*(ch_p(',') | ch_p('(') | ch_p(')'))[v_append] >> value))[e_append])[e_append])
|
2008-08-31 09:08:24 +00:00
|
|
|
;
|
|
|
|
parameters
|
|
|
|
= expression >> *(*space >> ch_p(',') >> *space >> expression)
|
|
|
|
;
|
|
|
|
variable
|
|
|
|
= ch_p('%') >> identifier
|
|
|
|
;
|
|
|
|
assignment
|
|
|
|
= !(str_p("var") >> *space)
|
|
|
|
>> variable[v_def]
|
|
|
|
>> *space >> ch_p('=') >> *space
|
|
|
|
>> expression[v_assign]
|
|
|
|
;
|
|
|
|
alias_action
|
|
|
|
= !ch_p('/') >> !ch_p('/')
|
2009-04-22 18:24:39 +00:00
|
|
|
>> (identifier[a_set] >> *(*space >> parameters))[a_call]
|
2008-08-31 09:08:24 +00:00
|
|
|
;
|
|
|
|
alias_function
|
2009-04-22 18:24:39 +00:00
|
|
|
= ch_p('$') >> identifier[a_set]
|
2008-08-31 09:08:24 +00:00
|
|
|
>> !(
|
|
|
|
ch_p('(') >> *space
|
|
|
|
>> parameters >> *space
|
|
|
|
>> ch_p(')')
|
|
|
|
)
|
|
|
|
;
|
|
|
|
alias_definition
|
|
|
|
= str_p("alias") >> *space
|
|
|
|
>> if_p(str_p("-l") >> *space)[identifier[l_def]].else_p[identifier[a_def]]
|
|
|
|
>> *space >> !eol_p
|
|
|
|
>> code_block
|
|
|
|
;
|
|
|
|
code_line
|
|
|
|
= *space
|
|
|
|
>> ( comment
|
|
|
|
| (
|
|
|
|
assignment /* Must come first to avoid "var" being caught as an action */
|
|
|
|
| alias_action
|
|
|
|
)[s_code]
|
2009-04-17 02:45:22 +00:00
|
|
|
)[c_stack]
|
2008-08-31 09:08:24 +00:00
|
|
|
>> !eol_p
|
|
|
|
;
|
|
|
|
code_block
|
|
|
|
= ( *space >> ch_p('{') >> *space >> !eol_p
|
|
|
|
>> (*code_line)
|
|
|
|
>> ch_p('}') >> *space >> !eol_p
|
|
|
|
)
|
|
|
|
| code_line
|
|
|
|
;
|
|
|
|
comment = comment_p(";");
|
|
|
|
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(script);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(space);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(identifier);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(string);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(expression);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(variable);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(assignment);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(alias_action);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(alias_function);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(alias_definition);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(code_line);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(code_block);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(comment);
|
|
|
|
BOOST_SPIRIT_DEBUG_NODE(*this);
|
|
|
|
}
|
|
|
|
rule<ScannerT> const& start() const { return script; }
|
|
|
|
};
|
|
|
|
};
|
2008-08-31 17:00:39 +00:00
|
|
|
|
|
|
|
#endif
|