parse use namespaces from php files

This commit is contained in:
Alan Hamlett 2014-12-23 12:40:10 -06:00
parent 0ec8240725
commit 34c92951e0

View file

@ -29,6 +29,10 @@ class PhpParser(TokenParser):
self._process_keyword(token, content) self._process_keyword(token, content)
elif u(token) == 'Token.Literal.String.Single' or u(token) == 'Token.Literal.String.Double': elif u(token) == 'Token.Literal.String.Single' or u(token) == 'Token.Literal.String.Double':
self._process_literal_string(token, content) self._process_literal_string(token, content)
elif u(token) == 'Token.Name.Other':
self._process_name(token, content)
elif u(token) == 'Token.Name.Function':
self._process_function(token, content)
elif u(token).split('.')[-1] == 'Punctuation': elif u(token).split('.')[-1] == 'Punctuation':
self._process_punctuation(token, content) self._process_punctuation(token, content)
elif u(token).split('.')[-1] == 'Text': elif u(token).split('.')[-1] == 'Text':
@ -36,9 +40,28 @@ class PhpParser(TokenParser):
else: else:
self._process_other(token, content) self._process_other(token, content)
def _process_name(self, token, content):
if self.state == 'use':
content = content.split("\\")
content = content[0] if len(content) == 1 else "\\".join(content[0:len(content)-1])
self.append(content, truncate=False)
def _process_function(self, token, content):
if self.state == 'use function':
content = content.split("\\")
content = content[0] if len(content) == 1 else "\\".join(content[0:len(content)-1])
self.append(content, truncate=False)
self.state = 'use'
def _process_keyword(self, token, content): def _process_keyword(self, token, content):
if content == 'include' or content == 'include_once' or content == 'require' or content == 'require_once': if content == 'include' or content == 'include_once' or content == 'require' or content == 'require_once':
self.state = 'include' self.state = 'include'
elif content == 'use':
self.state = 'use'
elif content == 'as':
self.state = 'as'
elif self.state == 'use' and content == 'function':
self.state = 'use function'
else: else:
self.state = None self.state = None
@ -46,8 +69,8 @@ class PhpParser(TokenParser):
if self.state == 'include': if self.state == 'include':
if content != '"': if content != '"':
content = content.strip() content = content.strip()
if u(token) == 'Token.Literal.String.Single': if u(token) == 'Token.Literal.String.Double':
content = content.strip("'") content = u('"{0}"').format(content)
self.append(content, truncate=False) self.append(content, truncate=False)
self.state = None self.state = None
@ -56,6 +79,8 @@ class PhpParser(TokenParser):
self.parens += 1 self.parens += 1
elif content == ')': elif content == ')':
self.parens -= 1 self.parens -= 1
elif (self.state == 'use' or self.state == 'as') and content == ',':
self.state = 'use'
else: else:
self.state = None self.state = None