From b6e02f4224975a6425f62095bc35478e8866db77 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 24 May 2017 13:26:13 +0200 Subject: Various improvements to char_scanner --- libbutl/char-scanner.hxx | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'libbutl/char-scanner.hxx') diff --git a/libbutl/char-scanner.hxx b/libbutl/char-scanner.hxx index 71f8313..e71f286 100644 --- a/libbutl/char-scanner.hxx +++ b/libbutl/char-scanner.hxx @@ -33,8 +33,12 @@ namespace butl // public: - // Extended character. It includes line/column information - // and is capable of representing EOF. + // Extended character. It includes line/column information and is capable + // of representing EOF. + // + // Note that implicit conversion of EOF to char_type results in NUL + // character (which means in most cases it is safe to compare xchar to + // char without checking for EOF). // class xchar { @@ -47,7 +51,12 @@ namespace butl std::uint64_t line; std::uint64_t column; - operator char_type () const {return static_cast (value);} + operator char_type () const + { + return value != traits_type::eof () + ? static_cast (value) + : char_type (0); + } xchar (int_type v, std::uint64_t l = 0, std::uint64_t c = 0) : value (v), line (l), column (c) {} @@ -57,6 +66,9 @@ namespace butl get (); void + get (const xchar& peeked); // Get previously peeked character (faster). + + void unget (const xchar&); // Note that if there is an "ungot" character, peek() will return @@ -71,20 +83,26 @@ namespace butl static bool eos (const xchar& c) {return c.value == xchar::traits_type::eof ();} - // Line and column of the furthest seen (either via get() or - // peek()) character. + // Line and column of the next character to be extracted from the stream + // by peek() or get(). // - std::uint64_t line {1}; - std::uint64_t column {1}; + std::uint64_t line = 1; + std::uint64_t column = 1; protected: std::istream& is_; + bool crlf_; + bool eos_ = false; + + bool unget_ = false; + bool unpeek_ = false; - bool unget_ {false}; - xchar buf_ = '\0'; - bool eos_ {false}; + xchar ungetc_ = '\0'; + xchar unpeekc_ = '\0'; }; } +#include + #endif // LIBBUTL_CHAR_SCANNER_HXX -- cgit v1.1