-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbathtub_pkg.sv
2333 lines (1787 loc) · 69.1 KB
/
bathtub_pkg.sv
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
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
616
617
618
619
620
621
622
623
624
625
626
627
628
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
`include "bathtub_macros.sv"
// ===================================================================
package bathtub_pkg;
// ===================================================================
import uvm_pkg::*;
import meta_pkg::*;
typedef enum {Given, When, Then} step_keyword_t;
virtual class bathtub_utils;
// ===================================================================
static function bit split_string;
// ===================================================================
`FUNCTION_METADATA('{
description:
"Take a string containing tokens separated by white space, split the tokens, and return them in the supplied SystemVerilog queue.",
details:
"",
categories:
"utility",
string: ""
})
// Parameters:
input string str; // Incoming string of white space-separated tokans
ref string tokens[$]; // Fills given queue with individual tokens
// ===================================================================
typedef enum {START, TOKEN, WHITE_SPACE, FINISH} lex_state_t;
lex_state_t state;
byte c;
string token;
bit ok;
ok = 1;
tokens.delete();
state = START;
foreach (str[i]) begin
c = str.getc(i);
case (state)
START: begin
token = "";
case (c)
" ", "\t", "\n" : begin
state = WHITE_SPACE;
end
default : begin
token = {token, c};
state = TOKEN;
end
endcase
end
TOKEN: begin
case (c)
" ", "\t", "\n" : begin
tokens.push_back(token);
token = "";
state = WHITE_SPACE;
end
default : begin
token = {token, c};
state = TOKEN;
end
endcase
end
WHITE_SPACE: begin
case (c)
" ", "\t", "\n" : begin
state = WHITE_SPACE;
end
default : begin
token = {token, c};
state = TOKEN;
end
endcase
end
default: begin
$fatal(1, "Unknown lexer state");
end
endcase
end
if (state == TOKEN) begin
tokens.push_back(token);
state = FINISH;
end
return ok;
endfunction : split_string
static function string get_conversion_spec(string str);
typedef enum {LOOKING_FOR_START, START, WIDTH, CODE} lex_state_t;
lex_state_t state;
byte c;
string spec;
state = LOOKING_FOR_START;
spec = "";
foreach (str[i]) begin
c = str[i];
case (state)
LOOKING_FOR_START : begin : state_$looking_for_start
case (c)
"%" : begin
state = START;
spec = {spec, string'(c)};
end
endcase
end
START : begin : state_$start
case (c)
"%" : begin : case_$escaped_percent_sign
state = LOOKING_FOR_START;
spec = "";
end
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9" : begin : case_$optional_width
state = WIDTH;
spec = {spec, string'(c)};
end
"b", "o", "d", "h", "x", "f", "e", "g", "c", "s",
"B", "O", "D", "H", "X", "F", "E", "G", "C", "S": begin : case_$code
state = CODE;
spec = {spec, string'(c)};
end
default : begin : case_$unsupported_code
$fatal(1, $sformatf("Unsupported conversion specification character: %s", c));
end
endcase
end
WIDTH : begin : state_$width
case (c)
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9" : begin : case_$optional_width
state = WIDTH;
spec = {spec, string'(c)};
end
"b", "o", "d", "h", "x", "f", "e", "g", "c", "s",
"B", "O", "D", "H", "X", "F", "E", "G", "C", "S": begin : case_$code
state = CODE;
spec = {spec, string'(c)};
end
default : begin : case_$unsupported_code
$fatal(1, $sformatf("Unsupported conversion specification character: %s", c));
end
endcase
end
CODE : begin : state_$CODE
break;
end
default: begin
$fatal(1, "Unknown lexer state");
end
endcase
end
return spec;
endfunction : get_conversion_spec
static function string get_conversion_code(string token);
typedef enum {LOOKING_FOR_START, START, WIDTH, CODE} lex_state_t;
lex_state_t state;
byte c;
string code;
string spec;
spec = get_conversion_spec(token);
code = (spec == "") ? "" : string'(spec[spec.len() - 1]);
return code;
endfunction : get_conversion_code
static function bit is_regex(string expression);
bit result;
byte first_char;
byte last_char;
expression = trim_white_space(expression);
first_char = expression[0];
last_char = expression[expression.len() - 1];
result = (first_char == "/" && last_char == "/") ||
(first_char == "^" && last_char =="$");
return result;
endfunction : is_regex
static function string bathtub_to_regexp(string bathtub_exp);
string regexp_from_code[string] = '{
"b" : "([0-1XxZz?_]+)",
"o" : "([0-7XxZz?_]+)",
"d" : "(([-+]?[0-9_]+)|[xXzZ?])",
"h" : "([0-9a-fA-FxXzZ?_]+)",
"x" : "([0-9a-fA-FxXzZ?_]+)",
"f" : "([+-]?[0-9]+.?[0-9]*[eE]?[+-]?[0-9]*)",
"e" : "([+-]?[0-9]+.?[0-9]*[eE]?[+-]?[0-9]*)",
"g" : "([+-]?[0-9]+.?[0-9]*[eE]?[+-]?[0-9]*)",
"s" : "(\\S*)",
"c" : "(.)"
};
string result = bathtub_exp;
forever begin
string spec = get_conversion_spec(result);
string code;
string subst_regexp;
int result_length = result.len();
int spec_length = spec.len();
string before_subst = result;
// $info(result);
if (spec == "") break;
code = get_conversion_code(spec);
code = code.tolower();
subst_regexp = regexp_from_code[code];
for (int i = 0; i < result_length; i++) begin
if (result.substr(i, i + spec_length - 1) == spec) begin
result = {result.substr(0, i - 1), subst_regexp, result.substr(i + spec.len(), result_length - 1)};
break;
end
end
// Prevents an infinite loop.
assert_substitution_done : assert (before_subst != result) else
$fatal(1, "Failed to substitute conversion spec '%s' in expression '%s'", spec, result);
end
return {"/^", result, "$/"};
endfunction : bathtub_to_regexp
static function int re_match(string re, string str);
return uvm_re_match(re, str);
endfunction : re_match
static function bit string_starts_with(string text_to_search, string search_text);
return (search_text == text_to_search.substr(0, search_text.len() - 1));
endfunction : string_starts_with
static function string trim_white_space(string line_buf);
byte c;
int index_of_first_non_white_space;
int index_of_last_non_white_space;
index_of_first_non_white_space = line_buf.len(); // Beyond the end
for (int i = 0; i < line_buf.len(); i++) begin
c = line_buf[i];
if (!(c inside {" ", "\t", "\n"})) begin
index_of_first_non_white_space = i;
break;
end
end
index_of_last_non_white_space = -1; // Beyond the end
for (int i = line_buf.len() - 1; i >= 0; i--) begin
c = line_buf[i];
if (!(c inside {" ", "\t", "\n"})) begin
index_of_last_non_white_space = i;
break;
end
end
line_buf = line_buf.substr(index_of_first_non_white_space, index_of_last_non_white_space);
return line_buf;
endfunction : trim_white_space
endclass : bathtub_utils
class line_value;
string text;
string file_name;
int line_number;
bit eof;
function new(string text, string file_name, int line_number=0, bit eof=0);
this.text = text;
this.file_name = file_name;
this.line_number = line_number;
this.eof = eof;
endfunction : new
endclass : line_value
class step_parameter_arg extends uvm_object;
typedef enum {INVALID, INT, REAL, STRING} arg_type_t;
protected int int_arg;
protected real real_arg;
protected string string_arg;
protected arg_type_t arg_type;
function new(string name="step_parameter_arg");
super.new(name);
arg_type = INVALID;
endfunction : new
`uvm_object_utils_begin(step_parameter_arg)
`uvm_field_enum(arg_type_t, arg_type, UVM_ALL_ON)
`uvm_field_int(int_arg, UVM_ALL_ON)
`uvm_field_real(real_arg, UVM_ALL_ON)
`uvm_field_string(string_arg, UVM_ALL_ON)
`uvm_object_utils_end
static function step_parameter_arg create_new_int_arg(string name, int value);
step_parameter_arg new_obj = new(name);
new_obj.int_arg = value;
new_obj.arg_type = INT;
return new_obj;
endfunction : create_new_int_arg
static function step_parameter_arg create_new_real_arg(string name, real value);
step_parameter_arg new_obj = new(name);
new_obj.real_arg = value;
new_obj.arg_type = REAL;
return new_obj;
endfunction : create_new_real_arg
static function step_parameter_arg create_new_string_arg(string name, string value);
step_parameter_arg new_obj = new(name);
new_obj.string_arg = value;
new_obj.arg_type = STRING;
return new_obj;
endfunction : create_new_string_arg
virtual function int as_int();
case (arg_type)
INVALID : return 0;
INT : return int_arg;
REAL : return int'(real_arg);
STRING : return string_arg.atoi(); // decimal
endcase
endfunction : as_int
virtual function real as_real();
case (arg_type)
INVALID : return 0.0;
INT : return real'(int_arg);
REAL : return real_arg;
STRING : return string_arg.atoreal();
endcase
endfunction : as_real
virtual function string as_string();
case (arg_type)
INVALID : return "";
INT : return $sformatf("%d", int_arg);
REAL : return $sformatf("%f", real_arg);
STRING : return string_arg;
endcase
endfunction : as_string
endclass : step_parameter_arg
class step_parameters extends uvm_object;
protected step_parameter_arg argv[$];
protected string step_text;
protected string format;
local bit has_been_scanned;
`uvm_field_utils_begin(step_parameters)
`uvm_field_utils_end
function new(string str="", string format="");
super.new("step_parameters");
has_been_scanned = 1'b0;
endfunction : new
static function step_parameters create_new(string name, string step_text="", string format="");
step_parameters new_obj = new(name);
new_obj.step_text = step_text;
new_obj.format = format;
return new_obj;
endfunction : create_new
virtual function step_parameter_arg get_arg(int i);
if (!has_been_scanned) begin
void'(step_parameters::scan_step_params(step_text, format, argv));
has_been_scanned = 1'b1;
end
return argv[i];
endfunction : get_arg
virtual function int num_args();
if (!has_been_scanned) begin
void'(step_parameters::scan_step_params(step_text, format, argv));
has_been_scanned = 1'b1;
end
return argv.size();
endfunction : num_args
// ===================================================================
static function int scan_step_params(string step_text, string scanf_format, ref step_parameter_arg step_argv[$]);
// ===================================================================
string text_tokens[$];
string format_tokens[$];
bit ok;
`uvm_info_begin(`get_scope_name(-2), "", UVM_HIGH)
`uvm_message_add_string(step_text)
`uvm_message_add_string(scanf_format)
`uvm_info_end
step_argv.delete();
ok = bathtub_utils::split_string(scanf_format, format_tokens);
ok = bathtub_utils::split_string(step_text, text_tokens);
for (int i = 0; i < format_tokens.size(); i++) begin
int sscanf_code;
string conversion_code;
int int_arg;
real real_arg;
string string_arg;
conversion_code = bathtub_utils::get_conversion_code(format_tokens[i]);
case (conversion_code)
"b", "o", "d", "h", "x",
"B", "O", "D", "H", "X" : begin : case_$int
sscanf_code = $sscanf(text_tokens[i], format_tokens[i], int_arg);
if (sscanf_code == 1) begin
step_argv.push_back(step_parameter_arg::create_new_int_arg("anonymous", int_arg));
end
else begin
$fatal(1, $sformatf("Unexpected result (%0d) while parsing string '%s' with format '%s'",
sscanf_code, text_tokens[i], format_tokens[i]));
end
end
"f", "e", "g",
"F", "E", "G" : begin : case_$real
sscanf_code = $sscanf(text_tokens[i], format_tokens[i], real_arg);
if (sscanf_code == 1) begin
step_argv.push_back(step_parameter_arg::create_new_real_arg("anonymous", real_arg));
end
else begin
$fatal(1, $sformatf("Unexpected result (%0d) while parsing string '%s' with format '%s'",
sscanf_code, text_tokens[i], format_tokens[i]));
end
end
"s", "c",
"S", "C" : begin : case_$string
sscanf_code = $sscanf(text_tokens[i], format_tokens[i], string_arg);
if (sscanf_code == 1) begin
step_argv.push_back(step_parameter_arg::create_new_string_arg("anonymous", string_arg));
end
else begin
$fatal(1, $sformatf("Unexpected result (%0d) while parsing string '%s' with format '%s'",
sscanf_code, text_tokens[i], format_tokens[i]));
end
end
default : begin : case_$no_arg
sscanf_code = $sscanf(text_tokens[i], format_tokens[i]);
if (sscanf_code != 0) begin
$fatal(1, $sformatf("Unexpected result (%0d) while parsing string '%s' with format '%s'",
sscanf_code, text_tokens[i], format_tokens[i]));
end
end
endcase
end
foreach (step_argv[i]) begin
`uvm_info(`get_scope_name(), {"\n", step_argv[i].sprint()}, UVM_HIGH)
end
return step_argv.size();
endfunction : scan_step_params
endclass : step_parameters
interface class step_static_attributes_interface;
// Set keyword
pure virtual function void set_keyword(step_keyword_t keyword);
// Get keyword
pure virtual function step_keyword_t get_keyword();
// Set regexp
pure virtual function void set_regexp(string regexp);
// Get regexp
pure virtual function string get_regexp();
// Get expression
pure virtual function string get_expression();
// Set expression
pure virtual function void set_expression(string expression);
// Get step_obj
pure virtual function uvm_object_wrapper get_step_obj();
// Set obj_name
pure virtual function void set_step_obj(uvm_object_wrapper step_obj);
// Get step_obj_name
pure virtual function string get_step_obj_name();
pure virtual function void print_attributes(uvm_verbosity verbosity);
endclass : step_static_attributes_interface
class step_nature extends uvm_object implements step_static_attributes_interface;
protected step_keyword_t keyword;
protected string expression;
protected string regexp;
protected uvm_object_wrapper step_obj;
protected string step_obj_name;
function new(string name="step_nature");
super.new(name);
endfunction : new
`uvm_object_utils_begin(step_nature)
`uvm_field_enum(step_keyword_t, keyword, UVM_DEFAULT)
`uvm_field_string(expression, UVM_DEFAULT)
`uvm_field_string(regexp, UVM_DEFAULT)
`uvm_field_string(step_obj_name, UVM_DEFAULT)
`uvm_object_utils_end
static function step_static_attributes_interface register_step(step_keyword_t keyword, string expression, uvm_object_wrapper step_obj);
step_nature new_obj;
new_obj = new("static_step_object");
new_obj.keyword = keyword;
new_obj.expression = expression;
new_obj.set_step_obj(step_obj);
if (bathtub_utils::is_regex(expression)) begin
new_obj.regexp = expression;
end
else begin
new_obj.regexp = bathtub_utils::bathtub_to_regexp(expression);
end
uvm_resource_db#(uvm_object_wrapper)::set(new_obj.regexp, keyword.name(), step_obj);
`uvm_info(`get_scope_name(), {"\n", new_obj.sprint()}, UVM_HIGH)
return new_obj;
endfunction
virtual function void print_attributes(uvm_verbosity verbosity);
`uvm_info_begin(get_name(), "", verbosity)
`uvm_message_add_tag("keyword", keyword.name())
`uvm_message_add_string(expression)
`uvm_message_add_string(regexp)
`uvm_message_add_string(step_obj_name)
`uvm_info_end
endfunction : print_attributes
// Set keyword
virtual function void set_keyword(step_keyword_t keyword);
this.keyword = keyword;
endfunction : set_keyword
// Get keyword
virtual function step_keyword_t get_keyword();
return keyword;
endfunction : get_keyword
// Get expression
virtual function string get_expression();
return expression;
endfunction : get_expression
// Set expression
virtual function void set_expression(string expression);
this.expression = expression;
endfunction : set_expression
// Set regexp
virtual function void set_regexp(string regexp);
this.regexp = regexp;
endfunction : set_regexp
// Get regexp
virtual function string get_regexp();
return regexp;
endfunction : get_regexp
// Get obj_name
virtual function uvm_object_wrapper get_step_obj();
return step_obj;
endfunction : get_step_obj
// Set obj_name
virtual function void set_step_obj(uvm_object_wrapper step_obj);
this.step_obj = step_obj;
this.step_obj_name = step_obj.get_type_name();
endfunction : set_step_obj
// Get step_obj_name
virtual function string get_step_obj_name();
return step_obj_name;
endfunction : get_step_obj_name
// Set step_obj_name
virtual function void set_step_obj_name(string step_obj_name);
this.step_obj_name = step_obj_name;
endfunction : set_step_obj_name
endclass : step_nature
interface class step_attributes_interface;
pure virtual function string get_runtime_keyword();
pure virtual function void set_runtime_keyword(string runtime_keyword);
pure virtual function string get_text();
pure virtual function void set_text(string step_text);
pure virtual function gherkin_pkg::step_argument get_argument();
pure virtual function void set_argument(gherkin_pkg::step_argument step_argument);
pure virtual function step_static_attributes_interface get_static_attributes();
pure virtual function void set_static_attributes(step_static_attributes_interface static_attributes);
pure virtual function string get_format();
pure virtual function step_keyword_t get_static_keyword();
pure virtual function string get_expression();
pure virtual function string get_regexp();
pure virtual function uvm_object_wrapper get_step_obj();
pure virtual function string get_step_obj_name();
pure virtual function void print_attributes(uvm_verbosity verbosity);
endclass : step_attributes_interface
class step_nurture extends uvm_object implements step_attributes_interface;
string runtime_keyword;
string text;
gherkin_pkg::step_argument argument;
step_static_attributes_interface static_attributes;
function new(string name="step_nurture");
super.new(name);
endfunction : new
`uvm_object_utils_begin(step_nurture)
`uvm_field_string(runtime_keyword, UVM_ALL_ON)
`uvm_field_string(text, UVM_ALL_ON)
`uvm_field_object(argument, UVM_ALL_ON)
`uvm_object_utils_end
virtual function void print_attributes(uvm_verbosity verbosity);
`uvm_info_begin(get_name(), "", verbosity)
`uvm_message_add_string(runtime_keyword)
`uvm_message_add_string(text)
`uvm_message_add_object(argument)
`uvm_info_end
static_attributes.print_attributes(verbosity);
endfunction : print_attributes
virtual function string get_runtime_keyword();
return this.runtime_keyword;
endfunction : get_runtime_keyword
virtual function void set_runtime_keyword(string runtime_keyword);
this.runtime_keyword = runtime_keyword;
endfunction : set_runtime_keyword
virtual function string get_text();
return this.text;
endfunction : get_text
virtual function void set_text(string step_text);
this.text = step_text;
endfunction : set_text
virtual function gherkin_pkg::step_argument get_argument();
return this.argument;
endfunction : get_argument
virtual function void set_argument(gherkin_pkg::step_argument step_argument);
this.argument = step_argument;
endfunction : set_argument
virtual function void set_static_attributes(step_static_attributes_interface static_attributes);
this.static_attributes = static_attributes;
endfunction : set_static_attributes
virtual function step_static_attributes_interface get_static_attributes();
return this.static_attributes;
endfunction : get_static_attributes
virtual function string get_format();
return static_attributes.get_expression();
endfunction : get_format
virtual function step_keyword_t get_static_keyword();
return static_attributes.get_keyword();
endfunction : get_static_keyword
virtual function string get_expression();
return static_attributes.get_expression();
endfunction : get_expression
virtual function string get_regexp();
return static_attributes.get_regexp();
endfunction : get_regexp
virtual function uvm_object_wrapper get_step_obj();
return static_attributes.get_step_obj();
endfunction : get_step_obj
virtual function string get_step_obj_name();
return static_attributes.get_step_obj_name();
endfunction : get_step_obj_name
endclass : step_nurture
interface class step_definition_interface;
pure virtual function step_attributes_interface get_step_attributes();
pure virtual function void set_step_attributes(step_attributes_interface step_attributes);
pure virtual function step_static_attributes_interface get_step_static_attributes();
endclass : step_definition_interface
typedef class gherkin_document_printer;
typedef class gherkin_document_runner;
// Bundle the document with its file name externally.
// The AST doesn't provide a place for the file name inside the document.
class gherkin_doc_bundle;
string file_name;
gherkin_pkg::gherkin_document document;
function new(string file_name, gherkin_pkg::gherkin_document document);
this.file_name = file_name;
this.document = document;
endfunction : new
endclass : gherkin_doc_bundle
class bathtub extends uvm_object;
typedef struct {
string token_before_space;
string token_before_colon;
string remainder_after_space;
string remainder_after_colon;
string secondary_keyword;
string remainder_after_secondary_keyword;
} line_analysis_result_t;
string feature_files[$];
gherkin_doc_bundle gherkin_docs[$];
mailbox line_mbox;
mailbox gherkin_mbox;
uvm_sequencer_base sequencer;
uvm_sequence_base parent_sequence;
int sequence_priority;
bit sequence_call_pre_post;
bit dry_run;
int starting_scenario_number;
int stopping_scenario_number;
`uvm_object_utils_begin(bathtub)
`uvm_field_queue_string(feature_files, UVM_ALL_ON)
`uvm_field_int(dry_run, UVM_ALL_ON)
`uvm_field_int(starting_scenario_number, UVM_ALL_ON)
`uvm_field_int(stopping_scenario_number, UVM_ALL_ON)
`uvm_object_utils_end
function new(string name = "bathtub");
super.new(name);
feature_files.delete();
line_mbox = new(1);
gherkin_mbox = new(1);
sequencer = null;
parent_sequence = null;
sequence_priority = -1;
sequence_call_pre_post = 1;
dry_run = 0;
starting_scenario_number = 0;
stopping_scenario_number = 0;
endfunction : new
virtual function void configure(
uvm_sequencer_base sequencer,
uvm_sequence_base parent_sequence = null,
int sequence_priority = -1,
bit sequence_call_pre_post = 1
);
this.sequencer = sequencer;
this.parent_sequence = parent_sequence;
this.sequence_priority = sequence_priority;
this.sequence_call_pre_post = sequence_call_pre_post;
endfunction : configure
virtual task run_test();
integer fd;
integer code;
line_value line_obj;
int line_number;
gherkin_doc_bundle gherkin_doc_bundle;
gherkin_document_printer printer;
gherkin_document_runner runner;
foreach (feature_files[i]) begin
`uvm_info(`get_scope_name(-2), {"Feature file: ", feature_files[i]}, UVM_LOW)
start_file_parser(feature_files[i]);
fd = $fopen(feature_files[i], "r");
assert_fopen_succeeded : assert (fd != 0) else begin
string ferror_msg;
integer errno;
errno = $ferror(fd, ferror_msg);
`uvm_fatal(`get_scope_name(-2), ferror_msg)
end
line_number = 1;
while (!$feof(fd)) begin
string line_buf;
code = $fgets(line_buf, fd);
line_obj = new(line_buf, feature_files[i], line_number);
line_number++;
line_mbox.put(line_obj);
end
$fclose(fd);
line_obj = new(.eof (1),
.text (""),
.file_name (feature_files[i])
); // Special signal that file is done
line_mbox.put(line_obj);
gherkin_mbox.get(gherkin_doc_bundle);
assert_gherkin_doc_is_not_null : assert (gherkin_doc_bundle.document);
if (uvm_get_report_object().get_report_verbosity_level() >= UVM_HIGH) begin
printer = gherkin_document_printer::create_new("printer", gherkin_doc_bundle.document);
printer.print();
end
runner = gherkin_document_runner::create_new("runner", gherkin_doc_bundle.document);
runner.configure(sequencer, parent_sequence, sequence_priority, sequence_call_pre_post, dry_run, starting_scenario_number, stopping_scenario_number);
runner.run();
end
endtask : run_test
virtual task start_file_parser(string file_name);
fork
parse_lines();
join_none
endtask : start_file_parser
function void analyze_line(string line_buf, ref line_analysis_result_t result);
int start_of_keyword;
int first_space_after_keyword;
int first_colon_after_keyword;
byte c;
static string secondary_strings[] = {"\"\"\"", "|", "@", "#"};
start_of_keyword = -1;
first_space_after_keyword = -1;
first_colon_after_keyword = -1;
line_buf = bathtub_utils::trim_white_space(line_buf);
for (int i = 0; i < line_buf.len(); i++) begin
c = line_buf[i];
if (start_of_keyword == -1) begin
if (!(c inside {" ", "\t", "\n"})) begin
start_of_keyword = i;
end
end
if (start_of_keyword != -1 && first_space_after_keyword == -1) begin
if (c inside {" ", "\t", "\n"}) begin
first_space_after_keyword = i;
end
end
if (start_of_keyword != -1 && first_colon_after_keyword == -1) begin
if (c == ":") begin
first_colon_after_keyword = i;
end
end
end
result.token_before_space = bathtub_utils::trim_white_space(line_buf.substr(start_of_keyword, first_space_after_keyword - 1));
result.token_before_colon = bathtub_utils::trim_white_space(line_buf.substr(start_of_keyword, first_colon_after_keyword - 1));
result.remainder_after_space = bathtub_utils::trim_white_space(line_buf.substr(first_space_after_keyword + 1, line_buf.len() - 1));
result.remainder_after_colon = bathtub_utils::trim_white_space(line_buf.substr(first_colon_after_keyword + 1, line_buf.len() - 1));
result.secondary_keyword = "";
result.remainder_after_secondary_keyword = "";
foreach (secondary_strings[i]) begin
int length = secondary_strings[i].len();
string leading_string = line_buf.substr(0, length - 1);
if (leading_string == secondary_strings[i]) begin
result.secondary_keyword = leading_string;
result.remainder_after_secondary_keyword = bathtub_utils::trim_white_space(line_buf.substr(length, line_buf.len() - 1));
break;
end
end
endfunction : analyze_line
virtual task get_next_line(ref line_value line_obj);
forever begin
line_mbox.get(line_obj);
if (line_obj.eof) begin
return;
end
else begin
$write("%s [%4d]: %s", line_obj.file_name, line_obj.line_number, line_obj.text);
if (bathtub_utils::trim_white_space(line_obj.text) == "") begin
// Ignore empty lines
continue;
end
else begin
return;
end