Added back word boundary check to keyword rule; but it's checked without \b in the regular expression

master
Yevgen Muntyan 2006-04-29 02:49:59 -05:00
parent a699621d65
commit eda83c059a
4 changed files with 133 additions and 110 deletions

View File

@ -2074,6 +2074,7 @@ rule_keywords_xml_create_rule (RuleKeywordsXML *xml,
return _moo_rule_keywords_new (kw_xml->words,
rule_xml_get_flags (xml),
kw_xml->prefix, kw_xml->suffix,
kw_xml->word_boundary,
rule_xml_get_style (xml));
}

View File

@ -104,7 +104,7 @@ typedef MooRuleMatchFlags MatchFlags;
#define MooRuleAnyChar MooRuleAsciiAnyChar
typedef MooRule* (*MatchFunc) (MooRule *self,
MooRuleMatchData *data,
const MooRuleMatchData *data,
MooRuleMatchResult *result,
MooRuleMatchFlags flags);
typedef void (*DestroyFunc) (MooRule *self);
@ -474,7 +474,7 @@ _moo_rule_set_end_switch (MooRule *rule,
static MooRule*
rule_string_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -564,7 +564,7 @@ _moo_rule_string_new (const char *string,
static MooRule*
rule_regex_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -572,16 +572,19 @@ rule_regex_match (MooRule *rule,
/* XXX line start and stuff */
int n_matches, start_pos, end_pos;
EggRegexMatchFlags regex_flags = 0;
egg_regex_clear (rule->regex.regex);
char *start = data->start;
if (flags & MATCH_START_ONLY)
regex_flags |= EGG_REGEX_MATCH_ANCHORED;
while (start <= data->limit)
{
egg_regex_clear (rule->regex.regex);
n_matches = egg_regex_match_extended (rule->regex.regex,
data->line_string,
data->line_string_len,
data->start - data->line_string,
start - data->line_string,
regex_flags);
if (n_matches < 1)
@ -595,13 +598,29 @@ rule_regex_match (MooRule *rule,
result->match_start = data->line_string + start_pos;
result->match_end = data->line_string + end_pos;
result->match_len = -1;
result->match_offset = -1;
if (rule->regex.left_word_bndry && result->match_start > data->line_string &&
CHAR_IS_WORD (result->match_start[0]) && CHAR_IS_WORD (result->match_start[-1]))
{
start = result->match_start + 1;
continue;
}
if (rule->regex.right_word_bndry && result->match_end > data->line_string &&
CHAR_IS_WORD (result->match_end[0]) && CHAR_IS_WORD (result->match_end[-1]))
{
start = result->match_start + 1;
continue;
}
return rule;
}
return NULL;
}
static void
rule_regex_destroy (MooRule *rule)
@ -674,7 +693,7 @@ _moo_rule_regex_new (const char *pattern,
static MooRule*
rule_char_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -714,7 +733,7 @@ rule_char_match (MooRule *rule,
static MooRule*
rule_2char_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -804,7 +823,7 @@ _moo_rule_2char_new (char ch1,
static MooRule*
rule_any_char_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -904,6 +923,7 @@ _moo_rule_keywords_new (GSList *words,
MooRuleFlags flags,
const char *prefix,
const char *suffix,
gboolean word_boundary,
const char *style)
{
GSList *l;
@ -936,6 +956,13 @@ _moo_rule_keywords_new (GSList *words,
suffix ? suffix : "");
rule = _moo_rule_regex_new (pattern->str, TRUE, 0, 0, flags, style);
g_return_val_if_fail (rule != NULL, NULL);
if (word_boundary)
{
rule->regex.left_word_bndry = TRUE;
rule->regex.right_word_bndry = TRUE;
}
out:
g_string_free (pattern, TRUE);
@ -949,11 +976,12 @@ out:
static MooRule*
rule_include_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
return rules_match_real (rule->incl.ctx->rules, data, result, flags);
return rules_match_real (rule->incl.ctx->rules,
(MatchData*) data, result, flags);
}
@ -981,7 +1009,7 @@ _moo_rule_include_new (MooContext *ctx)
static MooRule*
rule_int_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -1028,7 +1056,7 @@ _moo_rule_int_new (MooRuleFlags flags,
static MooRule*
rule_float_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -1117,7 +1145,7 @@ _moo_rule_float_new (MooRuleFlags flags,
static MooRule*
rule_octal_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -1170,7 +1198,7 @@ _moo_rule_octal_new (MooRuleFlags flags,
static MooRule*
rule_hex_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -1223,7 +1251,7 @@ _moo_rule_hex_new (MooRuleFlags flags,
static MooRule*
rule_escaped_char_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -1302,7 +1330,7 @@ _moo_rule_escaped_char_new (MooRuleFlags flags,
static MooRule*
rule_c_char_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -1323,19 +1351,10 @@ rule_c_char_match (MooRule *rule,
if (start[1] != '\\')
{
if (start[2] != '\'')
{
start = start + 2;
start++;
continue;
}
result->match_start = start;
result->match_end = start + 3;
result->match_len = 3;
result->match_offset = -1;
return rule;
}
switch (start[2])
{
case '\\':
@ -1414,7 +1433,7 @@ _moo_rule_c_char_new (MooRuleFlags flags,
static MooRule*
rule_whitespace_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
G_GNUC_UNUSED MatchFlags flags)
{
@ -1447,7 +1466,7 @@ _moo_rule_whitespace_new (MooRuleFlags flags,
static MooRule*
rule_identifier_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{
@ -1492,7 +1511,7 @@ _moo_rule_identifier_new (MooRuleFlags flags,
static MooRule*
rule_line_continue_match (MooRule *rule,
MatchData *data,
const MatchData *data,
MatchResult *result,
MatchFlags flags)
{

View File

@ -75,6 +75,7 @@ MooRule *_moo_rule_keywords_new (GSList *words,
MooRuleFlags flags,
const char *prefix,
const char *suffix,
gboolean word_boundary,
const char *style);
MooRule *_moo_rule_zero_new (MooRuleFlags flags);
MooRule *_moo_rule_include_new (MooContext *context);

View File

@ -128,6 +128,8 @@ typedef struct {
typedef struct {
gpointer regex; /* EggRegex* */
guint non_empty : 1;
guint left_word_bndry : 1;
guint right_word_bndry : 1;
} MooRuleRegex;
typedef struct {
@ -152,7 +154,7 @@ typedef struct {
struct _MooRule
{
MooRule* (*match) (MooRule *self,
MooRuleMatchData *data,
const MooRuleMatchData *data,
MooRuleMatchResult *result,
MooRuleMatchFlags flags);
void (*destroy) (MooRule *self);