-
Notifications
You must be signed in to change notification settings - Fork 14
/
tcl.tcl
104 lines (89 loc) · 2.47 KB
/
tcl.tcl
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
# Sqawk, an SQL Awk.
# Copyright (c) 2015-2018, 2020 D. Bohdan
# License: MIT
namespace eval ::sqawk::parsers::tcl {
variable formats {
tcl
}
variable options {
kv 0
lines 0
}
}
::snit::type ::sqawk::parsers::tcl::parser {
variable kv
variable linesMode
variable ch
variable data
variable i
variable keys
variable len
constructor {channel options} {
set kv [dict get $options kv]
set linesMode [dict get $options lines]
set i [expr { $kv ? -1 : 0 }]
if {$linesMode} {
if {$kv} {
set lines [split [string trim [read $channel]] \n]
set data [lmap line $lines {
if {[regexp {^\s*$} $line]} continue
set line
}]
} else {
set ch $channel
set data %NEVER_USED%
}
} else {
set data [read $channel]
}
set len [llength $data]
}
method next {} {
if {$i == $len} {
return -code break
}
if {!$kv} {
if {$linesMode} {
set line {}
while {[set blank [regexp {^\s*$} $line]] && ![eof $ch]} {
gets $ch line
}
if {$blank && [eof $ch]} {
return -code break
}
set list $line
} else {
set list [lindex $data $i]
incr i
}
return [list $list {*}$list]
}
if {$i == -1} {
set allKeys [lsort -unique [concat {*}[lmap record $data {
dict keys $record
}]]]
# Order the keys like they are ordered in the first row for
# ergonomics. Keys that aren't in the first row follow in
# alphabetical order after those that are.
set keys [dict keys [lindex $data 0]]
foreach key $allKeys {
if {$key ni $keys} {
lappend keys $key
}
}
incr i
return [list $keys {*}$keys]
}
set record [lindex $data $i]
set row [list $record]
foreach key $keys {
if {[dict exists $record $key]} {
lappend row [dict get $record $key]
} else {
lappend row {}
}
}
incr i
return $row
}
}