| 1 | package Dallycot::CLI; | ||
| 2 | our $AUTHORITY = 'cpan:JSMITH'; | ||
| 3 | |||
| 4 | # ABSTRACT: command line interface application | ||
| 5 | |||
| 6 | use utf8; | ||
| 7 | use Moose; | ||
| 8 | with 'MooseX::Getopt'; | ||
| 9 | |||
| 10 | use Mojo; | ||
| 11 | use AnyEvent; | ||
| 12 | use Promises qw(deferred), backend => ['AnyEvent']; | ||
| 13 | |||
| 14 | use Dallycot; | ||
| 15 | use Dallycot::Compiler; | ||
| 16 | use Dallycot::Parser; | ||
| 17 | use Dallycot::Processor; | ||
| 18 | use Dallycot::Channel::Terminal; | ||
| 19 | use MooseX::Types::Moose qw(ArrayRef); | ||
| 20 | |||
| 21 | my $has_tangle = eval { | ||
| 22 | require Dallycot::Tangle; | ||
| 23 | 1; | ||
| 24 | }; | ||
| 25 | |||
| 26 | if ($@) { print STDERR "$@\n"; } | ||
| 27 | |||
| 28 | BEGIN { | ||
| 29 | require Dallycot::Library; | ||
| 30 | Dallycot::Library->libraries; | ||
| 31 | } | ||
| 32 | |||
| 33 | has 'c' => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => 'Bool', | ||
| 36 | traits => ['Getopt'], | ||
| 37 | documentation => 'check syntax only (parses but does not execute)', | ||
| 38 | ); | ||
| 39 | |||
| 40 | has 'o' => ( | ||
| 41 | is => 'ro', | ||
| 42 | isa => 'Str', | ||
| 43 | traits => ['Getopt'], | ||
| 44 | documentation => 'output RDF/XML to provided file (does not execute)', | ||
| 45 | ); | ||
| 46 | |||
| 47 | #has 'O' => ( | ||
| 48 | # is => 'ro', | ||
| 49 | # isa => 'Str', | ||
| 50 | # traits => ['Getopt'], | ||
| 51 | # documentation => 'output result of execution as RDF/XML to provided file', | ||
| 52 | # cmd_flag => 'O' | ||
| 53 | #); | ||
| 54 | |||
| 55 | has 'S' => ( | ||
| 56 | is => 'ro', | ||
| 57 | isa => 'Bool', | ||
| 58 | documentation => 'look for programfile using PATH environment variable' | ||
| 59 | ); | ||
| 60 | |||
| 61 | has 'v' => ( | ||
| 62 | is => 'ro', | ||
| 63 | isa => 'Bool', | ||
| 64 | traits => ['Getopt'], | ||
| 65 | documentation => 'print version and licensing banner and exit', | ||
| 66 | cmd_flag => 'v', | ||
| 67 | ); | ||
| 68 | |||
| 69 | has 'V' => ( | ||
| 70 | is => 'ro', | ||
| 71 | isa => 'Bool', | ||
| 72 | traits => ['Getopt'], | ||
| 73 | documentation => 'print installed namespaces and exit', | ||
| 74 | cmd_flag => 'V', | ||
| 75 | ); | ||
| 76 | |||
| 77 | has '_parser' => ( | ||
| 78 | accessor => 'parser', | ||
| 79 | default => sub { Dallycot::Parser->new } | ||
| 80 | ); | ||
| 81 | |||
| 82 | has '_engine' => ( | ||
| 83 | accessor => 'engine', | ||
| 84 | default => sub { | ||
| 85 | Dallycot::Processor->new( | ||
| 86 | max_cost => 1_000_000, | ||
| 87 | ignore_cost => 1 | ||
| 88 | ); | ||
| 89 | } | ||
| 90 | ); | ||
| 91 | |||
| 92 | has '_channel' => ( accessor => 'channel', ); | ||
| 93 | |||
| 94 | has '_prompt' => ( | ||
| 95 | accessor => 'prompt', | ||
| 96 | default => 'in[%d] := ', | ||
| 97 | ); | ||
| 98 | |||
| 99 | has '_deep_prompt' => ( | ||
| 100 | accessor => 'deep_prompt', | ||
| 101 | default => '>>> ', | ||
| 102 | ); | ||
| 103 | |||
| 104 | has '_statement_counter' => ( | ||
| 105 | accessor => 'statement_counter', | ||
| 106 | default => 1, | ||
| 107 | isa => 'Int' | ||
| 108 | ); | ||
| 109 | |||
| 110 | has '_done' => ( accessor => 'done', ); | ||
| 111 | |||
| 112 | has '_in' => ( | ||
| 113 | accessor => 'in', | ||
| 114 | default => sub { Dallycot::Value::Vector->new } | ||
| 115 | ); | ||
| 116 | |||
| 117 | has '_out' => ( | ||
| 118 | accessor => 'out', | ||
| 119 | default => sub { Dallycot::Value::Vector->new } | ||
| 120 | ); | ||
| 121 | |||
| 122 | sub check { | ||
| 123 | my ($self) = @_; | ||
| 124 | return $self->c; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub run { | ||
| 128 | my ($app) = @_; | ||
| 129 | |||
| 130 | $app->channel( Dallycot::Channel::Terminal->new( | ||
| 131 | completion_provider => $app | ||
| 132 | ) ); | ||
| 133 | |||
| 134 | if ( $app->v ) { | ||
| 135 | $app->print_banner; | ||
| 136 | my $d = deferred; | ||
| 137 | $d->resolve(undef); | ||
| 138 | return $d->promise; | ||
| 139 | } | ||
| 140 | |||
| 141 | if ( $app->V ) { | ||
| 142 | $app->print_library_namespaces; | ||
| 143 | my $d = deferred; | ||
| 144 | $d->resolve(undef); | ||
| 145 | return $d->promise; | ||
| 146 | } | ||
| 147 | |||
| 148 | $app->engine->create_channel( '$OUTPUT', $app->channel ); | ||
| 149 | |||
| 150 | $app->engine->create_channel( '$INPUT', $app->channel ); | ||
| 151 | |||
| 152 | my @args = @{ $app->extra_argv }; | ||
| 153 | |||
| 154 | if (@args) { | ||
| 155 | return $app->run_files(@args); | ||
| 156 | } | ||
| 157 | |||
| 158 | my $d = deferred; | ||
| 159 | |||
| 160 | $app->done($d); | ||
| 161 | |||
| 162 | $app->print_banner; | ||
| 163 | |||
| 164 | # load .dallycot - but no error if it doesn't exist | ||
| 165 | $app->run_file( $ENV{'HOME'} . "/.dallycot", 1 )->done( | ||
| 166 | sub { | ||
| 167 | my ( $in, $out ); | ||
| 168 | |||
| 169 | $app->engine->append_namespace_search_path(Dallycot::Library::CLI->namespace); | ||
| 170 | |||
| 171 | $app->engine->add_assignment( 'in', $app->in ); | ||
| 172 | |||
| 173 | $app->engine->add_assignment( 'out', $app->out ); | ||
| 174 | |||
| 175 | $app->primary_prompt; | ||
| 176 | }, | ||
| 177 | sub { | ||
| 178 | print STDERR "*** ", @_, "\n"; | ||
| 179 | } | ||
| 180 | ); | ||
| 181 | |||
| 182 | return $d->promise; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub print_banner { | ||
| 186 | my ($app) = @_; | ||
| 187 | |||
| 188 | my $out = $app->channel; | ||
| 189 | |||
| 190 | $Dallycot::VERSION //= 'm.xxyyyz'; | ||
| 191 | $out->send_data( | ||
| 192 | "Dallycot, version $Dallycot::VERSION.\n", | ||
| 193 | "Copyright (C) 2014-2015 James Smith.\n", | ||
| 194 | "This is free software licensed under the same terms as Perl 5.\n", | ||
| 195 | "There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or\n", | ||
| 196 | "FITNESS FOR A PARTICULAR PURPOSE.\n\n", | ||
| 197 | "Additional information about Dallycot is available at http://www.dallycot.net/.\n\n", | ||
| 198 | "Please contribute if you find this software useful.\n", | ||
| 199 | "For more information, visit http://www.dallycot.net/get-involved/.\n" | ||
| 200 | ); | ||
| 201 | return; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub print_library_namespaces { | ||
| 205 | my ($app) = @_; | ||
| 206 | |||
| 207 | my %namespaces; | ||
| 208 | |||
| 209 | foreach my $lib ( Dallycot::Library->libraries ) { | ||
| 210 | $namespaces{ $lib->instance->namespace } = $lib; | ||
| 211 | } | ||
| 212 | |||
| 213 | my $out = $app->channel; | ||
| 214 | |||
| 215 | if ( keys %namespaces ) { | ||
| 216 | $out->send_data("The following namespaces are installed:\n"); | ||
| 217 | } | ||
| 218 | else { | ||
| 219 | $out->send_data("No namespaces are installed\n"); | ||
| 220 | } | ||
| 221 | |||
| 222 | foreach my $ns ( sort keys %namespaces ) { | ||
| 223 | $out->send_data(" $ns\n"); | ||
| 224 | } | ||
| 225 | return; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub run_files { | ||
| 229 | my ( $app, @files ) = @_; | ||
| 230 | |||
| 231 | my $d = deferred; | ||
| 232 | |||
| 233 | $app->_run_files( $d, @files ); | ||
| 234 | |||
| 235 | return $d->promise; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub _run_files { | ||
| 239 | my ( $app, $d, $file, @files ) = @_; | ||
| 240 | |||
| 241 | $app->run_file($file)->done( | ||
| 242 | sub { | ||
| 243 | if (@files) { | ||
| 244 | $app->_run_files( $d, @files ); | ||
| 245 | } | ||
| 246 | else { | ||
| 247 | $d->resolve(); | ||
| 248 | } | ||
| 249 | }, | ||
| 250 | sub { | ||
| 251 | $d->reject(@_); | ||
| 252 | } | ||
| 253 | ); | ||
| 254 | return; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub primary_prompt { | ||
| 258 | my ($app) = @_; | ||
| 259 | |||
| 260 | $app->channel->receive_data( | ||
| 261 | prompt => Dallycot::Value::String->new( sprintf( "\n" . $app->prompt, $app->statement_counter ) ) ) | ||
| 262 | ->done( | ||
| 263 | sub { | ||
| 264 | my ($line) = @_; | ||
| 265 | if ( $line->isa('Dallycot::Value::String') ) { | ||
| 266 | if ( $line->value =~ m{^\s*$} ) { | ||
| 267 | $app->add_history($line); | ||
| 268 | $app->primary_prompt; | ||
| 269 | } | ||
| 270 | else { | ||
| 271 | $app->check_parse($line); | ||
| 272 | } | ||
| 273 | } | ||
| 274 | else { | ||
| 275 | $app->channel->send_data("\n"); | ||
| 276 | $app->done->resolve(undef); | ||
| 277 | } | ||
| 278 | }, | ||
| 279 | sub { | ||
| 280 | my ($err) = @_; | ||
| 281 | $app->channel->send_data("*** $err\n"); | ||
| 282 | $app->done->resolve(undef); | ||
| 283 | } | ||
| 284 | ); | ||
| 285 | return; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub symbol_completions { | ||
| 289 | my($app, $text) = @_; | ||
| 290 | |||
| 291 | # gather symbols from libraries we've "used" | ||
| 292 | # then see which ones start with $text | ||
| 293 | my $registry = Dallycot::Registry->instance; | ||
| 294 | my($length) = length($text); | ||
| 295 | my @symbols = grep { $text eq substr($_, 0, $length) } | ||
| 296 | map { $registry -> get_assignments($_) } | ||
| 297 | @{$app -> engine -> get_namespace_search_path} | ||
| 298 | ; | ||
| 299 | push @symbols, grep { $text eq substr($_, 0, $length) } | ||
| 300 | keys %{$app -> engine -> context -> environment} | ||
| 301 | ; | ||
| 302 | return @symbols; | ||
| 303 | } | ||
| 304 | |||
| 305 | sub check_parse { | ||
| 306 | my ( $app, $line ) = @_; | ||
| 307 | |||
| 308 | my $parse = $app->parser->parse( $line->value ); | ||
| 309 | if ( !defined($parse) | ||
| 310 | || @$parse == 1 && $parse->[0]->isa('Dallycot::AST::Expr') && !$app->parser->error ) | ||
| 311 | { | ||
| 312 | $app->secondary_prompt($line); | ||
| 313 | } | ||
| 314 | else { | ||
| 315 | $app->process_line( $line, $parse ); | ||
| 316 | } | ||
| 317 | return; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub secondary_prompt { | ||
| 321 | my ( $app, $line ) = @_; | ||
| 322 | |||
| 323 | $app->channel->receive_data( prompt => Dallycot::Value::String->new( $app->deep_prompt ) )->done( | ||
| 324 | sub { | ||
| 325 | my ($next_line) = @_; | ||
| 326 | if ( $next_line->is_defined ) { | ||
| 327 | $line = Dallycot::Value::String->new( $line->value . "\n" . $next_line->value ); | ||
| 328 | $app->check_parse($line); | ||
| 329 | } | ||
| 330 | else { | ||
| 331 | $app->process_line( $line, undef ); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | ); | ||
| 335 | return; | ||
| 336 | } | ||
| 337 | |||
| 338 | sub add_history { | ||
| 339 | my ( $app, $line ) = @_; | ||
| 340 | |||
| 341 | my $in = $app->in; | ||
| 342 | my $stmt_counter = $app->statement_counter; | ||
| 343 | $app->statement_counter( $app->statement_counter + 1 ); | ||
| 344 | $app->channel->add_history($line); | ||
| 345 | |||
| 346 | ${$in}[ $stmt_counter - 1 ] = $line; | ||
| 347 | return $stmt_counter; | ||
| 348 | } | ||
| 349 | |||
| 350 | sub process_line { | ||
| 351 | my ( $app, $line, $parse ) = @_; | ||
| 352 | |||
| 353 | my $stmt_counter = $app->add_history($line); | ||
| 354 | |||
| 355 | if ( $app->parser->error ) { | ||
| 356 | $app->channel->send_data( $app->parser->error ); | ||
| 357 | $app->primary_prompt; | ||
| 358 | } | ||
| 359 | elsif ( defined $parse ) { | ||
| 360 | $app->execute($parse)->then( | ||
| 361 | sub { | ||
| 362 | my ($ret) = @_; | ||
| 363 | if ( defined $ret ) { | ||
| 364 | $app->channel->send_data( "\nout[$stmt_counter] := ", $ret->as_text ); | ||
| 365 | my $out = $app->out; | ||
| 366 | ${$out}[ $stmt_counter - 1 ] = $ret; | ||
| 367 | } | ||
| 368 | }, | ||
| 369 | sub { | ||
| 370 | my ($error) = @_; | ||
| 371 | $app->channel->send_data("*** $error\n"); | ||
| 372 | } | ||
| 373 | )->finally( | ||
| 374 | sub { | ||
| 375 | $app->channel->send_data("\n"); | ||
| 376 | $app->primary_prompt; | ||
| 377 | } | ||
| 378 | )->catch( sub { | ||
| 379 | print STDERR "Uh oh: @_\n"; | ||
| 380 | } )->done( sub { } ); | ||
| 381 | } | ||
| 382 | else { | ||
| 383 | $app->channel->send_data("*** Unable to parse\n"); | ||
| 384 | $app->primary_prompt; | ||
| 385 | } | ||
| 386 | return; | ||
| 387 | } | ||
| 388 | |||
| 389 | sub run_file { | ||
| 390 | my ( $app, $filename, $ignore_existance ) = @_; | ||
| 391 | |||
| 392 | my $file; | ||
| 393 | if ( $app->S ) { | ||
| 394 | my @dirs = ( split( /:/, $ENV{PATH} ), '.' ); | ||
| 395 | $file = shift(@dirs) . '/' . $filename; | ||
| 396 | while ( !-f $file && @dirs ) { | ||
| 397 | $file = shift(@dirs) . '/' . $filename; | ||
| 398 | } | ||
| 399 | } | ||
| 400 | else { | ||
| 401 | $file = $filename; | ||
| 402 | } | ||
| 403 | if ( -f $file ) { | ||
| 404 | open my $file, "<", $file or do { | ||
| 405 | my $d = deferred; | ||
| 406 | $d->reject("Unable to read $filename"); | ||
| 407 | return $d->promise; | ||
| 408 | }; | ||
| 409 | local ($/) = undef; | ||
| 410 | my $source = <$file>; | ||
| 411 | close $file; | ||
| 412 | if ( $filename =~ m{\.md$} ) { | ||
| 413 | if ($has_tangle) { | ||
| 414 | $source = Dallycot::Tangle->new->parse($source); | ||
| 415 | } | ||
| 416 | else { | ||
| 417 | my $d = deferred; | ||
| 418 | $d->reject("Unable to parse markdown: Markdent or its prerequisites are not installed"); | ||
| 419 | return $d->promise; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | my $parse = $app->parser->parse($source); | ||
| 423 | if ( $app->parser->warnings ) { | ||
| 424 | $app->channel->send_data( "Warnings:\n " . join( "\n ", $app->parser->warnings ) . "\n" ); | ||
| 425 | } | ||
| 426 | if ( !$parse ) { | ||
| 427 | my $err = $app->parser->error; | ||
| 428 | my $d = deferred; | ||
| 429 | if ($err) { | ||
| 430 | $d->reject("In $filename:\n$err"); | ||
| 431 | } | ||
| 432 | else { | ||
| 433 | $d->reject("Unable to parse $filename"); | ||
| 434 | } | ||
| 435 | return $d->promise; | ||
| 436 | } | ||
| 437 | elsif ( $app->check ) { | ||
| 438 | my $d = deferred; | ||
| 439 | $d->resolve(); | ||
| 440 | return $d->promise; | ||
| 441 | } | ||
| 442 | elsif ( $app->o ) { | ||
| 443 | my $model = Dallycot::Compiler -> new; | ||
| 444 | my $root = $model -> compile(@$parse); | ||
| 445 | my $xml; | ||
| 446 | if($app->o =~ /\.ttl$/) { | ||
| 447 | $xml = $model -> as_turtle; | ||
| 448 | } | ||
| 449 | elsif($app->o =~ /\.n3/) { | ||
| 450 | $xml = $model -> as_ntriples; | ||
| 451 | } | ||
| 452 | elsif($app->o =~ /\.tsv/) { | ||
| 453 | $xml = $model -> as_tsv; | ||
| 454 | } | ||
| 455 | elsif($app->o =~ /\.dot/) { | ||
| 456 | $xml = $model -> as_dot; | ||
| 457 | } | ||
| 458 | else { | ||
| 459 | $xml = $model -> as_xml; | ||
| 460 | } | ||
| 461 | open my $fh, ">", $app->o or die "Unable to open " . $app->o . " for writing\n"; | ||
| 462 | print $fh $xml; | ||
| 463 | close $fh; | ||
| 464 | my $d = deferred; | ||
| 465 | $d -> resolve; | ||
| 466 | return $d -> promise; | ||
| 467 | } | ||
| 468 | else { | ||
| 469 | return $app->execute($parse); | ||
| 470 | } | ||
| 471 | } | ||
| 472 | elsif ( !$ignore_existance ) { | ||
| 473 | my $d = deferred; | ||
| 474 | $d->reject("Unable to read $filename"); | ||
| 475 | return $d->promise; | ||
| 476 | } | ||
| 477 | else { | ||
| 478 | my $d = deferred; | ||
| 479 | $d->resolve(undef); | ||
| 480 | return $d->promise; | ||
| 481 | } | ||
| 482 | } | ||
| 483 | |||
| 484 | sub execute { | ||
| 485 | my ( $app, $parse ) = @_; | ||
| 486 | |||
| 487 | my $d = eval { | ||
| 488 | if ( !is_ArrayRef($parse) ) { | ||
| 489 | $parse = [$parse]; | ||
| 490 | } | ||
| 491 | $app->engine->add_cost( -$app->engine->cost ); | ||
| 492 | return $app->engine->execute( @{$parse} )->catch( | ||
| 493 | sub { | ||
| 494 | my ($err) = @_; | ||
| 495 | while ( $err =~ s{\s+at\s.+?\sline\s+\d+.*?$}{}x ) { | ||
| 496 | |||
| 497 | # noop | ||
| 498 | } | ||
| 499 | |||
| 500 | $app->channel->send_data( '*** ' . $err . "\n" ); | ||
| 501 | } | ||
| 502 | ); | ||
| 503 | }; | ||
| 504 | if ($d) { | ||
| 505 | return $d; | ||
| 506 | } | ||
| 507 | elsif ($@) { | ||
| 508 | my $err = $@; | ||
| 509 | while ( $err =~ s{\s+at\s.+?\sline\s+\d+.*?$}{}x ) { | ||
| 510 | |||
| 511 | # noop | ||
| 512 | } | ||
| 513 | |||
| 514 | $app->channel->send_data( '*** ' . $err . "\n" ); | ||
| 515 | } | ||
| 516 | else { | ||
| 517 | $app->channel->send_data('*** Unable to execute\n'); | ||
| 518 | } | ||
| 519 | $d = deferred; | ||
| 520 | $d->resolve(); | ||
| 521 | return $d->promise; | ||
| 522 | } | ||
| 523 | |||
| 524 | __PACKAGE__ -> meta -> make_immutable; | ||
| 525 | |||
| 526 | 1; |