File: itype.tcl

package info (click to toggle)
sbnc 1.2-26
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,216 kB
  • sloc: cpp: 17,556; ansic: 15,514; sh: 13,419; tcl: 5,567; php: 448; makefile: 284
file content (243 lines) | stat: -rw-r--r-- 4,291 bytes parent folder | download | duplicates (3)
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
proc itype_parse {value} {
	set chars [split $value ""]

	set escape 0
	set type ""
	set offset 0
	set data [list]
	set codeCount 0

	foreach char $chars {
		incr offset

		if {$escape} {
			set wasEscape 1
		} else {
			set wasEscape 0
		}

		if {$char == "\\" && !$escape} {
			set escape 1
		} else {
			set escape 0
		}

		if {!$wasEscape} {
			set controlCode 1

			switch -- $char {
				"\[" {
					if {$type == ""} {
						set type "exception"
					}

					set controlCode 0

					if {$type == "exception"} {
						incr codeCount
					}
				}

				"\{" {
					if {$type == ""} {
						set type "list"
					}

					set controlCode 0

					if {$type == "list"} {
						incr codeCount
					}
				}

				"(" {
					if {$type == ""} {
						set type "string"
					}

					set controlCode 0

					if {$type == "string"} {
						incr codeCount
					}
				}

				"\]" {
					if {$type != "exception"} {
						set controlCode 0
					} else {
						incr codeCount -1
					}
				}

				"\}" {
					if {$type != "list"} {
						set controlCode 0
					} else {
						incr codeCount -1
					}
				}

				")" {
					if {$type != "string"} {
						set controlCode 0
					} else {
						incr codeCount -1
					}
				}

				default {
					set controlCode 0
				}
			}
		} else {
			if {$char == "n"} {
				set char "\n"
			} elseif {$char == "r"} {
				set char "\r"
			}
		}

		if {$type == "list" && $wasEscape} {
			lappend data "\\"
		}

		if {$type != "" && !$escape} {
			lappend data $char
		}

		if {!$wasEscape && $controlCode && $codeCount == 0} {
			switch -- $type {
				"string" {
					return [list "string" [join [lrange $data 1 end-1] ""] $offset]
				}

				"exception" {
					return [list "exception" [join [lrange $data 1 end-1] ""] $offset]
				}

				"list" {
					set dataString [join [lrange $data 1 end-1] ""] 
					set listData [list]

					while {[string length $dataString] > 0} {
						set innerData [itype_parse $dataString]

						if {[lindex $innerData 0] == "empty"} {
							break
						}

						set dataString [string range $dataString [lindex $innerData 2] end]

						lappend listData $innerData
					}

					return [list "list" $listData $offset]
				}
			}
		}
	}

	return [list "empty" "" $offset]
}

proc itype_flat {value} {
	set type [lindex $value 0]

	if {$type == "list"} {
		set listItems [list]

		foreach item [lindex $value 1] {
			lappend listItems [itype_flat $item]
		}

		return $listItems
	} else {
		return [itype_unescape [lindex $value 1]]
	}
}

proc itype_escape {value} {
	set output $value

	set output [string map [list "\\" "\\\\"] $output]

	set output [string map [list "\r" "\\r"] $output]
	set output [string map [list "\n" "\\n"] $output]

	set output [string map [list "\{" "\\\{"] $output]
	set output [string map [list "\}" "\\\}"] $output]

	set output [string map [list "\[" "\\\["] $output]
	set output [string map [list "\]" "\\\]" ] $output]

	set output [string map [list "(" "\\("] $output]
	set output [string map [list ")" "\\)"] $output]

	return $output
}

proc itype_unescape {value} {
	set output $value

	set output [string map [list "\\r" "\r"] $output]
	set output [string map [list "\\n" "\n"] $output]

	set output [string map [list "\\\{" "\{"] $output]
	set output [string map [list "\\\}" "\}"] $output]

	set output [string map [list "\\\[" "\["] $output]
	set output [string map [list "\\\]" "\]"] $output]

	set output [string map [list "\\(" "("] $output]
	set output [string map [list "\\)" ")"] $output]

	set output [string map [list "\\\\" "\\"] $output]

	return $output
}

proc itype_totcl {value} {
	set parsed [itype_parse $value]
	return [itype_flat $parsed]
}

proc itype_string {value} {
	return "([itype_escape $value])"
}

proc itype_exception {value} {
	return "\[[itype_escape $value]\]"
}

proc itype_list_create {} {
	return "\{"
}

proc itype_list_insert {listName itype_value} {
	upvar 1 $listName list

	append list $itype_value
}

proc itype_list_end {listName} {
	upvar 1 $listName list

	append list "\}"
}

proc itype_list_strings {strings} {
	set itype_list [itype_list_create]

	foreach arg $strings {
		itype_list_insert itype_list [itype_string $arg]
	}

	itype_list_end itype_list

	return $itype_list
}

proc itype_list_strings_args {args} {
	return [itype_list_strings $args]
}