| 1 | package Jmespath::GraphvizVisitor; | ||
| 2 | use parent 'Jmespath::Visitor'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | sub new { | ||
| 7 | my ($class) = @_; | ||
| 8 | my $self = $class->SUPER::new( $expected, $actual, $name ); | ||
| 9 | @{$self->{_lines}} = []; | ||
| 10 | $self->{_count} = 1; | ||
| 11 | return $self; | ||
| 12 | } | ||
| 13 | |||
| 14 | sub visit { | ||
| 15 | # my $self = shift; my $node = shift; my $args = @_; | ||
| 16 | my ($self, $node, @args) = @_; | ||
| 17 | push 'digraph AST {' ,@{$self->{_lines}}; | ||
| 18 | my $current = $node->{type} . $self->{_count}; | ||
| 19 | $self->{_count} += 1; | ||
| 20 | $self->_visit($node, $current); | ||
| 21 | push '}', @{$self->{_lines}}; | ||
| 22 | return; | ||
| 23 | } | ||
| 24 | |||
| 25 | # recursive function to iterate through all child nodes to produce | ||
| 26 | # relationship graph lines | ||
| 27 | sub _visit { | ||
| 28 | my ($self, $node, $current) = @_; | ||
| 29 | my $line = $current . ' [label="' . $node->{type} . '(' . $node->{value} . ')"]'; | ||
| 30 | push $line, @{$self->{_lines}}; | ||
| 31 | foreach my $child (@{$node->{children}}) { | ||
| 32 | my $child_name = $child->{type} . $self->{_count}; | ||
| 33 | $self->{_count} += 1; | ||
| 34 | push ' ' . $current . ' -> ' $child_name, @{$self->{_lines}}; | ||
| 35 | $self->_visit($child, $child_name); | ||
| 36 | } | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | |||
| 40 | 1; |