~vcs-imports/gawk/master

« back to all changes in this revision

Viewing changes to regex_internal.h

  • Committer: Arnold D. Robbins
  • Date: 2010-07-16 11:47:02 UTC
  • Revision ID: git-v1:315bd501ca696bc3e3c938b4604d8dac7a6f512f
Tags: gawk-3.1.5
Move to gawk 3.1.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Extended regular expression matching and search library.
2
 
   Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
 
2
   Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3
3
   This file is part of the GNU C Library.
4
4
   Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
5
5
 
15
15
 
16
16
   You should have received a copy of the GNU Lesser General Public
17
17
   License along with the GNU C Library; if not, write to the Free
18
 
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19
 
   02111-1307 USA.  */
 
18
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
19
   02110-1301 USA.  */
20
20
 
21
21
#ifndef _REGEX_INTERNAL_H
22
22
#define _REGEX_INTERNAL_H 1
23
23
 
24
24
#include <assert.h>
25
25
#include <ctype.h>
26
 
#if 0
27
 
/* Don't include this here. On some systems it sets RE_DUP_MAX to a
28
 
 * lower value than GNU regex allows.  Instead, include it in
29
 
 * regex.c, before include of <regex.h>, which correctly
30
 
 * #undefs RE_DUP_MAX and sets it to the right value.
31
 
 */
32
 
#include <limits.h>
33
 
#endif
34
26
#include <stdio.h>
35
27
#include <stdlib.h>
36
28
#include <string.h>
37
29
 
 
30
#if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC
 
31
# include <langinfo.h>
 
32
#endif
38
33
#if defined HAVE_LOCALE_H || defined _LIBC
39
34
# include <locale.h>
40
35
#endif
45
40
# include <wctype.h>
46
41
#endif /* HAVE_WCTYPE_H || _LIBC */
47
42
 
 
43
#ifndef GAWK
48
44
/* In case that the system doesn't have isblank().  */
49
45
#if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank
50
46
# define isblank(ch) ((ch) == ' ' || (ch) == '\t')
51
47
#endif
 
48
#else /* GAWK */
 
49
/*
 
50
 * This is a freaking mess. On glibc systems you have to define
 
51
 * a magic constant to get isblank() out of <ctype.h>, since it's
 
52
 * a C99 function.  To heck wiht all that and borrow a page from
 
53
 * dfa.c's book.
 
54
 */
 
55
 
 
56
static int
 
57
is_blank (int c)
 
58
{
 
59
   return (c == ' ' || c == '\t');
 
60
}
 
61
#endif /* GAWK */
52
62
 
53
63
#ifdef _LIBC
54
64
# ifndef _RE_DEFINE_LOCALE_FUNCTIONS
77
87
# define gettext_noop(String) String
78
88
#endif
79
89
 
 
90
#ifndef NO_MBSUPPORT
80
91
#include "mbsupport.h" /* gawk */
 
92
#endif
 
93
#ifndef MB_CUR_MAX
 
94
#define MB_CUR_MAX 1
 
95
#endif
81
96
 
82
97
#if (defined MBS_SUPPORT) || _LIBC
83
98
# define RE_ENABLE_I18N
108
123
# define __btowc btowc
109
124
# define __mempcpy mempcpy
110
125
# define __wcrtomb wcrtomb
 
126
# define __regfree regfree
111
127
# define attribute_hidden
112
128
#endif /* not _LIBC */
113
129
 
117
133
# define __attribute(arg)
118
134
#endif
119
135
 
120
 
#if _LIBC || __GNUC__ >= 3
121
 
# define BE(expr, val) __builtin_expect (expr, val)
122
 
#else
123
 
# define BE(expr, val) (expr)
124
 
# define inline
 
136
/* Error messages (regcomp.c), with hackery to support very old compilers.  */
 
137
#ifdef NO_TOKEN_PASTING
 
138
#define RE_ERRMSG(idx)  __re_error_msgid[(int) (idx)]
 
139
#define ERRMSG_TYPE                 char * const        /* pointers */
 
140
#define ERRMSG_OFFSET(base,offset)  ((base)+1)          /* array index */
 
141
#define ERRMSG_SEPARATOR            ,               
 
142
#else                                               
 
143
#define RE_ERRMSG(idx)  (__re_error_msgid + __re_error_msgid_idx[(int) (idx)])
 
144
#define ERRMSG_TYPE                 char                /* one string */
 
145
#define ERRMSG_OFFSET(base,offset)  ((base)+(offset))   /* substring index */
 
146
#define ERRMSG_SEPARATOR            "\0"
125
147
#endif
126
148
 
127
 
extern const char __re_error_msgid[] attribute_hidden;
 
149
extern const ERRMSG_TYPE __re_error_msgid[] attribute_hidden;
128
150
extern const size_t __re_error_msgid_idx[] attribute_hidden;
129
151
 
130
152
/* Number of bits in an unsinged int.  */
156
178
#define NEXT_NEWLINE_CONSTRAINT 0x0020
157
179
#define PREV_BEGBUF_CONSTRAINT 0x0040
158
180
#define NEXT_ENDBUF_CONSTRAINT 0x0080
159
 
#define DUMMY_CONSTRAINT 0x0100
 
181
#define WORD_DELIM_CONSTRAINT 0x0100
 
182
#define NOT_WORD_DELIM_CONSTRAINT 0x0200
160
183
 
161
184
typedef enum
162
185
{
163
186
  INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
164
187
  WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
165
188
  WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
 
189
  INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
166
190
  LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
167
191
  LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
168
192
  BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
169
193
  BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
170
 
  WORD_DELIM = DUMMY_CONSTRAINT
 
194
  WORD_DELIM = WORD_DELIM_CONSTRAINT,
 
195
  NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
171
196
} re_context_type;
172
197
 
173
198
typedef struct
199
224
  OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
200
225
  OP_ALT = EPSILON_BIT | 2,
201
226
  OP_DUP_ASTERISK = EPSILON_BIT | 3,
202
 
  OP_DUP_PLUS = EPSILON_BIT | 4,
203
 
  OP_DUP_QUESTION = EPSILON_BIT | 5,
204
 
  ANCHOR = EPSILON_BIT | 6,
 
227
  ANCHOR = EPSILON_BIT | 4,
205
228
 
206
229
  /* Tree type, these are used only by tree. */
207
230
  CONCAT = 16,
 
231
  SUBEXP = 17,
208
232
 
209
233
  /* Token type, these are used only by token.  */
210
 
  OP_OPEN_BRACKET = 17,
 
234
  OP_DUP_PLUS = 18,
 
235
  OP_DUP_QUESTION,
 
236
  OP_OPEN_BRACKET,
211
237
  OP_CLOSE_BRACKET,
212
238
  OP_CHARSET_RANGE,
213
239
  OP_OPEN_DUP_NUM,
296
322
  unsigned int duplicated : 1;
297
323
  unsigned int opt_subexp : 1;
298
324
#ifdef RE_ENABLE_I18N
 
325
  unsigned int accept_mb : 1;
299
326
  /* These 2 bits can be moved into the union if needed (e.g. if running out
300
327
     of bits; move opr.c to opr.c.c and move the flags to opr.c.flags).  */
301
328
  unsigned int mb_partial : 1;
304
331
} re_token_t;
305
332
 
306
333
#define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
307
 
#define ACCEPT_MB_NODE(type) \
308
 
  ((type) >= OP_PERIOD && (type) <= OP_UTF8_PERIOD)
309
334
 
310
335
struct re_string_t
311
336
{
400
425
static void re_string_destruct (re_string_t *pstr) internal_function;
401
426
# ifdef RE_ENABLE_I18N
402
427
static int re_string_elem_size_at (const re_string_t *pstr, int idx)
403
 
     internal_function;
 
428
     internal_function __attribute ((pure));
404
429
static inline int re_string_char_size_at (const re_string_t *pstr, int idx)
405
 
     internal_function;
 
430
     internal_function __attribute ((pure));
406
431
static inline wint_t re_string_wchar_at (const re_string_t *pstr, int idx)
407
 
     internal_function;
 
432
     internal_function __attribute ((pure));
408
433
# endif /* RE_ENABLE_I18N */
409
434
static unsigned int re_string_context_at (const re_string_t *input, int idx,
410
 
                                          int eflags) internal_function;
 
435
                                          int eflags)
 
436
     internal_function __attribute ((pure));
411
437
static unsigned char re_string_peek_byte_case (const re_string_t *pstr,
412
 
                                               int idx) internal_function;
 
438
                                               int idx)
 
439
     internal_function __attribute ((pure));
413
440
static unsigned char re_string_fetch_byte_case (re_string_t *pstr)
414
 
     internal_function;
 
441
     internal_function __attribute ((pure));
415
442
#endif
416
443
#define re_string_peek_byte(pstr, offset) \
417
444
  ((pstr)->mbs[(pstr)->cur_idx + offset])
433
460
#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
434
461
/* SunOS 4.1.x realloc doesn't accept null pointers: pre-Standard C. Sigh. */
435
462
#define re_realloc(p,t,n) ((p != NULL) ? (t *) realloc (p,(n)*sizeof(t)) : (t *) calloc(n,sizeof(t)))
436
 
 
437
463
#define re_free(p) free (p)
438
464
 
439
465
struct bin_tree_t
441
467
  struct bin_tree_t *parent;
442
468
  struct bin_tree_t *left;
443
469
  struct bin_tree_t *right;
 
470
  struct bin_tree_t *first;
 
471
  struct bin_tree_t *next;
 
472
 
 
473
  re_token_t token;
444
474
 
445
475
  /* `node_idx' is the index in dfa->nodes, if `type' == 0.
446
476
     Otherwise `type' indicate the type of this node.  */
447
 
  re_token_type_t type;
448
477
  int node_idx;
449
 
 
450
 
  int first;
451
 
  int next;
452
 
  re_node_set eclosure;
453
478
};
454
479
typedef struct bin_tree_t bin_tree_t;
455
480
 
495
520
{
496
521
  unsigned int hash;
497
522
  re_node_set nodes;
 
523
  re_node_set non_eps_nodes;
 
524
  re_node_set inveclosure;
498
525
  re_node_set *entrance_nodes;
499
 
  struct re_dfastate_t **trtable;
 
526
  struct re_dfastate_t **trtable, **word_trtable;
500
527
  unsigned int context : 4;
501
528
  unsigned int halt : 1;
502
529
  /* If this state can accept `multi byte'.
506
533
  /* If this state has backreference node(s).  */
507
534
  unsigned int has_backref : 1;
508
535
  unsigned int has_constraint : 1;
509
 
  unsigned int word_trtable : 1;
510
536
};
511
537
typedef struct re_dfastate_t re_dfastate_t;
512
538
 
513
 
typedef struct
514
 
{
515
 
  /* start <= node < end  */
516
 
  int start;
517
 
  int end;
518
 
} re_subexp_t;
519
 
 
520
539
struct re_state_table_entry
521
540
{
522
541
  int num;
563
582
  int str_idx;
564
583
  int subexp_from;
565
584
  int subexp_to;
566
 
  int flag;
 
585
  char more;
 
586
  char unused;
 
587
  unsigned short int eps_reachable_subexps_map;
567
588
};
568
589
 
569
590
typedef struct
595
616
 
596
617
typedef struct
597
618
{
598
 
  int cur_bkref;
599
 
  int cls_subexp_idx;
600
 
 
601
619
  re_dfastate_t **sifted_states;
602
620
  re_dfastate_t **limited_states;
603
 
 
 
621
  int last_node;
 
622
  int last_str_idx;
604
623
  re_node_set limits;
605
 
 
606
 
  int last_node;
607
 
  int last_str_idx;
608
 
  int check_subexp;
609
624
} re_sift_context_t;
610
625
 
611
626
struct re_fail_stack_ent_t
625
640
 
626
641
struct re_dfa_t
627
642
{
628
 
  re_subexp_t *subexps;
629
643
  re_token_t *nodes;
630
644
  int nodes_alloc;
631
645
  int nodes_len;
645
659
  int str_tree_storage_idx;
646
660
 
647
661
  /* number of subexpressions `re_nsub' is in regex_t.  */
648
 
  int subexps_alloc;
649
662
  unsigned int state_hash_mask;
650
663
  int states_alloc;
651
664
  int init_node;
652
665
  int nbackref; /* The number of backreference in this dfa.  */
 
666
 
653
667
  /* Bitmap expressing which backreference is used.  */
654
668
  unsigned int used_bkref_map;
 
669
  unsigned int completed_bkref_map;
 
670
 
655
671
  unsigned int has_plural_match : 1;
656
672
  /* If this dfa has "multibyte node", which is a backreference or
657
673
     a node which can accept multibyte character or multi character
663
679
  int mb_cur_max;
664
680
  bitset word_char;
665
681
  reg_syntax_t syntax;
 
682
  int *subexp_map;
666
683
#ifdef DEBUG
667
684
  char* re_str;
668
685
#endif
685
702
static reg_errcode_t re_node_set_merge (re_node_set *dest,
686
703
                                        const re_node_set *src) internal_function;
687
704
static int re_node_set_insert (re_node_set *set, int elem) internal_function;
 
705
static int re_node_set_insert_last (re_node_set *set,
 
706
                                    int elem) internal_function;
688
707
static int re_node_set_compare (const re_node_set *set1,
689
 
                                const re_node_set *set2) internal_function;
690
 
static int re_node_set_contains (const re_node_set *set, int elem) internal_function;
 
708
                                const re_node_set *set2)
 
709
     internal_function __attribute ((pure));
 
710
static int re_node_set_contains (const re_node_set *set, int elem)
 
711
     internal_function __attribute ((pure));
691
712
static void re_node_set_remove_at (re_node_set *set, int idx) internal_function;
692
713
#define re_node_set_remove(set,id) \
693
714
  (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
694
715
#define re_node_set_empty(p) ((p)->nelem = 0)
695
716
#define re_node_set_free(set) re_free ((set)->elems)
696
 
static int re_dfa_add_node (re_dfa_t *dfa, re_token_t token, int mode) internal_function;
 
717
static int re_dfa_add_node (re_dfa_t *dfa, re_token_t token) internal_function;
697
718
static re_dfastate_t *re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa,
698
719
                                        const re_node_set *nodes) internal_function;
699
720
static re_dfastate_t *re_acquire_state_context (reg_errcode_t *err,