File: stapregex-tree.cxx

package info (click to toggle)
systemtap 5.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 47,964 kB
  • sloc: cpp: 80,838; ansic: 54,757; xml: 49,725; exp: 43,665; sh: 11,527; python: 5,003; perl: 2,252; tcl: 1,312; makefile: 1,006; javascript: 149; lisp: 105; awk: 101; asm: 91; java: 70; sed: 16
file content (573 lines) | stat: -rw-r--r-- 11,321 bytes parent folder | download | duplicates (4)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// -*- C++ -*-
// Copyright (C) 2012-2013 Red Hat Inc.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
//
// ---
//
// This file incorporates code from the re2c project; please see
// the file README.stapregex for details.

#include <string>
#include <deque>
#include <iterator>
#include <algorithm>
#include <utility>
#include <cmath>
#include <cassert>

#include "stapregex-parse.h"
#include "stapregex-tree.h"

using namespace std;

namespace stapregex {

range::range (rchar lb, rchar ub)
{
  segments.push_back(make_pair(lb,ub));
}

range::range (const string& str)
{
  cursor cur(&str); // no unescaping

  if (cur.finished) return;

  range *ran = stapregex_getrange(cur);

  while (!cur.finished)
    {
      range *add = stapregex_getrange(cur);
      range *new_ran = ( ran != NULL ? range_union(ran, add) : add );
      delete ran; if (new_ran != add) delete add;
      ran = new_ran;
    }

  segments = ran->segments;
  delete ran;
}

void
range::print (std::ostream& o) const
{
  if (segments.empty())
    {
      o << "{none}"; // XXX: pick a better pseudo-notation?
      return;
    }

  if (segments.size() == 1 && segments[0].first == segments[0].second)
    {
      print_escaped (o, segments[0].first);
      return;
    }

  o << "[";
  for (deque<segment>::const_iterator it = segments.begin();
       it != segments.end(); it++)
    {
      rchar lb = it->first; rchar ub = it->second;
      if (lb == ub)
        {
          print_escaped (o, lb);
        }
      else
        {
          print_escaped (o, lb);
          o << "-";
          print_escaped (o, ub);
        }
    }
  o << "]";
}

std::ostream&
operator << (std::ostream& o, const range& ran)
{
  ran.print(o);
  return o;
}

std::ostream&
operator << (std::ostream& o, const range* ran)
{
  if (ran)
    o << *ran;
  else
    o << "{none}"; // XXX: pick a better pseudo-notation?
  return o;
}

// ------------------------------------------------------------------------

range *
range_union(range *old_a, range *old_b)
{
  if (old_a == NULL && old_b == NULL) return NULL;
  if (old_a == NULL) return new range(*old_b);
  if (old_b == NULL) return new range(*old_a);

  /* First, gather the segments from both ranges into one sorted pile: */
  deque<segment> s;
  merge(old_a->segments.begin(), old_a->segments.end(),
        old_b->segments.begin(), old_b->segments.end(),
        inserter(s, s.end()));

  /* Now go through and merge overlapping segments. */
  range *ran = new range;

  while (!s.empty())
    {
      /* Merge adjacent overlapping segments. */
      while (s.size() >= 2 && s[0].second >= s[1].first)
        {
          segment merged = make_pair(min(s[0].first, s[1].first),
                                     max(s[0].second, s[1].second));
          s.pop_front(); s.pop_front();
          s.push_front(merged);
        } 

      /* Place non-overlapping segment in range. */
      ran->segments.push_back(s.front()); s.pop_front();
    }

  return ran;
}

range *
range_invert(range *old_ran)
{
  range ran(*old_ran);
  range *new_ran = new range;

  rchar start = '\1'; // exclude '\0'

  while (!ran.segments.empty()) {
    rchar end = ran.segments.front().first - 1;
    if (start <= end) new_ran->segments.push_back(make_pair(start, end));
    start = ran.segments.front().second + 1;
    ran.segments.pop_front();
  }

  if ((unsigned)start < (unsigned)NUM_REAL_CHARS)
    new_ran->segments.push_back(make_pair(start, NUM_REAL_CHARS-1));

  return new_ran;
}

// ------------------------------------------------------------------------

void
ins_optimize (ins *i)
{
  while (!marked(i))
    {
      mark (i); // -- aka "this node has already been optimized"

      if (i->i.tag == CHAR)
        {
          i = (ins *) i->i.link; // -- skip node
        }
      else if (i->i.tag == GOTO || i->i.tag == FORK)
        {
          ins *target = (ins *) i->i.link;
          ins_optimize(target);

          if (target->i.tag == GOTO)
            i->i.link = target->i.link == target ? i : target;

          if (i->i.tag == FORK)
            {
              ins *follow = (ins *) &i[1];
              ins_optimize(follow);

              if (follow->i.tag == GOTO && follow->i.link == follow)
                {
                  i->i.tag = GOTO;
                }
              else if (i->i.link == i)
                {
                  i->i.tag = GOTO;
                  i->i.link = follow;
                }
            }
        }
      else
        ++i; // -- skip node
    }
}

// ------------------------------------------------------------------------

const ins*
show_ins (std::ostream &o, const ins *i, const ins *base)
{
  o.width(3); o << (i - base) << ": ";

  const ins *ret = &i[1];

  switch (i->i.tag)
    {
    case CHAR:
      o << "match ";
      for (; ret < (ins *) i->i.link; ++ret) print_escaped(o, ret->c.value);
      break;

    case GOTO:
      o << "goto " << ((ins *) i->i.link - base);
      break;

    case FORK:
      o << "fork(" << ( i->i.param ? "prefer" : "avoid" ) << ") "
        << ((ins *) i->i.link - base);
      break;

    case ACCEPT:
      o << "accept(" << i->i.param << ")";
      break;

    case TAG:
      o << "tag(" << i->i.param << ")";
      break;

    case INIT:
      o << "init";
      break;
    }

  return ret;
}

// ------------------------------------------------------------------------

ins *
regexp::compile()
{
  unsigned k = ins_size();

  ins *i = new ins[k + 1];
  // XXX Keep Valgrind from complaining in ins_optimize():
  for (unsigned ix = 0; ix <= k; ix++) i[ix].i.marked = 0;
  compile(i);

  // Append an infinite-loop GOTO to avoid edges going outside the array:
  i[k].i.tag = GOTO;
  i[k].i.link = &i[k];

  return i;
}

std::ostream&
operator << (std::ostream &o, const regexp& re)
{
  re.print (o);
  return o;
}

std::ostream&
operator << (std::ostream &o, const regexp* re)
{
  o << *re;
  return o;
}

// ------------------------------------------------------------------------

void
null_op::calc_size()
{
  size = 0;
}

void
null_op::compile(ins *)
{
  ;
}

anchor_op::anchor_op(rchar type) : type(type) {}

void
anchor_op::calc_size()
{
  size = ( type == '^' ? 1 : 2 );
}

void
anchor_op::compile(ins *i)
{
  if (type == '^')
    {
      i->i.tag = INIT;
      i->i.link = &i[1];
    }
  else // type == '$'
    {
      i->i.tag = CHAR;
      i->i.link = &i[2];
      ins *j = &i[1];
      j->c.value = '\0';
      j->c.bump = 1;
    }
}

tag_op::tag_op(unsigned id) : id(id) {}

void
tag_op::calc_size()
{
  size = 1;
}

void
tag_op::compile(ins *i)
{
  i->i.tag = TAG;
  i->i.param = id;
}

match_op::match_op(range *ran) : ran(ran) {}

void
match_op::calc_size()
{
  size = 1;

  for (deque<segment>::iterator it = ran->segments.begin();
       it != ran->segments.end(); it++)
    {
      size += it->second - it->first + 1;
    }
}

void
match_op::compile(ins *i)
{
  unsigned bump = ins_size();
  i->i.tag = CHAR;
  i->i.link = &i[bump]; // mark end of table

  ins *j = &i[1];
  for (deque<segment>::iterator it = ran->segments.begin();
       it != ran->segments.end(); it++)
    {
      for (unsigned c = it->first; c <= (unsigned) it->second; c++)
        {
          j->c.value = c;
          j->c.bump = --bump; // mark end of table
          j++;
        }
    }
}

alt_op::alt_op(regexp *a, regexp *b, bool prefer_second)
  : a(a), b(b), prefer_second(prefer_second) {}

void
alt_op::calc_size()
{
  size = a->ins_size() + b->ins_size() + 2;
}

void
alt_op::compile(ins *i)
{
  i->i.tag = FORK;
  i->i.param = prefer_second ? 1 : 0; // preferred alternative to match
  ins *j = &i[a->ins_size() + 1];
  i->i.link = &j[1];
  a->compile(&i[1]);
  j->i.tag = GOTO;
  j->i.link = &j[b->ins_size() + 1];
  b->compile(&j[1]);
}

cat_op::cat_op(regexp *a, regexp *b) : a(a), b(b) {}

void
cat_op::calc_size()
{
  size = a->ins_size() + b->ins_size();
}

void
cat_op::compile(ins *i)
{
  a->compile(&i[0]);
  b->compile(&i[a->ins_size()]);
}

close_op::close_op(regexp *re, bool prefer_shorter)
  : re(re), prefer_shorter(prefer_shorter) {}

void
close_op::calc_size()
{
  size = re->ins_size() + 1;
}

void
close_op::compile(ins *i)
{
  re->compile(&i[0]);
  i += re->ins_size();
  i->i.tag = FORK;
  i->i.param = prefer_shorter ? 0 : 1; // XXX: match greedily by default
  i->i.link = i - re->ins_size();
}

closev_op::closev_op(regexp *re, int nmin, int nmax)
  : re(re), nmin(nmin), nmax(nmax) {}

void
closev_op::calc_size()
{
  unsigned k = re->ins_size();

  if (nmax >= 0)
    size = k * nmin + (nmax - nmin) * (1 + k);
  else
    size = k * nmin + 1;
}

void
closev_op::compile(ins *i)
{
  unsigned k = re->ins_size();

  ins *jumppoint = i + ((nmax - nmin) * (1 + k));

  for (int st = nmin; st < nmax; st++)
    {
      i->i.tag = FORK;
      i->i.param = 0; // XXX: this matches greedily
      i->i.link = jumppoint;
      i++;
      re->compile(&i[0]);
      i += k;
    }

  for (int st = 0; st < nmin; st++)
    {
      re->compile(&i[0]);
      i += k;

      if (nmax < 0 && st == 0)
        {
          i->i.tag = FORK;
          i->i.param = 1; // XXX: this matches greedily
          i->i.link = i - k;
          i++;
        }
    }
}

rule_op::rule_op(regexp *re, unsigned outcome) : re(re), outcome(outcome) {}

void
rule_op::calc_size()
{
  size = re->ins_size() + 1;
}

void
rule_op::compile(ins *i)
{
  re->compile(&i[0]);
  i += re->ins_size();
  i->i.tag = ACCEPT;
  i->i.param = outcome;
}

// ------------------------------------------------------------------------

regexp *
match_char(rchar c)
{
  return new match_op(new range(c,c));
}

regexp *
str_to_re(const string& str)
{
  if (str.empty()) return new null_op;

  regexp *re = match_char(str[0]);

  for (unsigned i = 1; i < str.length(); i++)
    re = new cat_op(re, match_char(str[i]));

  return re;
}

regexp *
do_alt(regexp *a, regexp *b)
{
  if (a == NULL) return b;
  if (b == NULL) return a;
  return new alt_op(a,b);
}

regexp *
make_alt(regexp *a, regexp *b)
{
  /* Optimize the case of building alternatives of match_op. */
  regexp *e1 = NULL, *e2 = NULL;
  range *r1 = NULL, *r2 = NULL;

  if (a->type_of() == "alt_op")
    {
      alt_op *aa = (alt_op *)a;
      if (aa->a->type_of() == "match_op")
        {
          r1 = ((match_op *) aa->a)->ran; e1 = aa->b;
        }
      else
        e1 = a;
    }
  else if (a->type_of() == "match_op")
    {
      r1 = ((match_op *) a)->ran; e1 = NULL;
    }
  else
    e1 = a;

  if (b->type_of() == "alt_op")
    {
      alt_op *bb = (alt_op *)b;
      if (bb->a->type_of() == "match_op")
        {
          r2 = ((match_op *) bb->a)->ran; e2 = bb->b;
        }
      else
        e2 = b;
    }
  else if (b->type_of() == "match_op")
    {
      r2 = ((match_op *) b)->ran; e2 = NULL;
    }
  else
    e2 = b;

  range *u = range_union(r1, r2);
  delete r1; delete r2;

  match_op *m = u != NULL ? new match_op(u) : NULL;
  regexp *r = do_alt(m, do_alt(e1, e2));
  assert (r != NULL);
  return r;
}

regexp *
make_dot(bool allow_zero)
{
  return new match_op(new range(allow_zero ? 0 : 1, NUM_REAL_CHARS-1));
}

};

/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */