diff options
author | Jörg Frings-Fürst <debian@jff.email> | 2024-03-06 10:24:08 +0100 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff.email> | 2024-03-06 10:24:08 +0100 |
commit | aad5ad9bf0c02aa4e79bc6b7d6c934612fff4026 (patch) | |
tree | 9cc224b059f248a6229ab0dcdc64eb4a73fa9800 /xsd/type-map/lexer.cxx | |
parent | c1034fc5e99197f507caf450aa15bc178698b26e (diff) |
New upstream version 4.2.0upstream/4.2.0upstream
Diffstat (limited to 'xsd/type-map/lexer.cxx')
-rw-r--r-- | xsd/type-map/lexer.cxx | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/xsd/type-map/lexer.cxx b/xsd/type-map/lexer.cxx new file mode 100644 index 0000000..ae318d1 --- /dev/null +++ b/xsd/type-map/lexer.cxx @@ -0,0 +1,131 @@ +// file : xsd/type-map/lexer.cxx +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include <iostream> + +#include <xsd/type-map/lexer.hxx> + +using std::wcerr; +using std::endl; + +namespace TypeMap +{ + Lexer::Lexer (std::istream& is, String const& path) + : locale_ ("C"), is_ (is), path_ (path), line_ (1), comment_ (false) + { + is_.exceptions (std::ios_base::badbit); + } + + Lexer::Token Lexer:: + next () + { + if (held_lexeme_) + { + Token t (Token::punct, held_lexeme_, line_); + held_lexeme_.clear (); + return t; + } + + typedef std::char_traits<char> Traits; + typedef Traits::char_type CharType; + typedef Traits::int_type IntType; + + IntType i; + CharType c ('\0'); + NarrowString lexeme; + + // Skip all whitespaces including comments. + // + while (!is_.eof ()) + { + i = is_.get (); + + if (i == Traits::eof ()) + break; + + c = Traits::to_char_type (i); + + if (comment_) + { + if (c == '\n') + comment_ = false; + } + else + { + if (!(std::isspace (c, locale_) || c == '#')) + break; + + if (c == '#') + comment_ = true; + } + + if (c == '\n') + ++line_; + } + + if (is_.eof ()) + return Token (Token::eos, L"<end-of-stream>", line_); + + bool quote (c == '"'); + + if (!quote) + lexeme += c; + + if (c != ';' && c != '{' && c != '}') + { + // Accumulate non-whitespace character sequence. + // + + while (!is_.eof ()) + { + i = is_.get (); + + if (i == Traits::eof ()) + break; + + c = Traits::to_char_type (i); + + if (!quote && c == '#') + { + comment_ = true; + break; + } + + if (std::isspace (c, locale_)) + { + if (c == '\n') + ++line_; + + if (!quote) + break; + } + + if (!quote && (c == ';' || c == '{' || c == '}')) + { + held_lexeme_ += c; + break; + } + + if (quote && c == '"') + break; + + lexeme += c; + } + + if (quote && c != '"') + { + wcerr << path_ << ":" << line_ << ": error: closing '\"' expected" + << endl; + + throw Failed (); + } + } + + if (!quote && (lexeme == ";" || lexeme == "{" || lexeme == "}")) + { + return Token (Token::punct, lexeme, line_); + } + else + return Token (Token::token, lexeme, line_); + } +} |