| 1 | package Data::Sah::Compiler::js; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $DATE = '2016-09-14'; # DATE | |||||||||
| 4 | 1 | our $VERSION = '0.87'; # VERSION | |||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | #use Log::Any '$log'; | ||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use Mo qw(build default); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use String::Indent (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | extends 'Data::Sah::Compiler::Prog'; | |||||||||
| 15 | |||||||||||
| 16 | 0 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 17 | 0 | my ($self, $args) = @_; | |||||||||
| 18 | |||||||||||
| 19 | 0 | $self->comment_style('cpp'); | |||||||||
| 20 | 0 | $self->indent_character(" " x 4); | |||||||||
| 21 | 0 | $self->var_sigil(""); | |||||||||
| 22 | 0 | $self->concat_op("+"); | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 0 | ▶ | sub name { "js" } | ||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | sub expr { | ||||||||
|
Subroutine
expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 28 | 0 | my ($self, $expr) = @_; | |||||||||
| 29 | 0 | $self->expr_compiler->js($expr); | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | sub literal { | ||||||||
|
Subroutine
literal: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 33 | 0 | my ($self, $val) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 0 | state $json = do { | |||||||||
| 36 | 0 | require JSON::MaybeXS; | |||||||||
| 37 | 0 | JSON::MaybeXS->new->allow_nonref; | |||||||||
| 38 | }; | ||||||||||
| 39 | |||||||||||
| 40 | # we need cleaning since json can't handle qr//, for one. | ||||||||||
| 41 | 0 | state $cleanser = do { | |||||||||
| 42 | 0 | require Data::Clean::JSON; | |||||||||
| 43 | 0 | Data::Clean::JSON->get_cleanser; | |||||||||
| 44 | }; | ||||||||||
| 45 | |||||||||||
| 46 | 0 | $json->encode($cleanser->clone_and_clean($val)); | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | sub compile { | ||||||||
|
Subroutine
compile: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 50 | 0 | my ($self, %args) = @_; | |||||||||
| 51 | |||||||||||
| 52 | #$self->expr_compiler->compiler->hook_var( | ||||||||||
| 53 | # ... | ||||||||||
| 54 | #); | ||||||||||
| 55 | |||||||||||
| 56 | #$self->expr_compiler->compiler->hook_func( | ||||||||||
| 57 | # ... | ||||||||||
| 58 | #); | ||||||||||
| 59 | |||||||||||
| 60 | 0 | $self->SUPER::compile(%args); | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | sub true { "true" } | ||||||||
|
Subroutine
true: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 64 | |||||||||||
| 65 | 0 | ▶ | sub false { "false" } | ||||||||
|
Subroutine
false: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub expr_defined { | ||||||||
|
Subroutine
expr_defined: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 68 | 0 | my ($self, $t) = @_; | |||||||||
| 69 | 0 | "!($t === undefined || $t === null)"; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | sub expr_array { | ||||||||
|
Subroutine
expr_array: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 73 | 0 | my ($self, @t) = @_; | |||||||||
| 74 | 0 | "[".join(",", @t)."]"; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | sub expr_array_subscript { | ||||||||
|
Subroutine
expr_array_subscript: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 78 | 0 | my ($self, $at, $idxt) = @_; | |||||||||
| 79 | 0 | "$at\[$idxt]"; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 0 | ▶ | sub expr_last_elem { | ||||||||
|
Subroutine
expr_last_elem: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 83 | 0 | my ($self, $at, $idxt) = @_; | |||||||||
| 84 | 0 | "$at\[($at).length-1]"; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub expr_array_0_nmin1 { | ||||||||
|
Subroutine
expr_array_0_nmin1: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 88 | 0 | my ($self, $n) = @_; | |||||||||
| 89 | 0 | "Array($n).join().split(',').map(function(e,i){return i})"; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | sub expr_array_1_n { | ||||||||
|
Subroutine
expr_array_1_n: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 93 | 0 | my ($self, $n) = @_; | |||||||||
| 94 | 0 | "Array($n).join().split(',').map(function(e,i){return i+1})"; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub expr_push { | ||||||||
|
Subroutine
expr_push: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 98 | 0 | my ($self, $at, $elt) = @_; | |||||||||
| 99 | 0 | "($at).push($elt)"; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | sub expr_pop { | ||||||||
|
Subroutine
expr_pop: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 103 | 0 | my ($self, $at, $elt) = @_; | |||||||||
| 104 | 0 | "($at).pop()"; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 0 | ▶ | sub expr_push_and_pop_dpath_between_expr { | ||||||||
|
Subroutine
expr_push_and_pop_dpath_between_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 108 | 0 | my ($self, $et) = @_; | |||||||||
| 109 | 0 | join( | |||||||||
| 110 | "", | ||||||||||
| 111 | "[", | ||||||||||
| 112 | $self->expr_push('_sahv_dpath', $self->literal(undef)), ", ", # 0 | ||||||||||
| 113 | $self->enclose_paren($et), ", ", #1 | ||||||||||
| 114 | $self->expr_pop('_sahv_dpath'), # 2 | ||||||||||
| 115 | "][1]", | ||||||||||
| 116 | ); | ||||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 0 | ▶ | sub expr_prefix_dpath { | ||||||||
|
Subroutine
expr_prefix_dpath: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 120 | 0 | my ($self, $t) = @_; | |||||||||
| 121 | 0 | '(_sahv_dpath.length ? "@" + _sahv_dpath.map(function(e,i){return "["+e+"]"}).join("") + ": " : "") + ' . $t; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | # $l = $r | ||||||||||
| 125 | 0 | ▶ | sub expr_set { | ||||||||
|
Subroutine
expr_set: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 126 | 0 | my ($self, $l, $r) = @_; | |||||||||
| 127 | 0 | "($l = $r)"; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | # $l //= $r | ||||||||||
| 131 | 0 | ▶ | sub expr_setif { | ||||||||
|
Subroutine
expr_setif: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 132 | 0 | my ($self, $l, $r) = @_; | |||||||||
| 133 | 0 | "$l = " . $self->expr_defined($l) . " ? $l : $r"; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 0 | ▶ | sub expr_set_err_str { | ||||||||
|
Subroutine
expr_set_err_str: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 137 | 0 | my ($self, $et, $err_expr) = @_; | |||||||||
| 138 | 0 | $self->expr_setif($et, $err_expr); | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 0 | ▶ | sub expr_set_err_full { | ||||||||
|
Subroutine
expr_set_err_full: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 142 | 0 | my ($self, $et, $k, $err_expr) = @_; | |||||||||
| 143 | 0 | join( | |||||||||
| 144 | "", | ||||||||||
| 145 | "(", | ||||||||||
| 146 | $self->expr_setif("$et\['$k']", "{}"), | ||||||||||
| 147 | ",", | ||||||||||
| 148 | $self->expr_setif("$et\['$k'][_sahv_dpath.join('/')]", $err_expr), | ||||||||||
| 149 | ")", | ||||||||||
| 150 | ); | ||||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | 0 | ▶ | sub expr_reset_err_str { | ||||||||
|
Subroutine
expr_reset_err_str: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 154 | 0 | my ($self, $et, $err_expr) = @_; | |||||||||
| 155 | 0 | "($et = null, true)"; | |||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | 0 | ▶ | sub expr_reset_err_full { | ||||||||
|
Subroutine
expr_reset_err_full: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 159 | 0 | my ($self, $et) = @_; | |||||||||
| 160 | 0 | join( | |||||||||
| 161 | "", | ||||||||||
| 162 | "(", | ||||||||||
| 163 | $self->expr_setif("$et\['errors']", "{}"), | ||||||||||
| 164 | ",", | ||||||||||
| 165 | "delete($et\['errors'][_sahv_dpath.join('/')])", | ||||||||||
| 166 | ")", | ||||||||||
| 167 | ); | ||||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | # $cond_term ? $true_term : $false_term | ||||||||||
| 171 | 0 | ▶ | sub expr_ternary { | ||||||||
|
Subroutine
expr_ternary: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 172 | 0 | my ($self, $cond_term, $true_term, $false_term) = @_; | |||||||||
| 173 | 0 | "$cond_term ? $true_term : $false_term"; | |||||||||
| 174 | } | ||||||||||
| 175 | |||||||||||
| 176 | 0 | ▶ | sub expr_log { | ||||||||
|
Subroutine
expr_log: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 177 | 0 | my ($self, $cd, $ccl) = @_; | |||||||||
| 178 | # currently not supported | ||||||||||
| 179 | 0 | ""; | |||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | 0 | ▶ | sub expr_block { | ||||||||
|
Subroutine
expr_block: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 183 | 0 | my ($self, $code) = @_; | |||||||||
| 184 | 0 | join( | |||||||||
| 185 | "", | ||||||||||
| 186 | "(function() {\n", | ||||||||||
| 187 | String::Indent::indent( | ||||||||||
| 188 | $self->indent_character, | ||||||||||
| 189 | $code, | ||||||||||
| 190 | ), | ||||||||||
| 191 | "})()", | ||||||||||
| 192 | ); | ||||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | # whether block is implemented using function | ||||||||||
| 196 | 0 | ▶ | sub block_uses_sub { 1 } | ||||||||
|
Subroutine
block_uses_sub: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 197 | |||||||||||
| 198 | 0 | ▶ | sub stmt_declare_local_var { | ||||||||
|
Subroutine
stmt_declare_local_var: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 199 | 0 | my $self = shift; | |||||||||
| 200 | 0 | my $v = shift; | |||||||||
| 201 | 0 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 202 | 0 | "var $v = $_[0];"; | |||||||||
| 203 | } else { | ||||||||||
| 204 | 0 | "var $v;"; | |||||||||
| 205 | } | ||||||||||
| 206 | } | ||||||||||
| 207 | |||||||||||
| 208 | 0 | ▶ | sub expr_anon_sub { | ||||||||
|
Subroutine
expr_anon_sub: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 209 | 0 | my ($self, $args, $code) = @_; | |||||||||
| 210 | 0 | join( | |||||||||
| 211 | "", | ||||||||||
| 212 | "function(".join(", ", @$args).") {\n", | ||||||||||
| 213 | String::Indent::indent( | ||||||||||
| 214 | $self->indent_character, | ||||||||||
| 215 | $code, | ||||||||||
| 216 | ), | ||||||||||
| 217 | "}" | ||||||||||
| 218 | ); | ||||||||||
| 219 | } | ||||||||||
| 220 | |||||||||||
| 221 | # enclose $stmt in an eval/try block, return true if succeeds, false if error | ||||||||||
| 222 | # was thrown. XXX error message was not recorded yet. | ||||||||||
| 223 | 0 | ▶ | sub expr_eval { | ||||||||
|
Subroutine
expr_eval: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 224 | 0 | my ($self, $stmt) = @_; | |||||||||
| 225 | 0 | "(function(_err) { try { $stmt } catch (e) { _err = e }; return !_err })()"; | |||||||||
| 226 | } | ||||||||||
| 227 | |||||||||||
| 228 | 0 | ▶ | sub stmt_require_module { | ||||||||
|
Subroutine
stmt_require_module: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 229 | 0 | my ($self, $mod_record) = @_; | |||||||||
| 230 | # currently loading module is not supported by js? | ||||||||||
| 231 | #"require $mod_record->{name};"; | ||||||||||
| 232 | 0 | ''; | |||||||||
| 233 | } | ||||||||||
| 234 | |||||||||||
| 235 | 0 | ▶ | sub stmt_require_log_module { | ||||||||
|
Subroutine
stmt_require_log_module: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 236 | 0 | my ($self) = @_; | |||||||||
| 237 | # currently logging is not supported by js | ||||||||||
| 238 | 0 | ''; | |||||||||
| 239 | } | ||||||||||
| 240 | |||||||||||
| 241 | 0 | ▶ | sub stmt_assign_hash_value { | ||||||||
|
Subroutine
stmt_assign_hash_value: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 242 | 0 | my ($self, $ht, $kt, $vt) = @_; | |||||||||
| 243 | 0 | "$ht\[$kt] = $vt;"; | |||||||||
| 244 | } | ||||||||||
| 245 | |||||||||||
| 246 | 0 | ▶ | sub stmt_return { | ||||||||
|
Subroutine
stmt_return: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 247 | 0 | my $self = shift; | |||||||||
| 248 | 0 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 249 | 0 | "return($_[0]);"; | |||||||||
| 250 | } else { | ||||||||||
| 251 | 0 | 'return;'; | |||||||||
| 252 | } | ||||||||||
| 253 | } | ||||||||||
| 254 | |||||||||||
| 255 | 0 | ▶ | sub expr_validator_sub { | ||||||||
|
Subroutine
expr_validator_sub: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 256 | 0 | my ($self, %args) = @_; | |||||||||
| 257 | |||||||||||
| 258 | 0 | $args{data_term} = 'data'; | |||||||||
| 259 | 0 | $self->SUPER::expr_validator_sub(%args); | |||||||||
| 260 | } | ||||||||||
| 261 | |||||||||||
| 262 | 0 | ▶ | sub _str2reliteral { | ||||||||
|
Subroutine
_str2reliteral: not called CC=3 SCAR=24.8
| |||||||||||
| 263 | 0 | my ($self, $cd, $str) = @_; | |||||||||
| 264 | |||||||||||
| 265 | 0 | my $re; | |||||||||
| 266 | 0 | ▶ | if (ref($str) eq 'Regexp') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 267 | 0 | $re = "$str"; | |||||||||
| 268 | } else { | ||||||||||
| 269 | 0 | eval { qr/$str/ }; | |||||||||
| 270 | 0 | ▶ | $self->_die($cd, "Invalid regex $str: $@") if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 271 | 0 | $re = $str; | |||||||||
| 272 | } | ||||||||||
| 273 | |||||||||||
| 274 | # i don't know if this is safe? | ||||||||||
| 275 | 0 | $re = "$re"; | |||||||||
| 276 | 0 | $re =~ s!/!\\/!g; | |||||||||
| 277 | 0 | "/$re/"; | |||||||||
| 278 | } | ||||||||||
| 279 | |||||||||||
| 280 | 1 | 1; | |||||||||
| 281 | # ABSTRACT: Compile Sah schema to JavaScript code | ||||||||||
| 282 | |||||||||||
| 283 | __END__ | ||||||||||