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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*-
*
* This file is part of GtkSourceView
*
* Copyright (C) 2003 - Gustavo Giráldez <gustavo.giraldez@gmx.net>
* Copyright (C) 2005, 2006 - Marco Barisione, Emanuele Aina
*
* GtkSourceView is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* GtkSourceView is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gtksourceregex.h"
#include <string.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include "gtksourceutils-private.h"
/*
* GRegex wrapper which adds a few features needed for syntax highlighting,
* in particular resolving "\%{...@start}" and forbidding the use of \C.
*/
/* Regex used to match "\%{...@start}". */
static GRegex *
get_start_ref_regex (void)
{
static GRegex *start_ref_regex = NULL;
if (start_ref_regex == NULL)
{
start_ref_regex = g_regex_new ("(?<!\\\\)(\\\\\\\\)*\\\\%\\{(.*?)@start\\}",
G_REGEX_OPTIMIZE, 0, NULL);
}
return start_ref_regex;
}
struct _GtkSourceRegex
{
union {
struct {
gchar *pattern;
GRegexCompileFlags flags;
} info;
struct {
GRegex *regex;
GMatchInfo *match;
} regex;
} u;
guint ref_count;
guint resolved : 1;
};
/* Check whether pattern contains \C escape sequence, which means "single byte"
* in pcre and naturally leads to crash if used for highlighting.
*/
static gboolean
find_single_byte_escape (const gchar *str)
{
return _gtk_source_utils_find_escaped_char (str, 'C') != NULL;
}
/**
* gtk_source_regex_new:
* @pattern: the regular expression.
* @flags: compile options for @pattern.
* @error: location to store the error occuring, or %NULL to ignore errors.
*
* Creates a new regex.
*
* Returns: a newly-allocated #GtkSourceRegex.
*/
GtkSourceRegex *
_gtk_source_regex_new (const gchar *pattern,
GRegexCompileFlags flags,
GError **error)
{
GtkSourceRegex *regex;
g_return_val_if_fail (pattern != NULL, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
if (find_single_byte_escape (pattern))
{
g_set_error_literal (error, G_REGEX_ERROR,
G_REGEX_ERROR_COMPILE,
_("using \\C is not supported in language definitions"));
return NULL;
}
regex = g_new0 (GtkSourceRegex, 1);
regex->ref_count = 1;
if (g_regex_match (get_start_ref_regex (), pattern, 0, NULL))
{
regex->resolved = FALSE;
regex->u.info.pattern = g_strdup (pattern);
regex->u.info.flags = flags;
}
else
{
regex->resolved = TRUE;
regex->u.regex.regex = g_regex_new (pattern,
flags | G_REGEX_OPTIMIZE | G_REGEX_NEWLINE_LF, 0,
error);
if (regex->u.regex.regex == NULL)
{
g_free (regex);
regex = NULL;
}
}
return regex;
}
GtkSourceRegex *
_gtk_source_regex_ref (GtkSourceRegex *regex)
{
if (regex != NULL)
regex->ref_count++;
return regex;
}
void
_gtk_source_regex_unref (GtkSourceRegex *regex)
{
if (regex != NULL && --regex->ref_count == 0)
{
if (regex->resolved)
{
g_regex_unref (regex->u.regex.regex);
if (regex->u.regex.match)
g_match_info_free (regex->u.regex.match);
}
else
{
g_free (regex->u.info.pattern);
}
g_free (regex);
}
}
struct RegexResolveData {
GtkSourceRegex *start_regex;
const gchar *matched_text;
};
static gboolean
replace_start_regex (const GMatchInfo *match_info,
GString *expanded_regex,
gpointer user_data)
{
gchar *num_string, *subst, *subst_escaped, *escapes;
gint num;
struct RegexResolveData *data = user_data;
escapes = g_match_info_fetch (match_info, 1);
num_string = g_match_info_fetch (match_info, 2);
num = _gtk_source_utils_string_to_int (num_string);
if (num < 0)
{
subst = g_match_info_fetch_named (data->start_regex->u.regex.match,
num_string);
}
else
{
subst = g_match_info_fetch (data->start_regex->u.regex.match,
num);
}
if (subst != NULL)
{
subst_escaped = g_regex_escape_string (subst, -1);
}
else
{
g_warning ("Invalid group: %s", num_string);
subst_escaped = g_strdup ("");
}
g_string_append (expanded_regex, escapes);
g_string_append (expanded_regex, subst_escaped);
g_free (escapes);
g_free (num_string);
g_free (subst);
g_free (subst_escaped);
return FALSE;
}
/**
* _gtk_source_regex_resolve:
* @regex: a #GtkSourceRegex.
* @start_regex: a #GtkSourceRegex.
* @matched_text: the text matched against @start_regex.
*
* If the regular expression does not contain references to the start
* regular expression, the functions increases the reference count
* of @regex and returns it.
*
* If the regular expression contains references to the start regular
* expression in the form "\%{start_sub_pattern@start}", it replaces
* them (they are extracted from @start_regex and @matched_text) and
* returns the new regular expression.
*
* Returns: a #GtkSourceRegex.
*/
GtkSourceRegex *
_gtk_source_regex_resolve (GtkSourceRegex *regex,
GtkSourceRegex *start_regex,
const gchar *matched_text)
{
gchar *expanded_regex;
GtkSourceRegex *new_regex;
struct RegexResolveData data;
if (regex == NULL || regex->resolved)
return _gtk_source_regex_ref (regex);
data.start_regex = start_regex;
data.matched_text = matched_text;
expanded_regex = g_regex_replace_eval (get_start_ref_regex (),
regex->u.info.pattern,
-1, 0, 0,
replace_start_regex,
&data, NULL);
new_regex = _gtk_source_regex_new (expanded_regex, regex->u.info.flags, NULL);
if (new_regex == NULL || !new_regex->resolved)
{
_gtk_source_regex_unref (new_regex);
g_warning ("Regular expression %s cannot be expanded.",
regex->u.info.pattern);
/* Returns a regex that nevers matches. */
new_regex = _gtk_source_regex_new ("$never-match^", 0, NULL);
}
g_free (expanded_regex);
return new_regex;
}
gboolean
_gtk_source_regex_is_resolved (GtkSourceRegex *regex)
{
return regex->resolved;
}
gboolean
_gtk_source_regex_match (GtkSourceRegex *regex,
const gchar *line,
gint byte_length,
gint byte_pos)
{
gboolean result;
g_assert (regex->resolved);
if (regex->u.regex.match)
{
g_match_info_free (regex->u.regex.match);
regex->u.regex.match = NULL;
}
result = g_regex_match_full (regex->u.regex.regex, line,
byte_length, byte_pos,
0, ®ex->u.regex.match,
NULL);
return result;
}
gchar *
_gtk_source_regex_fetch (GtkSourceRegex *regex,
gint num)
{
g_assert (regex->resolved);
return g_match_info_fetch (regex->u.regex.match, num);
}
void
_gtk_source_regex_fetch_pos (GtkSourceRegex *regex,
const gchar *text,
gint num,
gint *start_pos, /* character offsets */
gint *end_pos) /* character offsets */
{
gint byte_start_pos, byte_end_pos;
g_assert (regex->resolved);
if (!g_match_info_fetch_pos (regex->u.regex.match, num, &byte_start_pos, &byte_end_pos))
{
if (start_pos != NULL)
*start_pos = -1;
if (end_pos != NULL)
*end_pos = -1;
}
else
{
if (start_pos != NULL)
*start_pos = g_utf8_pointer_to_offset (text, text + byte_start_pos);
if (end_pos != NULL)
*end_pos = g_utf8_pointer_to_offset (text, text + byte_end_pos);
}
}
void
_gtk_source_regex_fetch_pos_bytes (GtkSourceRegex *regex,
gint num,
gint *start_pos_p, /* byte offsets */
gint *end_pos_p) /* byte offsets */
{
gint start_pos;
gint end_pos;
g_assert (regex->resolved);
if (!g_match_info_fetch_pos (regex->u.regex.match, num, &start_pos, &end_pos))
{
start_pos = -1;
end_pos = -1;
}
if (start_pos_p != NULL)
*start_pos_p = start_pos;
if (end_pos_p != NULL)
*end_pos_p = end_pos;
}
void
_gtk_source_regex_fetch_named_pos (GtkSourceRegex *regex,
const gchar *text,
const gchar *name,
gint *start_pos, /* character offsets */
gint *end_pos) /* character offsets */
{
gint byte_start_pos, byte_end_pos;
g_assert (regex->resolved);
if (!g_match_info_fetch_named_pos (regex->u.regex.match, name, &byte_start_pos, &byte_end_pos))
{
if (start_pos != NULL)
*start_pos = -1;
if (end_pos != NULL)
*end_pos = -1;
}
else
{
if (start_pos != NULL)
*start_pos = g_utf8_pointer_to_offset (text, text + byte_start_pos);
if (end_pos != NULL)
*end_pos = g_utf8_pointer_to_offset (text, text + byte_end_pos);
}
}
const gchar *
_gtk_source_regex_get_pattern (GtkSourceRegex *regex)
{
g_assert (regex->resolved);
return g_regex_get_pattern (regex->u.regex.regex);
}
|