| 1 |
// Copyright (c) 1996 James Clark, 2000 Matthias Clasen |
| 2 |
// See the file copying.txt for copying permission. |
| 3 |
|
| 4 |
#ifndef ProcessingMode_INCLUDED |
| 5 |
#define ProcessingMode_INCLUDED 1 |
| 6 |
|
| 7 |
#include <OpenSP/Named.h> |
| 8 |
#include <OpenSP/StringC.h> |
| 9 |
#include <OpenSP/Location.h> |
| 10 |
#include <OpenSP/Owner.h> |
| 11 |
#include <OpenSP/Vector.h> |
| 12 |
#include <OpenSP/NCVector.h> |
| 13 |
#include <OpenSP/NamedTable.h> |
| 14 |
#include "Expression.h" |
| 15 |
#include "Insn.h" |
| 16 |
#include <OpenSP/Boolean.h> |
| 17 |
#include "Node.h" |
| 18 |
#include "FOTBuilder.h" |
| 19 |
#include <OpenSP/IList.h> |
| 20 |
#include "Pattern.h" |
| 21 |
|
| 22 |
#ifdef DSSSL_NAMESPACE |
| 23 |
namespace DSSSL_NAMESPACE { |
| 24 |
#endif |
| 25 |
|
| 26 |
class Interpreter; |
| 27 |
class SosofoObj; |
| 28 |
|
| 29 |
class ProcessingMode : public Named { |
| 30 |
public: |
| 31 |
enum RuleType { |
| 32 |
styleRule, |
| 33 |
constructionRule |
| 34 |
}; |
| 35 |
enum { nRuleType = 2 }; |
| 36 |
|
| 37 |
class Specificity { |
| 38 |
public: |
| 39 |
Specificity(); |
| 40 |
bool isStyle() const; |
| 41 |
private: |
| 42 |
bool toInitial_; // 1 if the match fell through from a named processing mode to |
| 43 |
// the initial processing mode |
| 44 |
RuleType ruleType_; |
| 45 |
size_t nextRuleIndex_; |
| 46 |
friend class ProcessingMode; |
| 47 |
}; |
| 48 |
|
| 49 |
class Action : public Resource { |
| 50 |
public: |
| 51 |
Action(unsigned partIndex, Owner<Expression> &, const Location &); |
| 52 |
void compile(Interpreter &, RuleType); |
| 53 |
void get(InsnPtr &, SosofoObj *&) const; |
| 54 |
const Location &location() const; |
| 55 |
unsigned partIndex() const; |
| 56 |
private: |
| 57 |
Location defLoc_; |
| 58 |
Owner<Expression> expr_; |
| 59 |
// One of these will be non-null. |
| 60 |
InsnPtr insn_; |
| 61 |
// must be permanent |
| 62 |
SosofoObj *sosofo_; |
| 63 |
unsigned partIndex_; |
| 64 |
}; |
| 65 |
|
| 66 |
class ElementRule; |
| 67 |
class RootRule; |
| 68 |
class QueryRule; |
| 69 |
|
| 70 |
class Rule : virtual public MatchBase { |
| 71 |
public: |
| 72 |
Rule(); |
| 73 |
Rule(const Ptr<Action> &); |
| 74 |
const Action &action() const; |
| 75 |
Action &action(); |
| 76 |
virtual int compareSpecificity(const Rule &) const = 0; |
| 77 |
virtual int compareSpecificity2(const ElementRule *) const; |
| 78 |
virtual int compareSpecificity2(const RootRule *) const; |
| 79 |
virtual int compareSpecificity2(const QueryRule *) const; |
| 80 |
const Location &location() const; |
| 81 |
void swap(Rule &); |
| 82 |
private: |
| 83 |
Ptr<Action> action_; |
| 84 |
int compareParts(const Rule *) const; |
| 85 |
}; |
| 86 |
|
| 87 |
class ElementRule : public Rule, public Pattern, public Link { |
| 88 |
public: |
| 89 |
ElementRule(const Ptr<Action> &, Pattern &); |
| 90 |
int compareSpecificity(const Rule &) const; |
| 91 |
int compareSpecificity2(const ElementRule *) const; |
| 92 |
int compareSpecificity2(const QueryRule *) const; |
| 93 |
}; |
| 94 |
|
| 95 |
class QueryRule : public Rule, private Collector::DynamicRoot { |
| 96 |
public: |
| 97 |
QueryRule(const Ptr<Action> &, Owner<Expression> &, Owner<Expression> &, |
| 98 |
ProcessingMode *, Interpreter *); |
| 99 |
int compareSpecificity(const Rule &) const; |
| 100 |
int compareSpecificity2(const ElementRule *) const; |
| 101 |
int compareSpecificity2(const RootRule *) const; |
| 102 |
int compareSpecificity2(const QueryRule *) const; |
| 103 |
void trace(Collector &) const; |
| 104 |
bool matches(const NodePtr &, Pattern::MatchContext &) const; |
| 105 |
bool matches(ComponentName::Id) const; |
| 106 |
void compile(const NodePtr &); |
| 107 |
private: |
| 108 |
ProcessingMode *pm_; |
| 109 |
Interpreter *interp_; |
| 110 |
Owner<Expression> nlExpr_; |
| 111 |
mutable Owner<Expression> priorityExpr_; |
| 112 |
mutable NodeListObj *nl_; |
| 113 |
mutable long priority_; |
| 114 |
}; |
| 115 |
|
| 116 |
class RootRule : public Rule { |
| 117 |
public: |
| 118 |
RootRule(); |
| 119 |
RootRule(const Ptr<Action> &); |
| 120 |
int compareSpecificity(const Rule &) const; |
| 121 |
int compareSpecificity2(const RootRule *) const; |
| 122 |
int compareSpecificity2(const QueryRule *) const; |
| 123 |
bool matches(const NodePtr &, Pattern::MatchContext &) const; |
| 124 |
}; |
| 125 |
|
| 126 |
ProcessingMode(const StringC &, const ProcessingMode *initial = 0); |
| 127 |
void addElementRule(NCVector<Pattern> &, Owner<Expression> &expr, |
| 128 |
RuleType, const Location &, Interpreter &); |
| 129 |
void addRootRule(Owner<Expression> &expr, |
| 130 |
RuleType, const Location &, Interpreter &); |
| 131 |
void addQueryRule(Owner<Expression> &query, |
| 132 |
Owner<Expression> &expr, |
| 133 |
Owner<Expression> &priority, |
| 134 |
RuleType, |
| 135 |
const Location &, Interpreter &); |
| 136 |
// Specificity gives specificity of last match; gets specificity of current match. |
| 137 |
const Rule *findMatch(const NodePtr &, Pattern::MatchContext &, Messenger &, |
| 138 |
Specificity &) const; |
| 139 |
void compile(Interpreter &, const NodePtr &); |
| 140 |
bool defined() const; |
| 141 |
void setDefined(); |
| 142 |
bool hasCharRules() const; |
| 143 |
|
| 144 |
struct ElementRules : public Named { |
| 145 |
public: |
| 146 |
ElementRules(const StringC &); |
| 147 |
Vector<const Rule *> rules[nRuleType]; |
| 148 |
}; |
| 149 |
|
| 150 |
struct GroveRules { |
| 151 |
GroveRules(); |
| 152 |
bool built; |
| 153 |
NamedTable<ElementRules> elementTable; |
| 154 |
Vector<const Rule *> otherRules[nRuleType]; |
| 155 |
void build(const IList<ElementRule> *, const Vector<QueryRule *> *, |
| 156 |
const NodePtr &, Messenger &); |
| 157 |
}; |
| 158 |
private: |
| 159 |
const Rule *findElementMatch(const StringC &, const NodePtr &, |
| 160 |
Pattern::MatchContext &, Messenger &, |
| 161 |
Specificity &) const; |
| 162 |
const Rule *findRootMatch(const NodePtr &, Pattern::MatchContext &, Messenger &, |
| 163 |
Specificity &) const; |
| 164 |
const Rule *findQueryMatch(const NodePtr &, Pattern::MatchContext &, Messenger &, |
| 165 |
Specificity &) const; |
| 166 |
const GroveRules &groveRules(const NodePtr &, Messenger &) const; |
| 167 |
void ruleAdvance(const NodePtr &nd, Pattern::MatchContext &context, |
| 168 |
Messenger &mgr, Specificity &specificity, |
| 169 |
const Vector<const Rule *> &vec) const; |
| 170 |
void ruleAdvance(const NodePtr &nd, Pattern::MatchContext &context, |
| 171 |
Messenger &mgr, Specificity &specificity, |
| 172 |
const Vector<Rule *> &vec) const; |
| 173 |
void ruleAdvance(const NodePtr &nd, Pattern::MatchContext &context, |
| 174 |
Messenger &mgr, Specificity &specificity, |
| 175 |
const Vector<QueryRule *> &vec) const; |
| 176 |
|
| 177 |
Vector<Rule *> rootRules_[nRuleType]; |
| 178 |
Vector<QueryRule *> queryRules_[nRuleType]; |
| 179 |
IList<ElementRule> elementRules_[nRuleType]; |
| 180 |
NCVector<GroveRules> groveRules_; |
| 181 |
const ProcessingMode *initial_; // 0 for initial mode |
| 182 |
bool defined_; |
| 183 |
bool hasCharRules_; |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
inline |
| 188 |
bool ProcessingMode::defined() const |
| 189 |
{ |
| 190 |
return defined_; |
| 191 |
} |
| 192 |
|
| 193 |
inline |
| 194 |
void ProcessingMode::setDefined() |
| 195 |
{ |
| 196 |
defined_ = 1; |
| 197 |
} |
| 198 |
|
| 199 |
inline |
| 200 |
bool ProcessingMode::hasCharRules() const |
| 201 |
{ |
| 202 |
if (hasCharRules_) |
| 203 |
return 1; |
| 204 |
if (initial_) |
| 205 |
return initial_->hasCharRules(); |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
inline |
| 210 |
ProcessingMode::Specificity::Specificity() |
| 211 |
: toInitial_(0), nextRuleIndex_(0), ruleType_(styleRule) |
| 212 |
{ |
| 213 |
} |
| 214 |
|
| 215 |
inline |
| 216 |
bool ProcessingMode::Specificity::isStyle() const |
| 217 |
{ |
| 218 |
return ruleType_ == styleRule; |
| 219 |
} |
| 220 |
|
| 221 |
inline |
| 222 |
void ProcessingMode::Action::get(InsnPtr &insn, SosofoObj *&sosofo) const |
| 223 |
{ |
| 224 |
insn = insn_; |
| 225 |
sosofo = sosofo_; |
| 226 |
} |
| 227 |
|
| 228 |
inline |
| 229 |
const Location &ProcessingMode::Action::location() const |
| 230 |
{ |
| 231 |
return defLoc_; |
| 232 |
} |
| 233 |
|
| 234 |
inline |
| 235 |
ProcessingMode::Action &ProcessingMode::Rule::action() |
| 236 |
{ |
| 237 |
return *action_; |
| 238 |
} |
| 239 |
|
| 240 |
inline |
| 241 |
const ProcessingMode::Action &ProcessingMode::Rule::action() const |
| 242 |
{ |
| 243 |
return *action_; |
| 244 |
} |
| 245 |
|
| 246 |
inline |
| 247 |
unsigned ProcessingMode::Action::partIndex() const |
| 248 |
{ |
| 249 |
return partIndex_; |
| 250 |
} |
| 251 |
|
| 252 |
inline |
| 253 |
const Location &ProcessingMode::Rule::location() const |
| 254 |
{ |
| 255 |
return action_->location(); |
| 256 |
} |
| 257 |
|
| 258 |
inline |
| 259 |
void ProcessingMode::Rule::swap(Rule &r) |
| 260 |
{ |
| 261 |
action_.swap(r.action_); |
| 262 |
} |
| 263 |
|
| 264 |
#ifdef DSSSL_NAMESPACE |
| 265 |
} |
| 266 |
#endif |
| 267 |
|
| 268 |
#endif /* not ProcessingMode_INCLUDED */ |