X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Utils%2FConsole%2FParse.ih;h=d99e168f24c02f82c40ccc7c51970aa3a61aeb5f;hb=97b74f74397997f7ea02eaa7f44689aa0eb99839;hp=38b9fc924edc073385e53fbff55e2514b2e4eb44;hpb=a22f6d21df6c911d65f76d7731bcf92b1906bb09;p=senf.git diff --git a/Utils/Console/Parse.ih b/Utils/Console/Parse.ih index 38b9fc9..d99e168 100644 --- a/Utils/Console/Parse.ih +++ b/Utils/Console/Parse.ih @@ -43,6 +43,30 @@ namespace detail { #ifndef DOXYGEN + struct FilePositionWithIndex + : public boost::spirit::file_position + { + int index; + + FilePositionWithIndex(std::string const & file_ = std::string(), + int line_ = 1, int column_ = 1, int index_ = 0) + : boost::spirit::file_position (file_, line_, column_), index (index_) + {} + + bool operator==(const FilePositionWithIndex & fp) const + { + return boost::spirit::file_position::operator==(fp) && index == fp.index; + } + }; + + struct PositionOf { + template struct result { typedef FilePositionWithIndex type; }; + template FilePositionWithIndex operator()(A1 & a1) { return a1.get_position(); } + FilePositionWithIndex operator()(char const * a1) { return FilePositionWithIndex(); } + }; + + ::phoenix::function const positionOf; + /////////////////////////////////////////////////////////////////////////// // Grammar @@ -64,6 +88,7 @@ namespace detail { std::vector path; char ch; Token token; + FilePositionWithIndex pos; }; Context & context; @@ -78,6 +103,16 @@ 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 @@ -104,44 +139,20 @@ namespace detail { punctuation, hexbyte, balanced_tokens, simple_argument, complex_argument, builtin, skip, statement, relpath, abspath, arguments, group_start, group_close, statement_end, opt_path; - 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('/') ) + definition(CommandGrammar const & self) { 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 > > path_ (self.context.path); - actor< variable< Token > > token_ (self.context.token); - actor< variable< ParseDispatcher > > d_ (self.dispatcher); + actor< variable< char > > ch_ (self.context.ch); + actor< variable< std::string > > str_ (self.context.str); + actor< variable< std::vector > > path_ (self.context.path); + actor< variable< Token > > token_ (self.context.token); + actor< variable< FilePositionWithIndex > > pos_ (self.context.pos); + actor< variable< ParseDispatcher > > d_ (self.dispatcher); assertion end_of_statement_expected (EndOfStatementExpected); assertion path_expected (PathExpected); @@ -203,14 +214,14 @@ namespace detail { ; 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_) ] ; @@ -239,7 +250,7 @@ namespace detail { ; string // Returns value in context.token - = eps_p [ clear(str_) ] + = eps_p [ pos_ = positionOf(arg1) ][ clear(str_) ] >> lexeme_d [ ch_p('"') @@ -249,17 +260,19 @@ namespace detail { ) >> quote_expected(ch_p('"')) [ token_ = construct_(Token::BasicString, - str_) ] + str_, + pos_) ] ] ; hexstring // Returns value in context.token - = eps_p [ clear(str_) ] + = eps_p [ pos_ = positionOf(arg1) ][ clear(str_) ] >> "x\"" >> * ( hexbyte - ch_p('"') ) >> quote_expected(ch_p('"')) [ token_ = construct_(Token::HexString, - str_) ] + str_, + pos_) ] ; opt_path @@ -285,15 +298,19 @@ namespace detail { ; balanced_tokens - = ch_p('(') [ token_ = construct_( + = eps_p [ pos_ = positionOf(arg1) ] + >> ch_p('(') [ token_ = construct_( Token::ArgumentGroupOpen, - "(") ] + "(", + pos_) ] [ bind(&PD::pushToken)(d_, token_) ] >> * token + >> eps_p [ pos_ = positionOf(arg1) ] >> closing_paren_expected(ch_p(')')) [ token_ = construct_( Token::ArgumentGroupClose, - ")") ] + ")", + pos_) ] [ bind(&PD::pushToken)(d_, token_) ] ; @@ -304,31 +321,37 @@ namespace detail { ; punctuation // Returns value in context.str - = ch_p('/') [ token_ = construct_( - Token::PathSeparator, - "/") ] - | ch_p('{') [ token_ = construct_( - Token::DirectoryGroupOpen, - "{") ] - | ch_p('}') [ token_ = construct_( - Token::DirectoryGroupClose, - "}") ] - | ch_p(';') [ token_ = construct_( - Token::CommandTerminator, - ";") ] - | punctuation_p [ token_ = construct_( - Token::OtherPunctuation, - construct_(1u, arg1)) ] + = eps_p [ pos_ = positionOf(arg1) ] + >> ( + ch_p('/') [ token_ = construct_( + Token::PathSeparator, + "/") ] + | ch_p('{') [ token_ = construct_( + Token::DirectoryGroupOpen, + "{") ] + | ch_p('}') [ token_ = construct_( + Token::DirectoryGroupClose, + "}") ] + | ch_p(';') [ token_ = construct_( + Token::CommandTerminator, + ";") ] + | self.punctuation_p [ token_ = construct_( + Token::OtherPunctuation, + construct_(1u, arg1), + pos_) ] + ) ; word // Returns value in context.token - = lexeme_d + = eps_p [ pos_ = positionOf(arg1) ] + >> lexeme_d [ - (+ word_p) [ str_ = construct_(arg1, arg2) ] + (+ self.word_p) [ str_ = construct_(arg1, arg2) ] ] >> eps_p [ token_ = construct_( Token::Word, - str_) ] + str_, + pos_) ] ; hexbyte @@ -347,7 +370,7 @@ namespace detail { ; skip - = space_p | comment_p('#') + = self.space_p | comment_p('#') ; /////////////////////////////////////////////////////////////////// @@ -381,6 +404,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 }}}