~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to regparse.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
109
109
  BBuf *to;
110
110
 
111
111
  *rto = to = (BBuf* )xmalloc(sizeof(BBuf));
112
 
  CHECK_NULL_RETURN_VAL(to, ONIGERR_MEMORY);
 
112
  CHECK_NULL_RETURN_MEMERR(to);
113
113
  r = BBUF_INIT(to, from->alloc);
114
114
  if (r != 0) return r;
115
115
  to->used = from->used;
300
300
  int*   back_refs;
301
301
} NameEntry;
302
302
 
303
 
#ifdef USE_ST_HASH_TABLE
 
303
#ifdef USE_ST_LIBRARY
304
304
 
305
 
#include "st.h"
 
305
#include "ruby/st.h"
306
306
 
307
307
typedef struct {
308
308
  unsigned char* s;
309
309
  unsigned char* end;
310
310
} st_strend_key;
311
311
 
312
 
static int strend_cmp(st_strend_key*, st_strend_key*);
313
 
static int strend_hash(st_strend_key*);
314
 
 
315
 
static struct st_hash_type type_strend_hash = {
316
 
  strend_cmp,
317
 
  strend_hash,
318
 
};
 
312
static int
 
313
str_end_cmp(st_strend_key* x, st_strend_key* y)
 
314
{
 
315
  unsigned char *p, *q;
 
316
  int c;
 
317
 
 
318
  if ((x->end - x->s) != (y->end - y->s))
 
319
    return 1;
 
320
 
 
321
  p = x->s;
 
322
  q = y->s;
 
323
  while (p < x->end) {
 
324
    c = (int )*p - (int )*q;
 
325
    if (c != 0) return c;
 
326
 
 
327
    p++; q++;
 
328
  }
 
329
 
 
330
  return 0;
 
331
}
 
332
 
 
333
static int
 
334
str_end_hash(st_strend_key* x)
 
335
{
 
336
  UChar *p;
 
337
  int val = 0;
 
338
 
 
339
  p = x->s;
 
340
  while (p < x->end) {
 
341
    val = val * 997 + (int )*p++;
 
342
  }
 
343
 
 
344
  return val + (val >> 5);
 
345
}
319
346
 
320
347
extern hash_table_type*
321
348
onig_st_init_strend_table_with_size(int size)
322
349
{
323
 
  return (hash_table_type* )onig_st_init_table_with_size(&type_strend_hash,
324
 
                                                         size);
 
350
  static struct st_hash_type hashType = {
 
351
    str_end_cmp,
 
352
    str_end_hash,
 
353
  };
 
354
 
 
355
  return (hash_table_type* )
 
356
           onig_st_init_table_with_size(&hashType, size);
325
357
}
326
358
 
327
359
extern int
330
362
{
331
363
  st_strend_key key;
332
364
 
333
 
  key.s   = (unsigned char* )str_key;
334
 
  key.end = (unsigned char* )end_key;
 
365
  key.s   = (UChar* )str_key;
 
366
  key.end = (UChar* )end_key;
335
367
 
336
368
  return onig_st_lookup(table, (st_data_t )(&key), value);
337
369
}
344
376
  int result;
345
377
 
346
378
  key = (st_strend_key* )xmalloc(sizeof(st_strend_key));
347
 
  key->s   = (unsigned char* )str_key;
348
 
  key->end = (unsigned char* )end_key;
 
379
  key->s   = (UChar* )str_key;
 
380
  key->end = (UChar* )end_key;
349
381
  result = onig_st_insert(table, (st_data_t )key, value);
350
382
  if (result) {
351
383
    xfree(key);
353
385
  return result;
354
386
}
355
387
 
356
 
static int
357
 
strend_cmp(st_strend_key* x, st_strend_key* y)
358
 
{
359
 
  unsigned char *p, *q;
360
 
  int c;
361
 
 
362
 
  if ((x->end - x->s) != (y->end - y->s))
363
 
    return 1;
364
 
 
365
 
  p = x->s;
366
 
  q = y->s;
367
 
  while (p < x->end) {
368
 
    c = (int )*p - (int )*q;
369
 
    if (c != 0) return c;
370
 
 
371
 
    p++; q++;
372
 
  }
373
 
 
374
 
  return 0;
375
 
}
376
 
 
377
 
static int
378
 
strend_hash(st_strend_key* x)
379
 
{
380
 
  int val;
381
 
  unsigned char *p;
382
 
 
383
 
  val = 0;
384
 
  p = x->s;
385
 
  while (p < x->end) {
386
 
    val = val * 997 + (int )*p++;
387
 
  }
388
 
 
389
 
  return val + (val >> 5);
390
 
}
391
388
 
392
389
typedef st_table  NameTable;
393
390
typedef st_data_t HashDataType;   /* 1.6 st.h doesn't define st_data_t type */
561
558
    return 0;
562
559
}
563
560
 
564
 
#else  /* USE_ST_HASH_TABLE */
 
561
#else  /* USE_ST_LIBRARY */
565
562
 
566
563
#define INIT_NAMES_ALLOC_NUM    8
567
564
 
697
694
    return 0;
698
695
}
699
696
 
700
 
#endif /* else USE_ST_HASH_TABLE */
 
697
#endif /* else USE_ST_LIBRARY */
701
698
 
702
699
static int
703
700
name_add(regex_t* reg, UChar* name, UChar* name_end, int backref, ScanEnv* env)
711
708
 
712
709
  e = name_find(reg, name, name_end);
713
710
  if (IS_NULL(e)) {
714
 
#ifdef USE_ST_HASH_TABLE
 
711
#ifdef USE_ST_LIBRARY
715
712
    if (IS_NULL(t)) {
716
713
      t = onig_st_init_strend_table_with_size(5);
717
714
      reg->name_table = (void* )t;
718
715
    }
719
716
    e = (NameEntry* )xmalloc(sizeof(NameEntry));
720
 
    CHECK_NULL_RETURN_VAL(e, ONIGERR_MEMORY);
 
717
    CHECK_NULL_RETURN_MEMERR(e);
721
718
 
722
719
    e->name = strdup_with_null(reg->enc, name, name_end);
723
720
    if (IS_NULL(e->name)) return ONIGERR_MEMORY;
734
731
    if (IS_NULL(t)) {
735
732
      alloc = INIT_NAMES_ALLOC_NUM;
736
733
      t = (NameTable* )xmalloc(sizeof(NameTable));
737
 
      CHECK_NULL_RETURN_VAL(t, ONIGERR_MEMORY);
 
734
      CHECK_NULL_RETURN_MEMERR(t);
738
735
      t->e     = NULL;
739
736
      t->alloc = 0;
740
737
      t->num   = 0;
753
750
 
754
751
      alloc = t->alloc * 2;
755
752
      t->e = (NameEntry* )xrealloc(t->e, sizeof(NameEntry) * alloc);
756
 
      CHECK_NULL_RETURN_VAL(t->e, ONIGERR_MEMORY);
 
753
      CHECK_NULL_RETURN_MEMERR(t->e);
757
754
      t->alloc = alloc;
758
755
 
759
756
    clear:
787
784
    if (e->back_num == 2) {
788
785
      alloc = INIT_NAME_BACKREFS_ALLOC_NUM;
789
786
      e->back_refs = (int* )xmalloc(sizeof(int) * alloc);
790
 
      CHECK_NULL_RETURN_VAL(e->back_refs, ONIGERR_MEMORY);
 
787
      CHECK_NULL_RETURN_MEMERR(e->back_refs);
791
788
      e->back_alloc = alloc;
792
789
      e->back_refs[0] = e->back_ref1;
793
790
      e->back_refs[1] = backref;
796
793
      if (e->back_num > e->back_alloc) {
797
794
        alloc = e->back_alloc * 2;
798
795
        e->back_refs = (int* )xrealloc(e->back_refs, sizeof(int) * alloc);
799
 
        CHECK_NULL_RETURN_VAL(e->back_refs, ONIGERR_MEMORY);
 
796
        CHECK_NULL_RETURN_MEMERR(e->back_refs);
800
797
        e->back_alloc = alloc;
801
798
      }
802
799
      e->back_refs[e->back_num - 1] = backref;
950
947
        alloc = env->mem_alloc * 2;
951
948
        p = (Node** )xrealloc(env->mem_nodes_dynamic, sizeof(Node*) * alloc);
952
949
      }
953
 
      CHECK_NULL_RETURN_VAL(p, ONIGERR_MEMORY);
 
950
      CHECK_NULL_RETURN_MEMERR(p);
954
951
 
955
952
      for (i = env->num_mem + 1; i < alloc; i++)
956
953
        p[i] = NULL_NODE;
975
972
}
976
973
 
977
974
 
978
 
#ifdef USE_RECYCLE_NODE
 
975
#ifdef USE_PARSE_TREE_NODE_RECYCLE
979
976
typedef struct _FreeNode {
980
977
  struct _FreeNode* next;
981
978
} FreeNode;
990
987
  if (IS_NULL(node)) return ;
991
988
 
992
989
  switch (NTYPE(node)) {
993
 
  case N_STRING:
994
 
    if (NSTRING(node).capa != 0 &&
995
 
        IS_NOT_NULL(NSTRING(node).s) && NSTRING(node).s != NSTRING(node).buf) {
996
 
      xfree(NSTRING(node).s);
 
990
  case NT_STR:
 
991
    if (NSTR(node)->capa != 0 &&
 
992
        IS_NOT_NULL(NSTR(node)->s) && NSTR(node)->s != NSTR(node)->buf) {
 
993
      xfree(NSTR(node)->s);
997
994
    }
998
995
    break;
999
996
 
1000
 
  case N_LIST:
1001
 
  case N_ALT:
1002
 
    onig_node_free(NCONS(node).left);
1003
 
    /* onig_node_free(NCONS(node).right); */
 
997
  case NT_LIST:
 
998
  case NT_ALT:
 
999
    onig_node_free(NCAR(node));
1004
1000
    {
1005
 
      Node* next_node = NCONS(node).right;
 
1001
      Node* next_node = NCDR(node);
1006
1002
 
1007
 
#ifdef USE_RECYCLE_NODE
 
1003
#ifdef USE_PARSE_TREE_NODE_RECYCLE
1008
1004
      {
1009
1005
        FreeNode* n = (FreeNode* )node;
1010
1006
 
1021
1017
    }
1022
1018
    break;
1023
1019
 
1024
 
  case N_CCLASS:
 
1020
  case NT_CCLASS:
1025
1021
    {
1026
 
      CClassNode* cc = &(NCCLASS(node));
 
1022
      CClassNode* cc = NCCLASS(node);
1027
1023
 
1028
 
      if (IS_CCLASS_SHARE(cc)) return ;
 
1024
      if (IS_NCCLASS_SHARE(cc)) return ;
1029
1025
      if (cc->mbuf)
1030
1026
        bbuf_free(cc->mbuf);
1031
1027
    }
1032
1028
    break;
1033
1029
 
1034
 
  case N_QUANTIFIER:
1035
 
    if (NQUANTIFIER(node).target)
1036
 
      onig_node_free(NQUANTIFIER(node).target);
1037
 
    break;
1038
 
 
1039
 
  case N_EFFECT:
1040
 
    if (NEFFECT(node).target)
1041
 
      onig_node_free(NEFFECT(node).target);
1042
 
    break;
1043
 
 
1044
 
  case N_BACKREF:
1045
 
    if (IS_NOT_NULL(NBACKREF(node).back_dynamic))
1046
 
      xfree(NBACKREF(node).back_dynamic);
1047
 
    break;
1048
 
 
1049
 
  case N_ANCHOR:
1050
 
    if (NANCHOR(node).target)
1051
 
      onig_node_free(NANCHOR(node).target);
 
1030
  case NT_QTFR:
 
1031
    if (NQTFR(node)->target)
 
1032
      onig_node_free(NQTFR(node)->target);
 
1033
    break;
 
1034
 
 
1035
  case NT_ENCLOSE:
 
1036
    if (NENCLOSE(node)->target)
 
1037
      onig_node_free(NENCLOSE(node)->target);
 
1038
    break;
 
1039
 
 
1040
  case NT_BREF:
 
1041
    if (IS_NOT_NULL(NBREF(node)->back_dynamic))
 
1042
      xfree(NBREF(node)->back_dynamic);
 
1043
    break;
 
1044
 
 
1045
  case NT_ANCHOR:
 
1046
    if (NANCHOR(node)->target)
 
1047
      onig_node_free(NANCHOR(node)->target);
1052
1048
    break;
1053
1049
  }
1054
1050
 
1055
 
#ifdef USE_RECYCLE_NODE
 
1051
#ifdef USE_PARSE_TREE_NODE_RECYCLE
1056
1052
  {
1057
1053
    FreeNode* n = (FreeNode* )node;
1058
1054
 
1066
1062
#endif
1067
1063
}
1068
1064
 
1069
 
#ifdef USE_RECYCLE_NODE
 
1065
#ifdef USE_PARSE_TREE_NODE_RECYCLE
1070
1066
extern int
1071
1067
onig_free_node_list(void)
1072
1068
{
1088
1084
{
1089
1085
  Node* node;
1090
1086
 
1091
 
#ifdef USE_RECYCLE_NODE
 
1087
#ifdef USE_PARSE_TREE_NODE_RECYCLE
1092
1088
  THREAD_ATOMIC_START;
1093
1089
  if (IS_NOT_NULL(FreeNodeList)) {
1094
1090
    node = (Node* )FreeNodeList;
1100
1096
#endif
1101
1097
 
1102
1098
  node = (Node* )xmalloc(sizeof(Node));
 
1099
  /* xmemset(node, 0, sizeof(Node)); */
1103
1100
  return node;
1104
1101
}
1105
1102
 
1108
1105
initialize_cclass(CClassNode* cc)
1109
1106
{
1110
1107
  BITSET_CLEAR(cc->bs);
 
1108
  /* cc->base.flags = 0; */
1111
1109
  cc->flags = 0;
1112
1110
  cc->mbuf  = NULL;
1113
1111
}
1117
1115
{
1118
1116
  Node* node = node_new();
1119
1117
  CHECK_NULL_RETURN(node);
1120
 
  node->type = N_CCLASS;
1121
1118
 
1122
 
  initialize_cclass(&(NCCLASS(node)));
 
1119
  SET_NTYPE(node, NT_CCLASS);
 
1120
  initialize_cclass(NCCLASS(node));
1123
1121
  return node;
1124
1122
}
1125
1123
 
1131
1129
  CClassNode* cc;
1132
1130
  OnigCodePoint j;
1133
1131
 
1134
 
  Node* node = node_new();
 
1132
  Node* node = node_new_cclass();
1135
1133
  CHECK_NULL_RETURN(node);
1136
 
  node->type = N_CCLASS;
1137
1134
 
1138
 
  cc = &(NCCLASS(node));
1139
 
  cc->flags = 0;
1140
 
  if (not != 0) CCLASS_SET_NOT(cc);
 
1135
  cc = NCCLASS(node);
 
1136
  if (not != 0) NCCLASS_SET_NOT(cc);
1141
1137
 
1142
1138
  BITSET_CLEAR(cc->bs);
1143
1139
  if (sb_out > 0 && IS_NOT_NULL(ranges)) {
1164
1160
    if (n == 0) goto is_null;
1165
1161
 
1166
1162
    bbuf = (BBuf* )xmalloc(sizeof(BBuf));
1167
 
    CHECK_NULL_RETURN_VAL(bbuf, NULL);
 
1163
    CHECK_NULL_RETURN(bbuf);
1168
1164
    bbuf->alloc = n + 1;
1169
1165
    bbuf->used  = n + 1;
1170
1166
    bbuf->p     = (UChar* )((void* )ranges);
1180
1176
{
1181
1177
  Node* node = node_new();
1182
1178
  CHECK_NULL_RETURN(node);
1183
 
  node->type = N_CTYPE;
1184
 
  NCTYPE(node).ctype = type;
1185
 
  NCTYPE(node).not   = not;
 
1179
 
 
1180
  SET_NTYPE(node, NT_CTYPE);
 
1181
  NCTYPE(node)->ctype = type;
 
1182
  NCTYPE(node)->not   = not;
1186
1183
  return node;
1187
1184
}
1188
1185
 
1191
1188
{
1192
1189
  Node* node = node_new();
1193
1190
  CHECK_NULL_RETURN(node);
1194
 
  node->type = N_ANYCHAR;
 
1191
 
 
1192
  SET_NTYPE(node, NT_CANY);
1195
1193
  return node;
1196
1194
}
1197
1195
 
1200
1198
{
1201
1199
  Node* node = node_new();
1202
1200
  CHECK_NULL_RETURN(node);
1203
 
  node->type = N_LIST;
1204
 
  NCONS(node).left  = left;
1205
 
  NCONS(node).right = right;
 
1201
 
 
1202
  SET_NTYPE(node, NT_LIST);
 
1203
  NCAR(node)  = left;
 
1204
  NCDR(node) = right;
1206
1205
  return node;
1207
1206
}
1208
1207
 
1221
1220
  if (IS_NULL(n)) return NULL_NODE;
1222
1221
 
1223
1222
  if (IS_NOT_NULL(list)) {
1224
 
    while (IS_NOT_NULL(NCONS(list).right))
1225
 
      list = NCONS(list).right;
 
1223
    while (IS_NOT_NULL(NCDR(list)))
 
1224
      list = NCDR(list);
1226
1225
 
1227
 
    NCONS(list).right = n;
 
1226
    NCDR(list) = n;
1228
1227
  }
1229
1228
 
1230
1229
  return n;
1235
1234
{
1236
1235
  Node* node = node_new();
1237
1236
  CHECK_NULL_RETURN(node);
1238
 
  node->type = N_ALT;
1239
 
  NCONS(node).left  = left;
1240
 
  NCONS(node).right = right;
 
1237
 
 
1238
  SET_NTYPE(node, NT_ALT);
 
1239
  NCAR(node)  = left;
 
1240
  NCDR(node) = right;
1241
1241
  return node;
1242
1242
}
1243
1243
 
1246
1246
{
1247
1247
  Node* node = node_new();
1248
1248
  CHECK_NULL_RETURN(node);
1249
 
  node->type = N_ANCHOR;
1250
 
  NANCHOR(node).type     = type;
1251
 
  NANCHOR(node).target   = NULL;
1252
 
  NANCHOR(node).char_len = -1;
 
1249
 
 
1250
  SET_NTYPE(node, NT_ANCHOR);
 
1251
  NANCHOR(node)->type     = type;
 
1252
  NANCHOR(node)->target   = NULL;
 
1253
  NANCHOR(node)->char_len = -1;
1253
1254
  return node;
1254
1255
}
1255
1256
 
1264
1265
  Node* node = node_new();
1265
1266
 
1266
1267
  CHECK_NULL_RETURN(node);
1267
 
  node->type = N_BACKREF;
1268
 
  NBACKREF(node).state    = 0;
1269
 
  NBACKREF(node).back_num = back_num;
1270
 
  NBACKREF(node).back_dynamic = (int* )NULL;
 
1268
 
 
1269
  SET_NTYPE(node, NT_BREF);
 
1270
  NBREF(node)->state    = 0;
 
1271
  NBREF(node)->back_num = back_num;
 
1272
  NBREF(node)->back_dynamic = (int* )NULL;
1271
1273
  if (by_name != 0)
1272
 
    NBACKREF(node).state |= NST_NAME_REF;
 
1274
    NBREF(node)->state |= NST_NAME_REF;
1273
1275
 
1274
1276
#ifdef USE_BACKREF_AT_LEVEL
1275
1277
  if (exist_level != 0) {
1276
 
    NBACKREF(node).state |= NST_NEST_LEVEL;
1277
 
    NBACKREF(node).nest_level  = nest_level;
 
1278
    NBREF(node)->state |= NST_NEST_LEVEL;
 
1279
    NBREF(node)->nest_level  = nest_level;
1278
1280
  }
1279
1281
#endif
1280
1282
 
1281
1283
  for (i = 0; i < back_num; i++) {
1282
1284
    if (backrefs[i] <= env->num_mem &&
1283
1285
        IS_NULL(SCANENV_MEM_NODES(env)[backrefs[i]])) {
1284
 
      NBACKREF(node).state |= NST_RECURSION;   /* /...(\1).../ */
 
1286
      NBREF(node)->state |= NST_RECURSION;   /* /...(\1).../ */
1285
1287
      break;
1286
1288
    }
1287
1289
  }
1288
1290
 
1289
1291
  if (back_num <= NODE_BACKREFS_SIZE) {
1290
1292
    for (i = 0; i < back_num; i++)
1291
 
      NBACKREF(node).back_static[i] = backrefs[i];
 
1293
      NBREF(node)->back_static[i] = backrefs[i];
1292
1294
  }
1293
1295
  else {
1294
1296
    int* p = (int* )xmalloc(sizeof(int) * back_num);
1296
1298
      onig_node_free(node);
1297
1299
      return NULL;
1298
1300
    }
1299
 
    NBACKREF(node).back_dynamic = p;
 
1301
    NBREF(node)->back_dynamic = p;
1300
1302
    for (i = 0; i < back_num; i++)
1301
1303
      p[i] = backrefs[i];
1302
1304
  }
1310
1312
  Node* node = node_new();
1311
1313
  CHECK_NULL_RETURN(node);
1312
1314
 
1313
 
  node->type = N_CALL;
1314
 
  NCALL(node).state    = 0;
1315
 
  NCALL(node).ref_num  = CALLNODE_REFNUM_UNDEF;
1316
 
  NCALL(node).target   = NULL_NODE;
1317
 
  NCALL(node).name     = name;
1318
 
  NCALL(node).name_end = name_end;
 
1315
  SET_NTYPE(node, NT_CALL);
 
1316
  NCALL(node)->state    = 0;
 
1317
  NCALL(node)->ref_num  = CALLNODE_REFNUM_UNDEF;
 
1318
  NCALL(node)->target   = NULL_NODE;
 
1319
  NCALL(node)->name     = name;
 
1320
  NCALL(node)->name_end = name_end;
1319
1321
  return node;
1320
1322
}
1321
1323
#endif
1325
1327
{
1326
1328
  Node* node = node_new();
1327
1329
  CHECK_NULL_RETURN(node);
1328
 
  node->type = N_QUANTIFIER;
1329
 
  NQUANTIFIER(node).state  = 0;
1330
 
  NQUANTIFIER(node).target = NULL;
1331
 
  NQUANTIFIER(node).lower  = lower;
1332
 
  NQUANTIFIER(node).upper  = upper;
1333
 
  NQUANTIFIER(node).greedy = 1;
1334
 
  NQUANTIFIER(node).target_empty_info = NQ_TARGET_ISNOT_EMPTY;
1335
 
  NQUANTIFIER(node).head_exact        = NULL_NODE;
1336
 
  NQUANTIFIER(node).next_head_exact   = NULL_NODE;
1337
 
  NQUANTIFIER(node).is_refered        = 0;
 
1330
 
 
1331
  SET_NTYPE(node, NT_QTFR);
 
1332
  NQTFR(node)->state  = 0;
 
1333
  NQTFR(node)->target = NULL;
 
1334
  NQTFR(node)->lower  = lower;
 
1335
  NQTFR(node)->upper  = upper;
 
1336
  NQTFR(node)->greedy = 1;
 
1337
  NQTFR(node)->target_empty_info = NQ_TARGET_ISNOT_EMPTY;
 
1338
  NQTFR(node)->head_exact        = NULL_NODE;
 
1339
  NQTFR(node)->next_head_exact   = NULL_NODE;
 
1340
  NQTFR(node)->is_refered        = 0;
1338
1341
  if (by_number != 0)
1339
 
    NQUANTIFIER(node).state |= NST_BY_NUMBER;
 
1342
    NQTFR(node)->state |= NST_BY_NUMBER;
1340
1343
 
1341
1344
#ifdef USE_COMBINATION_EXPLOSION_CHECK
1342
 
  NQUANTIFIER(node).comb_exp_check_num = 0;
 
1345
  NQTFR(node)->comb_exp_check_num = 0;
1343
1346
#endif
1344
1347
 
1345
1348
  return node;
1346
1349
}
1347
1350
 
1348
1351
static Node*
1349
 
node_new_effect(int type)
 
1352
node_new_enclose(int type)
1350
1353
{
1351
1354
  Node* node = node_new();
1352
1355
  CHECK_NULL_RETURN(node);
1353
 
  node->type = N_EFFECT;
1354
 
  NEFFECT(node).type      = type;
1355
 
  NEFFECT(node).state     =  0;
1356
 
  NEFFECT(node).regnum    =  0;
1357
 
  NEFFECT(node).option    =  0;
1358
 
  NEFFECT(node).target    = NULL;
1359
 
  NEFFECT(node).call_addr = -1;
1360
 
  NEFFECT(node).opt_count =  0;
 
1356
 
 
1357
  SET_NTYPE(node, NT_ENCLOSE);
 
1358
  NENCLOSE(node)->type      = type;
 
1359
  NENCLOSE(node)->state     =  0;
 
1360
  NENCLOSE(node)->regnum    =  0;
 
1361
  NENCLOSE(node)->option    =  0;
 
1362
  NENCLOSE(node)->target    = NULL;
 
1363
  NENCLOSE(node)->call_addr = -1;
 
1364
  NENCLOSE(node)->opt_count =  0;
1361
1365
  return node;
1362
1366
}
1363
1367
 
1364
1368
extern Node*
1365
 
onig_node_new_effect(int type)
 
1369
onig_node_new_enclose(int type)
1366
1370
{
1367
 
  return node_new_effect(type);
 
1371
  return node_new_enclose(type);
1368
1372
}
1369
1373
 
1370
1374
static Node*
1371
 
node_new_effect_memory(OnigOptionType option, int is_named)
 
1375
node_new_enclose_memory(OnigOptionType option, int is_named)
1372
1376
{
1373
 
  Node* node = node_new_effect(EFFECT_MEMORY);
 
1377
  Node* node = node_new_enclose(ENCLOSE_MEMORY);
1374
1378
  CHECK_NULL_RETURN(node);
1375
1379
  if (is_named != 0)
1376
 
    SET_EFFECT_STATUS(node, NST_NAMED_GROUP);
 
1380
    SET_ENCLOSE_STATUS(node, NST_NAMED_GROUP);
1377
1381
 
1378
1382
#ifdef USE_SUBEXP_CALL
1379
 
  NEFFECT(node).option = option;
 
1383
  NENCLOSE(node)->option = option;
1380
1384
#endif
1381
1385
  return node;
1382
1386
}
1384
1388
static Node*
1385
1389
node_new_option(OnigOptionType option)
1386
1390
{
1387
 
  Node* node = node_new_effect(EFFECT_OPTION);
 
1391
  Node* node = node_new_enclose(ENCLOSE_OPTION);
1388
1392
  CHECK_NULL_RETURN(node);
1389
 
  NEFFECT(node).option = option;
 
1393
  NENCLOSE(node)->option = option;
1390
1394
  return node;
1391
1395
}
1392
1396
 
1396
1400
  int addlen = end - s;
1397
1401
 
1398
1402
  if (addlen > 0) {
1399
 
    int len  = NSTRING(node).end - NSTRING(node).s;
 
1403
    int len  = NSTR(node)->end - NSTR(node)->s;
1400
1404
 
1401
 
    if (NSTRING(node).capa > 0 || (len + addlen > NODE_STR_BUF_SIZE - 1)) {
 
1405
    if (NSTR(node)->capa > 0 || (len + addlen > NODE_STR_BUF_SIZE - 1)) {
1402
1406
      UChar* p;
1403
1407
      int capa = len + addlen + NODE_STR_MARGIN;
1404
1408
 
1405
 
      if (capa <= NSTRING(node).capa) {
1406
 
        onig_strcpy(NSTRING(node).s + len, s, end);
 
1409
      if (capa <= NSTR(node)->capa) {
 
1410
        onig_strcpy(NSTR(node)->s + len, s, end);
1407
1411
      }
1408
1412
      else {
1409
 
        if (NSTRING(node).s == NSTRING(node).buf)
1410
 
          p = strcat_capa_from_static(NSTRING(node).s, NSTRING(node).end,
 
1413
        if (NSTR(node)->s == NSTR(node)->buf)
 
1414
          p = strcat_capa_from_static(NSTR(node)->s, NSTR(node)->end,
1411
1415
                                      s, end, capa);
1412
1416
        else
1413
 
          p = strcat_capa(NSTRING(node).s, NSTRING(node).end, s, end, capa);
 
1417
          p = strcat_capa(NSTR(node)->s, NSTR(node)->end, s, end, capa);
1414
1418
 
1415
 
        CHECK_NULL_RETURN_VAL(p, ONIGERR_MEMORY);
1416
 
        NSTRING(node).s    = p;
1417
 
        NSTRING(node).capa = capa;
 
1419
        CHECK_NULL_RETURN_MEMERR(p);
 
1420
        NSTR(node)->s    = p;
 
1421
        NSTR(node)->capa = capa;
1418
1422
      }
1419
1423
    }
1420
1424
    else {
1421
 
      onig_strcpy(NSTRING(node).s + len, s, end);
 
1425
      onig_strcpy(NSTR(node)->s + len, s, end);
1422
1426
    }
1423
 
    NSTRING(node).end = NSTRING(node).s + len + addlen;
 
1427
    NSTR(node)->end = NSTR(node)->s + len + addlen;
1424
1428
  }
1425
1429
 
1426
1430
  return 0;
1445
1449
extern void
1446
1450
onig_node_conv_to_str_node(Node* node, int flag)
1447
1451
{
1448
 
  node->type = N_STRING;
1449
 
 
1450
 
  NSTRING(node).flag = flag;
1451
 
  NSTRING(node).capa = 0;
1452
 
  NSTRING(node).s    = NSTRING(node).buf;
1453
 
  NSTRING(node).end  = NSTRING(node).buf;
 
1452
  SET_NTYPE(node, NT_STR);
 
1453
  NSTR(node)->flag = flag;
 
1454
  NSTR(node)->capa = 0;
 
1455
  NSTR(node)->s    = NSTR(node)->buf;
 
1456
  NSTR(node)->end  = NSTR(node)->buf;
1454
1457
}
1455
1458
 
1456
1459
extern void
1457
1460
onig_node_str_clear(Node* node)
1458
1461
{
1459
 
  if (NSTRING(node).capa != 0 &&
1460
 
      IS_NOT_NULL(NSTRING(node).s) && NSTRING(node).s != NSTRING(node).buf) {
1461
 
    xfree(NSTRING(node).s);
 
1462
  if (NSTR(node)->capa != 0 &&
 
1463
      IS_NOT_NULL(NSTR(node)->s) && NSTR(node)->s != NSTR(node)->buf) {
 
1464
    xfree(NSTR(node)->s);
1462
1465
  }
1463
1466
 
1464
 
  NSTRING(node).capa = 0;
1465
 
  NSTRING(node).flag = 0;
1466
 
  NSTRING(node).s    = NSTRING(node).buf;
1467
 
  NSTRING(node).end  = NSTRING(node).buf;
 
1467
  NSTR(node)->capa = 0;
 
1468
  NSTR(node)->flag = 0;
 
1469
  NSTR(node)->s    = NSTR(node)->buf;
 
1470
  NSTR(node)->end  = NSTR(node)->buf;
1468
1471
}
1469
1472
 
1470
1473
static Node*
1473
1476
  Node* node = node_new();
1474
1477
  CHECK_NULL_RETURN(node);
1475
1478
 
1476
 
  node->type = N_STRING;
1477
 
  NSTRING(node).capa = 0;
1478
 
  NSTRING(node).flag = 0;
1479
 
  NSTRING(node).s    = NSTRING(node).buf;
1480
 
  NSTRING(node).end  = NSTRING(node).buf;
 
1479
  SET_NTYPE(node, NT_STR);
 
1480
  NSTR(node)->capa = 0;
 
1481
  NSTR(node)->flag = 0;
 
1482
  NSTR(node)->s    = NSTR(node)->buf;
 
1483
  NSTR(node)->end  = NSTR(node)->buf;
1481
1484
  if (onig_node_str_cat(node, s, end)) {
1482
1485
    onig_node_free(node);
1483
1486
    return NULL;
1659
1662
  BBuf* bbuf;
1660
1663
 
1661
1664
  bbuf = *pbuf = (BBuf* )xmalloc(sizeof(BBuf));
1662
 
  CHECK_NULL_RETURN_VAL(*pbuf, ONIGERR_MEMORY);
 
1665
  CHECK_NULL_RETURN_MEMERR(*pbuf);
1663
1666
  r = BBUF_INIT(*pbuf, INIT_MULTI_BYTE_RANGE_SIZE);
1664
1667
  if (r) return r;
1665
1668
 
1958
1961
  BBuf *tbuf;
1959
1962
  int r;
1960
1963
 
1961
 
  if (IS_CCLASS_NOT(cc)) {
 
1964
  if (IS_NCCLASS_NOT(cc)) {
1962
1965
    bitset_invert(cc->bs);
1963
1966
 
1964
1967
    if (! ONIGENC_IS_SINGLEBYTE(enc)) {
1969
1972
      cc->mbuf = tbuf;
1970
1973
    }
1971
1974
 
1972
 
    CCLASS_CLEAR_NOT(cc);
 
1975
    NCCLASS_CLEAR_NOT(cc);
1973
1976
  }
1974
1977
 
1975
1978
  return 0;
1983
1986
  BitSetRef bsr1, bsr2;
1984
1987
  BitSet bs1, bs2;
1985
1988
 
1986
 
  not1 = IS_CCLASS_NOT(dest);
 
1989
  not1 = IS_NCCLASS_NOT(dest);
1987
1990
  bsr1 = dest->bs;
1988
1991
  buf1 = dest->mbuf;
1989
 
  not2 = IS_CCLASS_NOT(cc);
 
1992
  not2 = IS_NCCLASS_NOT(cc);
1990
1993
  bsr2 = cc->bs;
1991
1994
  buf2 = cc->mbuf;
1992
1995
 
2041
2044
  BitSetRef bsr1, bsr2;
2042
2045
  BitSet bs1, bs2;
2043
2046
 
2044
 
  not1 = IS_CCLASS_NOT(dest);
 
2047
  not1 = IS_NCCLASS_NOT(dest);
2045
2048
  bsr1 = dest->bs;
2046
2049
  buf1 = dest->mbuf;
2047
 
  not2 = IS_CCLASS_NOT(cc);
 
2050
  not2 = IS_NCCLASS_NOT(cc);
2048
2051
  bsr2 = cc->bs;
2049
2052
  buf2 = cc->mbuf;
2050
2053
 
2120
2123
is_invalid_quantifier_target(Node* node)
2121
2124
{
2122
2125
  switch (NTYPE(node)) {
2123
 
  case N_ANCHOR:
 
2126
  case NT_ANCHOR:
2124
2127
    return 1;
2125
2128
    break;
2126
2129
 
2127
 
  case N_EFFECT:
2128
 
    if (NEFFECT(node).type == EFFECT_OPTION)
2129
 
      return is_invalid_quantifier_target(NEFFECT(node).target);
 
2130
  case NT_ENCLOSE:
 
2131
    /* allow enclosed elements */
 
2132
    /* return is_invalid_quantifier_target(NENCLOSE(node)->target); */
2130
2133
    break;
2131
2134
 
2132
 
  case N_LIST: /* ex. (?:\G\A)* */
 
2135
  case NT_LIST:
2133
2136
    do {
2134
 
      if (! is_invalid_quantifier_target(NCONS(node).left)) return 0;
2135
 
    } while (IS_NOT_NULL(node = NCONS(node).right));
 
2137
      if (! is_invalid_quantifier_target(NCAR(node))) return 0;
 
2138
    } while (IS_NOT_NULL(node = NCDR(node)));
2136
2139
    return 0;
2137
2140
    break;
2138
2141
 
2139
 
  case N_ALT:  /* ex. (?:abc|\A)* */
 
2142
  case NT_ALT:
2140
2143
    do {
2141
 
      if (is_invalid_quantifier_target(NCONS(node).left)) return 1;
2142
 
    } while (IS_NOT_NULL(node = NCONS(node).right));
 
2144
      if (is_invalid_quantifier_target(NCAR(node))) return 1;
 
2145
    } while (IS_NOT_NULL(node = NCDR(node)));
2143
2146
    break;
2144
2147
 
2145
2148
  default:
2150
2153
 
2151
2154
/* ?:0, *:1, +:2, ??:3, *?:4, +?:5 */
2152
2155
static int
2153
 
popular_quantifier_num(QuantifierNode* q)
 
2156
popular_quantifier_num(QtfrNode* q)
2154
2157
{
2155
2158
  if (q->greedy) {
2156
2159
    if (q->lower == 0) {
2197
2200
onig_reduce_nested_quantifier(Node* pnode, Node* cnode)
2198
2201
{
2199
2202
  int pnum, cnum;
2200
 
  QuantifierNode *p, *c;
 
2203
  QtfrNode *p, *c;
2201
2204
 
2202
 
  p = &(NQUANTIFIER(pnode));
2203
 
  c = &(NQUANTIFIER(cnode));
 
2205
  p = NQTFR(pnode);
 
2206
  c = NQTFR(cnode);
2204
2207
  pnum = popular_quantifier_num(p);
2205
2208
  cnum = popular_quantifier_num(c);
 
2209
  if (pnum < 0 || cnum < 0) return ;
2206
2210
 
2207
2211
  switch(ReduceTypeTable[cnum][pnum]) {
2208
2212
  case RQ_DEL:
2209
 
    *p = *c;
 
2213
    *pnode = *cnode;
2210
2214
    break;
2211
2215
  case RQ_A:
2212
2216
    p->target = c->target;
2705
2709
}
2706
2710
 
2707
2711
static void
2708
 
CCEND_ESC_WARN(ScanEnv* env, UChar* c)
 
2712
CLOSE_BRACKET_WITHOUT_ESC_WARN(ScanEnv* env, UChar* c)
2709
2713
{
2710
2714
  if (onig_warn == onig_null_warn) return ;
2711
2715
 
3568
3572
      if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LINE_ANCHOR)) break;
3569
3573
      tok->type = TK_ANCHOR;
3570
3574
      tok->u.subtype = (IS_SINGLELINE(env->option)
3571
 
                        ? ANCHOR_END_BUF : ANCHOR_END_LINE);
 
3575
                        ? ANCHOR_SEMI_END_BUF : ANCHOR_END_LINE);
3572
3576
      break;
3573
3577
 
3574
3578
    case '[':
3578
3582
 
3579
3583
    case ']':
3580
3584
      if (*src > env->pattern)   /* /].../ is allowed. */
3581
 
        CCEND_ESC_WARN(env, (UChar* )"]");
 
3585
        CLOSE_BRACKET_WITHOUT_ESC_WARN(env, (UChar* )"]");
3582
3586
      break;
3583
3587
 
3584
3588
    case '#':
3887
3891
  if (ctype < 0) return ctype;
3888
3892
 
3889
3893
  *np = node_new_cclass();
3890
 
  CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
3891
 
  cc = &(NCCLASS(*np));
 
3894
  CHECK_NULL_RETURN_MEMERR(*np);
 
3895
  cc = NCCLASS(*np);
3892
3896
  r = add_ctype_to_cc(cc, ctype, 0, env);
3893
3897
  if (r != 0) return r;
3894
 
  if (tok->u.prop.not != 0) CCLASS_SET_NOT(cc);
 
3898
  if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
3895
3899
 
3896
3900
  return 0;
3897
3901
}
4069
4073
  }
4070
4074
 
4071
4075
  *np = node = node_new_cclass();
4072
 
  CHECK_NULL_RETURN_VAL(node, ONIGERR_MEMORY);
4073
 
  cc = &(NCCLASS(node));
 
4076
  CHECK_NULL_RETURN_MEMERR(node);
 
4077
  cc = NCCLASS(node);
4074
4078
 
4075
4079
  and_start = 0;
4076
4080
  state = CCS_START;
4258
4262
 
4259
4263
        r = parse_char_class(&anode, tok, &p, end, env);
4260
4264
        if (r != 0) goto cc_open_err;
4261
 
        acc = &(NCCLASS(anode));
 
4265
        acc = NCCLASS(anode);
4262
4266
        r = or_cclass(cc, acc, env->enc);
4263
4267
 
4264
4268
        onig_node_free(anode);
4323
4327
  }
4324
4328
 
4325
4329
  if (neg != 0)
4326
 
    CCLASS_SET_NOT(cc);
 
4330
    NCCLASS_SET_NOT(cc);
4327
4331
  else
4328
 
    CCLASS_CLEAR_NOT(cc);
4329
 
  if (IS_CCLASS_NOT(cc) &&
 
4332
    NCCLASS_CLEAR_NOT(cc);
 
4333
  if (IS_NCCLASS_NOT(cc) &&
4330
4334
      IS_SYNTAX_BV(env->syntax, ONIG_SYN_NOT_NEWLINE_IN_NEGATIVE_CC)) {
4331
4335
    int is_empty;
4332
4336
 
4349
4353
  return 0;
4350
4354
 
4351
4355
 err:
4352
 
  if (cc != &(NCCLASS(*np)))
 
4356
  if (cc != NCCLASS(*np))
4353
4357
    bbuf_free(cc->mbuf);
4354
4358
  onig_node_free(*np);
4355
4359
  return r;
4359
4363
                        UChar** src, UChar* end, ScanEnv* env);
4360
4364
 
4361
4365
static int
4362
 
parse_effect(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
4363
 
             ScanEnv* env)
 
4366
parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
 
4367
              ScanEnv* env)
4364
4368
{
4365
4369
  int r, num;
4366
4370
  int list_capture;
4399
4403
      *np = onig_node_new_anchor(ANCHOR_PREC_READ_NOT);
4400
4404
      break;
4401
4405
    case '>':            /* (?>...) stop backtrack */
4402
 
      *np = node_new_effect(EFFECT_STOP_BACKTRACK);
 
4406
      *np = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
4403
4407
      break;
4404
4408
 
4405
4409
    case '\'':
4440
4444
 
4441
4445
          r = name_add(env->reg, name, name_end, num, env);
4442
4446
          if (r != 0) return r;
4443
 
          *np = node_new_effect_memory(env->option, 1);
4444
 
          CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
4445
 
          NEFFECT(*np).regnum = num;
 
4447
          *np = node_new_enclose_memory(env->option, 1);
 
4448
          CHECK_NULL_RETURN_MEMERR(*np);
 
4449
          NENCLOSE(*np)->regnum = num;
4446
4450
          if (list_capture != 0)
4447
4451
            BIT_STATUS_ON_AT_SIMPLE(env->capture_history, num);
4448
4452
          env->num_named++;
4470
4474
          PUNFETCH;
4471
4475
        }
4472
4476
#endif
4473
 
        *np = node_new_effect_memory(env->option, 0);
4474
 
        CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
4477
        *np = node_new_enclose_memory(env->option, 0);
 
4478
        CHECK_NULL_RETURN_MEMERR(*np);
4475
4479
        num = scan_env_add_mem_entry(env);
4476
4480
        if (num < 0) {
4477
4481
          onig_node_free(*np);
4481
4485
          onig_node_free(*np);
4482
4486
          return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY;
4483
4487
        }
4484
 
        NEFFECT(*np).regnum = num;
 
4488
        NENCLOSE(*np)->regnum = num;
4485
4489
        BIT_STATUS_ON_AT_SIMPLE(env->capture_history, num);
4486
4490
      }
4487
4491
      else {
4534
4538
 
4535
4539
          if (c == ')') {
4536
4540
            *np = node_new_option(option);
4537
 
            CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
4541
            CHECK_NULL_RETURN_MEMERR(*np);
4538
4542
            *src = p;
4539
4543
            return 2; /* option only */
4540
4544
          }
4548
4552
            env->option = prev;
4549
4553
            if (r < 0) return r;
4550
4554
            *np = node_new_option(option);
4551
 
            CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
4552
 
            NEFFECT(*np).target = target;
 
4555
            CHECK_NULL_RETURN_MEMERR(*np);
 
4556
            NENCLOSE(*np)->target = target;
4553
4557
            *src = p;
4554
4558
            return 0;
4555
4559
          }
4568
4572
    if (ONIG_IS_OPTION_ON(env->option, ONIG_OPTION_DONT_CAPTURE_GROUP))
4569
4573
      goto group;
4570
4574
 
4571
 
    *np = node_new_effect_memory(env->option, 0);
4572
 
    CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
4575
    *np = node_new_enclose_memory(env->option, 0);
 
4576
    CHECK_NULL_RETURN_MEMERR(*np);
4573
4577
    num = scan_env_add_mem_entry(env);
4574
4578
    if (num < 0) return num;
4575
 
    NEFFECT(*np).regnum = num;
 
4579
    NENCLOSE(*np)->regnum = num;
4576
4580
  }
4577
4581
 
4578
 
  CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
4582
  CHECK_NULL_RETURN_MEMERR(*np);
4579
4583
  r = fetch_token(tok, &p, end, env);
4580
4584
  if (r < 0) return r;
4581
4585
  r = parse_subexp(&target, tok, term, &p, end, env);
4582
4586
  if (r < 0) return r;
4583
4587
 
4584
 
  if (NTYPE(*np) == N_ANCHOR)
4585
 
    NANCHOR(*np).target = target;
 
4588
  if (NTYPE(*np) == NT_ANCHOR)
 
4589
    NANCHOR(*np)->target = target;
4586
4590
  else {
4587
 
    NEFFECT(*np).target = target;
4588
 
    if (NEFFECT(*np).type == EFFECT_MEMORY) {
 
4591
    NENCLOSE(*np)->target = target;
 
4592
    if (NENCLOSE(*np)->type == ENCLOSE_MEMORY) {
4589
4593
      /* Don't move this to previous of parse_subexp() */
4590
 
      r = scan_env_set_mem_node(env, NEFFECT(*np).regnum, *np);
 
4594
      r = scan_env_set_mem_node(env, NENCLOSE(*np)->regnum, *np);
4591
4595
      if (r != 0) return r;
4592
4596
    }
4593
4597
  }
4607
4611
static int
4608
4612
set_quantifier(Node* qnode, Node* target, int group, ScanEnv* env)
4609
4613
{
4610
 
  QuantifierNode* qn;
 
4614
  QtfrNode* qn;
4611
4615
 
4612
 
  qn = &(NQUANTIFIER(qnode));
 
4616
  qn = NQTFR(qnode);
4613
4617
  if (qn->lower == 1 && qn->upper == 1) {
4614
4618
    return 1;
4615
4619
  }
4616
4620
 
4617
4621
  switch (NTYPE(target)) {
4618
 
  case N_STRING:
 
4622
  case NT_STR:
4619
4623
    if (! group) {
4620
 
      StrNode* sn = &(NSTRING(target));
 
4624
      StrNode* sn = NSTR(target);
4621
4625
      if (str_node_can_be_split(sn, env->enc)) {
4622
4626
        Node* n = str_node_split_last_char(sn, env->enc);
4623
4627
        if (IS_NOT_NULL(n)) {
4628
4632
    }
4629
4633
    break;
4630
4634
 
4631
 
  case N_QUANTIFIER:
 
4635
  case NT_QTFR:
4632
4636
    { /* check redundant double repeat. */
4633
4637
      /* verbose warn (?:.?)? etc... but not warn (.?)? etc... */
4634
 
      QuantifierNode* qnt = &(NQUANTIFIER(target));
 
4638
      QtfrNode* qnt   = NQTFR(target);
4635
4639
      int nestq_num   = popular_quantifier_num(qn);
4636
4640
      int targetq_num = popular_quantifier_num(qnt);
4637
4641
 
4718
4722
static int type_cclass_hash(type_cclass_key* key)
4719
4723
{
4720
4724
  int i, val;
4721
 
  unsigned char *p;
 
4725
  UChar *p;
4722
4726
 
4723
4727
  val = 0;
4724
4728
 
4725
 
  p = (unsigned char* )&(key->enc);
 
4729
  p = (UChar* )&(key->enc);
4726
4730
  for (i = 0; i < sizeof(key->enc); i++) {
4727
4731
    val = val * 997 + (int )*p++;
4728
4732
  }
4729
4733
 
4730
 
  p = (unsigned char* )(&key->type);
 
4734
  p = (UChar* )(&key->type);
4731
4735
  for (i = 0; i < sizeof(key->type); i++) {
4732
4736
    val = val * 997 + (int )*p++;
4733
4737
  }
4736
4740
  return val + (val >> 5);
4737
4741
}
4738
4742
 
4739
 
static struct st_hash_type type_type_cclass_hash = {
 
4743
static const struct st_hash_type type_type_cclass_hash = {
4740
4744
    type_cclass_cmp,
4741
4745
    type_cclass_hash,
4742
4746
};
4748
4752
i_free_shared_class(type_cclass_key* key, Node* node, void* arg)
4749
4753
{
4750
4754
  if (IS_NOT_NULL(node)) {
4751
 
    CClassNode* cc = &(NCCLASS(node));
 
4755
    CClassNode* cc = NCCLASS(node);
4752
4756
    if (IS_NOT_NULL(cc->mbuf)) xfree(cc->mbuf);
4753
4757
    xfree(node);
4754
4758
  }
4777
4781
  CClassNode* cc;
4778
4782
  Node*       alt_root;
4779
4783
  Node**      ptail;
4780
 
} ICaseFoldArgType;
 
4784
} IApplyCaseFoldArg;
4781
4785
 
4782
4786
static int
4783
 
i_case_fold(OnigCodePoint from, OnigCodePoint to[], int to_len, void* arg)
 
4787
i_apply_case_fold(OnigCodePoint from, OnigCodePoint to[],
 
4788
                  int to_len, void* arg)
4784
4789
{
4785
 
  ICaseFoldArgType* iarg;
 
4790
  IApplyCaseFoldArg* iarg;
4786
4791
  ScanEnv* env;
4787
4792
  CClassNode* cc;
4788
4793
  BitSetRef bs;
4789
4794
 
4790
 
  iarg = (ICaseFoldArgType* )arg;
 
4795
  iarg = (IApplyCaseFoldArg* )arg;
4791
4796
  env = iarg->env;
4792
4797
  cc  = iarg->cc;
4793
4798
  bs = cc->bs;
4795
4800
  if (to_len == 1) {
4796
4801
    int in_cc;
4797
4802
    in_cc = onig_is_code_in_cc(env->enc, from, cc);
4798
 
    if ((in_cc != 0 && !IS_CCLASS_NOT(cc)) ||
4799
 
        (in_cc == 0 &&  IS_CCLASS_NOT(cc))) {
4800
 
      if (ONIGENC_MBC_MINLEN(env->enc) > 1 || from >= SINGLE_BYTE_SIZE) {
 
4803
    if ((in_cc != 0 && !IS_NCCLASS_NOT(cc)) ||
 
4804
        (in_cc == 0 &&  IS_NCCLASS_NOT(cc))) {
 
4805
      if (ONIGENC_MBC_MINLEN(env->enc) > 1 || *to >= SINGLE_BYTE_SIZE) {
 
4806
        if (IS_NCCLASS_NOT(cc)) clear_not_flag_cclass(cc, env->enc);
4801
4807
        add_code_range(&(cc->mbuf), env, *to, *to);
4802
4808
      }
4803
4809
      else {
4804
 
        if (BITSET_AT(bs, from)) {
4805
 
          /* /(?i:[^A-C])/.match("a") ==> fail. */
 
4810
        /* /(?i:[^A-C])/.match("a") ==> fail. */
 
4811
        if (IS_NCCLASS_NOT(cc))
 
4812
          BITSET_CLEAR_BIT(bs, *to);
 
4813
        else
4806
4814
          BITSET_SET_BIT(bs, *to);
4807
 
        }
4808
4815
      }
4809
4816
    }
4810
4817
  }
4814
4821
    Node *snode = NULL_NODE;
4815
4822
 
4816
4823
    if (onig_is_code_in_cc(env->enc, from, cc)) {
4817
 
      if (IS_CCLASS_NOT(cc)) clear_not_flag_cclass(cc, env->enc);
 
4824
      if (IS_NCCLASS_NOT(cc)) clear_not_flag_cclass(cc, env->enc);
4818
4825
 
4819
4826
      for (i = 0; i < to_len; i++) {
4820
4827
        len = ONIGENC_CODE_TO_MBC(env->enc, to[i], buf);
4821
4828
        if (i == 0) {
4822
4829
          snode = onig_node_new_str(buf, buf + len);
4823
 
          CHECK_NULL_RETURN_VAL(snode, ONIGERR_MEMORY);
 
4830
          CHECK_NULL_RETURN_MEMERR(snode);
4824
4831
 
4825
4832
          /* char-class expanded multi-char only
4826
4833
             compare with string folded at match time. */
4836
4843
      }
4837
4844
 
4838
4845
      *(iarg->ptail) = onig_node_new_alt(snode, NULL_NODE);
4839
 
      CHECK_NULL_RETURN_VAL(*(iarg->ptail), ONIGERR_MEMORY);
4840
 
      iarg->ptail = &(NCONS((*(iarg->ptail))).right);
 
4846
      CHECK_NULL_RETURN_MEMERR(*(iarg->ptail));
 
4847
      iarg->ptail = &(NCDR((*(iarg->ptail))));
4841
4848
    }
4842
4849
  }
4843
4850
 
4865
4872
  break;
4866
4873
 
4867
4874
  case TK_SUBEXP_OPEN:
4868
 
    r = parse_effect(np, tok, TK_SUBEXP_CLOSE, src, end, env);
 
4875
    r = parse_enclose(np, tok, TK_SUBEXP_CLOSE, src, end, env);
4869
4876
    if (r < 0) return r;
4870
4877
    if (r == 1) group = 1;
4871
4878
    else if (r == 2) { /* option only */
4872
4879
      Node* target;
4873
4880
      OnigOptionType prev = env->option;
4874
4881
 
4875
 
      env->option = NEFFECT(*np).option;
 
4882
      env->option = NENCLOSE(*np)->option;
4876
4883
      r = fetch_token(tok, src, end, env);
4877
4884
      if (r < 0) return r;
4878
4885
      r = parse_subexp(&target, tok, term, src, end, env);
4879
4886
      env->option = prev;
4880
4887
      if (r < 0) return r;
4881
 
      NEFFECT(*np).target = target;     
 
4888
      NENCLOSE(*np)->target = target;   
4882
4889
      return tok->type;
4883
4890
    }
4884
4891
    break;
4895
4902
  tk_byte:
4896
4903
    {
4897
4904
      *np = node_new_str(tok->backp, *src);
4898
 
      CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
4905
      CHECK_NULL_RETURN_MEMERR(*np);
4899
4906
 
4900
4907
      while (1) {
4901
4908
        r = fetch_token(tok, src, end, env);
4916
4923
  tk_raw_byte:
4917
4924
    {
4918
4925
      *np = node_new_str_raw_char((UChar )tok->u.c);
4919
 
      CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
4926
      CHECK_NULL_RETURN_MEMERR(*np);
4920
4927
      len = 1;
4921
4928
      while (1) {
4922
4929
        if (len >= ONIGENC_MBC_MINLEN(env->enc)) {
4923
 
          if (len == enc_len(env->enc, NSTRING(*np).s)) {
 
4930
          if (len == enc_len(env->enc, NSTR(*np)->s)) {
4924
4931
            r = fetch_token(tok, src, end, env);
4925
4932
            NSTRING_CLEAR_RAW(*np);
4926
4933
            goto string_end;
4935
4942
          int rem;
4936
4943
          if (len < ONIGENC_MBC_MINLEN(env->enc)) {
4937
4944
            rem = ONIGENC_MBC_MINLEN(env->enc) - len;
4938
 
            (void )node_str_head_pad(&NSTRING(*np), rem, (UChar )0);
4939
 
            if (len + rem == enc_len(env->enc, NSTRING(*np).s)) {
 
4945
            (void )node_str_head_pad(NSTR(*np), rem, (UChar )0);
 
4946
            if (len + rem == enc_len(env->enc, NSTR(*np)->s)) {
4940
4947
              NSTRING_CLEAR_RAW(*np);
4941
4948
              goto string_end;
4942
4949
            }
4963
4970
#else
4964
4971
      *np = node_new_str(buf, buf + num);
4965
4972
#endif
4966
 
      CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
4973
      CHECK_NULL_RETURN_MEMERR(*np);
4967
4974
    }
4968
4975
    break;
4969
4976
 
4980
4987
        nextp = qend = end;
4981
4988
      }
4982
4989
      *np = node_new_str(qstart, qend);
4983
 
      CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
4990
      CHECK_NULL_RETURN_MEMERR(*np);
4984
4991
      *src = nextp;
4985
4992
    }
4986
4993
    break;
4990
4997
      switch (tok->u.prop.ctype) {
4991
4998
      case ONIGENC_CTYPE_WORD:
4992
4999
        *np = node_new_ctype(tok->u.prop.ctype, tok->u.prop.not);
4993
 
        CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
5000
        CHECK_NULL_RETURN_MEMERR(*np);
4994
5001
        break;
4995
5002
 
4996
5003
      case ONIGENC_CTYPE_SPACE:
5040
5047
              return ONIGERR_MEMORY;
5041
5048
            }
5042
5049
 
5043
 
            cc = &(NCCLASS(*np));
5044
 
            CCLASS_SET_SHARE(cc);
 
5050
            cc = NCCLASS(*np);
 
5051
            NCCLASS_SET_SHARE(cc);
5045
5052
            new_key = (type_cclass_key* )xmalloc(sizeof(type_cclass_key));
 
5053
            xmemcpy(new_key, &key, sizeof(type_cclass_key));
5046
5054
            onig_st_add_direct(OnigTypeCClassTable, (st_data_t )new_key,
5047
5055
                               (st_data_t )*np);
5048
5056
            
5051
5059
          else {
5052
5060
#endif
5053
5061
            *np = node_new_cclass();
5054
 
            CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
5055
 
            cc = &(NCCLASS(*np));
 
5062
            CHECK_NULL_RETURN_MEMERR(*np);
 
5063
            cc = NCCLASS(*np);
5056
5064
            add_ctype_to_cc(cc, tok->u.prop.ctype, 0, env);
5057
 
            if (tok->u.prop.not != 0) CCLASS_SET_NOT(cc);
 
5065
            if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
5058
5066
#ifdef USE_SHARED_CCLASS_TABLE
5059
5067
          }
5060
5068
#endif
5080
5088
      r = parse_char_class(np, tok, src, end, env);
5081
5089
      if (r != 0) return r;
5082
5090
 
5083
 
      cc = &(NCCLASS(*np));
5084
 
 
 
5091
      cc = NCCLASS(*np);
5085
5092
      if (IS_IGNORECASE(env->option)) {
5086
 
        ICaseFoldArgType iarg;
 
5093
        IApplyCaseFoldArg iarg;
5087
5094
 
5088
 
        iarg.env = env;
5089
 
        iarg.cc  = cc;
 
5095
        iarg.env      = env;
 
5096
        iarg.cc       = cc;
5090
5097
        iarg.alt_root = NULL_NODE;
5091
5098
        iarg.ptail    = &(iarg.alt_root);
5092
5099
 
5093
5100
        r = ONIGENC_APPLY_ALL_CASE_FOLD(env->enc, env->case_fold_flag,
5094
 
                                        i_case_fold, &iarg);
 
5101
                                        i_apply_case_fold, &iarg);
5095
5102
        if (r != 0) {
5096
 
          if (IS_NOT_NULL(iarg.alt_root))
5097
 
            onig_node_free(iarg.alt_root);
 
5103
          onig_node_free(iarg.alt_root);
5098
5104
          return r;
5099
5105
        }
5100
5106
        if (IS_NOT_NULL(iarg.alt_root)) {
5111
5117
 
5112
5118
  case TK_ANYCHAR:
5113
5119
    *np = node_new_anychar();
5114
 
    CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
5120
    CHECK_NULL_RETURN_MEMERR(*np);
5115
5121
    break;
5116
5122
 
5117
5123
  case TK_ANYCHAR_ANYTIME:
5118
5124
    *np = node_new_anychar();
5119
 
    CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
5125
    CHECK_NULL_RETURN_MEMERR(*np);
5120
5126
    qn = node_new_quantifier(0, REPEAT_INFINITE, 0);
5121
 
    CHECK_NULL_RETURN_VAL(qn, ONIGERR_MEMORY);
5122
 
    NQUANTIFIER(qn).target = *np;
 
5127
    CHECK_NULL_RETURN_MEMERR(qn);
 
5128
    NQTFR(qn)->target = *np;
5123
5129
    *np = qn;
5124
5130
    break;
5125
5131
 
5133
5139
                           tok->u.backref.level,
5134
5140
#endif
5135
5141
                           env);
5136
 
    CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
5142
    CHECK_NULL_RETURN_MEMERR(*np);
5137
5143
    break;
5138
5144
 
5139
5145
#ifdef USE_SUBEXP_CALL
5140
5146
  case TK_CALL:
5141
5147
    *np = node_new_call(tok->u.call.name, tok->u.call.name_end);
5142
 
    CHECK_NULL_RETURN_VAL(*np, ONIGERR_MEMORY);
 
5148
    CHECK_NULL_RETURN_MEMERR(*np);
5143
5149
    env->num_call++;
5144
5150
    break;
5145
5151
#endif
5179
5185
        return ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID;
5180
5186
 
5181
5187
      qn = node_new_quantifier(tok->u.repeat.lower, tok->u.repeat.upper,
5182
 
                              (r == TK_INTERVAL ? 1 : 0));
5183
 
      CHECK_NULL_RETURN_VAL(qn, ONIGERR_MEMORY);
5184
 
      NQUANTIFIER(qn).greedy = tok->u.repeat.greedy;
 
5188
                               (r == TK_INTERVAL ? 1 : 0));
 
5189
      CHECK_NULL_RETURN_MEMERR(qn);
 
5190
      NQTFR(qn)->greedy = tok->u.repeat.greedy;
5185
5191
      r = set_quantifier(qn, *targetp, group, env);
5186
5192
      if (r < 0) return r;
5187
5193
      
5188
5194
      if (tok->u.repeat.possessive != 0) {
5189
5195
        Node* en;
5190
 
        en = node_new_effect(EFFECT_STOP_BACKTRACK);
5191
 
        CHECK_NULL_RETURN_VAL(en, ONIGERR_MEMORY);
5192
 
        NEFFECT(en).target = qn;
 
5196
        en = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
 
5197
        CHECK_NULL_RETURN_MEMERR(en);
 
5198
        NENCLOSE(en)->target = qn;
5193
5199
        qn = en;
5194
5200
      }
5195
5201
 
5200
5206
        Node *tmp;
5201
5207
 
5202
5208
        *targetp = node_new_list(*targetp, NULL);
5203
 
        CHECK_NULL_RETURN_VAL(*targetp, ONIGERR_MEMORY);
5204
 
        tmp = NCONS(*targetp).right = node_new_list(qn, NULL);
5205
 
        CHECK_NULL_RETURN_VAL(tmp, ONIGERR_MEMORY);
5206
 
        targetp = &(NCONS(tmp).left);
 
5209
        CHECK_NULL_RETURN_MEMERR(*targetp);
 
5210
        tmp = NCDR(*targetp) = node_new_list(qn, NULL);
 
5211
        CHECK_NULL_RETURN_MEMERR(tmp);
 
5212
        targetp = &(NCAR(tmp));
5207
5213
      }
5208
5214
      goto re_entry;
5209
5215
    }
5228
5234
  }
5229
5235
  else {
5230
5236
    *top  = node_new_list(node, NULL);
5231
 
    headp = &(NCONS(*top).right);
 
5237
    headp = &(NCDR(*top));
5232
5238
    while (r != TK_EOT && r != term && r != TK_ALT) {
5233
5239
      r = parse_exp(&node, tok, term, src, end, env);
5234
5240
      if (r < 0) return r;
5235
5241
 
5236
 
      if (NTYPE(node) == N_LIST) {
 
5242
      if (NTYPE(node) == NT_LIST) {
5237
5243
        *headp = node;
5238
 
        while (IS_NOT_NULL(NCONS(node).right)) node = NCONS(node).right;
5239
 
        headp = &(NCONS(node).right);
 
5244
        while (IS_NOT_NULL(NCDR(node))) node = NCDR(node);
 
5245
        headp = &(NCDR(node));
5240
5246
      }
5241
5247
      else {
5242
5248
        *headp = node_new_list(node, NULL);
5243
 
        headp = &(NCONS(*headp).right);
 
5249
        headp = &(NCDR(*headp));
5244
5250
      }
5245
5251
    }
5246
5252
  }
5268
5274
  }
5269
5275
  else if (r == TK_ALT) {
5270
5276
    *top  = onig_node_new_alt(node, NULL);
5271
 
    headp = &(NCONS(*top).right);
 
5277
    headp = &(NCDR(*top));
5272
5278
    while (r == TK_ALT) {
5273
5279
      r = fetch_token(tok, src, end, env);
5274
5280
      if (r < 0) return r;
5276
5282
      if (r < 0) return r;
5277
5283
 
5278
5284
      *headp = onig_node_new_alt(node, NULL);
5279
 
      headp = &(NCONS(*headp).right);
 
5285
      headp = &(NCDR(*headp));
5280
5286
    }
5281
5287
 
5282
5288
    if (tok->type != term)
5307
5313
}
5308
5314
 
5309
5315
extern int
5310
 
onig_parse_make_tree(Node** root, const UChar* pattern, const UChar* end, regex_t* reg,
5311
 
                      ScanEnv* env)
 
5316
onig_parse_make_tree(Node** root, const UChar* pattern, const UChar* end,
 
5317
                     regex_t* reg, ScanEnv* env)
5312
5318
{
5313
5319
  int r;
5314
5320
  UChar* p;