File: basic_io.hh

package info (click to toggle)
monotone 0.18-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 16,440 kB
  • ctags: 13,394
  • sloc: sh: 130,618; ansic: 70,657; cpp: 51,980; perl: 421; makefile: 359; python: 184; lisp: 132; sql: 83
file content (224 lines) | stat: -rw-r--r-- 5,656 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#ifndef __BASIC_IO_HH__
#define __BASIC_IO_HH__

// copyright (C) 2004 graydon hoare <graydon@pobox.com>
// all rights reserved.
// licensed to the public under the terms of the GNU GPL (>= 2)
// see the file COPYING for details

// this file provides parsing and printing primitives used by the higher
// level parser and printer routines for the two datatypes change_set and
// revision_set. every revision_set contains a number of change_sets, so
// their i/o routines are somewhat related.

#include <iosfwd>
#include <string>
#include <vector>
#include <map>

namespace basic_io
{

  typedef enum
    {
      TOK_SYMBOL,
      TOK_STRING,
      TOK_HEX,
      TOK_NONE,
    } token_type;

  struct 
  input_source
  {
    size_t line, col;
    std::istream & in;
    std::string name;
    int lookahead;
    char c;
    input_source(std::istream & i, std::string const & nm)
      : line(1), col(1), in(i), name(nm), lookahead(0), c('\0')
    {}
    inline void peek() { lookahead = in.peek(); }
    inline void eat()
    {
      in.get(c);
      ++col;
      if (c == '\n')
        {
          col = 1;
          ++line;
        }
    }
    inline void advance() { eat(); peek(); }
    void err(std::string const & s);
  };

  struct
  tokenizer
  {  
    input_source & in;
    tokenizer(input_source & i) : in(i) {}

    inline token_type get_token(std::string & val)
    {
      val.clear();
      val.reserve(80);
      in.peek();
  
      while (true)
        {
          if (in.lookahead == EOF)
            return TOK_NONE;
          if (!std::isspace(in.lookahead))
            break;
          in.advance();
        }
  
      switch (in.lookahead)
        {

        case '"':
          {
            in.advance();
            while (static_cast<char>(in.lookahead) != '"')
              {
                if (in.lookahead == EOF)
                  in.err("input stream ended in string");
                if (static_cast<char>(in.lookahead) == '\\')
                  {
                    // possible escape: we understand escaped quotes
                    // and escaped backslashes. nothing else.
                    in.advance();
                    if (!(static_cast<char>(in.lookahead) == '"' 
                          || static_cast<char>(in.lookahead) == '\\'))
                      {
                        in.err("unrecognized character escape");
                      }
                  }
                in.advance();
                val += in.c;
              }

            if (static_cast<char>(in.lookahead) != '"')
              in.err("string did not end with '\"'");
            in.eat();
	
            return basic_io::TOK_STRING;
          }

        case '[':
          {
            in.advance();
            while (static_cast<char>(in.lookahead) != ']')
              {
                if (in.lookahead == EOF)
                  in.err("input stream ended in hex string");
                if (!std::isxdigit(in.lookahead))
                  in.err("non-hex character in hex string");
                in.advance();
                val += in.c;
              }

            if (static_cast<char>(in.lookahead) != ']')
              in.err("hex string did not end with ']'");
            in.eat();
	
            return basic_io::TOK_HEX;	
          }
        default:
          if (std::isalpha(in.lookahead))
            {
              while (std::isalnum(in.lookahead) || in.lookahead == '_')
                {
                  in.advance();
                  val += in.c;
                }
              return basic_io::TOK_SYMBOL;
            }
        }
      return basic_io::TOK_NONE;
    }
   void err(std::string const & s);
  };

  struct 
  stanza
  {
    stanza();
    size_t indent;  
    std::vector<std::pair<std::string, std::string> > entries;
    void push_hex_pair(std::string const & k, std::string const & v);
    void push_str_pair(std::string const & k, std::string const & v);
  };

  struct 
  printer
  {
    bool empty_output;
    std::ostream & out;
    printer(std::ostream & ost);
    void print_stanza(stanza const & st);
  };

  struct
  parser
  {
    tokenizer & tok;
    parser(tokenizer & t) : tok(t) 
    {
      advance();
    }
    
    std::string token;
    token_type ttype;

    void err(std::string const & s);
    std::string tt2str(token_type tt);
    
    inline void advance()
    {
      ttype = tok.get_token(token);
    }

    inline void eat(token_type want)
    {
      if (ttype != want)
        err("wanted " 
            + tt2str(want)
            + ", got "
            + tt2str(ttype)
            + (token.empty() 
               ? std::string("") 
               : (std::string(" with value ") + token)));
      advance();
    }
    
    inline void str() { eat(basic_io::TOK_STRING); }
    inline void sym() { eat(basic_io::TOK_SYMBOL); }
    inline void hex() { eat(basic_io::TOK_HEX); }
    
    inline void str(std::string & v) { v = token; str(); }
    inline void sym(std::string & v) { v = token; sym(); }
    inline void hex(std::string & v) { v = token; hex(); }
    inline bool symp() { return ttype == basic_io::TOK_SYMBOL; }
    inline bool symp(std::string const & val) 
    {
      return ttype == basic_io::TOK_SYMBOL && token == val;
    }
    inline void esym(std::string const & val)
    {
      if (!(ttype == basic_io::TOK_SYMBOL && token == val))
        err("wanted symbol '" 
            + val +
            + "', got "
            + tt2str(ttype)
            + (token.empty() 
               ? std::string("") 
               : (std::string(" with value ") + token)));
      advance();
    }
  };

}

#endif // __BASIC_IO_HH__