aboutsummaryrefslogtreecommitdiff
path: root/xsd/type-map/lexer.cxx
blob: fe7ecfcb105ecf9e0f592fe305e39a0daf8b1847 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// file      : xsd/type-map/lexer.cxx
// copyright : Copyright (c) 2007-2014 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <iostream>

#include <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_);
  }
}