Skip to content

Commit

Permalink
fix: display path when ParseError is encountered
Browse files Browse the repository at this point in the history
fixes #7
fixes #9
  • Loading branch information
SOF3 committed Jul 5, 2023
1 parent ef4c57a commit 203ff2d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Args.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ private function inferComposerArgs(string $vendorPath, string $path, ?string $na

if (isset($cj["autoload"])) {
foreach ($cj["autoload"]["psr-0"] ?? [] as $srcs) {
foreach((array) $srcs as $src) {
foreach ((array) $srcs as $src) {
$this->sourceRoots[] = $path . "/" . $src;
}
}
foreach ($cj["autoload"]["psr-4"] ?? [] as $srcs) {
foreach((array) $srcs as $src) {
foreach ((array) $srcs as $src) {
$this->sourceRoots[] = $path . "/" . $src;
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/PhpFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace SOFe\Pharynx;

use ParseError;

use function array_slice;
use function assert;
use function count;
use function defined;
use function is_array;
use function realpath;
use function substr_count;
use function token_get_all;
use function trim;
Expand All @@ -29,7 +32,12 @@ public static function parse(string $filePath, bool $verbose) : self {
Terminal::print("Parsing $filePath", $verbose);

$phpCode = Files::read($filePath);
$rawTokens = token_get_all($phpCode, TOKEN_PARSE);
try {
$rawTokens = token_get_all($phpCode, TOKEN_PARSE);
} catch(ParseError $e) {
$realpath = realpath($filePath);
throw Terminal::fatal("Syntax error: {$e->getMessage()} on {$realpath}:{$e->getLine()}");
}

$tokens = [];

Expand Down
16 changes: 12 additions & 4 deletions src/Virion/VirionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace SOFe\Pharynx\Virion;

use ParseError;
use RuntimeException;
use SOFe\Pharynx\Processor;
use SOFe\Pharynx\Terminal;

use function is_string;
use function preg_match;
use function str_ends_with;
Expand Down Expand Up @@ -34,22 +37,27 @@ public function process(array &$files) : void {
$file->namespace = $this->epitope . "\\" . $file->namespace;
}

$file->header = $this->replace($file->header);
$file->header = $this->replace($file->header, $file->originalPath);

foreach ($file->items as $item) {
$item->code = $this->replace($item->code);
$item->code = $this->replace($item->code, $file->originalPath);
}
}
}

private function replace(string &$code) : string {
private function replace(string &$code, string $originalPath) : string {
$hasHeader = str_starts_with($code, "<?php");
$parsedCode = $code;
if (!$hasHeader) {
$parsedCode = "<?php\n$code";
}

$tokens = token_get_all($parsedCode, TOKEN_PARSE);
try {
$tokens = token_get_all($parsedCode, TOKEN_PARSE);
} catch(ParseError $e) {
throw Terminal::fatal("Syntax error: {$e->getMessage()} on {$originalPath}:{$e->getLine()}");
}

$output = "";
foreach ($tokens as $i => $token) {
if (!$hasHeader && $i === 0 && $token[0] === T_OPEN_TAG) {
Expand Down

0 comments on commit 203ff2d

Please sign in to comment.