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, return _moo_rule_keywords_new (kw_xml->words,
rule_xml_get_flags (xml), rule_xml_get_flags (xml),
kw_xml->prefix, kw_xml->suffix, kw_xml->prefix, kw_xml->suffix,
kw_xml->word_boundary,
rule_xml_get_style (xml)); rule_xml_get_style (xml));
} }

View File

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

View File

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

View File

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