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
// Dedicated to the public domain by Christopher Diggins
// http://www.cdiggins.com
//
// Contains definitions for sample parser management classes
// to be used with the YARD framework

#ifndef YARD_PARSER_HPP
#define YARD_PARSER_HPP

namespace yard
{
////////////////////////////////////////////////////////////////////////
// A skeleton parser base class

template<typename Token_T, typename Iter_T = const Token_T*>
struct BasicParser
{
// Public typedefs
typedef Iter_T Iterator;
typedef Token_T Token;

// Constructor
BasicParser()
{ }

// Parse function
template<typename StartRule_T>
bool Parse(Iterator first, Iterator last)
{
mBegin = first;
mEnd = last;
mIter = first;

try {
return StartRule_T::Match(*this);
}
catch(...) {
return false;
}
}

// Input pointer functions
Token GetElem() { return *mIter; }
void GotoNext() { assert(mIter < End()); ++mIter; }
Iterator GetPos() { return mIter; }
void SetPos(Iterator pos) { mIter = pos; }
bool AtEnd() { return GetPos() >= End(); }
Iterator Begin() { return mBegin; }
Iterator End() { return mEnd; }

template<typename T>
void LogMessage(const T& x)
{ }

protected:

// Member fields
Iterator mBegin;
Iterator mEnd;
Iterator mIter;
};

////////////////////////////////////////////////////////////////////////
// Extends the basic parser with abstract syntax tree (AST)
// building capabilities

template<typename Token_T, typename Iter_T = const Token_T*>
struct TreeBuildingParser : BasicParser<Token_T, Iter_T>
{
// Constructor
TreeBuildingParser()
: BasicParser<Token_T, Iter_T>(), mTree()
{ }

// Public typedefs
typedef typename Iter_T Iterator;
typedef typename Token_T Token;
typedef typename Ast<Iterator> Tree;
typedef typename Tree::AbstractNode Node;

void OutputLocation()
{
char line[257];
int nLine = 1;
Iterator pFirst = this->GetPos();

Iterator pTmp = this->mBegin;
while (pTmp < pFirst) {
if (*pTmp++ == '\n')
++nLine;
}

while (pFirst > this->mBegin && *pFirst != '\n')
pFirst--;
if (*pFirst == '\n') {
++pFirst;
}
Iterator pLast = this->GetPos();
while (pLast < this->mEnd && *pLast != '\n') {
pLast++;
}
size_t n = pLast - pFirst;
n = n < 255 ? n : 255;
strncpy(line, pFirst, n);
line[n] = '\0';

char marker[256];
n = this->GetPos() - pFirst;
n = n < 254 ? n : 254;
for (size_t i=0; i < n; ++i)
marker[i] = ' ';
marker[n] = '^';
marker[n + 1] = '\0';

fprintf(stderr, "line number %d\n", nLine);
fprintf(stderr, "character number %d\n", this->GetPos() - this->mBegin);
fprintf(stderr, "%s\n", line);
fprintf(stderr, "%s\n", marker);
}

// Parse function
template<typename StartRule_T>
bool Parse(Iterator first, Iterator last)
{
this->mBegin = first;
this->mEnd = last;
this->mIter = first;

try {
return StartRule_T::Match(*this);
}
catch(...) {
fprintf(stderr, "parsing error occured\n");
OutputLocation();
return false;
}
}

// AST functions
Node* GetAstRoot() { return mTree.GetRoot(); }
template<typename Rule_T>
void CreateNode() { mTree.CreateNode<Rule_T>(*this); }
void CompleteNode() { mTree.CompleteNode(*this); }
void AbandonNode() { mTree.AbandonNode(*this); }

protected:

// Member fields
Tree mTree;
};
}

#endif
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

r26 by cdiggins on Feb 17, 2008   Diff
Slowly getting there.
r22 by cdiggins on Feb 17, 2008   Diff
Generating some working Java.
r13 by cdiggins on Feb 13, 2008   Diff

 
All revisions of this file

File info

Size: 3762 bytes, 153 lines