| 1 | package Padre::Plugin::YAML::Syntax; | ||
| 2 | |||
| 3 | 2 | ▶ | use v5.10.1; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | # turn off experimental warnings | ||
| 8 | 2 | ▶ | no if $] > 5.017010, warnings => 'experimental::smartmatch'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 2 | ▶ | use English qw( -no_match_vars ); # Avoids reg-ex performance penalty |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use Padre::Logger; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use Padre::Task::Syntax (); | ||
| 13 | use Padre::Wx (); | ||
| 14 | use Try::Tiny; | ||
| 15 | |||
| 16 | our $VERSION = '0.10'; | ||
| 17 | use parent qw(Padre::Task::Syntax); | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | my $class = shift; | ||
| 21 | return $class->SUPER::new(@_); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub run { | ||
| 25 | my $self = shift; | ||
| 26 | |||
| 27 | # Pull the text off the task so we won't need to serialize | ||
| 28 | # it back up to the parent Wx thread at the end of the task. | ||
| 29 | my $text = delete $self->{text}; | ||
| 30 | |||
| 31 | # Get the syntax model object | ||
| 32 | $self->{model} = $self->syntax($text); | ||
| 33 | |||
| 34 | return 1; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub syntax { | ||
| 38 | my $self = shift; | ||
| 39 | my $text = shift; | ||
| 40 | |||
| 41 | TRACE("\n$text") if DEBUG; | ||
| 42 | |||
| 43 | try { | ||
| 44 | if ( $OSNAME =~ /Win32/i ) | ||
| 45 | { | ||
| 46 | require YAML; | ||
| 47 | YAML::Load($text); | ||
| 48 | } else { | ||
| 49 | require YAML::XS; | ||
| 50 | YAML::XS::Load($text); | ||
| 51 | } | ||
| 52 | # No errors... | ||
| 53 | return {}; | ||
| 54 | } | ||
| 55 | catch { | ||
| 56 | TRACE("\nInfo: from YAML::XS::Load:\n $_") if DEBUG; | ||
| 57 | if ( $OSNAME =~ /Win32/i ) { | ||
| 58 | # send errors to syantax panel | ||
| 59 | return $self->_parse_error_win32($_); | ||
| 60 | } else { | ||
| 61 | # send errors to syantax panel | ||
| 62 | return $self->_parse_error($_); | ||
| 63 | } | ||
| 64 | }; | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _parse_error { | ||
| 69 | my $self = shift; | ||
| 70 | my $error = shift; | ||
| 71 | |||
| 72 | my @issues = (); | ||
| 73 | my ( $type, $message, $code, $line, $column ) = ( | ||
| 74 | 'Error', | ||
| 75 | Wx::gettext('Unknown YAML error'), | ||
| 76 | undef, | ||
| 77 | 1 | ||
| 78 | ); | ||
| 79 | |||
| 80 | # from the following in scanner.c inside YAML::XS | ||
| 81 | foreach ( split '\n', $error ) { | ||
| 82 | when (/YAML::XS::Load (\w+)\: .+/) { | ||
| 83 | $type = $1; | ||
| 84 | } | ||
| 85 | when (/^\s+(block.+)/) { | ||
| 86 | $message = $1; | ||
| 87 | } | ||
| 88 | when (/^\s+(cannot.+)/) { | ||
| 89 | $message = $1; | ||
| 90 | } | ||
| 91 | when (/^\s+(could not.+)/) { | ||
| 92 | $message = $1; | ||
| 93 | } | ||
| 94 | when (/^\s+(did not.+)/) { | ||
| 95 | $message = $1; | ||
| 96 | } | ||
| 97 | when (/^\s+(found.+)/) { | ||
| 98 | $message = $1; | ||
| 99 | } | ||
| 100 | when (/^\s+(mapping.+)/) { | ||
| 101 | $message = $1; | ||
| 102 | } | ||
| 103 | when (/^\s+Code: (.+)/) { | ||
| 104 | $code = $1; | ||
| 105 | } | ||
| 106 | when (/line:\s(\d+), column:\s(\d+)/) { | ||
| 107 | $line = $1; | ||
| 108 | $column = $2; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | if (DEBUG) { | ||
| 113 | say "type = $type" if $type; | ||
| 114 | say "message = $message" if $message; | ||
| 115 | say "code = $code" if $code; | ||
| 116 | say "line = $line" if $line; | ||
| 117 | say "column = $column" if $column; | ||
| 118 | } | ||
| 119 | |||
| 120 | push @issues, | ||
| 121 | { | ||
| 122 | # YAML::XS dose not produce error codes, hence we can use defined or // | ||
| 123 | # message => $message . ( defined $code ? " ( $code )" : q{} ), | ||
| 124 | message => $message . ( $code // q{} ), | ||
| 125 | line => $line, | ||
| 126 | type => $type eq 'Error' ? 'F' : 'W', | ||
| 127 | file => $self->{filename}, | ||
| 128 | }; | ||
| 129 | |||
| 130 | return { | ||
| 131 | issues => \@issues, | ||
| 132 | stderr => $error, | ||
| 133 | }; | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | sub _parse_error_win32 { | ||
| 138 | my $self = shift; | ||
| 139 | my $error = shift; | ||
| 140 | |||
| 141 | my @issues = (); | ||
| 142 | my ( $type, $message, $code, $line ) = ( | ||
| 143 | 'Error', | ||
| 144 | Wx::gettext('Unknown YAML error'), | ||
| 145 | undef, | ||
| 146 | 1 | ||
| 147 | ); | ||
| 148 | for ( split '\n', $error ) { | ||
| 149 | if (/YAML (\w+)\: (.+)/) { | ||
| 150 | $type = $1; | ||
| 151 | $message = $2; | ||
| 152 | } elsif (/^\s+Code: (.+)/) { | ||
| 153 | $code = $1; | ||
| 154 | } elsif (/^\s+Line: (.+)/) { | ||
| 155 | $line = $1; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | push @issues, | ||
| 159 | { | ||
| 160 | message => $message . ( defined $code ? " ( $code )" : q{} ), | ||
| 161 | line => $line, | ||
| 162 | type => $type eq 'Error' ? 'F' : 'W', | ||
| 163 | file => $self->{filename}, | ||
| 164 | }; | ||
| 165 | |||
| 166 | return { | ||
| 167 | issues => \@issues, | ||
| 168 | stderr => $error, | ||
| 169 | } | ||
| 170 | |||
| 171 | } | ||
| 172 | |||
| 173 | 1; | ||
| 174 | |||
| 175 | __END__ | ||