-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_utils.nim
130 lines (106 loc) · 3.43 KB
/
state_utils.nim
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
import constructs / cs_all_constructs
import stacks, tables, json, sequtils, options
import state, types
import constructs/justtypes
import strutils, options
import constructs/cs_root
import tables, sets
var parentTable* = initTable[int, string]()
proc nsPathNS*(r: var CsRoot): seq[CsNamespace] =
# echo "in nsPathNS ", $blocks
var started = false
# we assume blocks starts with namespaces.
# echo blocks
for b in blocks.toSeq:
if b.name == "StartBlock": continue # ignore.
elif b.name == "NamespaceDeclaration":
started = true
let ns = r.fetch(b.id)
if ns.isNone: return
else:
if ns.get.kind == ckNamespace:
# echo "the name: " & ns.get.namespace.name
result.add(ns.get.namespace)
# let name = extract(CsNamespace, b.info).name
# # assert r.nsTables.hasKey(name), "r.nsTables doesn't have it:" & $name
# let actual = r.ns.toSeq.filterIt(it.name == name)
# let actualNs =
# r.nsTables[name]
else:
if not started:
continue # ignore the beginning until we find a namespace.
else: # both started and not ns: stop
break # stop after last ns.
proc nsPath*(r: var CsRoot): string = # SAME?
let ns = nsPathNS(r)
result = if ns.len > 0:
ns.mapIt(it.name).join(".")
else:
"default"
import common_utils
proc itemName*(b: Block): string =
b.info.essentials[0]
proc pairs[A, B](t: TableRef[A, B]): seq[(A, B)] =
for p in pairs(t):
result.add p
proc keys[A, B](t: TableRef[A, B]): seq[A] =
for k in keys(t):
result.add k
proc previousBlock*(a: int = 2): Option[Block] =
let idx = -2 * a + 1
let prev = blocks.peek(idx) # -2*2+1 = -3
result = prev
proc isVisitBlock*(info: Info): bool =
result = info.extras.len > 0 and info.extras[0] == "VisitBlock"
import state
proc previousConstructObj*(r: var CsRoot): Option[Construct] =
let previd = previousConstruct().id
result = r.fetch(previd)
proc getLastClass*(root: var CsRoot): Option[CsClass] =
var ns = nsPathNS(root)
if ns.len == 0: ns = @[root.global]
result = ns.last.getLastClass()
proc getCurrentNs*(root: var CsRoot): (string, CsNamespace) =
var p = nsPath(root)
if p == "": p = "default"
# echo p
assert root.nsTables.hasKey(p)
let ns = root.nsTables[p]
result = (p, ns)
proc getIndexer(c: CsClass): Option[CsIndexer] =
if not c.hasIndexer():
echo "no indexer in class"
result = none(CsIndexer)
else:
result = some(c.indexer)
proc getLastIndexer*(ns: CsNamespace): Option[CsIndexer] =
assert ns.lastAddedTo.isSome
case ns.lastAddedTo.get
of NamespaceParts.Classes:
let c = ns.getLastClass()
if c.isNone:
echo "no last class"
result = none(CsIndexer)
else:
result = c.get.getIndexer()
of [NamespaceParts.Interfaces, NamespaceParts.Unset, NamespaceParts.Enums,
NamespaceParts.Using]: discard
proc getLastIndexer*(root: var CsRoot): Option[CsIndexer] =
var (_, ns) = root.getCurrentNs
result = ns.getLastIndexer()
import common_utils
import constructs/[justtypes] #,cs_root]
proc getLastClass*(ns: CsNamespace): Option[CsClass] =
# echo ns
if ns.classes.len == 0:
result = none(CsClass)
else:
result = some(ns.classes.last)
proc getLastMethod*(cls: CsClass): Option[CsMethod] =
if cls.methods.len == 0: return
else:
return some(cls.methods.last)
proc getLastCtor*(cls: CsClass): Option[CsConstructor] =
if cls.ctors.len == 0: return
else:
return some(cls.ctors.last)