From 897a0e4fdf9ca90ee8d236a38e138a8ae6bc3627 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 6 Mar 2015 09:15:40 +0200 Subject: Add support for lexing and parsing name pairs We will need it for the buildspec and also if/when we support map variable types. --- build/lexer | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'build/lexer') diff --git a/build/lexer b/build/lexer index d6817f2..787ba72 100644 --- a/build/lexer +++ b/build/lexer @@ -15,11 +15,29 @@ namespace build { + // Context-dependent lexing mode. In the value mode we don't treat + // certain characters (e.g., +, =) as special so that we can use + // them in the variable values, e.g., 'foo = g++'. In contrast, + // in the variable mode, we restrict certain character (e.g., /) + // from appearing in the name. The pairs mode is just like value + // except that we split names separated by '='. The pairs mode must + // be set manually. + // + enum class lexer_mode {normal, value, variable, pairs}; + class lexer { public: lexer (std::istream& is, const std::string& name): is_ (is), fail (name) {} + // Note: sets mode for the next token. + // + void + mode (lexer_mode m) {next_mode_ = m;} + + lexer_mode + mode () const {return mode_;} + // Scanner. // token @@ -108,15 +126,9 @@ namespace build xchar buf_ {0, 0, 0}; bool eos_ {false}; - - // Context-dependent lexing mode. In the value mode we don't treat - // certain characters (e.g., +, =) as special so that we can use - // them in the variable values, e.g., 'foo = g++'. In contrast, - // in the variable mode, we restrict certain character (e.g., /) - // from appearing in the name. - // - enum class mode {normal, value, variable}; - mode mode_ {mode::normal}; + lexer_mode mode_ {lexer_mode::normal}; + lexer_mode next_mode_; // Mode to switch to for the next token. + lexer_mode prev_mode_; // Mode to return to after this mode expires. }; } -- cgit v1.1