def test_is_variation_between_nodes(self): variation = ast.WhitespaceVariation([ ast.SequencePattern([PatternConstructor.build_node_desc('newline')]), ast.SequencePattern([PatternConstructor.build_node_desc('space'), PatternConstructor.build_node_desc('newline')])]) root = ParseTreeConstructor.add_root_to_siblings('comment', 'newline', 'comment') present = self.matcher.is_variation_between_nodes(variation, root.value[0], root.value[2]) assert present root = ParseTreeConstructor.add_root_to_siblings('comment', 'space', 'newline', 'comment') present = self.matcher.is_variation_between_nodes(variation, root.value[0], root.value[3]) assert present
def test_is_start_of_sequence(self): node_list = [PatternConstructor.build_node_desc('newline'), PatternConstructor.build_node_desc('space')] seq = ast.SequencePattern(node_list) root = ParseTreeConstructor.add_root_to_siblings('newline', 'space', 'space') is_match, nodes = self.matcher.is_start_of_sequence(seq, root.value[0]) assert is_match assert len(nodes) == 1
def get_ignores(self, context): result = [] if not context.ignore: return result current_pattern = [] for i in range(1, len(context.ignore.children)): child = context.ignore.children[i] if self.is_terminal(child): result.append(ast.SequencePattern(current_pattern)) current_pattern = [] else: node = self.visitWhitespace_node(child) current_pattern.append(node) if current_pattern: result.append(ast.SequencePattern(current_pattern)) return result
def is_start_of_sequence(self, sequence, node): """ The method checks whether a sequence exists starting from a given node. No filter applied. Returns boolean """ assert node nodes = TreeWalker.get_next_siblings_including(node) is_match, rem_nodes = self._get_match_and_remainder( sequence.root, nodes) if not is_match: return False, None rem_desc = self._get_remaining_desc(sequence.nodes, sequence.root) if not rem_desc: return True, rem_nodes return self.is_start_of_sequence(ast.SequencePattern(rem_desc), rem_nodes[0])
def _is_sequence_exact_nodes_match(self, sequence, nodes): """ The method checks whether the given nodes match exactly the given sequence Returns boolean """ assert nodes is_match, rem_nodes = self._get_match_and_remainder( sequence.root, nodes) if not is_match: return False rem_desc = [ item for item in sequence.nodes if item not in [sequence.root] ] if not rem_desc: return not rem_nodes s = ast.SequencePattern(rem_desc) return self._is_sequence_exact_nodes_match(s, rem_nodes)
def test_is_sequence_exact_nodes_match_more(self): node_list = [PatternConstructor.build_node_desc('newline'), PatternConstructor.build_node_desc('space')] seq = ast.SequencePattern(node_list) is_match = self.matcher._is_sequence_exact_nodes_match(seq, [self.node0, self.node1, self.node1]) assert not is_match
def test_is_sequence_exact_nodes_match_multi_two(self): rep_node = PatternConstructor.build_seq_desc_type('newline', ast.Repeater(lower=2, upper=3)) node_list = [PatternConstructor.build_node_desc('space'), rep_node] seq = ast.SequencePattern(node_list) is_match = self.matcher._is_sequence_exact_nodes_match(seq, [self.node1, self.node0, self.node0]) assert is_match
def visitWhitespace_variation(self, context): sequences = [] for i in range(0, len(context.children), 2): descriptor = self.visitWhitespace_node(context.children[i]) sequences.append(ast.SequencePattern([descriptor])) return ast.WhitespaceVariation(sequences)
def build_next_pattern(self, wrappers): relations = ast.NodeRelations() for i in range(1, len(wrappers)): relations.register_relation(wrappers[i-1], ast.IsPreviousSiblingOf(wrappers[i])) return ast.SequencePattern(wrappers)
def _get_sequence(node_type): return ast.SequencePattern( [ast.Node(ast.NodeTypeDescriptor.build_type(type_=node_type))])