Skip to content

Commit

Permalink
CLI: command line utility implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
remorhaz committed Apr 17, 2018
1 parent fcedec0 commit 3bf21a1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
46 changes: 46 additions & 0 deletions bin/unilex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env php
<?php

use Garden\Cli\Cli;
use Remorhaz\UniLex\Lexer\TokenMatcherGenerator;
use Remorhaz\UniLex\Lexer\TokenMatcherSpecParser;

$isComposerLoaded = false;
foreach (['..', '../vendor'] as $composerAutoloadPath) {
$composerAutoloadFile = __DIR__ . "/{$composerAutoloadPath}/autoload.php";
if (is_file($composerAutoloadFile) === true) {
/** @noinspection PhpIncludeInspection */
require_once $composerAutoloadFile;
$isComposerLoaded = true;
break;
}
}
if (!$isComposerLoaded) {
fwrite(STDERR, "Composer autoload file not found!\n");
exit(1);
}

$cli = new Cli;
/** @noinspection PhpUnhandledExceptionInspection */
$cli
->description("UniLex: PHP lexical analyzer generator with Unicode support.")
->opt('desc:d', "Token matcher description")
->arg('source', "File to place generated matcher class.", true);

$args = $cli->parse($argv, true);

$matcherSpec = TokenMatcherSpecParser::loadFromFile($args->getArg('source'))
->getMatcherSpec();
$description = $args->getOpt('desc');
if (isset($description)) {
$matcherSpec->addFileComment($description, '');
}
$matcherSpec
->addFileComment(
"Auto-generated file, please don't edit manually.",
"Generated by UniLex."
);
$generator = new TokenMatcherGenerator($matcherSpec);

$output = $generator->getOutput();
fwrite(STDOUT, $output);
1 change: 1 addition & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<target name="all" depends="prepare-dir,lookup-tables,token-matchers"/>

<target name="prepare-dir" description="Prepares build directory">
<chmod file="${application.startdir}/bin/unilex" mode="0755" />
<mkdir dir="${application.startdir}/build/logs"/>
</target>

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"role": "Developer"
}
],
"bin": ["bin/unilex"],
"require": {
"php": "^7.1",
"vanilla/garden-cli": "^2.0",
Expand Down

0 comments on commit 3bf21a1

Please sign in to comment.