You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
12 KiB

6 years ago
  1. """SCons.Tool.JavaCommon
  2. Stuff for processing Java.
  3. """
  4. #
  5. # Copyright (c) 2001 - 2017 The SCons Foundation
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining
  8. # a copy of this software and associated documentation files (the
  9. # "Software"), to deal in the Software without restriction, including
  10. # without limitation the rights to use, copy, modify, merge, publish,
  11. # distribute, sublicense, and/or sell copies of the Software, and to
  12. # permit persons to whom the Software is furnished to do so, subject to
  13. # the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included
  16. # in all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  19. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  20. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. #
  26. __revision__ = "src/engine/SCons/Tool/JavaCommon.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  27. import os
  28. import os.path
  29. import re
  30. java_parsing = 1
  31. default_java_version = '1.4'
  32. if java_parsing:
  33. # Parse Java files for class names.
  34. #
  35. # This is a really cool parser from Charles Crain
  36. # that finds appropriate class names in Java source.
  37. # A regular expression that will find, in a java file:
  38. # newlines;
  39. # double-backslashes;
  40. # a single-line comment "//";
  41. # single or double quotes preceeded by a backslash;
  42. # single quotes, double quotes, open or close braces, semi-colons,
  43. # periods, open or close parentheses;
  44. # floating-point numbers;
  45. # any alphanumeric token (keyword, class name, specifier);
  46. # any alphanumeric token surrounded by angle brackets (generics);
  47. # the multi-line comment begin and end tokens /* and */;
  48. # array declarations "[]".
  49. _reToken = re.compile(r'(\n|\\\\|//|\\[\'"]|[\'"\{\}\;\.\(\)]|' +
  50. r'\d*\.\d*|[A-Za-z_][\w\$\.]*|<[A-Za-z_]\w+>|' +
  51. r'/\*|\*/|\[\])')
  52. class OuterState(object):
  53. """The initial state for parsing a Java file for classes,
  54. interfaces, and anonymous inner classes."""
  55. def __init__(self, version=default_java_version):
  56. if not version in ('1.1', '1.2', '1.3','1.4', '1.5', '1.6', '1.7',
  57. '1.8', '5', '6'):
  58. msg = "Java version %s not supported" % version
  59. raise NotImplementedError(msg)
  60. self.version = version
  61. self.listClasses = []
  62. self.listOutputs = []
  63. self.stackBrackets = []
  64. self.brackets = 0
  65. self.nextAnon = 1
  66. self.localClasses = []
  67. self.stackAnonClassBrackets = []
  68. self.anonStacksStack = [[0]]
  69. self.package = None
  70. def trace(self):
  71. pass
  72. def __getClassState(self):
  73. try:
  74. return self.classState
  75. except AttributeError:
  76. ret = ClassState(self)
  77. self.classState = ret
  78. return ret
  79. def __getPackageState(self):
  80. try:
  81. return self.packageState
  82. except AttributeError:
  83. ret = PackageState(self)
  84. self.packageState = ret
  85. return ret
  86. def __getAnonClassState(self):
  87. try:
  88. return self.anonState
  89. except AttributeError:
  90. self.outer_state = self
  91. ret = SkipState(1, AnonClassState(self))
  92. self.anonState = ret
  93. return ret
  94. def __getSkipState(self):
  95. try:
  96. return self.skipState
  97. except AttributeError:
  98. ret = SkipState(1, self)
  99. self.skipState = ret
  100. return ret
  101. def __getAnonStack(self):
  102. return self.anonStacksStack[-1]
  103. def openBracket(self):
  104. self.brackets = self.brackets + 1
  105. def closeBracket(self):
  106. self.brackets = self.brackets - 1
  107. if len(self.stackBrackets) and \
  108. self.brackets == self.stackBrackets[-1]:
  109. self.listOutputs.append('$'.join(self.listClasses))
  110. self.localClasses.pop()
  111. self.listClasses.pop()
  112. self.anonStacksStack.pop()
  113. self.stackBrackets.pop()
  114. if len(self.stackAnonClassBrackets) and \
  115. self.brackets == self.stackAnonClassBrackets[-1]:
  116. self.__getAnonStack().pop()
  117. self.stackAnonClassBrackets.pop()
  118. def parseToken(self, token):
  119. if token[:2] == '//':
  120. return IgnoreState('\n', self)
  121. elif token == '/*':
  122. return IgnoreState('*/', self)
  123. elif token == '{':
  124. self.openBracket()
  125. elif token == '}':
  126. self.closeBracket()
  127. elif token in [ '"', "'" ]:
  128. return IgnoreState(token, self)
  129. elif token == "new":
  130. # anonymous inner class
  131. if len(self.listClasses) > 0:
  132. return self.__getAnonClassState()
  133. return self.__getSkipState() # Skip the class name
  134. elif token in ['class', 'interface', 'enum']:
  135. if len(self.listClasses) == 0:
  136. self.nextAnon = 1
  137. self.stackBrackets.append(self.brackets)
  138. return self.__getClassState()
  139. elif token == 'package':
  140. return self.__getPackageState()
  141. elif token == '.':
  142. # Skip the attribute, it might be named "class", in which
  143. # case we don't want to treat the following token as
  144. # an inner class name...
  145. return self.__getSkipState()
  146. return self
  147. def addAnonClass(self):
  148. """Add an anonymous inner class"""
  149. if self.version in ('1.1', '1.2', '1.3', '1.4'):
  150. clazz = self.listClasses[0]
  151. self.listOutputs.append('%s$%d' % (clazz, self.nextAnon))
  152. elif self.version in ('1.5', '1.6', '1.7', '1.8', '5', '6'):
  153. self.stackAnonClassBrackets.append(self.brackets)
  154. className = []
  155. className.extend(self.listClasses)
  156. self.__getAnonStack()[-1] = self.__getAnonStack()[-1] + 1
  157. for anon in self.__getAnonStack():
  158. className.append(str(anon))
  159. self.listOutputs.append('$'.join(className))
  160. self.nextAnon = self.nextAnon + 1
  161. self.__getAnonStack().append(0)
  162. def setPackage(self, package):
  163. self.package = package
  164. class AnonClassState(object):
  165. """A state that looks for anonymous inner classes."""
  166. def __init__(self, old_state):
  167. # outer_state is always an instance of OuterState
  168. self.outer_state = old_state.outer_state
  169. self.old_state = old_state
  170. self.brace_level = 0
  171. def parseToken(self, token):
  172. # This is an anonymous class if and only if the next
  173. # non-whitespace token is a bracket. Everything between
  174. # braces should be parsed as normal java code.
  175. if token[:2] == '//':
  176. return IgnoreState('\n', self)
  177. elif token == '/*':
  178. return IgnoreState('*/', self)
  179. elif token == '\n':
  180. return self
  181. elif token[0] == '<' and token[-1] == '>':
  182. return self
  183. elif token == '(':
  184. self.brace_level = self.brace_level + 1
  185. return self
  186. if self.brace_level > 0:
  187. if token == 'new':
  188. # look further for anonymous inner class
  189. return SkipState(1, AnonClassState(self))
  190. elif token in [ '"', "'" ]:
  191. return IgnoreState(token, self)
  192. elif token == ')':
  193. self.brace_level = self.brace_level - 1
  194. return self
  195. if token == '{':
  196. self.outer_state.addAnonClass()
  197. return self.old_state.parseToken(token)
  198. class SkipState(object):
  199. """A state that will skip a specified number of tokens before
  200. reverting to the previous state."""
  201. def __init__(self, tokens_to_skip, old_state):
  202. self.tokens_to_skip = tokens_to_skip
  203. self.old_state = old_state
  204. def parseToken(self, token):
  205. self.tokens_to_skip = self.tokens_to_skip - 1
  206. if self.tokens_to_skip < 1:
  207. return self.old_state
  208. return self
  209. class ClassState(object):
  210. """A state we go into when we hit a class or interface keyword."""
  211. def __init__(self, outer_state):
  212. # outer_state is always an instance of OuterState
  213. self.outer_state = outer_state
  214. def parseToken(self, token):
  215. # the next non-whitespace token should be the name of the class
  216. if token == '\n':
  217. return self
  218. # If that's an inner class which is declared in a method, it
  219. # requires an index prepended to the class-name, e.g.
  220. # 'Foo$1Inner'
  221. # http://scons.tigris.org/issues/show_bug.cgi?id=2087
  222. if self.outer_state.localClasses and \
  223. self.outer_state.stackBrackets[-1] > \
  224. self.outer_state.stackBrackets[-2]+1:
  225. locals = self.outer_state.localClasses[-1]
  226. try:
  227. idx = locals[token]
  228. locals[token] = locals[token]+1
  229. except KeyError:
  230. locals[token] = 1
  231. token = str(locals[token]) + token
  232. self.outer_state.localClasses.append({})
  233. self.outer_state.listClasses.append(token)
  234. self.outer_state.anonStacksStack.append([0])
  235. return self.outer_state
  236. class IgnoreState(object):
  237. """A state that will ignore all tokens until it gets to a
  238. specified token."""
  239. def __init__(self, ignore_until, old_state):
  240. self.ignore_until = ignore_until
  241. self.old_state = old_state
  242. def parseToken(self, token):
  243. if self.ignore_until == token:
  244. return self.old_state
  245. return self
  246. class PackageState(object):
  247. """The state we enter when we encounter the package keyword.
  248. We assume the next token will be the package name."""
  249. def __init__(self, outer_state):
  250. # outer_state is always an instance of OuterState
  251. self.outer_state = outer_state
  252. def parseToken(self, token):
  253. self.outer_state.setPackage(token)
  254. return self.outer_state
  255. def parse_java_file(fn, version=default_java_version):
  256. return parse_java(open(fn, 'r').read(), version)
  257. def parse_java(contents, version=default_java_version, trace=None):
  258. """Parse a .java file and return a double of package directory,
  259. plus a list of .class files that compiling that .java file will
  260. produce"""
  261. package = None
  262. initial = OuterState(version)
  263. currstate = initial
  264. for token in _reToken.findall(contents):
  265. # The regex produces a bunch of groups, but only one will
  266. # have anything in it.
  267. currstate = currstate.parseToken(token)
  268. if trace: trace(token, currstate)
  269. if initial.package:
  270. package = initial.package.replace('.', os.sep)
  271. return (package, initial.listOutputs)
  272. else:
  273. # Don't actually parse Java files for class names.
  274. #
  275. # We might make this a configurable option in the future if
  276. # Java-file parsing takes too long (although it shouldn't relative
  277. # to how long the Java compiler itself seems to take...).
  278. def parse_java_file(fn):
  279. """ "Parse" a .java file.
  280. This actually just splits the file name, so the assumption here
  281. is that the file name matches the public class name, and that
  282. the path to the file is the same as the package name.
  283. """
  284. return os.path.split(file)
  285. # Local Variables:
  286. # tab-width:4
  287. # indent-tabs-mode:nil
  288. # End:
  289. # vim: set expandtab tabstop=4 shiftwidth=4: