libt3highlight
 All Data Structures Functions Variables Typedefs Enumerator Modules Pages
internal.h
1 /* Copyright (C) 2011-2012 G.P. Halkes
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License version 3, as
4  published by the Free Software Foundation.
5 
6  This program is distributed in the hope that it will be useful,
7  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  GNU General Public License for more details.
10 
11  You should have received a copy of the GNU General Public License
12  along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14 #ifndef INTERNAL_H
15 #define INTERNAL_H
16 
17 #include <pcre.h>
18 #include "highlight_api.h"
19 #include "vector.h"
20 
21 #if defined(PCRE_MAJOR) && defined(PCRE_MINOR) && PCRE_MAJOR >= 8 && PCRE_MINOR >= 20
22 #define free_pcre_study pcre_free_study
23 #else
24 #define free_pcre_study pcre_free
25 #endif
26 
27 /* For debugging purposes, we define distinct enums to represent various
28  index types. This makes it possible to distinguish from the type, which
29  datastructure is supposed to be indexed by a variable. Furthermore, the
30  compiler will emit a warning when trying to assign one to the other. However,
31  as the integer type used is compiler dependent, we use a regular int for
32  non-debug builds. */
33 #ifdef DEBUG
34 #define INDEX_TYPE(name) typedef enum { FAKE_CONST_##name = -1 } name
35 #else
36 #define INDEX_TYPE(name) typedef int name
37 #endif
38 INDEX_TYPE(pattern_idx_t);
39 INDEX_TYPE(dst_idx_t);
40 
41 #define NO_CHANGE ((pattern_idx_t) -1)
42 /* EXIT_STATE is equal to exit = 1. For higher values of the exit attribute,
43  subtract from EXIT_STATE. I.e. -3 equals exit = 2, -4 equals exit = 3, etc. */
44 #define EXIT_STATE ((pattern_idx_t) -2)
45 
46 /* WARNING: make sure any flags defined here don't clash with the ones in
47  highlight.h */
48 #define T3_HIGHLIGHT_ALLOW_EMPTY_START (1<<15)
49 
50 typedef struct {
51  pcre *regex;
52  pcre_extra *extra;
53 } full_pcre_t;
54 
55 typedef struct {
56  char *end_pattern;
57  pattern_idx_t state;
58 } on_entry_info_t;
59 
60 typedef struct {
61  char *dynamic_name;
62  char *dynamic_pattern;
63  on_entry_info_t *on_entry;
64  int on_entry_cnt;
65 } pattern_extra_t;
66 
67 typedef struct {
68  full_pcre_t regex;
69  pattern_extra_t *extra; /* Only set for start patterns. */
70  pattern_idx_t next_state; /* Values: NO_CHANGE, EXIT_STATE or smaller, or a value >= 0. */
71  int attribute_idx;
72 } pattern_t;
73 
74 typedef VECTOR(pattern_t) patterns_t;
75 
76 typedef struct {
77  patterns_t patterns;
78  int attribute_idx;
79 } state_t;
80 
81 typedef VECTOR(state_t) states_t;
82 
84  states_t states;
85  char *lang_file;
86  int flags;
87 };
88 
89 
90 typedef struct {
91  full_pcre_t regex;
92  char *extracted;
93  int extracted_length;
94 } dynamic_state_t;
95 
96 typedef struct {
97  dst_idx_t parent;
98  pattern_idx_t highlight_state;
99  dynamic_state_t *dynamic;
100 } state_mapping_t;
101 
103  const t3_highlight_t *highlight;
104  VECTOR(state_mapping_t) mapping;
105  size_t start,
106  match_start,
107  end,
108  last_progress;
109  dst_idx_t state;
110  int begin_attribute,
111  match_attribute,
112  last_progress_state;
113  t3_bool utf8_checked;
114 };
115 
116 typedef struct {
117  const char *name;
118  pattern_idx_t state;
119 } use_mapping_t;
120 
121 /* Structs to make passing a large number of arguments easier. */
122 typedef struct {
123  int (*map_style)(void *, const char *);
124  void *map_style_data;
125  t3_highlight_t *highlight;
126  t3_config_t *syntax;
127  int flags;
128  VECTOR(use_mapping_t) use_map;
129  t3_highlight_error_t *error;
130 } highlight_context_t;
131 
132 typedef struct {
133  t3_highlight_match_t *match;
134  const char *line;
135  size_t size;
136  state_t *state;
137  int ovector[30],
138  best_end,
139  extract_start,
140  extract_end;
141  pattern_t *best;
142 } match_context_t;
143 
144 typedef struct {
145  size_t i;
146  pattern_idx_t state;
147 } state_stack_t;
148 
149 
150 T3_HIGHLIGHT_LOCAL char *_t3_highlight_strdup(const char *str);
151 T3_HIGHLIGHT_LOCAL t3_bool _t3_compile_highlight(const char *highlight, full_pcre_t *regex, const t3_config_t *error_context,
152  int flags, t3_highlight_error_t *error);
153 T3_HIGHLIGHT_LOCAL t3_bool _t3_check_empty_start_cycle(highlight_context_t *context);
154 T3_HIGHLIGHT_LOCAL t3_bool _t3_check_use_cycle(highlight_context_t *context);
155 T3_HIGHLIGHT_LOCAL void _t3_highlight_set_error(t3_highlight_error_t *error,
156  int code, int line_number, const char *file_name, const char *extra, int flags);
157 T3_HIGHLIGHT_LOCAL void _t3_highlight_set_error_simple(t3_highlight_error_t *error, int code, int flags);
158 #endif
An opaque struct representing a match and current state during highlighting.
Definition: internal.h:102
An opaque struct representing a highlighting pattern.
Definition: internal.h:83
char t3_bool
A boolean type that does not clash with C++ or C99 bool.
Definition: highlight_api.h:47
A struct with error information.
Definition: highlight.h:104