FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeql/python/extractor/lark/tree.py at codeql-cli/v2.26.3 · github/codeql · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
github
/
codeql
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10k
Code
Issues
997
Pull requests
459
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codeql
/
python
/
extractor
/
lark
/
tree.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
162 lines (125 loc) · 4.35 KB
Breadcrumbs
codeql
/
python
/
extractor
/
lark
/
tree.py
Copy path
File metadata and controls
162 lines (125 loc) · 4.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
try
:
from
future_builtins
import
filter
except
ImportError
:
pass
from
copy
import
deepcopy
class
Meta
:
pass
###{standalone
class
Tree
(
object
):
def
__init__
(
self
,
data
,
children
,
meta
=
None
):
self
.
data
=
data
self
.
children
=
children
self
.
_meta
=
meta
@
property
def
meta
(
self
):
if
self
.
_meta
is
None
:
self
.
_meta
=
Meta
()
return
self
.
_meta
def
__repr__
(
self
):
return
'Tree(%s, %s)'
%
(
self
.
data
,
self
.
children
)
def
_pretty_label
(
self
):
return
self
.
data
def
_pretty
(
self
,
level
,
indent_str
):
if
len
(
self
.
children
)
==
1
and
not
isinstance
(
self
.
children
[
0
],
Tree
):
return
[
indent_str
*
level
,
self
.
_pretty_label
(),
'
\t
'
,
'%s'
%
(
self
.
children
[
0
],),
'
\n
'
]
l
=
[
indent_str
*
level
,
self
.
_pretty_label
(),
'
\n
'
]
for
n
in
self
.
children
:
if
isinstance
(
n
,
Tree
):
l
+=
n
.
_pretty
(
level
+
1
,
indent_str
)
else
:
l
+=
[
indent_str
*
(
level
+
1
),
'%s'
%
(
n
,),
'
\n
'
]
return
l
def
pretty
(
self
,
indent_str
=
' '
):
return
''
.
join
(
self
.
_pretty
(
0
,
indent_str
))
###}
def
expand_kids_by_index
(
self
,
*
indices
):
"Expand (inline) children at the given indices"
for
i
in
sorted
(
indices
,
reverse
=
True
):
# reverse so that changing tail won't affect indices
kid
=
self
.
children
[
i
]
self
.
children
[
i
:
i
+
1
]
=
kid
.
children
def
__eq__
(
self
,
other
):
try
:
return
self
.
data
==
other
.
data
and
self
.
children
==
other
.
children
except
AttributeError
:
return
False
def
__ne__
(
self
,
other
):
return
not
(
self
==
other
)
def
__hash__
(
self
):
return
hash
((
self
.
data
,
tuple
(
self
.
children
)))
def
find_pred
(
self
,
pred
):
"Find all nodes where pred(tree) == True"
return
filter
(
pred
,
self
.
iter_subtrees
())
def
find_data
(
self
,
data
):
"Find all nodes where tree.data == data"
return
self
.
find_pred
(
lambda
t
:
t
.
data
==
data
)
def
scan_values
(
self
,
pred
):
for
c
in
self
.
children
:
if
isinstance
(
c
,
Tree
):
for
t
in
c
.
scan_values
(
pred
):
yield
t
else
:
if
pred
(
c
):
yield
c
def
iter_subtrees
(
self
):
# TODO: Re-write as a more efficient version
visited
=
set
()
q
=
[
self
]
l
=
[]
while
q
:
subtree
=
q
.
pop
()
l
.
append
(
subtree
)
if
id
(
subtree
)
in
visited
:
continue
# already been here from another branch
visited
.
add
(
id
(
subtree
))
q
+=
[
c
for
c
in
subtree
.
children
if
isinstance
(
c
,
Tree
)]
seen
=
set
()
for
x
in
reversed
(
l
):
if
id
(
x
)
not
in
seen
:
yield
x
seen
.
add
(
id
(
x
))
def
__deepcopy__
(
self
,
memo
):
return
type
(
self
)(
self
.
data
,
deepcopy
(
self
.
children
,
memo
))
def
copy
(
self
):
return
type
(
self
)(
self
.
data
,
self
.
children
)
def
set
(
self
,
data
,
children
):
self
.
data
=
data
self
.
children
=
children
# XXX Deprecated! Here for backwards compatibility <0.6.0
@
property
def
line
(
self
):
return
self
.
meta
.
line
@
property
def
column
(
self
):
return
self
.
meta
.
column
@
property
def
end_line
(
self
):
return
self
.
meta
.
end_line
@
property
def
end_column
(
self
):
return
self
.
meta
.
end_column
class
SlottedTree
(
Tree
):
__slots__
=
'data'
,
'children'
,
'rule'
,
'_meta'
def
pydot__tree_to_png
(
tree
,
filename
):
"Creates a colorful image that represents the tree (data+children, without meta)"
import
pydot
graph
=
pydot
.
Dot
(
graph_type
=
'digraph'
,
rankdir
=
"LR"
)
i
=
[
0
]
def
new_leaf
(
leaf
):
node
=
pydot
.
Node
(
i
[
0
],
label
=
repr
(
leaf
))
i
[
0
]
+=
1
graph
.
add_node
(
node
)
return
node
def
_to_pydot
(
subtree
):
color
=
hash
(
subtree
.
data
)
&
0xffffff
color
|=
0x808080
subnodes
=
[
_to_pydot
(
child
)
if
isinstance
(
child
,
Tree
)
else
new_leaf
(
child
)
for
child
in
subtree
.
children
]
node
=
pydot
.
Node
(
i
[
0
],
style
=
"filled"
,
fillcolor
=
"#%x"
%
color
,
label
=
subtree
.
data
)
i
[
0
]
+=
1
graph
.
add_node
(
node
)
for
subnode
in
subnodes
:
graph
.
add_edge
(
pydot
.
Edge
(
node
,
subnode
))
return
node
_to_pydot
(
tree
)
graph
.
write_png
(
filename
)
Back
|
FazBrowse Home
|
New Git URL