def handle_array(prefix, nodes, lexer): for item in nodes: assert isinstance(item, ArrayElement) # Skip empty keys if item.key == '': continue if isinstance(item.key, BinaryOp): name = '\'{0}\''.format(concatenate(item.key)) elif isinstance(item.key, (int, float)): name = '{0}'.format(item.key) else: name = '\'{0}\''.format(item.key) if prefix: name = '{0}->{1}'.format(prefix, name) if isinstance(item.value, Array): handle_array(name, item.value.nodes, lexer) elif isinstance(item.value, six.string_types): self.create_and_add_unit( name, item.value, lexer.extract_quote(), lexer.extract_comments(item.lexpositions[1]), )
def handle_array(prefix, nodes, lexer): prefix += lexer.extract_array() for item in nodes: assert isinstance(item, ArrayElement) # Skip empty keys if item.key == '': continue if isinstance(item.key, BinaryOp): name = '\'{0}\''.format(concatenate(item.key)) elif isinstance(item.key, (int, float)): name = '{0}'.format(item.key) else: name = '\'{0}\''.format(item.key) if prefix: name = '{0}->{1}'.format(prefix, name) if isinstance(item.value, Array): handle_array(name, item.value.nodes, lexer) elif isinstance(item.value, six.string_types): self.create_and_add_unit( name, item.value, lexer.extract_quote(), lexer.extract_comments(item.lexpositions[1]), )
def parse(self, phpsrc): """Read the source of a PHP file in and include them as units.""" def handle_array(prefix, nodes, lexer): prefix += lexer.extract_array() for item in nodes: assert isinstance(item, ArrayElement) # Skip empty keys if item.key == '': continue if isinstance(item.key, BinaryOp): name = '\'{0}\''.format(concatenate(item.key)) elif isinstance(item.key, (int, float)): name = '{0}'.format(item.key) else: name = '\'{0}\''.format(item.key) if prefix: name = '{0}->{1}'.format(prefix, name) if isinstance(item.value, Array): handle_array(name, item.value.nodes, lexer) elif isinstance(item.value, six.string_types): self.create_and_add_unit( name, item.value, lexer.extract_quote(), lexer.extract_comments(item.lexpositions[1]), ) def concatenate(item): if isinstance(item, six.string_types): return item elif isinstance(item, Variable): return item.name assert isinstance(item, BinaryOp) return concatenate(item.left) + concatenate(item.right) parser = make_parser() for item in parser.productions: item.callable = wrap_production(item.callable) lexer = PHPLexer() tree = parser.parse(phpsrc.decode(self.encoding), lexer=lexer, tracking=True) # Handle text without PHP start if len(tree) == 1 and isinstance(tree[0], InlineHTML): return self.parse(b'<?php\n' + phpsrc) for item in tree: if isinstance(item, FunctionCall): if item.name == 'define': self.create_and_add_unit( lexer.extract_name('COMMA', *item.lexpositions), item.params[1].node, lexer.extract_quote(), lexer.extract_comments(item.lexpositions[1]), ) elif isinstance(item, Assignment): if isinstance(item.node, ArrayOffset): name = lexer.extract_name('EQUALS', *item.lexpositions) if isinstance(item.expr, six.string_types): self.create_and_add_unit( name, item.expr, lexer.extract_quote(), lexer.extract_comments(item.lexpositions[1]), ) elif isinstance(item.expr, BinaryOp) and item.expr.op == '.': self.create_and_add_unit( name, concatenate(item.expr), lexer.extract_quote(), lexer.extract_comments(item.lexpositions[1]), ) elif isinstance(item.node, Variable): name = lexer.extract_name('EQUALS', *item.lexpositions) if isinstance(item.expr, Array): handle_array(name, item.expr.nodes, lexer) elif isinstance(item.expr, six.string_types): self.create_and_add_unit( name, item.expr, lexer.extract_quote(), lexer.extract_comments(item.lexpositions[1]), ) elif isinstance(item.expr, BinaryOp) and item.expr.op == '.': self.create_and_add_unit( name, concatenate(item.expr), lexer.extract_quote(), lexer.extract_comments(item.lexpositions[1]), ) elif isinstance(item, Return): if isinstance(item.node, Array): handle_array('return', item.node.nodes, lexer)