aboutsummaryrefslogtreecommitdiff
path: root/libbutl/char-scanner.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-05-24 13:26:13 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-05-24 13:28:51 +0200
commitb6e02f4224975a6425f62095bc35478e8866db77 (patch)
tree51627eef813ebb670efca83aed7dd284cb738806 /libbutl/char-scanner.hxx
parenteb3c27b8f47c793244436cd082512bb8235bea89 (diff)
Various improvements to char_scanner
Diffstat (limited to 'libbutl/char-scanner.hxx')
-rw-r--r--libbutl/char-scanner.hxx38
1 files changed, 28 insertions, 10 deletions
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<char_type> (value);}
+ operator char_type () const
+ {
+ return value != traits_type::eof ()
+ ? static_cast<char_type> (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 <libbutl/char-scanner.ixx>
+
#endif // LIBBUTL_CHAR_SCANNER_HXX