What's new? | Help | Directory | Sign in
Google
heron-language
Heron is an object-oriented programming language for model driven architecture
  
  
  
  
    
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Public domain, by Christopher Diggins
// http://www.cdiggins.com
//
// This file defines the core YARD grammar rules for text parsing.
// Each type in this file represents a separate BNF grammar production
// (parsing rule).

#ifndef YARD_TEXT_GRAMMAR_HPP
#define YARD_TEXT_GRAMMAR_HPP

namespace yard
{
namespace text_grammar
{
// accepts a single char, returns false only if at the end of the file
struct AnyChar
{
template<typename ParserState_T>
static bool Match(ParserState_T& p) {
if (p.AtEnd()) {
return false;
};
p.GotoNext();
return true;
}
};

// accepts a single char as specified by the template parameter
template<char C>
struct Char
{
template<typename ParserState_T>
static bool Match(ParserState_T& p) {
if (p.AtEnd()) { return false; }
if (p.GetElem() == C) { p.GotoNext(); return true; }
return false;
}
};

// consume a single character but prints an error message and throws an exception,
// if the character is not found in the input stream.
template<char C>
struct ExpectChar {
template<typename ParserState_T>
static bool Match(ParserState_T& p) {
if (p.AtEnd() || p.GetElem() != C)
{
printf("expected character: %c\n", C);
throw 0;
}
p.GotoNext();
return true;
}
};

// accepts anything except a specific Char
// equivalent to Seq<Not<Char<C>, AnyChar> > but more efficient
template<char C>
struct NotChar
{
template<typename ParserState_T>
static bool Match(ParserState_T& p) {
if (p.AtEnd()) { return false; }
if (p.GetElem() != C) { p.GotoNext(); return true; }
return false;
}
};

// CharSetParser matches a single character in the character set
template<typename CharSet_T>
struct CharSetParser
{
static const CharSet_T cs;

template<typename ParserState_T>
static bool Match(ParserState_T& p) {
if (p.AtEnd()) return false;
if (cs.a[p.GetElem()]) {
p.GotoNext();
return true;
}
return false;
}
};

template<typename CharSet_T>
const CharSet_T CharSetParser<CharSet_T>::cs;

// accepts any single char, except for the specified character set
template<typename CharSet_T>
struct AnyCharExcept
: Seq<NotAt<CharSetParser<CharSet_T> >, AnyChar>
{ };

// CharSetRangeParser is a shorthand for CharSetParser<char_set_range>
template<char C0, char C1>
struct CharRange : CharSetParser<CharSetRange<C0, C1> > { };

// decimal digit parser
struct Digit : CharSetParser<DigitCharSet> { };

// binary digit parser
struct BinDigit : Or<Char<'0'>, Char<'1'> > { };

// hexadecimal digit parser
struct HexDigit : CharSetParser<HexDigitCharSet> { };

// octal digit parser
struct OctDigit : CharSetParser<OctDigitCharSet> { };

// parses letters and underscores
struct IdentFirstChar : CharSetParser<IdentFirstCharSet> { };

// parses letters and underscores and numbers
struct IdentNextChar : CharSetParser<IdentNextCharSet> { };

// parses letters
struct Letter : CharSetParser<LetterCharSet> { };

// parses letters or numbers
struct AlphaNum : CharSetParser<AlphaNumCharSet> { };

// Not an alpha numeric character or underscore
struct NotAlphaNum : NotAt<IdentNextChar> { };

// parses lower case letters
struct LowerCaseLetter : CharSetParser<LowerCaseLetterCharSet> { };

// parses upper case letters
struct UpperCaseLetter : CharSetParser<UpperCaseLetterCharSet> { };

// Ident matches C/C++/Java/etc. identifiers. A letter or underscore followed
// by a sequence of letters, underscores and numbers of arbitrary length
struct Ident
{
template<typename ParserState_T>
static bool Match(ParserState_T& p) {
if (p.AtEnd()) { return false; }
if (IdentFirstChar::template Match(p))
{
while (IdentNextChar::template Match(p)) { }
return true;
}
return false;
}
};

// this represents the NULL string
struct NS {
static char GetChar(int n) {
return '\0';
}
};

// this matches a sequence of characters
template
<
char C0 = '\0', char C1 = '\0', char C2 = '\0', char C3 = '\0',
char C4 = '\0', char C5 = '\0', char C6 = '\0', char C7 = '\0',
char C8 = '\0', char C9 = '\0', char C10 = '\0', char C11 = '\0',
char C12 = '\0', char C13 = '\0', char C14 = '\0', char C15 = '\0'
>
struct CharSeq
{
static char GetChar(int n) {
switch(n) {
case (0) : return C0;
case (1) : return C1;
case (2) : return C2;
case (3) : return C3;
case (4) : return C4;
case (5) : return C5;
case (6) : return C6;
case (7) : return C7;
case (8) : return C8;
case (9) : return C9;
case (10) : return C10;
case (11) : return C11;
case (12) : return C12;
case (13) : return C13;
case (14) : return C14;
case (15) : return C15;
default : assert(false && "maximum length of strings is 16 chars");
}
return '\0';
}

template<typename ParserState_T>
static bool Match(ParserState_T& p) {
typename ParserState_T::Iterator pos = p.GetPos();
int n = 0;
char ch = GetChar(n);
while (ch != '\0')
{
if (p.AtEnd() || p.GetElem() != ch)
{
p.SetPos(pos);
return false;
}
p.GotoNext();
ch = GetChar(++n);
}
return true;
}
};

// this matches sequences of lower case characters, ignoring the case
// of the input
template
<
char C0 = '\0', char C1 = '\0', char C2 = '\0', char C3 = '\0',
char C4 = '\0', char C5 = '\0', char C6 = '\0', char C7 = '\0',
char C8 = '\0', char C9 = '\0', char C10 = '\0', char C11 = '\0',
char C12 = '\0', char C13 = '\0', char C14 = '\0', char C15 = '\0'
>
struct CharSeqIgnoreCase
{
static char GetChar(int n) {
switch(n) {
case (0) : return C0;
case (1) : return C1;
case (2) : return C2;
case (3) : return C3;
case (4) : return C4;
case (5) : return C5;
case (6) : return C6;
case (7) : return C7;
case (8) : return C8;
case (9) : return C9;
case (10) : return C10;
case (11) : return C11;
case (12) : return C12;
case (13) : return C13;
case (14) : return C14;
case (15) : return C15;
default : assert(false && "maximum length of strings is 16 chars");
}
return '\0';
}

static char ToLowercase(char x)
{
if (x >= 'A' && x <= 'Z')
return 'a' + (x - 'A');
return x;
}

template<typename ParserState_T>
static bool Match(ParserState_T& p) {
typename ParserState_T::Iterator pos = p.GetPos();
for (int n = 0; GetChar(n) != '\0'; ++n) {
if (p.AtEnd())
{
p.SetPos(pos);
return false;
}
if (ToLowercase(p.GetElem()) != GetChar(n))
{
p.SetPos(pos);
return false;
}
p.GotoNext();
}
return true;
}
};

// this matches parser types which end a word boundary
template<typename T>
struct Word :
Seq<
T,
NotAt<IdentNextChar>
>
{ };

template<typename T>
struct DoubleQuoted :
Seq<Char<'\"'>, T, Char<'\"'> >
{ };

template<typename T>
struct SingleQuoted :
Seq<Char<'\''>, T, Char<'\''> >
{ };

template<typename T>
struct SingleOrDoubleQuoted :
Or<SingleQuoted<T>, DoubleQuoted<T> >
{ };

} // text_grammar
} // yard

#endif // #ifndef YARD_TEXT_GRAMMAR_HPP
Show details Hide details

Change log

r43 by cdiggins on Mar 25, 2008   Diff
Improved C++ compliance of YARD library.
Started using unit tests again. Made a
bunch of improvements.
Go to: 
Project members, sign in to write a code review

Older revisions

r2 by cdiggins on Jan 30, 2008   Diff

 
All revisions of this file

File info

Size: 7487 bytes, 295 lines