This repository has been archived by the owner on Jul 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.nim
119 lines (99 loc) · 3.25 KB
/
state.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
# common.nim
import stacks, sets, uuids, sequtils
# import json
# import constructs/cs_root
import types
type Block* = object
id*: UUID
name*: string
info*: Info
type SearchOption* = enum
soBlocks, soAll
var blocks* = newStack[Block]()
proc currentPath*(): seq[Block] = blocks.toSeq()
var currentConstruct* = newSeq[Block]()
import algorithm, hashes
import options
import strutils
# proc getLastBlockTypes_Orig*(typeStrs: openArray[string]): Option[Block] =
# let loweredTypeStrs = typeStrs.mapIt(it.toLowerAscii)
# result = getLastBlock(proc(c: Block): bool = c.name.toLowerAscii in loweredTypeStrs)
proc getLastBlockTypes(typeStrs: openArray[string], so: SearchOption): Option[Block] =
var x = if so == soBlocks: currentPath() else: currentConstruct
let loweredTypeStrsSet = typeStrs.mapIt(it.toLowerAscii).toHashSet()
# let constrSet = currentConstruct.mapIt(it.name.toLowerAscii).toHashSet()
let constrSet = x.mapIt(it.name.toLowerAscii).toHashSet()
# the order is important though
if (constrSet - loweredTypeStrsSet).len > 0:
# for c in currentConstruct.reversed:
for c in x.reversed:
if c.name.toLowerAscii in loweredTypeStrsSet:
result = some(c)
else: result = none(Block)
proc getLastTypes*(typeStrs: openArray[string]): Option[Block] =
result = getLastBlockTypes(typeStrs, soAll)
proc getLastBlocks*(typeStrs: openArray[string]): Option[Block] =
result = getLastBlockTypes(typeStrs, soBlocks)
proc getLastType*(typeStr: string): Option[Block] =
result = getLastTypes(@[typestr])
proc getLastBlock*(cond: (proc(c: Block): bool), so = soBlocks): Option[Block] =
var x = if so == soBlocks: currentPath() else: currentConstruct
for c in x.reversed:
if c.cond(): return c.some
return none(Block)
proc getLastType*(cond: (proc(c: Block): bool)): Option[Block] =
result = getLastBlock(cond, soAll)
# import construct, tables
proc prevprevConstruct*: Block =
let skipList = @[
"BlockStarts"
].toHashSet()
assert currentConstruct.len >= 3
for i, c in currentConstruct[0 .. ^3].reversed:
if not (c.name in skipList):
return c
proc previousConstruct*: Block =
let skipList = @[
"BlockStarts"
].toHashSet()
assert currentConstruct.len >= 2
for i, c in currentConstruct[0 .. ^2].reversed:
# if i > 0:
if not (c.name in skipList):
return c
# declaration string as received from cs side.
let blockTypesTxt* = [ # everything in C# that has an opening { brace
"BlockStarts", # needed!!
"AccessorDeclaration",
"AnonymousMethodExpression",
"CatchClause",
"CheckedStatement",
"ClassDeclaration",
"ConstructorDeclaration",
"ConversionOperatorDeclaration",
"DestructorDeclaration",
"DoStatement",
"ElseClause",
"EnumDeclaration",
"FinallyClause",
"FixedStatement",
"ForEachStatement",
"ForStatement",
"IfStatement",
"IndexerDeclaration",
"LockStatement",
"MethodDeclaration",
"NamespaceDeclaration",
"OperatorDeclaration",
"ParenthesizedLambdaExpression",
"PropertyDeclaration",
"SimpleLambdaExpression",
"StructDeclaration",
"SwitchSection",
"SwitchStatement",
"TryStatement",
"UnsafeStatement",
"UsingStatement",
"WhileStatement",
].toHashSet
# note: if endblock raises an assert, it means a previous construct was not recorded here.