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
|
#!/usr/bin/env python3
#
# Copyright (c) 2015-2016 Apple Inc. 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. AND ITS CONTRIBUTORS ``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 ITS 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 logging
import re
import os
from builtins_templates import BuiltinsGeneratorTemplates as Templates
log = logging.getLogger('global')
_FRAMEWORK_CONFIG_MAP = {
"JavaScriptCore": {
"macro_prefix": "JSC",
"namespace": "JSC",
},
"WebCore": {
"macro_prefix": "WEBCORE",
"namespace": "WebCore",
},
}
functionHeadRegExp = re.compile(r"(?:@[\w|=\[\] \"\.]+\s*\n)*(?:async\s+)?function\s+\w+\s*\(.*?\)", re.MULTILINE | re.DOTALL)
functionLinkTimeConstantRegExp = re.compile(r".*^@linkTimeConstant", re.MULTILINE | re.DOTALL)
functionAlwaysInlineRegExp = re.compile(r".*^@alwaysInline", re.MULTILINE | re.DOTALL)
functionVisibilityRegExp = re.compile(r".*^@visibility=(\w+)", re.MULTILINE | re.DOTALL)
functionNakedConstructorRegExp = re.compile(r".*^@nakedConstructor", re.MULTILINE | re.DOTALL)
functionIntrinsicRegExp = re.compile(r".*^@intrinsic=(\w+)", re.MULTILINE | re.DOTALL)
functionIsConstructorRegExp = re.compile(r".*^@constructor", re.MULTILINE | re.DOTALL)
functionIsGetterRegExp = re.compile(r".*^@getter", re.MULTILINE | re.DOTALL)
functionIsAsyncRegExp = re.compile(r".*^(async)?\s*function", re.MULTILINE | re.DOTALL)
functionNameRegExp = re.compile(r"function\s+(\w+)\s*\(", re.MULTILINE | re.DOTALL)
functionOverriddenNameRegExp = re.compile(r".*^@overriddenName=(\".+\")$", re.MULTILINE | re.DOTALL)
functionParameterFinder = re.compile(r"^(?:async\s+)?function\s+(?:\w+)\s*\(((?:\s*\w+)?\s*(?:\s*,\s*\w+)*)?\s*\)", re.MULTILINE | re.DOTALL)
functionParametersSplitter = re.compile(r",\s*(?![^{}]*\})", re.MULTILINE | re.DOTALL)
multilineCommentRegExp = re.compile(r"\/\*.*?\*\/", re.MULTILINE | re.DOTALL)
singleLineCommentRegExp = re.compile(r"\/\/.*?\n", re.MULTILINE | re.DOTALL)
keyValueAnnotationCommentRegExp = re.compile(r"^\/\/ @(\w+)=([^=]+?)\n", re.MULTILINE | re.DOTALL)
flagAnnotationCommentRegExp = re.compile(r"^\/\/ @(\w+)[^=]*?\n", re.MULTILINE | re.DOTALL)
lineWithOnlySingleLineCommentRegExp = re.compile(r"^\s*\/\/\n", re.MULTILINE | re.DOTALL)
lineWithTrailingSingleLineCommentRegExp = re.compile(r"\s*\/\/\n", re.MULTILINE | re.DOTALL)
leadingWhitespaceRegExp = re.compile(r"^ +", re.MULTILINE | re.DOTALL)
multipleEmptyLinesRegExp = re.compile(r"\n{2,}", re.MULTILINE | re.DOTALL)
class ParseException(Exception):
pass
class Framework:
def __init__(self, name):
self._settings = _FRAMEWORK_CONFIG_MAP[name]
self.name = name
def setting(self, key, default=''):
return self._settings.get(key, default)
@staticmethod
def fromString(frameworkString):
if frameworkString == "JavaScriptCore":
return Frameworks.JavaScriptCore
if frameworkString == "WebCore":
return Frameworks.WebCore
raise ParseException("Unknown framework: %s" % frameworkString)
class Frameworks:
JavaScriptCore = Framework("JavaScriptCore")
WebCore = Framework("WebCore")
class BuiltinObject:
def __init__(self, object_name, annotations, functions):
self.object_name = object_name
self.annotations = annotations
self.functions = functions
self.collection = None # Set by the owning BuiltinsCollection
for function in self.functions:
function.object = self
class BuiltinFunction:
def __init__(self, function_name, function_source, parameters, is_async, is_constructor, is_link_time_constant, is_naked_constructor, is_always_inline, intrinsic, visibility, overridden_name):
self.function_name = function_name
self.function_source = function_source
self.parameters = parameters
self.is_async = is_async
self.is_constructor = is_constructor
self.is_naked_constructor = is_naked_constructor
self.is_always_inline = is_always_inline
self.is_link_time_constant = is_link_time_constant
self.intrinsic = intrinsic
self.visibility = visibility
self.overridden_name = overridden_name
self.object = None # Set by the owning BuiltinObject
@staticmethod
def fromString(function_string):
function_source = multilineCommentRegExp.sub("", function_string)
intrinsic = "NoIntrinsic"
if "@intrinsic=" in function_source:
intrinsicMatch = functionIntrinsicRegExp.search(function_source)
if intrinsicMatch:
intrinsic = intrinsicMatch.group(1)
function_source = function_source.replace(intrinsicMatch.group(0), "")
overridden_name = None
if "@overriddenName=" in function_source:
overriddenNameMatch = functionOverriddenNameRegExp.search(function_source)
if overriddenNameMatch:
overridden_name = overriddenNameMatch.group(1)
function_source = function_source.replace(overriddenNameMatch.group(0), "")
if not os.getenv("CONFIGURATION", "Debug").startswith("Debug"):
function_source = lineWithOnlySingleLineCommentRegExp.sub("", function_source)
function_source = lineWithTrailingSingleLineCommentRegExp.sub("\n", function_source)
function_source = leadingWhitespaceRegExp.sub("", function_source)
function_source = multipleEmptyLinesRegExp.sub("\n", function_source)
function_name = functionNameRegExp.findall(function_source)[0]
async_match = functionIsAsyncRegExp.match(function_source)
is_async = async_match != None and async_match.group(1) == "async"
is_constructor = functionIsConstructorRegExp.match(function_source) != None
is_getter = functionIsGetterRegExp.match(function_source) != None
is_link_time_constant = functionLinkTimeConstantRegExp.match(function_source) != None
is_naked_constructor = functionNakedConstructorRegExp.match(function_source) != None
is_always_inline = functionAlwaysInlineRegExp.match(function_source) != None
if is_naked_constructor:
is_constructor = True
visibility = "Public"
if "@visibility=" in function_source:
visibilityMatch = functionVisibilityRegExp.search(function_source)
if visibilityMatch:
visibility = visibilityMatch.group(1)
function_source = function_source.replace(visibilityMatch.group(1), "")
elif is_link_time_constant:
visibility = "Private"
parameters = [s.strip() for s in functionParametersSplitter.split(functionParameterFinder.findall(function_source)[0])]
if len(parameters[0]) == 0:
parameters = []
if is_getter and not overridden_name:
overridden_name = "\"get %s\"_s" % (function_name)
if not overridden_name:
overridden_name = "ASCIILiteral()"
if overridden_name[-1] == "\"":
overridden_name += "_s"
return BuiltinFunction(function_name, function_source, parameters, is_async, is_constructor, is_link_time_constant, is_naked_constructor, is_always_inline, intrinsic, visibility, overridden_name)
def __str__(self):
interface = "%s(%s)" % (self.function_name, ', '.join(self.parameters))
if self.is_constructor:
interface = interface + " [Constructor]"
if self.is_async:
interface = "async " + interface
return interface
def __lt__(self, other):
return self.function_name < other.function_name
class BuiltinsCollection:
def __init__(self, framework_name):
self._copyright_lines = set()
self.objects = []
self.framework = Framework.fromString(framework_name)
log.debug("Created new Builtins collection.")
def parse_builtins_file(self, filename, text):
log.debug("Parsing builtins file: %s" % filename)
parsed_copyrights = set(self._parse_copyright_lines(text))
self._copyright_lines = self._copyright_lines.union(parsed_copyrights)
log.debug("Found copyright lines:")
for line in self._copyright_lines:
log.debug(line)
log.debug("")
object_annotations = self._parse_annotations(text)
object_name, ext = os.path.splitext(os.path.basename(filename))
log.debug("Parsing object: %s" % object_name)
parsed_functions = self._parse_functions(text)
for function in parsed_functions:
function.object = object_name
log.debug("Parsed functions:")
for func in parsed_functions:
log.debug(func)
log.debug("")
new_object = BuiltinObject(object_name, object_annotations, parsed_functions)
new_object.collection = self
self.objects.append(new_object)
def copyrights(self):
owner_to_years = dict()
copyrightYearRegExp = re.compile(r"(\d{4})[, ]{0,2}")
ownerStartRegExp = re.compile(r"[^\d, ]")
# Returns deduplicated copyrights keyed on the owner.
for line in self._copyright_lines:
years = set(copyrightYearRegExp.findall(line))
ownerIndex = ownerStartRegExp.search(line).start()
owner = line[ownerIndex:]
log.debug("Found years: %s and owner: %s" % (years, owner))
if owner not in owner_to_years:
owner_to_years[owner] = set()
owner_to_years[owner] = owner_to_years[owner].union(years)
result = []
for owner, years in list(owner_to_years.items()):
sorted_years = list(years)
sorted_years.sort()
result.append("%s %s" % (', '.join(sorted_years), owner))
return result
def all_functions(self):
result = []
for object in self.objects:
result.extend(object.functions)
result.sort()
return result
def all_internal_functions(self):
result = []
for object in [o for o in self.objects if 'internal' in o.annotations]:
result.extend(object.functions)
result.sort()
return result
# Private methods.
def _parse_copyright_lines(self, text):
licenseBlock = multilineCommentRegExp.findall(text)[0]
licenseBlock = licenseBlock[:licenseBlock.index("Redistribution")]
copyrightLines = [Templates.DefaultCopyright]
for line in licenseBlock.split("\n"):
line = line.replace("/*", "")
line = line.replace("*/", "")
line = line.replace("*", "")
line = line.replace("Copyright", "")
line = line.replace("copyright", "")
line = line.replace("(C)", "")
line = line.replace("(c)", "")
line = line.strip()
if len(line) == 0:
continue
copyrightLines.append(line)
return copyrightLines
def _parse_annotations(self, text):
annotations = {}
for match in keyValueAnnotationCommentRegExp.finditer(text):
(key, value) = match.group(1, 2)
log.debug("Found annotation: '%s' => '%s'" % (key, value))
if key in annotations:
raise ParseException("Duplicate annotation found: %s" % key)
annotations[key] = value
for match in flagAnnotationCommentRegExp.finditer(text):
key = match.group(1)
log.debug("Found annotation: '%s' => 'TRUE'" % key)
if key in annotations:
raise ParseException("Duplicate annotation found: %s" % key)
annotations[key] = True
return annotations
def _parse_functions(self, text):
text = multilineCommentRegExp.sub("/**/", singleLineCommentRegExp.sub("//\n", text))
matches = [func for func in functionHeadRegExp.finditer(text)]
functionBounds = []
start = 0
end = 0
for match in matches:
start = match.start()
if start < end:
continue
end = match.end()
while text[end] != '{':
end += 1
depth = 1
isEscapingCharacter = False
currentStringStartCharacter = None
while depth > 0:
end += 1
currentCharacter = text[end]
if isEscapingCharacter:
isEscapingCharacter = False
continue
if currentCharacter in "`'\"":
if currentStringStartCharacter is None:
currentStringStartCharacter = currentCharacter
elif currentCharacter == currentStringStartCharacter:
currentStringStartCharacter = None
continue
if currentCharacter == '\\' and currentStringStartCharacter is not None:
isEscapingCharacter = True
continue
# FIXME: <webkit.org/b/239817> Regular expressions containing unbalanced quotation marks (like the one
# found in `StringPrototype.js`'s `createHTML`) can confuse the state of tracking our being
# inside/outside a string. To work around this, just reset our string state at the end of each line
# (unless we are inside a template string). This will work unless a regular expression contains
# unbalanced curly brackets or a closing curly bracket we care about is on the same line as the earlier
# regular expression with an unbalanced quote.
if currentCharacter == '\n' and currentStringStartCharacter != '`':
currentStringStartCharacter = None
continue
if currentStringStartCharacter is not None:
continue
if currentCharacter == '{':
depth += 1
elif currentCharacter == '}':
depth -= 1
end += 1
functionBounds.append((start, end))
functionStrings = [text[start:end].strip() for (start, end) in functionBounds]
return list(map(BuiltinFunction.fromString, functionStrings))
|