aboutsummaryrefslogtreecommitdiff
path: root/libbutl/char-scanner.cxx
blob: a97ed482cee36bf856aa2548cc24753c98067b4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// file      : libbutl/char-scanner.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <libbutl/char-scanner.hxx>

using namespace std;

namespace butl
{
  char_scanner::
  char_scanner (istream& is, bool crlf)
      : is_ (is),
        buf_ (dynamic_cast<fdbuf*> (is.rdbuf ())),
        gptr_ (nullptr),
        egptr_ (nullptr),
        crlf_ (crlf)
  {
  }

  auto char_scanner::
  peek () -> xchar
  {
    if (unget_)
      return ungetc_;

    if (unpeek_)
      return unpeekc_;

    if (eos_)
      return xchar (xchar::traits_type::eof (), line, column);

    int_type v (peek_ ());

    if (v == xchar::traits_type::eof ())
      eos_ = true;
    else if (crlf_ && v == '\r')
    {
      get_ ();
      int_type v1 (peek_ ());

      if (v1 != '\n')
      {
        // We need to make sure subsequent calls to peek() return newline.
        //
        unpeek_ = true;
        unpeekc_ = xchar ('\n', line, column);
      }

      v = '\n';
    }

    return xchar (v, line, column);
  }

  void char_scanner::
  get (const xchar& c)
  {
    if (unget_)
      unget_ = false;
    else if (unpeek_)
      unpeek_ = false;
    else
    {
      // When is_.get () returns eof, the failbit is also set (stupid,
      // isn't?) which may trigger an exception. To work around this
      // we will call peek() first and only call get() if it is not
      // eof. But we can only call peek() on eof once; any subsequent
      // calls will spoil the failbit (even more stupid).
      //
      if (!eos (c))
      {
        get_ ();

        if (c == '\n')
        {
          line++;
          column = 1;
        }
        else
          column++;
      }
    }
  }
}