forked from pmichaud/pmtcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TclList.pm
155 lines (133 loc) · 5.13 KB
/
TclList.pm
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
# This class is currently created via PIR in src/class/tcllist.pir
INIT {
my $interp := pir::getinterp__p();
my $tcl := pir::get_class__ps('TclList'),
my $core := pir::get_class__ps('ResizablePMCArray'),
$interp.hll_map($core, $tcl);
$core := pir::get_class__ps('ResizableStringArray'),
$interp.hll_map($core, $tcl);
$tcl.add_vtable_override('get_string', TclList::get_string)
}
module TclList {
method __dump($dumper, $label) {
$dumper.genericArray( $label, self );
}
method getIndex($index) {
my $parse := Partcl::Grammar.parse(
$index, :rule('index'),
:actions(Partcl::Actions)
);
if ?$parse && $parse.chars() == pir::length__is($index) {
my @pos := $parse.ast();
my $len := +self;
my $loc := @pos[1];
if @pos[0] == 2 { # position relative from end.
$loc := $len - 1 + $loc;
}
return $loc;
} else {
error("bad index \"$index\": must be integer?[+-]integer? or end?[+-]integer?");
}
}
method getList() {
return self;
}
method reverse() {
my $low := 0;
my $high := +self -1;
while $low < $high {
my $lowVal := self[$low];
my $highVal := self[$high];
self[$high] := $lowVal;
self[$low] := $highVal;
$low++; $high--;
}
return self;
}
method get_string() {
my @retval := ();
my $first := 1;
for self -> $element {
my $elem_length := pir::length__is($element);
my $new_s := '';
if $elem_length == 0 {
$new_s := '{}';
} else {
my $count := 0;
my $pos := 0;
my $brace_check_pos := 0;
my $has_braces := 0;
my $escaped := 0;
while $pos < $elem_length && !$escaped {
my $char := pir::ord__isi($element, $pos);
if $char == 0x7b { # open brace
$count++;
$has_braces := 1;
} elsif $char == 0x7d { # close brace
$count--;
if $count < 0 {
$new_s := self.'escape_element'($element);
$escaped := 1;
} else {
$brace_check_pos := +$pos;
}
}
$pos++;
}
unless $escaped {
if $count {
$new_s := self.'escape_element'($element);
} else {
if $has_braces && $brace_check_pos != $elem_length -1 {
if $brace_check_pos != $elem_length -2 {
$new_s := '{' ~ $element ~ '}';
} elsif pir::ord__isi($element,$elem_length-1) != 0x5c {
# 0x5c == backslash
$new_s := '{' ~ $element ~ '}';
} else {
$new_s := self.'escape_element'($element);
}
} elsif $elem_length -1 == pir::index__issi($element,"\\", $elem_length-1) {
$new_s := self.'escape_element'($element);
} elsif pir::index__iss($element, '"') != -1 ||
pir::index__iss($element, '[') != -1 ||
pir::index__iss($element, '$') != -1 ||
pir::index__iss($element, ';') != -1 ||
( $first && pir::index__iss($element, '#') != -1 ) {
$new_s := '{' ~ $element ~ '}';
} elsif pir::index__iss($element, ']') != -1 {
$new_s := self.'escape_element'($element);
} elsif pir::find_cclass__iisii(32, $element, 0, $elem_length) != $elem_length {
# .macro_const CCLASS_WHITESPACE 32
$new_s := '{' ~ $element ~ '}';
} else {
$new_s := $element
}
}
}
}
@retval.push($new_s);
$first := 0;
}
return pir::join__ssp(' ', @retval);
}
method escape_element($string) {
my $repl := ~$string;
$repl.replace("\\", "\\\\");
$repl.replace("\t", "\\t");
$repl.replace("\f", "\\f");
$repl.replace("\n", "\\n");
$repl.replace("\r", "\\r");
$repl.replace("\v", "\\v");
$repl.replace("\;", "\\;" );
$repl.replace('$', "\\\$" );
$repl.replace('{', "\\\x7b" );
$repl.replace('}', "\\\x7d" );
$repl.replace(' ', "\\ " );
$repl.replace('[', "\\[" );
$repl.replace(']', "\\]" );
$repl.replace('"', "\\\"");
return $repl;
}
}
# vim: filetype=perl6: