rana-cli/wakatime/dependencies/jvm.py

192 lines
5.4 KiB
Python
Raw Normal View History

2014-12-23 05:47:24 +00:00
# -*- coding: utf-8 -*-
"""
2018-03-11 20:29:30 +00:00
wakatime.dependencies.java
~~~~~~~~~~~~~~~~~~~~~~~~~~
2014-12-23 05:47:24 +00:00
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
2018-03-12 01:09:25 +00:00
2018-03-13 16:24:37 +00:00
class KotlinParser(TokenParser):
state = None
exclude = [
r'^java\.',
]
def parse(self):
for index, token, content in self.tokens:
self._process_token(token, content)
return self.dependencies
def _process_token(self, token, content):
if self.partial(token) == 'Keyword':
self._process_keyword(token, content)
elif self.partial(token) == 'Text':
self._process_text(token, content)
elif self.partial(token) == 'Namespace':
self._process_namespace(token, content)
else:
self._process_other(token, content)
def _process_keyword(self, token, content):
self.state = content
def _process_text(self, token, content):
pass
def _process_namespace(self, token, content):
if self.state == 'import':
self.append(self._format(content))
self.state = None
def _process_other(self, token, content):
self.state = None
def _format(self, content):
content = content.split(u('.'))
if len(content) == 0:
return None
if len(content) == 1:
return content[0]
if len(content[0]) == 3:
content = content[1:]
if content[-1] == u('*'):
content = content[:len(content) - 1]
if len(content) == 0:
return None
elif len(content) == 1:
content = content[0]
elif len(content) > 1:
content = u('.').join(content[:2])
return content
2018-03-12 01:09:25 +00:00
class ScalaParser(TokenParser):
state = None
def parse(self):
for index, token, content in self.tokens:
self._process_token(token, content)
return self.dependencies
def _process_token(self, token, content):
if self.partial(token) == 'Keyword':
self._process_keyword(token, content)
elif self.partial(token) == 'Text':
self._process_text(token, content)
elif self.partial(token) == 'Namespace':
self._process_namespace(token, content)
else:
self._process_other(token, content)
def _process_keyword(self, token, content):
self.state = content
def _process_text(self, token, content):
pass
def _process_namespace(self, token, content):
if self.state == 'import':
self.append(self._format(content))
self.state = None
def _process_other(self, token, content):
self.state = None
def _format(self, content):
return content.strip().lstrip('__root__').strip('_').strip('.')