PatternString.cc 26 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Adam Knapp's avatar
Adam Knapp committed
2
 * Copyright (c) 2000-2021 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
 *
 * Contributors:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Delic, Adam
 *   Forstner, Matyas
 *   Raduly, Csaba
14
 *   Szabo, Bence Janos
Elemer Lelik's avatar
Elemer Lelik committed
15
16
17
18
 *   Szabo, Janos Zoltan – initial implementation
 *   Zalanyi, Balazs Andor
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
19
20
21
22
#include "PatternString.hh"
#include "../../common/pattern.hh"
#include "../CompilerError.hh"
#include "../Code.hh"
Elemer Lelik's avatar
Elemer Lelik committed
23
#include "../../common/JSON_Tokenizer.hh"
24
#include "../main.hh"
Elemer Lelik's avatar
Elemer Lelik committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include "TtcnTemplate.hh"

namespace Ttcn {

  // =================================
  // ===== PatternString::ps_elem_t
  // =================================

  struct PatternString::ps_elem_t {
    enum kind_t {
      PSE_STR,
      PSE_REF,
      PSE_REFDSET
    } kind;
40
41
42
43
44
45
    string *str;
    Ttcn::Reference *ref;
    Type * t; // The type of the reference in the case of PSE_REFDSET
    boolean with_N; // If the reference was given as \N{ref} in the pattern
    boolean is_charstring; // \N{charstring}
    boolean is_universal_charstring; // \N{universal charstring}
Elemer Lelik's avatar
Elemer Lelik committed
46
    ps_elem_t(kind_t p_kind, const string& p_str);
47
    ps_elem_t(kind_t p_kind, Ttcn::Reference *p_ref, boolean N);
Elemer Lelik's avatar
Elemer Lelik committed
48
49
50
51
52
53
54
55
56
57
    ps_elem_t(const ps_elem_t& p);
    ~ps_elem_t();
    ps_elem_t* clone() const;
    void set_fullname(const string& p_fullname);
    void set_my_scope(Scope *p_scope);
    void chk_ref(PatternString::pstr_type_t pstr_type, Type::expected_value_t expected_value);
    void set_code_section(GovernedSimple::code_section_t p_code_section);
  };

  PatternString::ps_elem_t::ps_elem_t(kind_t p_kind, const string& p_str)
58
59
    : kind(p_kind), ref(NULL), t(NULL), with_N(FALSE), is_charstring(FALSE),
    is_universal_charstring(FALSE)
Elemer Lelik's avatar
Elemer Lelik committed
60
61
62
63
  {
    str = new string(p_str);
  }

64
65
66
  PatternString::ps_elem_t::ps_elem_t(kind_t p_kind, Ttcn::Reference *p_ref, boolean N)
    : kind(p_kind), str(NULL), with_N(N), is_charstring(FALSE),
    is_universal_charstring(FALSE)
Elemer Lelik's avatar
Elemer Lelik committed
67
68
69
70
  {
    if (!p_ref) FATAL_ERROR("PatternString::ps_elem_t::ps_elem_t()");
    ref = p_ref;
  }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  
  PatternString::ps_elem_t::ps_elem_t(const PatternString::ps_elem_t& p)
    : kind(p.kind), str(NULL), ref(NULL), t(NULL), with_N(FALSE),
    is_charstring(FALSE), is_universal_charstring(FALSE)
  {
    switch(kind) {
    case PSE_STR:
      str = new string(*p.str);
      break;
    case PSE_REF:
      ref = p.ref->clone();
      break;
    case PSE_REFDSET:
      FATAL_ERROR("PatternString::ps_elem_t::ps_elem_t");
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
87
88
89
90
91
92

  PatternString::ps_elem_t::~ps_elem_t()
  {
    switch(kind) {
    case PSE_STR:
      delete str;
93
      // fall through
Elemer Lelik's avatar
Elemer Lelik committed
94
95
96
    case PSE_REF:
    case PSE_REFDSET:
      delete ref;
97
      // do not delete t
Elemer Lelik's avatar
Elemer Lelik committed
98
99
100
101
102
103
      break;
    } // switch kind
  }

  PatternString::ps_elem_t* PatternString::ps_elem_t::clone() const
  {
104
    return new ps_elem_t(*this);
Elemer Lelik's avatar
Elemer Lelik committed
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
  }

  void PatternString::ps_elem_t::set_fullname(const string& p_fullname)
  {
    switch(kind) {
    case PSE_REF:
    case PSE_REFDSET:
      ref->set_fullname(p_fullname);
      break;
    default:
      ;
    } // switch kind
  }

  void PatternString::ps_elem_t::set_my_scope(Scope *p_scope)
  {
    switch(kind) {
    case PSE_REF:
    case PSE_REFDSET:
      ref->set_my_scope(p_scope);
      break;
    default:
      ;
    } // switch kind
  }

  void PatternString::ps_elem_t::chk_ref(PatternString::pstr_type_t pstr_type, Type::expected_value_t expected_value)
  {
    if (kind != PSE_REF) FATAL_ERROR("PatternString::ps_elem_t::chk_ref()");
    Value* v = 0;
    Value* v_last = 0;
136
137
138
139
140
141
142
    if (ref->get_id()->get_name() == "CHARSTRING") {
      is_charstring = TRUE;
      return;
    } else if (ref->get_id()->get_name() == "UNIVERSAL_CHARSTRING") {
      is_universal_charstring = TRUE;
      return;
    }
Elemer Lelik's avatar
Elemer Lelik committed
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
    Common::Assignment* ass = ref->get_refd_assignment();
    if (!ass)
      return;
    Ttcn::FieldOrArrayRefs* t_subrefs = ref->get_subrefs();
    Type* ref_type = ass->get_Type()->get_type_refd_last()->get_field_type(
      t_subrefs, expected_value);
    Type::typetype_t tt;
    switch (pstr_type) {
      case PatternString::CSTR_PATTERN:
        tt = Type::T_CSTR;
        if (ref_type->get_typetype() != Type::T_CSTR)
          TTCN_pattern_error("Type of the referenced %s '%s' should be "
            "'charstring'", ass->get_assname(), ref->get_dispname().c_str());
        break;
      case PatternString::USTR_PATTERN:
        tt = ref_type->get_typetype();
        if (tt != Type::T_CSTR && tt != Type::T_USTR)
          TTCN_pattern_error("Type of the referenced %s '%s' should be either "
            "'charstring' or 'universal charstring'", ass->get_assname(),
            ref->get_dispname().c_str());
        break;
      default:
        FATAL_ERROR("Unknown pattern string type");
    }
    Type* refcheckertype = Type::get_pooltype(tt);
    switch (ass->get_asstype()) {
169
170
171
172
    case Common::Assignment::A_TYPE:
      kind = PSE_REFDSET;
      t = ass->get_Type();
      break;
Elemer Lelik's avatar
Elemer Lelik committed
173
    case Common::Assignment::A_MODULEPAR_TEMP:
174
175
176
177
    case Common::Assignment::A_VAR_TEMPLATE: 
    case Common::Assignment::A_PAR_TEMPL_IN:
    case Common::Assignment::A_PAR_TEMPL_OUT:
    case Common::Assignment::A_PAR_TEMPL_INOUT:
Elemer Lelik's avatar
Elemer Lelik committed
178
179
180
181
182
183
184
185
186
187
188
      // error reporting moved up
      break;
    case Common::Assignment::A_TEMPLATE: {
      Template* templ = ass->get_Template();
      refcheckertype->chk_this_template_ref(templ);
      refcheckertype->chk_this_template_generic(templ, INCOMPLETE_ALLOWED,
        OMIT_ALLOWED, ANY_OR_OMIT_ALLOWED, SUB_CHK, NOT_IMPLICIT_OMIT, 0);
      switch (templ->get_templatetype()) {
      case Template::SPECIFIC_VALUE:
        v_last = templ->get_specific_value();
        break;
189
      case Template::TEMPLATE_CONCAT:
190
191
192
        if (!use_runtime_2) {
          FATAL_ERROR("PatternString::ps_elem_t::chk_ref()");
        }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
        if (templ->is_Value()) {
          v = templ->get_Value();
          v->set_my_governor(refcheckertype);
          v->set_my_scope(ref->get_my_scope());
          v->set_location(*ref);
          refcheckertype->chk_this_value(v, 0, expected_value,
            INCOMPLETE_NOT_ALLOWED, OMIT_NOT_ALLOWED, SUB_CHK);
          v_last = v->get_value_refd_last();
        }
        else {
          TTCN_pattern_error("Unable to resolve referenced '%s' to character "
            "string type. Result of template concatenation is not a specific "
            "value.", ref->get_dispname().c_str());
        }
        break;
208
209
210
211
212
213
214
215
216
217
218
219
220
221
      case Template::CSTR_PATTERN: 
        if (!with_N) {
          Ttcn::PatternString* ps = templ->get_cstr_pattern();
          if (!ps->has_refs())
            v_last = ps->get_value();
          break;
        }
      case Template::USTR_PATTERN: 
        if (!with_N) {
          Ttcn::PatternString* ps = templ->get_ustr_pattern();
          if (!ps->has_refs())
            v_last = ps->get_value();
          break;
        }
Elemer Lelik's avatar
Elemer Lelik committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
      default:
        TTCN_pattern_error("Unable to resolve referenced '%s' to character "
          "string type. '%s' template cannot be used.",
          ref->get_dispname().c_str(), templ->get_templatetype_str());
        break;
      }
      break; }
    default: {
      Reference *t_ref = ref->clone();
      t_ref->set_location(*ref);
      v = new Value(Value::V_REFD, t_ref);
      v->set_my_governor(refcheckertype);
      v->set_my_scope(ref->get_my_scope());
      v->set_location(*ref);
      refcheckertype->chk_this_value(v, 0, expected_value,
        INCOMPLETE_NOT_ALLOWED, OMIT_NOT_ALLOWED, SUB_CHK);
      v_last = v->get_value_refd_last();
    }
    }
    if (v_last && (v_last->get_valuetype() == Value::V_CSTR ||
      v_last->get_valuetype() == Value::V_USTR)) {
      // the reference points to a constant
      // substitute the reference with the known value
245
246
247
248
      if (v_last->get_valuetype() == Value::V_CSTR) {
        if (with_N && v_last->get_val_str().size() != 1) {
          ref->error("The length of the charstring must be of length one, when it is being referenced in a pattern with \\N{ref}");
        }
Elemer Lelik's avatar
Elemer Lelik committed
249
        str = new string(v_last->get_val_str());
250
251
252
253
      } else {
        if (with_N && v_last->get_val_ustr().size() != 1) {
          ref->error("The length of the universal charstring must be of length one, when it is being referenced in a pattern with \\N{ref}");
        }
Elemer Lelik's avatar
Elemer Lelik committed
254
        str = new string(v_last->get_val_ustr().get_stringRepr_for_pattern());
255
256
      }
      kind = PSE_STR;
Elemer Lelik's avatar
Elemer Lelik committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
    }
    delete v;
  }

  void PatternString::ps_elem_t::set_code_section
    (GovernedSimple::code_section_t p_code_section)
  {
    switch(kind) {
    case PSE_REF:
    case PSE_REFDSET:
      ref->set_code_section(p_code_section);
      break;
    default:
      ;
    } // switch kind
  }

  // =================================
  // ===== PatternString
  // =================================

  PatternString::PatternString(const PatternString& p)
279
280
    : Node(p), my_scope(0), cstr_value(0), pattern_type(p.pattern_type),
      nocase(p.nocase)
Elemer Lelik's avatar
Elemer Lelik committed
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
  {
    size_t nof_elems = p.elems.size();
    for (size_t i = 0; i < nof_elems; i++) elems.add(p.elems[i]->clone());
  }

  PatternString::ps_elem_t *PatternString::get_last_elem() const
  {
    if (elems.empty()) return 0;
    ps_elem_t *last_elem = elems[elems.size() - 1];
    if (last_elem->kind == ps_elem_t::PSE_STR) return last_elem;
    else return 0;
  }

  PatternString::~PatternString()
  {
    size_t nof_elems = elems.size();
    for (size_t i = 0; i < nof_elems; i++) delete elems[i];
    elems.clear();
    delete cstr_value;
  }

  PatternString *PatternString::clone() const
  {
    return new PatternString(*this);
  }

  void PatternString::set_fullname(const string& p_fullname)
  {
    Node::set_fullname(p_fullname);
    size_t nof_elems = elems.size();
    for(size_t i = 0; i < nof_elems; i++) elems[i]->set_fullname(p_fullname);
  }

  void PatternString::set_my_scope(Scope *p_scope)
  {
    my_scope = p_scope;
    size_t nof_elems = elems.size();
    for (size_t i = 0; i < nof_elems; i++) elems[i]->set_my_scope(p_scope);
  }

  void PatternString::set_code_section
    (GovernedSimple::code_section_t p_code_section)
  {
    size_t nof_elems = elems.size();
    for (size_t i = 0; i < nof_elems; i++)
      elems[i]->set_code_section(p_code_section);
  }

  void PatternString::addChar(char c)
  {
    ps_elem_t *last_elem = get_last_elem();
    if (last_elem) *last_elem->str += c;
    else elems.add(new ps_elem_t(ps_elem_t::PSE_STR, string(c)));
  }

  void PatternString::addString(const char *p_str)
  {
    ps_elem_t *last_elem = get_last_elem();
    if (last_elem) *last_elem->str += p_str;
    else elems.add(new ps_elem_t(ps_elem_t::PSE_STR, string(p_str)));
  }

  void PatternString::addString(const string& p_str)
  {
    ps_elem_t *last_elem = get_last_elem();
    if (last_elem) *last_elem->str += p_str;
    else elems.add(new ps_elem_t(ps_elem_t::PSE_STR, p_str));
  }
349
350
351
  
  void PatternString::addStringUSI(char **usi_str, const size_t size)
  {
352
    ustring s = ustring(const_cast<const char**>(usi_str), size);
353
354
355
356
    ps_elem_t *last_elem = get_last_elem();
    if (last_elem) *last_elem->str += s.get_stringRepr_for_pattern().c_str();
    else elems.add(new ps_elem_t(ps_elem_t::PSE_STR, s.get_stringRepr_for_pattern()));
  }
Elemer Lelik's avatar
Elemer Lelik committed
357

358
  void PatternString::addRef(Ttcn::Reference *p_ref, boolean N)
Elemer Lelik's avatar
Elemer Lelik committed
359
  {
360
    elems.add(new ps_elem_t(ps_elem_t::PSE_REF, p_ref, N));
Elemer Lelik's avatar
Elemer Lelik committed
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
  }

  string PatternString::get_full_str() const
  {
    string s;
    for(size_t i=0; i<elems.size(); i++) {
      ps_elem_t *pse=elems[i];
      switch(pse->kind) {
      case ps_elem_t::PSE_STR:
        s+=*pse->str;
        break;
      case ps_elem_t::PSE_REFDSET:
        s+="\\N";
        /* no break */
      case ps_elem_t::PSE_REF:
        s+='{';
        s+=pse->ref->get_dispname();
        s+='}';
      } // switch kind
    } // for
    return s;
  }

  void PatternString::set_pattern_type(pstr_type_t p_type) {
    pattern_type = p_type;
  }

  PatternString::pstr_type_t PatternString::get_pattern_type() const {
    return pattern_type;
  }

  bool PatternString::has_refs() const
  {
    for (size_t i = 0; i < elems.size(); i++) {
      switch (elems[i]->kind) {
      case ps_elem_t::PSE_REF:
      case ps_elem_t::PSE_REFDSET:
        return true;
      default:
        break;
      }
    }
    return false;
  }

  void PatternString::chk_refs(Type::expected_value_t expected_value)
  {
    for(size_t i=0; i<elems.size(); i++) {
      ps_elem_t *pse=elems[i];
      switch(pse->kind) {
      case ps_elem_t::PSE_STR:
        break;
      case ps_elem_t::PSE_REFDSET:
        /* actually, not supported */
        break;
      case ps_elem_t::PSE_REF:
        pse->chk_ref(pattern_type, expected_value);
        break;
      } // switch kind
    } // for
  }

  /** \todo implement */
  void PatternString::chk_recursions(ReferenceChain&)
  {

  }

  void PatternString::chk_pattern()
  {
    string str;
    for (size_t i = 0; i < elems.size(); i++) {
      ps_elem_t *pse = elems[i];
      if (pse->kind != ps_elem_t::PSE_STR)
435
	      FATAL_ERROR("PatternString::chk_pattern()");
Elemer Lelik's avatar
Elemer Lelik committed
436
437
438
439
440
441
442
443
      str += *pse->str;
    }
    char* posix_str = 0;
    switch (pattern_type) {
      case CSTR_PATTERN:
        posix_str = TTCN_pattern_to_regexp(str.c_str());
        break;
      case USTR_PATTERN:
444
        posix_str = TTCN_pattern_to_regexp_uni(str.c_str(), nocase);
Elemer Lelik's avatar
Elemer Lelik committed
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
    }
    Free(posix_str);
  }

  bool PatternString::chk_self_ref(Common::Assignment *lhs)
  {
    for (size_t i = 0, e = elems.size(); i < e; ++i) {
      ps_elem_t *pse = elems[i];
      switch (pse->kind) {
      case ps_elem_t::PSE_STR:
        break;
      case ps_elem_t::PSE_REFDSET:
        /* actually, not supported */
        break;
      case ps_elem_t::PSE_REF: {
        Ttcn::Assignment *ass = pse->ref->get_refd_assignment();
        if (ass == lhs) return true;
        break; }
      } // switch
    }
    return false;
  }

  void PatternString::join_strings()
  {
    // points to the previous string element otherwise it is NULL
    ps_elem_t *prev_str = 0;
    for (size_t i = 0; i < elems.size(); ) {
      ps_elem_t *pse = elems[i];
      if (pse->kind == ps_elem_t::PSE_STR) {
        const string& str = *pse->str;
	if (str.size() > 0) {
	  // the current element is a non-empty string
	  if (prev_str) {
	    // append str to prev_str and drop pse
	    *prev_str->str += str;
	    delete pse;
	    elems.replace(i, 1);
	    // don't increment i
	  } else {
	    // keep pse for the next iteration
	    prev_str = pse;
	    i++;
	  }
	} else {
	  // the current element is an empty string
	  // simply drop it
	  delete pse;
	  elems.replace(i, 1);
	  // don't increment i
	}
      } else {
        // pse is not a string
	// forget prev_str
	prev_str = 0;
	i++;
      }
    }
  }

505
  string PatternString::create_charstring_literals(Common::Module *p_mod, string& preamble)
Elemer Lelik's avatar
Elemer Lelik committed
506
507
  {
    /* The cast is there for the benefit of OPTIONAL<CHARSTRING>, because
508
509
     * it doesn't have operator+(). In most cases only the first member needs
     * the cast (the others will be automagically converted to satisfy
Elemer Lelik's avatar
Elemer Lelik committed
510
511
512
513
514
515
516
517
518
     * CHARSTRING::operator+(const CHARSTRING&) ) */
    string s;
    if (pattern_type == CSTR_PATTERN)
      s = "CHARSTRING_template(STRING_PATTERN, (CHARSTRING)";
    else
      s = "UNIVERSAL_CHARSTRING_template(STRING_PATTERN, (CHARSTRING)";
    size_t nof_elems = elems.size();
    if (nof_elems > 0) {
      // the pattern is not empty
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
    for (size_t i = 0; i < nof_elems; i++) {
      if (i > 0) s += " + ";
      ps_elem_t *pse = elems[i];
      
      // \N{charstring} and \N{universal charstring}
      if (pse->is_charstring) {
        s += p_mod->add_charstring_literal(string("?"));
        continue;
      } else if (pse->is_universal_charstring) {
        s += p_mod->add_charstring_literal(string("?"));
        continue;
      }

      switch (pse->kind) {
        // Known in compile time: string literal, const etc.
        case ps_elem_t::PSE_STR:
Elemer Lelik's avatar
Elemer Lelik committed
535
536
          s += p_mod->add_charstring_literal(*pse->str);
          break;
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
        // Known in compile time: string type with(out) range or list
        case ps_elem_t::PSE_REFDSET: {
          if (!pse->t)
            FATAL_ERROR("PatternString::create_charstring_literals()");
          if (!pse->t->get_sub_type()) {
            // just a string type without any restrictions (or alias)
            s += "\"?\"";
            continue;
          }
          vector<Common::SubTypeParse> * vec = pse->t->get_sub_type()->get_subtype_parsed();

          while (vec == NULL) { // go through aliases to find where the restrictions are
            if (pse->t->get_Reference()) {
              pse->t = pse->t->get_Reference()->get_refd_assignment(FALSE)->get_Type();
            } else {
              break;
            }
            if (pse->t->get_sub_type()) {
              vec = pse->t->get_sub_type()->get_subtype_parsed();
            } else {
              break;
            }
          }
          if (vec == NULL) {
            // I don't think it can happen, but to be sure...
            s += "\"?\"";
            continue;
          }
          s+= "\"";
          if (vec->size() > 1) s+= "(";
          for (size_t j = 0; j < vec->size(); j++) {
            Common::SubTypeParse* stp = (*vec)[j];
            if (j > 0) {
              s+="|\"+"; // todo what if default    
            }else {
              s+= "\"+";
            }
            switch (stp->get_selection()) {
              case SubTypeParse::STP_RANGE: // type charstring ("a" .. "z")
                s+="\"[\" + ";
                switch (stp->Min()->get_valuetype()) {
                  case Value::V_CSTR:
                    s+= p_mod->add_charstring_literal(stp->Min()->get_val_str());
                    s+= "+ \"-\" +";
                    s+= p_mod->add_charstring_literal(stp->Max()->get_val_str());
                    break;
                  case Value::V_USTR:
                    s+= p_mod->add_charstring_literal(stp->Min()->get_val_ustr().get_stringRepr_for_pattern());
                    s+= "+ \"-\" +";
                    s+= p_mod->add_charstring_literal(stp->Max()->get_val_ustr().get_stringRepr_for_pattern());
                    break;
                  default:
                    FATAL_ERROR("PatternString::create_charstring_literals()");
                }
                s+=" + \"]\"";
                break;
              case SubTypeParse::STP_SINGLE: // type charstring ("a", "b")
                switch (stp->Single()->get_valuetype()) {
                  case Value::V_CSTR:
                    s+= p_mod->add_charstring_literal(stp->Single()->get_val_str());
                    break;
                  case Value::V_USTR:
                    s+= p_mod->add_charstring_literal(stp->Single()->get_val_ustr().get_stringRepr_for_pattern());
                    break;
                  default:
                    FATAL_ERROR("PatternString::create_charstring_literals()");
                    break;
                }
                break;
              default:
                FATAL_ERROR("PatternString::create_charstring_literals()");
            }
            s+= "+\"";
          }
          if (vec->size() > 1) s+= ")";
          s+= "\"";
          break; }
        // Not known in compile time
        case ps_elem_t::PSE_REF: {
616
617
618
619
620
621
622
          Common::Assignment* assign = pse->ref->get_refd_assignment();
          if (use_runtime_2 && i > 0 &&
              assign->get_Type()->field_is_optional(pse->ref->get_subrefs())) {
            // in RT2 convert all operands of type OPTIONAL<CHARSTRING> to
            // CHARSTRING, not just the first one
            s += "(CHARSTRING)";
          }
623
624
625
626
627
          expression_struct expr;
          Code::init_expr(&expr);
          pse->ref->generate_code(&expr);
          if (expr.preamble || expr.postamble)
            FATAL_ERROR("PatternString::create_charstring_literals()");
Elemer Lelik's avatar
Elemer Lelik committed
628
          s += expr.expr;
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
          char* str = NULL;
          
          // TODO: these checks will generated each time a reference is referenced in a pattern
          // and it could be generated once
          if (pse->with_N) {
            if ((assign->get_asstype() == Common::Assignment::A_TEMPLATE
                  || assign->get_asstype() == Common::Assignment::A_MODULEPAR_TEMP
                  || assign->get_asstype() == Common::Assignment::A_VAR_TEMPLATE
                  || assign->get_asstype() == Common::Assignment::A_PAR_TEMPL_IN
                  || assign->get_asstype() == Common::Assignment::A_PAR_TEMPL_OUT
                  || assign->get_asstype() == Common::Assignment::A_PAR_TEMPL_INOUT))
            {
            string value_literal = p_mod->add_charstring_literal(string("value"));
            str = mputprintf(str,
              "if (%s.get_istemplate_kind(%s) == FALSE) {\n"
              "TTCN_error(\"Only specific value template allowed in pattern reference with \\\\N{ref}\");\n"
              "}\n"
              , expr.expr
              , value_literal.c_str());
            }
            
            str = mputprintf(str,
              "if (%s.lengthof() != 1)\n"
              "{\n"
              "TTCN_error(\"The length of the %scharstring must be of length one, when it is being referenced in a pattern with \\\\N{ref}\");\n"
              "}\n"
              , expr.expr
              , assign->get_Type()->get_typetype() == Type::T_USTR ? "universal " : "");
            preamble += str;
            Free(str);
          }
Elemer Lelik's avatar
Elemer Lelik committed
660
661
          if ((assign->get_asstype() == Common::Assignment::A_TEMPLATE
            || assign->get_asstype() == Common::Assignment::A_MODULEPAR_TEMP
662
663
664
665
            || assign->get_asstype() == Common::Assignment::A_VAR_TEMPLATE
            || assign->get_asstype() == Common::Assignment::A_PAR_TEMPL_IN
            || assign->get_asstype() == Common::Assignment::A_PAR_TEMPL_OUT
            || assign->get_asstype() == Common::Assignment::A_PAR_TEMPL_INOUT))
Elemer Lelik's avatar
Elemer Lelik committed
666
667
          {
            if ((assign->get_Type()->get_typetype() == Type::T_CSTR
668
              || assign->get_Type()->get_typetype() == Type::T_USTR) && !pse->with_N) {
Elemer Lelik's avatar
Elemer Lelik committed
669
670
              s += ".get_single_value()";
            }
671
672
673
            else if (assign->get_Type()->get_typetype() == Type::T_USTR && pse->with_N) {
              s += ".valueof().get_stringRepr_for_pattern()";
            } else {
Elemer Lelik's avatar
Elemer Lelik committed
674
675
              s += ".valueof()";
            }
676
677
          } else if ((assign->get_asstype() == Common::Assignment::A_MODULEPAR
            || assign->get_asstype() == Common::Assignment::A_VAR
678
            || assign->get_asstype() == Common::Assignment::A_EXCEPTION
679
680
681
682
683
684
            || assign->get_asstype() == Common::Assignment::A_PAR_VAL
            || assign->get_asstype() == Common::Assignment::A_PAR_VAL_IN
            || assign->get_asstype() == Common::Assignment::A_PAR_VAL_OUT
            || assign->get_asstype() == Common::Assignment::A_PAR_VAL_INOUT)
            && assign->get_Type()->get_typetype() == Type::T_USTR) {
            s += ".get_stringRepr_for_pattern()";
Elemer Lelik's avatar
Elemer Lelik committed
685
686
          }

687
688
689
          Code::free_expr(&expr);
          break; }
        } // switch kind
Elemer Lelik's avatar
Elemer Lelik committed
690
691
692
693
694
      } // for
    } else {
      // empty pattern: create an empty string literal for it
      s += p_mod->add_charstring_literal(string());
    }
695
696
    s += ", ";
    s += nocase ? "TRUE" : "FALSE";
Elemer Lelik's avatar
Elemer Lelik committed
697
698
699
700
701
702
    s += ')';
    return s;
  }

  void PatternString::dump(unsigned level) const
  {
703
704
705
    if (nocase) {
      DEBUG(level, "@nocase");
    }
Elemer Lelik's avatar
Elemer Lelik committed
706
707
708
709
710
711
712
713
714
    DEBUG(level, "%s", get_full_str().c_str());
  }

  Common::Value* PatternString::get_value() {
    if (!cstr_value && !has_refs())
      cstr_value = new Common::Value(Common::Value::V_CSTR,
        new string(get_full_str()));
    return cstr_value;
  }
Elemer Lelik's avatar
Elemer Lelik committed
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
  
  char* PatternString::convert_to_json()
  {
    string pstr = get_value()->get_val_str();
    
    // convert the pattern into an extended regular expression
    char* regex_str = NULL;
    if (CSTR_PATTERN == pattern_type) {
      regex_str = TTCN_pattern_to_regexp(pstr.c_str());
    }
    else { // USTR_PATTERN
      // handle the unicode characters in \q{g,p,r,c} format
      string utf8str;
      for (size_t i = 0; i < pstr.size(); ++i) {
        if ('\\' == pstr[i]) {
          if ('q' == pstr[i + 1]) {
            // extract the unicode character
            unsigned int group, plane, row, cell;
            i = pstr.find('{', i + 1);
            sscanf(pstr.c_str() + i + 1, "%u", &group);
            i = pstr.find(',', i + 1);
            sscanf(pstr.c_str() + i + 1, "%u", &plane);
            i = pstr.find(',', i + 1);
            sscanf(pstr.c_str() + i + 1, "%u", &row);
            i = pstr.find(',', i + 1);
            sscanf(pstr.c_str() + i + 1, "%u", &cell);
            i = pstr.find('}', i + 1);
            
            // convert the character to UTF-8 format
Kristof Szabados's avatar
Kristof Szabados committed
744
            utf8str += ustring_to_uft8(ustring((unsigned char)group, (unsigned char)plane, (unsigned char)row, (unsigned char)cell));
Elemer Lelik's avatar
Elemer Lelik committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
            continue;
          }
          else if ('\\' == pstr[i + 1]) {
            // must be handled separately, so we don't confuse \\q with \q
            ++i;
            utf8str += '\\';
          }
        }
        utf8str += pstr[i];
      }
      
      // use the pattern converter for charstrings, the pattern should be in UTF-8
      // format now (setting the 2nd parameter will make sure that no error
      // messages are displayed for extended ASCII characters)
      regex_str = TTCN_pattern_to_regexp(utf8str.c_str(), true);
    }
    
762
763
764
    char* json_str = convert_to_json_string(regex_str);
    Free(regex_str);
    return json_str;
Elemer Lelik's avatar
Elemer Lelik committed
765
  }
Elemer Lelik's avatar
Elemer Lelik committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795

} // namespace Ttcn

  // =================================
  // ===== TTCN_pattern_XXXX
  // =================================

/* These functions are used by common charstring pattern parser. */

void TTCN_pattern_error(const char *fmt, ...)
{
  char *msg=mcopystr("Charstring pattern: ");
  msg=mputstr(msg, fmt);
  va_list args;
  va_start(args, fmt);
  Common::Error_Context::report_error(0, msg, args);
  va_end(args);
  Free(msg);
}

void TTCN_pattern_warning(const char *fmt, ...)
{
  char *msg=mcopystr("Charstring pattern: ");
  msg=mputstr(msg, fmt);
  va_list args;
  va_start(args, fmt);
  Common::Error_Context::report_warning(0, msg, args);
  va_end(args);
  Free(msg);
}