exp.h 5.9 KB
Newer Older
1
2
3
#ifndef EXP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define EXP_H_62B23520_7C8E_11DE_8A39_0800200C9A66

Jesse Beder's avatar
Jesse Beder committed
4
5
6
#if defined(_MSC_VER) ||                                            \
    (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
     (__GNUC__ >= 4))  // GCC supports "pragma once" correctly since 3.4
7
8
9
#pragma once
#endif

10
#include <ios>
Jesse Beder's avatar
Jesse Beder committed
11
12
13
#include <string>

#include "regex_yaml.h"
14
15
#include "stream.h"

Jesse Beder's avatar
Jesse Beder committed
16
17
18
19
namespace YAML {
////////////////////////////////////////////////////////////////////////////////
// Here we store a bunch of expressions for matching different parts of the
// file.
20

Jesse Beder's avatar
Jesse Beder committed
21
22
namespace Exp {
// misc
23
24
25
26
inline const RegEx& Empty() {
  static const RegEx e;
  return e;
}
Jesse Beder's avatar
Jesse Beder committed
27
28
29
30
31
32
33
34
35
inline const RegEx& Space() {
  static const RegEx e = RegEx(' ');
  return e;
}
inline const RegEx& Tab() {
  static const RegEx e = RegEx('\t');
  return e;
}
inline const RegEx& Blank() {
36
  static const RegEx e = Space() | Tab();
Jesse Beder's avatar
Jesse Beder committed
37
38
39
  return e;
}
inline const RegEx& Break() {
40
  static const RegEx e = RegEx('\n') | RegEx("\r");
Jesse Beder's avatar
Jesse Beder committed
41
42
43
  return e;
}
inline const RegEx& BlankOrBreak() {
44
  static const RegEx e = Blank() | Break();
Jesse Beder's avatar
Jesse Beder committed
45
46
47
48
49
50
51
  return e;
}
inline const RegEx& Digit() {
  static const RegEx e = RegEx('0', '9');
  return e;
}
inline const RegEx& Alpha() {
52
  static const RegEx e = RegEx('a', 'z') | RegEx('A', 'Z');
Jesse Beder's avatar
Jesse Beder committed
53
54
55
  return e;
}
inline const RegEx& AlphaNumeric() {
56
  static const RegEx e = Alpha() | Digit();
Jesse Beder's avatar
Jesse Beder committed
57
58
59
  return e;
}
inline const RegEx& Word() {
60
  static const RegEx e = AlphaNumeric() | RegEx('-');
Jesse Beder's avatar
Jesse Beder committed
61
62
63
  return e;
}
inline const RegEx& Hex() {
64
  static const RegEx e = Digit() | RegEx('A', 'F') | RegEx('a', 'f');
Jesse Beder's avatar
Jesse Beder committed
65
66
67
68
69
70
  return e;
}
// Valid Unicode code points that are not part of c-printable (YAML 1.2, sec.
// 5.1)
inline const RegEx& NotPrintable() {
  static const RegEx e =
71
72
73
74
      RegEx(0) |
      RegEx("\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x7F", REGEX_OR) |
      RegEx(0x0E, 0x1F) |
      (RegEx('\xC2') + (RegEx('\x80', '\x84') | RegEx('\x86', '\x9F')));
Jesse Beder's avatar
Jesse Beder committed
75
76
77
78
79
80
  return e;
}
inline const RegEx& Utf8_ByteOrderMark() {
  static const RegEx e = RegEx("\xEF\xBB\xBF");
  return e;
}
81

Jesse Beder's avatar
Jesse Beder committed
82
// actual tags
83

Jesse Beder's avatar
Jesse Beder committed
84
inline const RegEx& DocStart() {
85
  static const RegEx e = RegEx("---") + (BlankOrBreak() | RegEx());
Jesse Beder's avatar
Jesse Beder committed
86
87
88
  return e;
}
inline const RegEx& DocEnd() {
89
  static const RegEx e = RegEx("...") + (BlankOrBreak() | RegEx());
Jesse Beder's avatar
Jesse Beder committed
90
91
92
  return e;
}
inline const RegEx& DocIndicator() {
93
  static const RegEx e = DocStart() | DocEnd();
Jesse Beder's avatar
Jesse Beder committed
94
95
96
  return e;
}
inline const RegEx& BlockEntry() {
97
  static const RegEx e = RegEx('-') + (BlankOrBreak() | RegEx());
Jesse Beder's avatar
Jesse Beder committed
98
99
100
101
102
103
104
105
106
107
108
  return e;
}
inline const RegEx& Key() {
  static const RegEx e = RegEx('?') + BlankOrBreak();
  return e;
}
inline const RegEx& KeyInFlow() {
  static const RegEx e = RegEx('?') + BlankOrBreak();
  return e;
}
inline const RegEx& Value() {
109
  static const RegEx e = RegEx(':') + (BlankOrBreak() | RegEx());
Jesse Beder's avatar
Jesse Beder committed
110
111
112
  return e;
}
inline const RegEx& ValueInFlow() {
113
  static const RegEx e = RegEx(':') + (BlankOrBreak() | RegEx(",]}", REGEX_OR));
Jesse Beder's avatar
Jesse Beder committed
114
115
116
117
118
119
120
121
122
123
124
  return e;
}
inline const RegEx& ValueInJSONFlow() {
  static const RegEx e = RegEx(':');
  return e;
}
inline const RegEx Comment() {
  static const RegEx e = RegEx('#');
  return e;
}
inline const RegEx& Anchor() {
125
  static const RegEx e = !(RegEx("[]{},", REGEX_OR) | BlankOrBreak());
Jesse Beder's avatar
Jesse Beder committed
126
127
128
  return e;
}
inline const RegEx& AnchorEnd() {
129
  static const RegEx e = RegEx("?:,]}%@`", REGEX_OR) | BlankOrBreak();
Jesse Beder's avatar
Jesse Beder committed
130
131
132
  return e;
}
inline const RegEx& URI() {
133
  static const RegEx e = Word() | RegEx("#;/?:@&=+$,_.!~*'()[]", REGEX_OR) |
Jesse Beder's avatar
Jesse Beder committed
134
135
136
137
                         (RegEx('%') + Hex() + Hex());
  return e;
}
inline const RegEx& Tag() {
138
  static const RegEx e = Word() | RegEx("#;/?:@&=+$_.~*'()", REGEX_OR) |
Jesse Beder's avatar
Jesse Beder committed
139
140
141
                         (RegEx('%') + Hex() + Hex());
  return e;
}
142

Jesse Beder's avatar
Jesse Beder committed
143
144
145
146
147
148
149
150
// Plain scalar rules:
// . Cannot start with a blank.
// . Can never start with any of , [ ] { } # & * ! | > \' \" % @ `
// . In the block context - ? : must be not be followed with a space.
// . In the flow context ? is illegal and : and - must not be followed with a
// space.
inline const RegEx& PlainScalar() {
  static const RegEx e =
151
152
      !(BlankOrBreak() | RegEx(",[]{}#&*!|>\'\"%@`", REGEX_OR) |
        (RegEx("-?:", REGEX_OR) + (BlankOrBreak() | RegEx())));
Jesse Beder's avatar
Jesse Beder committed
153
154
155
156
  return e;
}
inline const RegEx& PlainScalarInFlow() {
  static const RegEx e =
157
      !(BlankOrBreak() | RegEx("?,[]{}#&*!|>\'\"%@`", REGEX_OR) |
158
        (RegEx("-:", REGEX_OR) + (Blank() | RegEx())));
Jesse Beder's avatar
Jesse Beder committed
159
160
161
  return e;
}
inline const RegEx& EndScalar() {
162
  static const RegEx e = RegEx(':') + (BlankOrBreak() | RegEx());
Jesse Beder's avatar
Jesse Beder committed
163
164
165
166
  return e;
}
inline const RegEx& EndScalarInFlow() {
  static const RegEx e =
167
      (RegEx(':') + (BlankOrBreak() | RegEx() | RegEx(",]}", REGEX_OR))) |
Jesse Beder's avatar
Jesse Beder committed
168
169
170
      RegEx(",?[]{}", REGEX_OR);
  return e;
}
171

Scott Wolchok's avatar
Scott Wolchok committed
172
inline const RegEx& ScanScalarEndInFlow() {
173
  static const RegEx e = (EndScalarInFlow() | (BlankOrBreak() + Comment()));
Scott Wolchok's avatar
Scott Wolchok committed
174
175
176
177
  return e;
}

inline const RegEx& ScanScalarEnd() {
178
  static const RegEx e = EndScalar() | (BlankOrBreak() + Comment());
Scott Wolchok's avatar
Scott Wolchok committed
179
180
  return e;
}
Jesse Beder's avatar
Jesse Beder committed
181
182
183
184
185
186
187
188
inline const RegEx& EscSingleQuote() {
  static const RegEx e = RegEx("\'\'");
  return e;
}
inline const RegEx& EscBreak() {
  static const RegEx e = RegEx('\\') + Break();
  return e;
}
189

Jesse Beder's avatar
Jesse Beder committed
190
191
192
193
194
inline const RegEx& ChompIndicator() {
  static const RegEx e = RegEx("+-", REGEX_OR);
  return e;
}
inline const RegEx& Chomp() {
195
196
  static const RegEx e = (ChompIndicator() + Digit()) |
                         (Digit() + ChompIndicator()) | ChompIndicator() |
Jesse Beder's avatar
Jesse Beder committed
197
198
199
                         Digit();
  return e;
}
200

Jesse Beder's avatar
Jesse Beder committed
201
202
// and some functions
std::string Escape(Stream& in);
203
}  // namespace Exp
204

Jesse Beder's avatar
Jesse Beder committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
namespace Keys {
const char Directive = '%';
const char FlowSeqStart = '[';
const char FlowSeqEnd = ']';
const char FlowMapStart = '{';
const char FlowMapEnd = '}';
const char FlowEntry = ',';
const char Alias = '*';
const char Anchor = '&';
const char Tag = '!';
const char LiteralScalar = '|';
const char FoldedScalar = '>';
const char VerbatimTagStart = '<';
const char VerbatimTagEnd = '>';
219
220
}  // namespace Keys
}  // namespace YAML
221

Jesse Beder's avatar
Jesse Beder committed
222
#endif  // EXP_H_62B23520_7C8E_11DE_8A39_0800200C9A66