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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
//
// Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2004-2008
//
// Copyright: See COPYING file that comes with this distribution
//
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <exception>
#include <cctype>
#include "highlightstatebuilder.hpp"
#include "langelems.h"
#include "stringlistlangelem.h"
#include "delimitedlangelem.h"
#include "highlightbuilderexception.h"
#include "highlightrulefactory.h"
#include "highlightrule.h"
#include "stringdef.h"
#include "namedsubexpslangelem.h"
#include "regexpreprocessor.h"
#include "statelangelem.h"
#include <sstream>
namespace srchilite {
/**
* Sets the exit level of the rule using the information contained in the passed elem
* @param elem
* @param rule
* @param inc the possible additional incrementation of the exit level
*/
static void setExitLevel(const StateStartLangElem *elem, HighlightRule *rule,
unsigned int inc = 0);
/**
* A string is to matched as isolated (word boundaries) basically if it is an alphanumerical
* string or a _.
*
* Notice that this should be called only on strings specified in double quotes,
* since other expressions are intended as regular expressions and should not
* be isolated. This is checked in the code that calls this function.
*
* @param s
* @return
*/
static bool is_to_isolate(const string &s);
HighlightStateBuilder::HighlightStateBuilder(
HighlightRuleFactory *_highlightRuleFactory) :
highlightRuleFactory(_highlightRuleFactory) {
}
HighlightStateBuilder::~HighlightStateBuilder() {
}
bool is_to_isolate(const string &s) {
if (s.size()) {
if ((isalnum(s[0]) || s[0] == '_') && (isalnum(s[s.size() - 1])
|| s[s.size() - 1] == '_'))
return true;
}
return false;
}
void HighlightStateBuilder::build(LangElems *elems, HighlightStatePtr mainState) {
if (!elems)
return;
for (LangElems::const_iterator it = elems->begin(); it != elems->end(); ++it) {
try {
build_DB(*it, mainState.get());
} catch (boost::regex_error &e) {
// catch all other exceptions
throw HighlightBuilderException("problem in this expression: "
+ (*it)->toStringOriginal(), *it, e);
}
}
}
void HighlightStateBuilder::build(LangElem *elem, HighlightState *state) {
// no common operation for the moment
}
void HighlightStateBuilder::build(StringListLangElem *elem,
HighlightState *state) {
const string &name = elem->getName();
StringDefs *alternatives = elem->getAlternatives();
WordList wordList;
bool doubleQuoted = false, nonDoubleQuoted = false, buildAsWordList = true;
for (StringDefs::const_iterator it = alternatives->begin(); it
!= alternatives->end(); ++it) {
const string &rep = (*it)->toString();
// double quoted strings generate WordListRules, otherwise simple ListRules
// we don't allow double quoted strings mixed with non double quoted
if (((*it)->isDoubleQuoted() && nonDoubleQuoted)
|| (!(*it)->isDoubleQuoted() && doubleQuoted)) {
throw HighlightBuilderException(
"cannot mix double quoted and non double quoted", elem);
}
doubleQuoted = (*it)->isDoubleQuoted();
nonDoubleQuoted = !(*it)->isDoubleQuoted();
wordList.push_back(rep);
// now check whether we must build a word list rule (word boundary) or an
// ordinary list; as soon as we find something that is not to be isolated
// we set buildAsWordList as false
if (buildAsWordList && (!doubleQuoted || !is_to_isolate(rep))) {
buildAsWordList = false;
}
}
HighlightRulePtr rule;
if (buildAsWordList)
rule = HighlightRulePtr(highlightRuleFactory->createWordListRule(name,
wordList, elem->isCaseSensitive()));
else
rule = HighlightRulePtr(highlightRuleFactory->createListRule(name,
wordList, elem->isCaseSensitive()));
rule->setAdditionalInfo(elem->toStringParserInfo());
state->addRule(rule);
setExitLevel(elem, rule.get());
}
void HighlightStateBuilder::build(DelimitedLangElem *elem,
HighlightState *state) {
const string &name = elem->getName();
StringDef *start = elem->getStart();
StringDef *end = elem->getEnd();
StringDef *escape = elem->getEscape();
string start_string = (start ? start->toString() : "");
string end_string = (end ? end->toString() : "");
string escape_string = (escape ? escape->toString() : "");
if (elem->isNested() && start_string == end_string) {
// the two delimiters must be different for nested elements
throw HighlightBuilderException(
"delimiters must be different for nested elements", elem);
}
bool end_string_has_references = false;
// check possible back reference markers and their correctness
if (end && end->hasBackRef() && end_string.size()) {
backreference_info ref_info = RegexPreProcessor::num_of_references(
end_string);
subexpressions_info info =
RegexPreProcessor::num_of_marked_subexpressions(start_string,
true, true);
// possible errors, e.g., unbalanced parenthesis
if (info.errors.size()) {
throw HighlightBuilderException(info.errors, elem);
}
// check that there are enough subexpressions as requested by the maximal
// back reference number
unsigned int max = ref_info.second;
if (max > info.marked) {
std::ostringstream error;
error << max << " subexpressions requested, but only "
<< info.marked << " found";
throw HighlightBuilderException(error.str(), elem);
}
end_string_has_references = true;
}
HighlightRulePtr rule;
// if this element starts a new state/environment, we must split it
if (elem->getStateLangElem() || elem->isMultiline()
|| end_string_has_references) {
rule = HighlightRulePtr(highlightRuleFactory->createMultiLineRule(name,
start_string, end_string, escape_string, elem->isNested()));
if (end_string_has_references) {
// record that the state (and the rule representing the end)
// need to have dynamic back references replaced
rule->getNextState()->setNeedsReferenceReplacement();
rule->getNextState()->getRuleList().front()->setNeedsReferenceReplacement();
// and that the starting rule has sub expressions
// (that will be used for replacing dynamic back references)
rule->setHasSubexpressions();
// if the element is nested, then the last rule is a sort of copy
// of the first one, so we need to record that it has subexpressions too
if (elem->isNested()) {
rule->getNextState()->getRuleList().back()->setHasSubexpressions();
}
}
} else {
rule = HighlightRulePtr(highlightRuleFactory->createLineRule(name,
start_string, end_string, escape_string, elem->isNested()));
}
rule->setAdditionalInfo(elem->toStringParserInfo());
state->addRule(rule);
if (rule->getNextState().get()) {
// as for exit level, if the rule was split using states, we must set
// the exit level of the first rule of the next state (i.e., the end expression) of the rule
// this exit level must be incremented by one: 1 is for exiting the inner state
// of the rule, and 1 for exiting the state this rule belongs to
setExitLevel(elem, rule->getNextState()->getRuleList().front().get(), 1);
// adjust the additional info of the exiting rule
rule->getNextState()->getRuleList().front()->setAdditionalInfo(
elem->toStringParserInfo());
// since this is a delimited element, we must set the default element for
// the inner state to the name of the element itself
rule->getNextState()->setDefaultElement(name);
} else {
setExitLevel(elem, rule.get());
}
}
void HighlightStateBuilder::build(NamedSubExpsLangElem *elem,
HighlightState *state) {
const ElementNames *elems = elem->getElementNames();
const StringDef *regexp = elem->getRegexpDef();
const string ®exp_string = regexp->toString();
// first check that the number of marked subexpressions is the same of
// the specified element names
subexpressions_info sexps =
RegexPreProcessor::num_of_marked_subexpressions(regexp_string);
if (sexps.errors.size()) {
throw HighlightBuilderException(sexps.errors, elem);
}
if (sexps.marked != elems->size()) {
throw HighlightBuilderException(
"number of marked subexpressions does not match number of elements",
elem);
}
HighlightRulePtr rule = HighlightRulePtr(
highlightRuleFactory->createCompoundRule(*elems, regexp_string));
rule->setAdditionalInfo(elem->toStringParserInfo());
state->addRule(rule);
setExitLevel(elem, rule.get());
}
void HighlightStateBuilder::build(StateLangElem *elem, HighlightState *state) {
StateStartLangElem *statestart = elem->getStateStart();
if (!elem->isState() && dynamic_cast<NamedSubExpsLangElem *> (statestart)) {
throw HighlightBuilderException(
"cannot use this element for environments (only for states)",
statestart);
}
/*
First act on the element that defines this new State
*/
build_DB(statestart, state);
/*
The last rule corresponds to the rule added for
the element that defines this state, i.e., statestart
*/
HighlightRulePtr last = state->getRuleList().back();
/*
We must make sure that this rule has a next state,
since we will use it to populate it with the elements of
this state.
For instance, a StringListLangElem does not have a next state,
so we must create one.
*/
if (!last->getNextState().get()) {
last->setNextState(HighlightStatePtr(new HighlightState));
}
/*
Use the last rule next state to populate it with
the elements of this State/Environment element
*/
HighlightStatePtr inner = last->getNextState();
/*
If it's a State then the default formatter corresponds to NORMAL,
otherwise (Environment) the default formatter is the same of
the element itself.
*/
if (elem->isState()) {
inner->setDefaultElement("normal");
} else {
// at this point we already checked that the rule represents only one element
inner->setDefaultElement(last->getElemList().front());
}
last->setAdditionalInfo(statestart->toStringParserInfo());
LangElems *elems = elem->getElems();
build(elems, inner);
}
void setExitLevel(const StateStartLangElem *elem, HighlightRule *rule,
unsigned int inc) {
if (elem->exitAll()) {
rule->setExitLevel(-1); // exit all
} else if (elem->getExit()) {
rule->setExitLevel(elem->getExit() + inc);
}
}
#include "highlightstatebuilder_dbtab.cc"
}
|