2014-12-23 11:05:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2018-03-11 20:29:30 +00:00
|
|
|
wakatime.dependencies.php
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
2014-12-23 11:05:28 +00:00
|
|
|
|
|
|
|
Parse dependencies from PHP code.
|
|
|
|
|
|
|
|
:copyright: (c) 2014 Alan Hamlett.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from . import TokenParser
|
|
|
|
from ..compat import u
|
|
|
|
|
|
|
|
|
|
|
|
class PhpParser(TokenParser):
|
|
|
|
state = None
|
|
|
|
parens = 0
|
2018-03-10 21:15:01 +00:00
|
|
|
exclude = [
|
|
|
|
r'^app$',
|
2018-03-10 21:19:13 +00:00
|
|
|
r'app\.php$',
|
2018-03-10 21:15:01 +00:00
|
|
|
]
|
2014-12-23 11:05:28 +00:00
|
|
|
|
2015-09-26 20:04:35 +00:00
|
|
|
def parse(self):
|
2014-12-23 11:05:28 +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) == 'Keyword':
|
2014-12-23 11:05:28 +00:00
|
|
|
self._process_keyword(token, content)
|
|
|
|
elif u(token) == 'Token.Literal.String.Single' or u(token) == 'Token.Literal.String.Double':
|
|
|
|
self._process_literal_string(token, content)
|
2014-12-23 18:40:10 +00:00
|
|
|
elif u(token) == 'Token.Name.Other':
|
|
|
|
self._process_name(token, content)
|
|
|
|
elif u(token) == 'Token.Name.Function':
|
|
|
|
self._process_function(token, content)
|
2015-09-16 21:59:30 +00:00
|
|
|
elif self.partial(token) == 'Punctuation':
|
2014-12-23 11:05:28 +00:00
|
|
|
self._process_punctuation(token, content)
|
2015-09-16 21:59:30 +00:00
|
|
|
elif self.partial(token) == 'Text':
|
2014-12-23 11:05:28 +00:00
|
|
|
self._process_text(token, content)
|
|
|
|
else:
|
|
|
|
self._process_other(token, content)
|
|
|
|
|
2014-12-23 18:40:10 +00:00
|
|
|
def _process_name(self, token, content):
|
|
|
|
if self.state == 'use':
|
2014-12-25 05:09:15 +00:00
|
|
|
self.append(content, truncate=True, separator=u("\\"))
|
2014-12-23 18:40:10 +00:00
|
|
|
|
|
|
|
def _process_function(self, token, content):
|
|
|
|
if self.state == 'use function':
|
2014-12-25 05:09:15 +00:00
|
|
|
self.append(content, truncate=True, separator=u("\\"))
|
2014-12-23 18:40:10 +00:00
|
|
|
self.state = 'use'
|
|
|
|
|
2014-12-23 11:05:28 +00:00
|
|
|
def _process_keyword(self, token, content):
|
|
|
|
if content == 'include' or content == 'include_once' or content == 'require' or content == 'require_once':
|
|
|
|
self.state = 'include'
|
2014-12-23 18:40:10 +00:00
|
|
|
elif content == 'use':
|
|
|
|
self.state = 'use'
|
|
|
|
elif content == 'as':
|
|
|
|
self.state = 'as'
|
|
|
|
elif self.state == 'use' and content == 'function':
|
|
|
|
self.state = 'use function'
|
2014-12-23 11:05:28 +00:00
|
|
|
else:
|
|
|
|
self.state = None
|
|
|
|
|
|
|
|
def _process_literal_string(self, token, content):
|
|
|
|
if self.state == 'include':
|
2015-09-30 05:00:17 +00:00
|
|
|
if content != '"' and content != "'":
|
2014-12-23 11:05:28 +00:00
|
|
|
content = content.strip()
|
2014-12-23 18:40:10 +00:00
|
|
|
if u(token) == 'Token.Literal.String.Double':
|
2015-09-30 05:00:17 +00:00
|
|
|
content = u("'{0}'").format(content)
|
2014-12-25 05:09:15 +00:00
|
|
|
self.append(content)
|
2014-12-23 11:05:28 +00:00
|
|
|
self.state = None
|
|
|
|
|
|
|
|
def _process_punctuation(self, token, content):
|
|
|
|
if content == '(':
|
|
|
|
self.parens += 1
|
|
|
|
elif content == ')':
|
|
|
|
self.parens -= 1
|
2014-12-23 18:40:10 +00:00
|
|
|
elif (self.state == 'use' or self.state == 'as') and content == ',':
|
|
|
|
self.state = 'use'
|
2014-12-23 11:05:28 +00:00
|
|
|
else:
|
|
|
|
self.state = None
|
|
|
|
|
|
|
|
def _process_text(self, token, content):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _process_other(self, token, content):
|
|
|
|
self.state = None
|