[r1]: trunk / Soul / Soul.Parsing / Rule.cpp Maximize Restore History

Download this file

Rule.cpp    176 lines (159 with data), 5.0 kB

  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
/*========================================================================
Copyright (c) 2011 Seppo Laakko
http://sourceforge.net/projects/soulparsing/
Distributed under the GNU General Public License, version 3 (GPLv3).
(See accompanying LICENSE.txt or http://www.gnu.org/licenses/gpl.html)
========================================================================*/
#include <Soul.Parsing/Rule.hpp>
#include <Soul.Parsing/Visitor.hpp>
#include <Soul.CppObjectModel/Visitor.hpp>
#include <map>
namespace Soul { namespace Parsing {
RuleParser::RuleParser(const String& name_, const String& fullName_, bool external_):
Parser("<" + name_ + ">"),
name(name_),
fullName(fullName_),
external(external_),
definition(),
inheritedAttributes(),
localVariables(),
valueTypeName(),
actions(),
actionScope(name + ".actions", "Action"),
nonTerminals(),
nonTerminalScope(name + ".nonterminals", "NonTerminal")
{
}
RuleParser::RuleParser(const String& name_, const String& fullName_, const ParserPtr& definition_):
Parser("<" + name_ + ">"),
name(name_),
fullName(fullName_),
external(false),
definition(definition_),
inheritedAttributes(),
localVariables(),
valueTypeName(),
actions(),
actionScope(name + ".actions", "Action"),
nonTerminals(),
nonTerminalScope(name + ".nonterminals", "NonTerminal")
{
}
void RuleParser::AddAction(const ActionParserPtr& action)
{
actionScope.Add(action);
actions.push_back(action);
}
ActionParserPtr RuleParser::GetAction(const String& actionName) const
{
return actionScope.Get(actionName);
}
void RuleParser::AddNonTerminal(const NonTerminalParserPtr& nonTerminal)
{
nonTerminalScope.Add(nonTerminal);
nonTerminals.push_back(nonTerminal);
}
NonTerminalParserPtr RuleParser::GetNonTerminal(const String& nonTerminalName) const
{
return nonTerminalScope.Get(nonTerminalName);
}
using Soul::CppObjectModel::IdExpr;
class CodeExpandingVisitor : public Soul::CppObjectModel::Visitor
{
public:
CodeExpandingVisitor(std::map<String, String>& expandMap_): expandMap(expandMap_) {}
virtual void Visit(IdExpr& expr)
{
std::map<String, String>::const_iterator i = expandMap.find(expr.Name());
if (i != expandMap.end())
{
expr.Name() = i->second;
}
}
private:
std::map<String, String>& expandMap;
};
void RuleParser::ExpandCode()
{
std::map<String, String> expandMap;
expandMap["value"] = "context->value";
int n = nonTerminals.size();
for (int i = 0; i < n; ++i)
{
NonTerminalParserPtr nonTerminal = nonTerminals[i];
expandMap[nonTerminal->Name()] = "context->" + nonTerminal->ValueFieldName();
}
n = inheritedAttributes.size();
for (int i = 0; i < n; ++i)
{
const AttrOrVariable& attr = inheritedAttributes[i];
expandMap[attr.Name()] = "context->" + attr.Name();
}
n = localVariables.size();
for (int i = 0; i < n; ++i)
{
const AttrOrVariable& var = localVariables[i];
expandMap[var.Name()] = "context->" + var.Name();
}
CodeExpandingVisitor visitor(expandMap);
n = actions.size();
for (int i = 0; i < n; ++i)
{
ActionParserPtr action = actions[i];
action->Code()->Accept(visitor);
}
n = nonTerminals.size();
for (int i = 0; i < n; ++i)
{
NonTerminalParserPtr nonTerminal = nonTerminals[i];
if (!nonTerminal->Arguments().empty())
{
int m = nonTerminal->Arguments().size();
for (int j = 0; j < m; ++j)
{
CppEntityPtr argument = nonTerminal->Arguments()[j];
argument->Accept(visitor);
}
}
}
}
Match RuleParser::Parse(Scanner& scanner, ObjectStack& stack)
{
bool writeToLog = !scanner.Skipping() && scanner.Log();
if (writeToLog)
{
scanner.Log()->WriteBeginRule(name);
scanner.Log()->IncIndent();
scanner.Log()->WriteTry(scanner.RestOfLine());
scanner.Log()->IncIndent();
}
int startIndex = scanner.GetPosition().index;
Enter(stack);
Match match = definition ? definition->Parse(scanner, stack) : Match::Nothing();
Leave(stack, match.Hit());
if (writeToLog)
{
scanner.Log()->DecIndent();
if (match.Hit())
{
scanner.Log()->WriteSuccess(scanner.GetMatch(startIndex));
}
else
{
scanner.Log()->WriteFail();
}
scanner.Log()->DecIndent();
scanner.Log()->WriteEndRule(name);
}
return match;
}
void RuleParser::Accept(Visitor& visitor)
{
visitor.BeginVisit(*this);
if (definition)
{
definition->Accept(visitor);
}
visitor.EndVisit(*this);
}
} } // namespace Soul.Parsing