// Custom includes
#include <vector>
-#include <boost/regex.hpp>
#include <boost/spirit.hpp>
#include <boost/spirit/utility/grammar_def.hpp>
-#include <boost/spirit/actor.hpp>
-#include <boost/bind.hpp>
-#include <boost/function.hpp>
-#include <boost/ref.hpp>
+#include <boost/spirit/dynamic.hpp>
+#include <boost/spirit/phoenix.hpp>
+#include "../Utils/Phoenix.hh"
///////////////////////////////ih.p////////////////////////////////////////
namespace console {
namespace detail {
- struct append_action
- {
- template <class T, class Value>
- void act(T & ref, Value const & value) const
- { ref += T(1, value); }
-
- template <class T, class Iterator>
- void act(T & ref, Iterator const & f, Iterator const & l) const
- { ref += T(f,l); }
- };
+#ifndef DOXYGEN
- template <class T>
- inline boost::spirit::ref_value_actor<T, append_action>
- append_a(T & ref)
- {
- return boost::spirit::ref_value_actor<T, append_action>(ref);
- }
-
- template <class T, class Value>
- inline boost::spirit::ref_const_ref_actor<T, Value, append_action>
- append_a(T & ref, Value const & value)
- {
- return boost::spirit::ref_const_ref_actor<T, Value, append_action>(ref, value);
- }
+ ///////////////////////////////////////////////////////////////////////////
+ // Grammar
template <class ParseDispatcher>
struct CommandGrammar : boost::spirit::grammar<CommandGrammar<ParseDispatcher> >
///////////////////////////////////////////////////////////////////////////
// Start rules
- enum { CommandParser, SkipParser };
+ enum { CommandParser, SkipParser, ArgumentsParser };
///////////////////////////////////////////////////////////////////////////
// The parse context (variables needed while parsing)
+ typedef Token::TokenType TokenType;
+
struct Context {
std::string str;
- std::vector<std::string> path;
+ std::vector<Token> path;
char ch;
+ Token token;
};
Context & context;
///////////////////////////////////////////////////////////////////////////
- // Dispatching semantic actions
-
- ParseDispatcher & dispatcher;
+ // Configuration
- struct Dispatch_actor
- {
- Dispatch_actor(boost::function<void ()> fn_) : fn (fn_) {}
+ bool incremental;
- template <class Value>
- void operator()(Value const & value) const
- { fn(); }
+ ///////////////////////////////////////////////////////////////////////////
+ // Dispatching semantic actions
- template <class Iterator>
- void operator()(Iterator const & f, Iterator const & l) const
- { fn(); }
+ ParseDispatcher & dispatcher;
- boost::function<void ()> fn;
+ ///////////////////////////////////////////////////////////////////////////
+ // Errors
+
+ enum Errors {
+ EndOfStatementExpected,
+ GroupOrArgumentsExpected,
+ PathExpected,
+ ClosingParenExpected,
+ QuoteExpected
};
-
- template <class Callback>
- Dispatch_actor dispatch(Callback cb) const
- { return Dispatch_actor(boost::bind(cb, boost::ref(dispatcher))); }
-
- template <class Callback, class Arg>
- Dispatch_actor dispatch(Callback cb, Arg const & arg) const
- { return Dispatch_actor(boost::bind(cb, boost::ref(dispatcher), arg)); }
///////////////////////////////////////////////////////////////////////////
CommandGrammar(ParseDispatcher & d, Context & c)
- : context(c), dispatcher(d) {}
+ : context(c), incremental(false), dispatcher(d) {}
template <class Scanner>
struct definition
: public boost::spirit::grammar_def< boost::spirit::rule<Scanner>,
+ boost::spirit::rule<Scanner>,
boost::spirit::rule<Scanner> >
{
boost::spirit::rule<Scanner> command, path, argument, word, string, hexstring, token,
punctuation, hexbyte, balanced_tokens, simple_argument, complex_argument, builtin,
- skip, commands, block, statement, relpath, abspath;
+ skip, statement, relpath, abspath, arguments, group_start, group_close,
+ statement_end;
boost::spirit::chset<> special_p, punctuation_p, space_p, invalid_p, word_p;
boost::spirit::distinct_parser<> keyword_p;
definition(CommandGrammar const & self) :
// Characters with a special meaning within the parser
- special_p ("/(){};"),
+ special_p ("/(){};\""),
- // Characters which are returned as punctuation tokens
+ // Additional characters which are returned as punctuation tokens
+ // (only allowed within '()').
punctuation_p (",="),
// Whitespace characters
{
using namespace boost::spirit;
+ using namespace ::phoenix;
+ using namespace senf::phoenix;
typedef ParseDispatcher PD;
+ actor< variable< char > > ch_ (self.context.ch);
+ actor< variable< std::string > > str_ (self.context.str);
+ actor< variable< std::vector<Token> > > path_ (self.context.path);
+ actor< variable< Token > > token_ (self.context.token);
+ actor< variable< ParseDispatcher > > d_ (self.dispatcher);
+
+ assertion<Errors> end_of_statement_expected (EndOfStatementExpected);
+ assertion<Errors> group_or_arguments_expected (GroupOrArgumentsExpected);
+ assertion<Errors> path_expected (PathExpected);
+ assertion<Errors> closing_paren_expected (ClosingParenExpected);
+ assertion<Errors> quote_expected (QuoteExpected);
+
///////////////////////////////////////////////////////////////////
// Spirit grammar
//
// Syntax summary:
// This is EBNF with some minor tweaks to accommodate C++ syntax
//
- // * and + precede their argument
- // >> is followed by
- // ! optional
- // a % b match any number of a's separated by b
- // a - b match a but not b
+ // * a any number of a's
+ // + a at least one a
+ // ! a an optional a
+ // a >> b a followed by b
+ // a | b a or b
+ // a % b any number of a's separated by b's
+ // a - b a but not b
//
// Beside this, we use some special parsers (ch_p, eps_p, confix_p, lex_escape_ch_p,
// keyword_p, comment_p) and directives (lexeme_d), however, the parser should be
//
// Aligned to the right at column 50 are semantic actions.
//
- // For clarity, I have used 'ch_p' explicitly throughout even though it is auxiliary
+ // For clarity, I have used 'ch_p' explicitly throughout even though it is optional
// in most cases.
//
// More info is in the Boost.Spirit documentation
- commands
- = * command
- ;
-
command
- = builtin >> (ch_p(';') | end_p)
- | path >> ( block | statement )
+ = builtin >> end_of_statement_expected(statement_end)
+ | group_close
| ch_p(';') // Ignore empty commands
+ | path_expected(path)
+ >> group_or_arguments_expected( group_start | statement )
;
builtin
= keyword_p("cd")
- >> path
- >> eps_p [ self.dispatch(&PD::builtin_cd,
- boost::ref(self.context.path)) ]
+ >> path_expected(path)
+ >> eps_p [ bind(&PD::builtin_cd)(d_, path_) ]
| keyword_p("ls")
>> ! path
- >> eps_p [ self.dispatch(&PD::builtin_ls,
- boost::ref(self.context.path)) ]
- | keyword_p("exit") [ self.dispatch(&PD::builtin_exit) ]
-
+ >> eps_p [ bind(&PD::builtin_ls)(d_, path_) ]
+ | keyword_p("exit") [ bind(&PD::builtin_exit)(d_) ]
| keyword_p("help")
>> ! path
- >> eps_p [ self.dispatch(&PD::builtin_help,
- boost::ref(self.context.path)) ]
+ >> eps_p [ bind(&PD::builtin_help)(d_, path_) ]
+ ;
+
+ group_start
+ = ch_p('{') [ bind(&PD::pushDirectory)(d_, path_) ]
;
- block
- = ch_p('{') [ self.dispatch(&PD::pushDirectory,
- boost::ref(self.context.path)) ]
- >> * command
- >> ch_p('}') [ self.dispatch(&PD::popDirectory) ]
+ group_close
+ = ch_p('}') [ bind(&PD::popDirectory)(d_) ]
;
statement
- = eps_p [ self.dispatch(&PD::beginCommand,
- boost::ref(self.context.path)) ]
- >> * argument
- >> (ch_p(';') | end_p)
- >> eps_p [ self.dispatch(&PD::endCommand) ]
+ = eps_p [ bind(&PD::beginCommand)(d_, path_) ]
+ >> arguments
+ >> end_of_statement_expected(statement_end)
+ [ bind(&PD::endCommand)(d_) ]
+ ;
+
+ arguments
+ = * argument
;
argument
- = simple_argument [ self.dispatch(&PD::pushArgument,
- boost::ref(self.context.str)) ]
- | complex_argument
+ = simple_argument [ bind(&PD::pushToken)(d_, token_) ]
+ | balanced_tokens
;
- simple_argument // All these return their value in context.str
+ simple_argument // All these return their value in context.token
= string
| hexstring
| word
;
- complex_argument // Argument consists of multiple tokens
- = ch_p('(') [ self.dispatch(&PD::openGroup) ]
- >> * token
- >> ch_p(')') [ self.dispatch(&PD::closeGroup) ]
- ;
-
- string // Returns value in context.str
- = eps_p [ clear_a(self.context.str) ]
+ string // Returns value in context.token
+ = eps_p [ clear(str_) ]
>> lexeme_d
[
ch_p('"')
- >> * ( ( lex_escape_ch_p[ assign_a(self.context.ch) ]
+ >> * ( ( lex_escape_ch_p[ ch_ = arg1 ]
- '"'
- ) [ append_a(self.context.str,
- self.context.ch) ]
+ ) [ str_ += ch_ ]
)
- >> ch_p('"')
+ >> quote_expected(ch_p('"'))
+ [ token_ = construct_<Token>(Token::BasicString,
+ str_) ]
]
;
- hexstring // Returns value in context.str
- = eps_p [ clear_a(self.context.str) ]
- >> confix_p( "x\"", * hexbyte, '"' )
+ hexstring // Returns value in context.token
+ = eps_p [ clear(str_) ]
+ >> "x\""
+ >> * ( hexbyte - ch_p('"') )
+ >> quote_expected(ch_p('"'))
+ [ token_ = construct_<Token>(Token::HexString,
+ str_) ]
;
path // Returns value in context.path
- = eps_p [ clear_a(self.context.path) ]
+ = eps_p [ clear(path_) ]
>> relpath | abspath
;
relpath
- = ( word [ push_back_a(self.context.path) ]
+ = ( word [ push_back(path_, token_) ]
% ch_p('/') )
- >> ( ! ch_p('/') [ push_back_a(self.context.path,"") ] )
+ >> ( ! ch_p('/') [ push_back(path_, construct_<Token>()) ] )
;
abspath
- = ch_p('/') [ push_back_a(self.context.path, "") ]
+ = ch_p('/') [ push_back(path_, construct_<Token>()) ]
>> ( relpath
- | eps_p [ push_back_a(self.context.path, "") ] )
+ | eps_p [ push_back(path_, construct_<Token>()) ] )
;
balanced_tokens
- = ch_p('(') [ self.dispatch(&PD::pushPunctuation, "(") ]
+ = ch_p('(') [ token_ = construct_<Token>(
+ Token::ArgumentGroupOpen,
+ "(") ]
+ [ bind(&PD::pushToken)(d_, token_) ]
>> * token
- >> ch_p(')') [ self.dispatch(&PD::pushPunctuation, ")") ]
+ >> closing_paren_expected(ch_p(')'))
+ [ token_ = construct_<Token>(
+ Token::ArgumentGroupClose,
+ ")") ]
+ [ bind(&PD::pushToken)(d_, token_) ]
;
token
- = simple_argument [ self.dispatch(&PD::pushWord,
- boost::ref(self.context.str)) ]
- | punctuation [ self.dispatch(&PD::pushPunctuation,
- boost::ref(self.context.str)) ]
+ = simple_argument [ bind(&PD::pushToken)(d_, token_) ]
+ | punctuation [ bind(&PD::pushToken)(d_, token_) ]
| balanced_tokens
;
punctuation // Returns value in context.str
- = punctuation_p [ assign_a(self.context.str) ]
+ = ch_p('/') [ token_ = construct_<Token>(
+ Token::PathSeparator,
+ "/") ]
+ | ch_p('{') [ token_ = construct_<Token>(
+ Token::DirectoryGroupOpen,
+ "{") ]
+ | ch_p('}') [ token_ = construct_<Token>(
+ Token::DirectoryGroupClose,
+ "}") ]
+ | ch_p(';') [ token_ = construct_<Token>(
+ Token::CommandTerminator,
+ ";") ]
+ | punctuation_p [ token_ = construct_<Token>(
+ Token::OtherPunctuation,
+ construct_<std::string>(1u, arg1)) ]
;
- word // Returns value in context.str
- = lexeme_d[ + word_p ] [ assign_a(self.context.str) ]
+ word // Returns value in context.token
+ = lexeme_d
+ [
+ (+ word_p) [ str_ = construct_<std::string>(arg1, arg2) ]
+ ]
+ >> eps_p [ token_ = construct_<Token>(
+ Token::Word,
+ str_) ]
;
hexbyte
= uint_parser<char, 16, 2, 2>()
- [ append_a(self.context.str) ]
+ [ push_back(str_, arg1) ]
+ ;
+
+ statement_end
+ = if_p(var(self.incremental)) [
+ ch_p(';')
+ ]
+ .else_p [
+ ch_p(';')
+ | end_p
+ ]
;
skip
///////////////////////////////////////////////////////////////////
start_parsers(
- commands, // CommandParser
- skip // SkipParser
+ command, // CommandParser
+ skip, // SkipParser
+ arguments // ArgumentsParser
);
BOOST_SPIRIT_DEBUG_TRACE_RULE(command,1);
};
};
+#endif
+
}}}
///////////////////////////////ih.e////////////////////////////////////////