rana-cli/wakatime/dependencies/jvm.py

97 lines
2.8 KiB
Python
Raw Normal View History

2014-12-23 05:47:24 +00:00
# -*- coding: utf-8 -*-
"""
wakatime.languages.java
~~~~~~~~~~~~~~~~~~~~~~~
Parse dependencies from Java code.
2014-12-23 07:24:48 +00:00
:copyright: (c) 2014 Alan Hamlett.
2014-12-23 05:47:24 +00:00
:license: BSD, see LICENSE for more details.
"""
from . import TokenParser
from ..compat import u
class JavaParser(TokenParser):
2015-09-16 21:59:30 +00:00
exclude = [
r'^java\.',
r'^javax\.',
r'^import$',
r'^package$',
r'^namespace$',
r'^static$',
]
state = None
buffer = u('')
2014-12-23 05:47:24 +00:00
def parse(self):
2014-12-23 05:47:24 +00:00
for index, token, content in self.tokens:
self._process_token(token, content)
return self.dependencies
def _process_token(self, token, content):
2015-09-16 21:59:30 +00:00
if self.partial(token) == 'Namespace':
2014-12-23 05:47:24 +00:00
self._process_namespace(token, content)
2015-09-16 21:59:30 +00:00
if self.partial(token) == 'Name':
self._process_name(token, content)
elif self.partial(token) == 'Attribute':
self._process_attribute(token, content)
elif self.partial(token) == 'Operator':
self._process_operator(token, content)
2014-12-23 05:47:24 +00:00
else:
self._process_other(token, content)
def _process_namespace(self, token, content):
2017-11-23 17:48:14 +00:00
if u(content).split() and u(content).split()[0] == u('import'):
2015-09-16 21:59:30 +00:00
self.state = 'import'
elif self.state == 'import':
keywords = [
u('package'),
u('namespace'),
u('static'),
]
if u(content) in keywords:
return
self.buffer = u('{0}{1}').format(self.buffer, u(content))
elif self.state == 'import-finished':
content = content.split(u('.'))
if len(content) == 1:
self.append(content[0])
elif len(content) > 1:
if len(content[0]) == 3:
content = content[1:]
if content[-1] == u('*'):
content = content[:len(content) - 1]
if len(content) == 1:
self.append(content[0])
elif len(content) > 1:
self.append(u('.').join(content[:2]))
self.state = None
def _process_name(self, token, content):
if self.state == 'import':
self.buffer = u('{0}{1}').format(self.buffer, u(content))
def _process_attribute(self, token, content):
if self.state == 'import':
self.buffer = u('{0}{1}').format(self.buffer, u(content))
def _process_operator(self, token, content):
if u(content) == u(';'):
self.state = 'import-finished'
self._process_namespace(token, self.buffer)
self.state = None
self.buffer = u('')
elif self.state == 'import':
self.buffer = u('{0}{1}').format(self.buffer, u(content))
2014-12-23 05:47:24 +00:00
def _process_other(self, token, content):
pass