From 99799e1d5a31d08598618235d3862bca58ce3438 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 13 Nov 2019 16:26:19 -0500 Subject: [PATCH] Slight tweak of php token handling --- README.md | 2 +- src/Termios.php | 3 +- src/functions.php | 123 +++++++++++++++++++++++----------------------- 3 files changed, 64 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 37e67cf..fede501 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ due to requiring the `FFI` extension. * The `editor` prefix has been removed from all the relevant functions, instead they are methods on the `Editor` class. * Enums are faked with class constants -* Basic PHP class auto-loading is used +* Composer is used for autoloading * Properties that must be manually updated in the C version (like counts/string length) are implemented with magic methods, so they are essentially calculated on read. * Generally, if a function exists in PHP, with the same name as the C function, the PHP version will be used. \ No newline at end of file diff --git a/src/Termios.php b/src/Termios.php index 159cf30..3fa885b 100644 --- a/src/Termios.php +++ b/src/Termios.php @@ -43,14 +43,13 @@ class Termios { // Make sure to restore normal mode on exit/die/crash register_shutdown_function([static::class, 'disableRawMode']); - // So, the only thing that seems to really matter here is that c_oflag is 0... $termios = clone $instance->originalTermios; // $termios->c_iflag &= ~(C::BRKINT | C::ICRNL | C::INPCK | C::ISTRIP | C::IXON); // $termios->c_oflag &= ~(C::OPOST); $termios->c_iflag = 0; $termios->c_oflag = 0; $termios->c_cflag |= (C::CS8); - $termios->c_lflag = $termios->c_lflag & ~( C::ECHO | C::ICANON | C::IEXTEN | C::ISIG ); + $termios->c_lflag &= ~( C::ECHO | C::ICANON | C::IEXTEN | C::ISIG ); $termios->c_cc[C::VMIN] = 0; $termios->c_cc[C::VTIME] = 1; diff --git a/src/functions.php b/src/functions.php index 9123d7c..09cd56f 100644 --- a/src/functions.php +++ b/src/functions.php @@ -294,74 +294,75 @@ function get_php_tokens(string $code): array $line = []; foreach($rawTokens as $t) { - // Simple characters, usually delimiters or single character operators - if ( ! is_array($t)) + if (is_array($t)) { + [$type, $rawChar, $currentLine] = $t; + $char = tabs_to_spaces($rawChar); + + $current = [ + 'type' => $type, + 'typeName' => token_name($type), + 'char' => $char, + 'line' => $currentLine, + ]; + + if ($char === "\n") + { + $line[] = $current; + $tokens[$lineNum] = $line; + $lineNum++; + $line = []; + } + + // Only return the first line of a multi-line token for this line array + if ($char !== "\n" && strpos($char, "\n") !== FALSE) + { + $chars = explode("\n", $char); + $current['original'] = [ + 'string' => $char, + 'lines' => $chars, + ]; + $current['char'] = array_shift($chars); + + // Add new lines for additional newline characters + $nextLine = $currentLine; + foreach ($chars as $char) + { + $nextLine++; + + if ( ! array_key_exists($nextLine, $tokens)) + { + $tokens[$nextLine] = []; + } + + $tokens[$nextLine][] = [ + 'type' => -1, + 'typeName' => 'RAW', + 'char' => $char, + ]; + } + } + + if ($currentLine !== $lineNum) + { + $existing = $tokens[$lineNum] ?? []; + $tokens[$lineNum] = array_merge($existing, $line); + + $lineNum = $currentLine; + $line = []; + } + + $line[] = $current; + } + else if (is_string($t)) + { + // Simple characters, usually delimiters or single character operators $line[] = [ 'type' => -1, 'typeName' => 'RAW', 'char' => tabs_to_spaces($t), ]; - continue; } - - [$type, $rawChar, $currentLine] = $t; - $char = tabs_to_spaces($rawChar); - - $current = [ - 'type' => $type, - 'typeName' => token_name($type), - 'char' => $char, - 'line' => $currentLine, - ]; - - if ($char === "\n") - { - $line[] = $current; - $tokens[$lineNum] = $line; - $lineNum++; - $line = []; - } - - // Only return the first line of a multi-line token for this line array - if ($char !== "\n" && strpos($char, "\n") !== FALSE) - { - $chars = explode("\n", $char); - $current['original'] = [ - 'string' => $char, - 'lines' => $chars, - ]; - $current['char'] = array_shift($chars); - - // Add new lines for additional newline characters - $nextLine = $currentLine; - foreach ($chars as $char) - { - $nextLine++; - - if ( ! array_key_exists($nextLine, $tokens)) - { - $tokens[$nextLine] = []; - } - - $tokens[$nextLine][] = [ - 'type' => -1, - 'typeName' => 'RAW', - 'char' => $char, - ]; - } - } - - if ($currentLine !== $lineNum) - { - $existing = $tokens[$lineNum] ?? []; - $tokens[$lineNum] = array_merge($existing, $line); - - $lineNum = $currentLine; - $line = []; - } - - $line[] = $current; } $tokens[$lineNum] = array_merge($tokens[$lineNum] ?? [], $line);