X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Utils%2FConsole%2FParse.ih;h=4f0569f59ec01dfc453ea39e97eae3e0b8cec0ce;hb=4195e3fc4bb545f2f7921396e2aec77edaa8c8c3;hp=afb4b78c2d9e29bfa728d3ebc3413c4caee46739;hpb=958bdb52c39fa39f4ef91cafd9628bcb4f85a03c;p=senf.git diff --git a/Utils/Console/Parse.ih b/Utils/Console/Parse.ih index afb4b78..4f0569f 100644 --- a/Utils/Console/Parse.ih +++ b/Utils/Console/Parse.ih @@ -28,6 +28,7 @@ // Custom includes #include +#include "../../config.hh" #include #include #include @@ -51,7 +52,7 @@ namespace detail { /////////////////////////////////////////////////////////////////////////// // Start rules - enum { CommandParser, SkipParser, ArgumentsParser }; + enum { CommandParser, SkipParser, ArgumentsParser, PathParser }; /////////////////////////////////////////////////////////////////////////// // The parse context (variables needed while parsing) @@ -77,12 +78,21 @@ namespace detail { ParseDispatcher & dispatcher; + ////////////////////////////////////////////////////////////////////////// + // charachter sets + + static boost::spirit::chset<> special_p; + static boost::spirit::chset<> punctuation_p; + static boost::spirit::chset<> space_p; + static boost::spirit::chset<> invalid_p; + static boost::spirit::chset<> word_p; + static boost::spirit::distinct_parser<> keyword_p; + /////////////////////////////////////////////////////////////////////////// // Errors enum Errors { EndOfStatementExpected, - GroupOrArgumentsExpected, PathExpected, ClosingParenExpected, QuoteExpected @@ -97,39 +107,15 @@ namespace detail { struct definition : public boost::spirit::grammar_def< boost::spirit::rule, boost::spirit::rule, + boost::spirit::rule, boost::spirit::rule > { boost::spirit::rule command, path, argument, word, string, hexstring, token, punctuation, hexbyte, balanced_tokens, simple_argument, complex_argument, builtin, 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 ("/(){};\""), - - // Additional characters which are returned as punctuation tokens - // (only allowed within '()'). - punctuation_p (",="), - - // Whitespace characters - space_p (" \t\n\r"), - - // Invalid characters: All chars below \x20 (space) which are not space_p - // (don't put a \0 in the chset<> argument *string* ...) - invalid_p ( (boost::spirit::chset<>('\0') - | boost::spirit::chset<>("\x01-\x20")) - space_p ), - - // Valid word characters - word_p ( - boost::spirit::anychar_p - special_p - punctuation_p - space_p - invalid_p), - - // Keywords must not be followed by a word char or '/' - keyword_p ( word_p | boost::spirit::ch_p('/') ) + statement_end, opt_path; + definition(CommandGrammar const & self) { using namespace boost::spirit; using namespace ::phoenix; @@ -143,7 +129,6 @@ namespace detail { actor< variable< ParseDispatcher > > d_ (self.dispatcher); assertion end_of_statement_expected (EndOfStatementExpected); - assertion group_or_arguments_expected (GroupOrArgumentsExpected); assertion path_expected (PathExpected); assertion closing_paren_expected (ClosingParenExpected); assertion quote_expected (QuoteExpected); @@ -190,38 +175,39 @@ namespace detail { = builtin >> end_of_statement_expected(statement_end) | group_close | ch_p(';') // Ignore empty commands - | path_expected(path) - >> group_or_arguments_expected( group_start | statement ) + | statement + ; + + statement + = path_expected(path) [ bind(&PD::beginCommand)(d_, path_) ] + >> arguments + >> end_of_statement_expected( + ( group_start | statement_end ) + [ bind(&PD::endCommand)(d_) ] + ) ; builtin - = keyword_p("cd") + = self.keyword_p("cd") >> path_expected(path) >> eps_p [ bind(&PD::builtin_cd)(d_, path_) ] - | keyword_p("ls") + | self.keyword_p("ls") >> ! path >> eps_p [ bind(&PD::builtin_ls)(d_, path_) ] - | keyword_p("exit") [ bind(&PD::builtin_exit)(d_) ] - | keyword_p("help") + | self.keyword_p("exit") [ bind(&PD::builtin_exit)(d_) ] + | self.keyword_p("help") >> ! path >> eps_p [ bind(&PD::builtin_help)(d_, path_) ] ; group_start - = ch_p('{') [ bind(&PD::pushDirectory)(d_, path_) ] + = ch_p('{') [ bind(&PD::pushDirectory)(d_) ] ; group_close = ch_p('}') [ bind(&PD::popDirectory)(d_) ] ; - statement - = eps_p [ bind(&PD::beginCommand)(d_, path_) ] - >> arguments - >> end_of_statement_expected(statement_end) - [ bind(&PD::endCommand)(d_) ] - ; - arguments = * argument ; @@ -260,6 +246,11 @@ namespace detail { [ token_ = construct_(Token::HexString, str_) ] ; + + opt_path + = ! path [ bind(&PD::beginCommand)(d_, path_) ] + [ bind(&PD::endCommand)(d_) ] + ; path // Returns value in context.path = eps_p [ clear(path_) ] @@ -310,7 +301,7 @@ namespace detail { | ch_p(';') [ token_ = construct_( Token::CommandTerminator, ";") ] - | punctuation_p [ token_ = construct_( + | self.punctuation_p [ token_ = construct_( Token::OtherPunctuation, construct_(1u, arg1)) ] ; @@ -318,7 +309,7 @@ namespace detail { word // Returns value in context.token = lexeme_d [ - (+ word_p) [ str_ = construct_(arg1, arg2) ] + (+ self.word_p) [ str_ = construct_(arg1, arg2) ] ] >> eps_p [ token_ = construct_( Token::Word, @@ -341,7 +332,7 @@ namespace detail { ; skip - = space_p | comment_p('#') + = self.space_p | comment_p('#') ; /////////////////////////////////////////////////////////////////// @@ -349,7 +340,8 @@ namespace detail { start_parsers( command, // CommandParser skip, // SkipParser - arguments // ArgumentsParser + arguments, // ArgumentsParser + opt_path // PathParser ); BOOST_SPIRIT_DEBUG_TRACE_RULE(command,1); @@ -374,6 +366,19 @@ namespace detail { }; }; + template boost::spirit::chset<> CommandGrammar::special_p ( + "/(){};\""); + template boost::spirit::chset<> CommandGrammar::punctuation_p ( + ",="); + template boost::spirit::chset<> CommandGrammar::space_p ( + " \t\n\r"); + template boost::spirit::chset<> CommandGrammar::invalid_p ( + (boost::spirit::chset<>('\0') | boost::spirit::chset<>("\x01-\x20")) - space_p ); + template boost::spirit::chset<> CommandGrammar::word_p ( + boost::spirit::anychar_p - special_p - punctuation_p - space_p - invalid_p); + template boost::spirit::distinct_parser<> CommandGrammar::keyword_p ( + word_p | boost::spirit::ch_p('/')); + #endif }}}