-
Notifications
You must be signed in to change notification settings - Fork 20
/
builtins_html_copy_go.bk
397 lines (366 loc) · 10.9 KB
/
builtins_html_copy_go.bk
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
//__ +build b_html
package evaldo
import (
"fmt"
"rye/env"
"golang.org/x/net/html"
//"fmt"
"io"
// "strings"
// "bufio"
// "io"
// "os"
// "strconv"
// "strings"
)
// { <div> .menu <a> { .content .print } }
// { <div> .menu <a> , .footer { .content .print } }
// { <div> .menu <a> [ .content .print ] <footer> { <a> .external [ .to-upper print ] }
// { <div> .menu <a> when [ [.attr? "href" |includes "www.google.com" ] { <b> [ .content? .collect ] } }
// when we have accessor words
// { <div> .menu <a> when [ [href] .includes "www.google.com" ] { <b> [ [content] .collect ] } }
type HtmlNavigCondition struct {
Class string
Id string
}
type HtmlDialectNode struct {
Condition *HtmlNavigCondition
Value interface{}
}
// tagname is stored in dict that we lookup. On match we look at condition if there.
// tagname could be wildcard in this case we look at condition each time
// xwords after another or conditions mean that we dive deeper, like a block { } would be
func load_html_Dict(es *env.ProgramState, block env.Block) (env.Dict, *env.Error) {
var key string
var condition *HtmlNavigCondition
var keys []string // keys
var conditions []*HtmlNavigCondition // conditions
data := make(map[string]interface{}) //
rmap := *env.NewDict(data) //
for block.Series.Pos() < block.Series.Len() {
obj := block.Series.Peek()
switch obj1 := obj.(type) {
case env.Void:
trace4("VOID")
keys = append(keys, "") // if void append empty string to keys
conditions = append(conditions, nil) // if void append empty string to keys
block.Series.Next()
continue
case env.Xword:
trace4("XWORD")
key = es.Idx.GetWord(obj1.Index)
// keys = append(keys, ) // if xword append to keys
block.Series.Next()
continue
case env.Tagword: // if tagword append -word- to keys
trace4("TAGWORD")
key = "-" + es.Idx.GetWord(obj1.Index) + "-"
//keys = append(keys, "-"+es.Idx.GetWord(obj1.Index)+"-")
block.Series.Next()
continue
case env.LSetword: // if tagword append -word- to keys
if condition == nil {
condition = &HtmlNavigCondition{"", ""}
}
condition.Id = es.Idx.GetWord(obj1.Index)
//keys = append(keys, "-"+es.Idx.GetWord(obj1.Index)+"-")
block.Series.Next()
continue
case env.Opword: // if tagword append -word- to keys
if condition == nil {
condition = &HtmlNavigCondition{"", ""}
}
condition.Class = es.Idx.GetWord(obj1.Index)
//keys = append(keys, "-"+es.Idx.GetWord(obj1.Index)+"-")
block.Series.Next()
continue
case env.Comma:
trace4("COMMA")
// push key and condition
keys = append(keys, key)
conditions = append(conditions, condition) // if void append empty string to keys
key = ""
condition = nil
block.Series.Next()
continue
case env.Block:
if key != "" || condition != nil {
keys = append(keys, key)
conditions = append(conditions, condition) // if void append empty string to keys
key = ""
condition = nil
}
trace4("BLOCK")
block.Series.Next()
if obj1.Mode == 1 {
// if code assign in to keys in Dict
if len(keys) > 0 {
for idx, k := range keys {
if conditions[idx] != nil {
rmap.Data[k] = HtmlDialectNode{conditions[idx], obj1}
} else {
rmap.Data[k] = obj1
}
keys = []string{}
}
} else {
rmap.Data["-start-"] = obj1
}
} else if obj1.Mode == 0 {
rm, err := load_html_Dict(es, obj1)
if err != nil {
return _emptyRM(), err
}
trace8(" **** FOR KEYS")
if len(keys) > 0 {
fmt.Println(keys)
fmt.Println(rm)
for _, k := range keys {
rmap.Data[k] = rm
}
keys = []string{}
} else {
return _emptyRM(), env.NewError("no selectors before tag map")
}
}
default:
// ni Dict ampak blok kode, vrni blok
return _emptyRM(), env.NewError("unknow type in block parsing TODO")
}
}
trace8("**** RETURNING **** ")
fmt.Println(rmap)
return rmap, nil
}
func do_html(es *env.ProgramState, reader io.Reader, rmap env.Dict) env.Object {
var stack []env.Dict // ??_?
var tags []string // list of tags ??_?
var curtag string // current tag ??_?
decoder := html.NewTokenizer(reader)
b, ok := rmap.Data["-start-"]
if ok {
switch obj := b.(type) {
case env.Block: // if values of start key is of type block, evaluate it
ser := es.Ser // TODO -- make helper function that "does" a block
es.Ser = obj.Series
EvalBlockInj(es, *env.NewNative(es.Idx, "", "rye-html-start"), true)
es.Ser = ser
default:
// TODO Err
}
}
//total := 0
for {
// Read tokens from the XML document in a stream.
t := decoder.Next()
// Inspect the type of the token just read.
switch t {
case html.StartTagToken:
trace8("START TOKEN (tag)")
tok := decoder.Token()
tag := tok.Data
trace8(tag)
// check if token exists in current rmap
ob, ok := rmap.Data[tag]
if ok {
// if it is then
trace8(" --- is in RMAP")
switch obj := ob.(type) { // swith on the type of object from rmap
case env.Dict: // if it is Dict
trace8(" --- THIS IS DICT OBJ")
tags = append(tags, curtag) // append current tag to tags
stack = append(stack, rmap) // append whole rmap to the stack
//fmt.Println(stack)
rmap = obj // set the received object as rmap
curtag = tag // set tag as curtag
// if new rmap has -start- run it
b, ok := rmap.Data["-start-"]
if ok {
switch obj := b.(type) {
case env.Block: // if values of start key is of type block, evaluate it
ser := es.Ser // TODO -- make helper function that "does" a block
es.Ser = obj.Series
EvalBlockInj(es, *env.NewNative(es.Idx, tok, "rye-html-start"), true)
es.Ser = ser
default:
// TODO Err
}
}
case HtmlDialectNode: // if it is Dict
trace4(" THIS IS DIALECT NODE")
blk := obj.Value // set the received object as rmap
id := ""
class := ""
// get the class and id attributes
for _, a := range tok.Attr {
if a.Key == "id" {
id = a.Val
} else if a.Key == "class" {
class = a.Val
}
}
if (obj.Condition.Class == "" || obj.Condition.Class == class) &&
(obj.Condition.Id == "" || obj.Condition.Id == id) {
switch blk2 := blk.(type) { // swith on the type of object from rmap
case env.Dict: // if it is Dict
trace8(" --- THIS IS DICT OBJ")
tags = append(tags, curtag) // append current tag to tags
stack = append(stack, rmap) // append whole rmap to the stack
//fmt.Println(stack)
rmap = blk2 // set the received object as rmap
curtag = tag // set tag as curtag
// if new rmap has -start- run it
b, ok := rmap.Data["-start-"]
if ok {
switch obj := b.(type) {
case env.Block: // if values of start key is of type block, evaluate it
ser := es.Ser // TODO -- make helper function that "does" a block
es.Ser = obj.Series
EvalBlockInj(es, *env.NewNative(es.Idx, tok, "rye-html-start"), true)
es.Ser = ser
default:
// TODO Err
}
}
case env.Block:
ser := es.Ser // TODO -- make helper function that "does" a block
es.Ser = blk2.Series
EvalBlockInj(es, *env.NewNative(es.Idx, tok, "rye-html-start"), true)
es.Ser = ser
}
}
case env.Block: // if it is block
trace4(" THIS IS BLOCK OBJ")
// stack = append(stack, rmap) // append rmap to stack and evaluate block
ser := es.Ser // TODO -- make helper function that "does" a block
es.Ser = obj.Series
EvalBlockInj(es, *env.NewNative(es.Idx, tok, "rye-html-start"), true)
es.Ser = ser
}
}
case html.TextToken:
tok := decoder.Token()
ob, ok := rmap.Data[""]
if ok {
switch obj := ob.(type) {
case env.Block:
ser := es.Ser // TODO -- make helper function that "does" a block
es.Ser = obj.Series
EvalBlockInj(es, env.String{string(tok.Data)}, true)
es.Ser = ser
}
}
case html.EndTagToken:
tok := decoder.Token()
inElement := tok.Data
// inElement := se.Name.Local
trace8("END TAG (elem, curtag)")
trace8(inElement)
trace8(curtag)
if inElement == curtag { // TODO -- solve the case of same named elements inside <person><person></person></person>
trace4("END2")
//fmt.Println(rmap)
b, ok := rmap.Data["-end-"]
if ok {
switch obj := b.(type) {
case env.Block:
ser := es.Ser // TODO -- make helper function that "does" a block
es.Ser = obj.Series
EvalBlockInj(es, *env.NewNative(es.Idx, tok, "rye-html-start"), true)
es.Ser = ser
default:
// TODO Err
}
}
trace8("*** RETURNING ***")
fmt.Println(stack)
fmt.Println(tags)
n := len(stack) - 1 // Top element
fmt.Println(n)
rmap = stack[n]
stack = stack[:n] // Pop
m := len(tags) - 1 // Top element
curtag = tags[m]
tags = tags[:m] // Pop
fmt.Println(stack)
fmt.Println(tags)
}
case html.ErrorToken:
return nil
default:
}
}
return nil
}
func trace4(s string) {
// fmt.Println(s)
}
func trace8(s string) {
if true {
fmt.Println(s)
}
}
var Builtins_html = map[string]*env.Builtin{
"rye-reader//do-html": {
Argsn: 2,
Fn: func(es *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
rm, err := load_html_Dict(es, arg1.(env.Block))
trace8("*** _--- GOT RM ++**")
fmt.Println(rm)
if err != nil {
es.FailureFlag = true
return err
}
return do_html(es, arg0.(env.Native).Value.(io.Reader), rm)
},
},
"rye-html-start//attr?": {
Argsn: 2,
Fn: func(es *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch tok1 := arg0.(type) {
case env.Native:
switch tok := tok1.Value.(type) {
case html.Token:
switch n := arg1.(type) {
case env.Integer:
if int(n.Value) < len(tok.Attr) {
return env.String{tok.Attr[int(n.Value)].Val}
} else {
return env.Void{}
}
case env.Tagword:
for _, a := range tok.Attr {
if a.Key == es.Idx.GetWord(n.Index) {
return env.String{a.Val}
}
}
return env.Void{}
default:
return env.NewError("second arg not integer")
}
default:
return env.NewError("Not xml-strat element")
}
default:
return env.NewError("first argument should be native")
}
},
},
/*
"rye-html-start//name?": {
Argsn: 1,
Fn: func(es *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch obj := arg0.(type) {
case env.Native:
switch obj1 := obj.Value.(type) {
case xml.StartElement:
return env.String{obj1.Name.Local}
default:
return env.NewError("Not xml-strat element")
}
default:
return env.NewError("first argument should be native")
}
},
}, */
}