FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
WebKit/Source/JavaScriptCore/KeywordLookupGenerator.py at main · WebKit/WebKit · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
WebKit
/
WebKit
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10.1k
Code
Pull requests
2.6k
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
WebKit
/
Source
/
JavaScriptCore
/
KeywordLookupGenerator.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
235 lines (205 loc) · 7.81 KB
Breadcrumbs
WebKit
/
Source
/
JavaScriptCore
/
KeywordLookupGenerator.py
Copy path
File metadata and controls
235 lines (205 loc) · 7.81 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
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
# Copyright (C) 2011-2019 Apple Inc. All rights reserved.
# Copyright (C) 2012 Sony Network Entertainment. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import
sys
import
string
import
operator
keywordsText
=
open
(
sys
.
argv
[
1
]).
read
()
# A second argument signifies that the output
# should be redirected to a file
redirect_to_file
=
len
(
sys
.
argv
)
>
2
# Change stdout to point to the file if requested
if
redirect_to_file
:
file_output
=
open
(
sys
.
argv
[
-
1
],
"w"
)
sys
.
stdout
=
file_output
# Observed weights of the most common keywords, rounded to 2.s.d
keyWordWeights
=
{
"catch"
:
0.01
,
"try"
:
0.01
,
"while"
:
0.01
,
"case"
:
0.01
,
"break"
:
0.01
,
"new"
:
0.01
,
"in"
:
0.01
,
"typeof"
:
0.02
,
"true"
:
0.02
,
"false"
:
0.02
,
"for"
:
0.03
,
"null"
:
0.03
,
"else"
:
0.03
,
"return"
:
0.13
,
"var"
:
0.13
,
"if"
:
0.16
,
"function"
:
0.18
,
"this"
:
0.18
,
}
def
allWhitespace
(
str
):
for
c
in
str
:
if
not
(
c
in
string
.
whitespace
):
return
False
return
True
def
parseKeywords
(
keywordsText
):
if
sys
.
platform
==
"cygwin"
:
keywordsText
=
keywordsText
.
replace
(
"
\r
\n
"
,
"
\n
"
)
lines
=
keywordsText
.
split
(
"
\n
"
)
lines
=
[
line
.
split
(
"#"
)[
0
]
for
line
in
lines
]
lines
=
[
line
for
line
in
lines
if
(
not
allWhitespace
(
line
))]
name
=
lines
[
0
].
split
()
terminator
=
lines
[
-
1
]
if
not
name
[
0
]
==
"@begin"
:
raise
Exception
(
"expected description beginning with @begin"
)
if
not
terminator
==
"@end"
:
raise
Exception
(
"expected description ending with @end"
)
lines
=
lines
[
1
:
-
1
]
# trim off the old heading
return
[
line
.
split
()
for
line
in
lines
]
def
makePadding
(
size
):
str
=
""
for
i
in
range
(
size
):
str
=
str
+
" "
return
str
class
Trie
:
def
__init__
(
self
,
prefix
):
self
.
prefix
=
prefix
self
.
keys
=
{}
self
.
value
=
None
def
insert
(
self
,
key
,
value
):
if
len
(
key
)
==
0
:
self
.
value
=
value
return
if
not
(
key
[
0
]
in
self
.
keys
):
self
.
keys
[
key
[
0
]]
=
Trie
(
key
[
0
])
self
.
keys
[
key
[
0
]].
insert
(
key
[
1
:],
value
)
def
coalesce
(
self
):
keys
=
{}
for
k
,
v
in
self
.
keys
.
items
():
t
=
v
.
coalesce
()
keys
[
t
.
prefix
]
=
t
self
.
keys
=
keys
if
self
.
value
!=
None
:
return
self
if
len
(
self
.
keys
)
!=
1
:
return
self
prefix
,
suffix
=
next
(
iter
(
self
.
keys
.
items
()))
res
=
Trie
(
self
.
prefix
+
prefix
)
res
.
value
=
suffix
.
value
res
.
keys
=
suffix
.
keys
return
res
def
fillOut
(
self
,
prefix
=
""
):
self
.
fullPrefix
=
prefix
+
self
.
prefix
weight
=
0
if
self
.
fullPrefix
in
keyWordWeights
:
weight
=
weight
+
keyWordWeights
[
self
.
fullPrefix
]
self
.
selfWeight
=
weight
for
trie
in
self
.
keys
.
values
():
trie
.
fillOut
(
self
.
fullPrefix
)
weight
=
weight
+
trie
.
weight
self
.
keys
=
[(
trie
.
prefix
,
trie
)
for
trie
in
sorted
(
self
.
keys
.
values
(),
key
=
operator
.
attrgetter
(
'weight'
),
reverse
=
True
)]
self
.
weight
=
weight
def
printSubTreeAsC
(
self
,
typeName
,
indent
):
str
=
makePadding
(
indent
)
if
self
.
value
!=
None
:
print
(
str
+
"if (cannotBeIdentPartOrEscapeStart(code[%d])) [[likely]] {"
%
(
len
(
self
.
fullPrefix
)))
print
(
str
+
" internalShift<%d>();"
%
len
(
self
.
fullPrefix
))
print
(
str
+
" if (shouldCreateIdentifier)"
)
print
(
str
+
(
" data->ident = &m_vm.propertyNames->%sKeyword;"
%
self
.
fullPrefix
))
print
(
str
+
" return "
+
self
.
value
+
";"
)
print
(
str
+
"}"
)
rootIndex
=
len
(
self
.
fullPrefix
)
itemCount
=
0
for
k
,
trie
in
self
.
keys
:
baseIndex
=
rootIndex
if
(
baseIndex
>
0
)
and
(
len
(
k
)
==
3
):
baseIndex
=
baseIndex
-
1
k
=
trie
.
fullPrefix
[
baseIndex
]
+
k
test
=
[(
"'%s'"
%
c
)
for
c
in
k
]
if
len
(
test
)
==
1
:
comparison
=
"code[%d] == %s"
%
(
baseIndex
,
test
[
0
])
else
:
base
=
"code"
if
baseIndex
>
0
:
base
=
"code + %d"
%
baseIndex
comparison
=
(
"compareCharacters(%s, "
%
(
base
,))
+
", "
.
join
(
test
)
+
")"
if
itemCount
==
0
:
print
(
str
+
"if ("
+
comparison
+
") {"
)
else
:
print
(
str
+
"} else if ("
+
comparison
+
") {"
)
trie
.
printSubTreeAsC
(
typeName
,
indent
+
4
)
itemCount
=
itemCount
+
1
if
itemCount
==
len
(
self
.
keys
):
print
(
str
+
"}"
)
def
maxLength
(
self
):
max
=
len
(
self
.
fullPrefix
)
for
(
_
,
trie
)
in
self
.
keys
:
l
=
trie
.
maxLength
()
if
l
>
max
:
max
=
l
return
max
def
printAsC
(
self
):
print
(
"WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN"
)
print
(
""
)
print
(
"namespace JSC {"
)
print
(
""
)
print
(
"static ALWAYS_INLINE bool cannotBeIdentPartOrEscapeStart(Latin1Character);"
)
print
(
"static ALWAYS_INLINE bool cannotBeIdentPartOrEscapeStart(char16_t);"
)
# max length + 1 so we don't need to do any bounds checking at all
print
(
"static constexpr int maxTokenLength = %d;"
%
(
self
.
maxLength
()
+
1
))
print
(
""
)
print
(
"template <>"
)
print
(
"template <bool shouldCreateIdentifier> ALWAYS_INLINE JSTokenType Lexer<char16_t>::parseKeyword(JSTokenData* data)"
)
print
(
"{"
)
print
(
" ASSERT(m_codeEnd - m_code >= maxTokenLength);"
)
print
(
""
)
print
(
" const char16_t* code = m_code;"
)
self
.
printSubTreeAsC
(
"UCHAR"
,
4
)
print
(
" return IDENT;"
)
print
(
"}"
)
print
(
""
)
print
(
"template <>"
)
print
(
"template <bool shouldCreateIdentifier> ALWAYS_INLINE JSTokenType Lexer<Latin1Character>::parseKeyword(JSTokenData* data)"
)
print
(
"{"
)
print
(
" ASSERT(m_codeEnd - m_code >= maxTokenLength);"
)
print
(
""
)
print
(
" const Latin1Character* code = m_code;"
)
self
.
printSubTreeAsC
(
"CHAR"
,
4
)
print
(
" return IDENT;"
)
print
(
"}"
)
print
(
""
)
print
(
"} // namespace JSC"
)
print
(
""
)
print
(
"WTF_ALLOW_UNSAFE_BUFFER_USAGE_END"
)
keywords
=
parseKeywords
(
keywordsText
)
trie
=
Trie
(
""
)
for
k
,
v
in
keywords
:
trie
.
insert
(
k
,
v
)
trie
.
coalesce
()
trie
.
fillOut
()
print
(
"// This file was generated by KeywordLookupGenerator.py. Do not edit."
)
print
(
"""
#include <wtf/StdLibExtras.h>
#include <wtf/text/FastCharacterComparison.h>
"""
)
trie
.
printAsC
()
# Close the redirected file if requested
if
(
redirect_to_file
):
file_output
.
close
()
sys
.
stdout
=
sys
.
__stdout__
Back
|
FazBrowse Home
|
New Git URL