Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

naively complete built-in functions and constants #18

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions lib/Boris/ReadlineClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ReadlineClient {
private $_prompt;
private $_historyFile;
private $_clear = false;
private $_parser;

/**
* Create a new ReadlineClient using $socket for communication.
Expand All @@ -39,10 +40,12 @@ public function start($prompt, $historyFile) {
pcntl_signal(SIGCHLD, SIG_IGN);
pcntl_signal(SIGINT, array($this, 'clear'), true);

$parser = new ShallowParser();
$this->_parser = new ShallowParser();
$buf = '';
$lineno = 1;

readline_completion_function(array($this, 'complete'));

for (;;) {
$this->_clear = false;
$line = readline(
Expand Down Expand Up @@ -70,7 +73,7 @@ public function start($prompt, $historyFile) {

$buf .= sprintf("%s\n", $line);

if ($statements = $parser->statements($buf)) {
if ($statements = $this->_parser->statements($buf)) {
++$lineno;

$buf = '';
Expand All @@ -94,6 +97,28 @@ public function start($prompt, $historyFile) {
}
}

/**
* @param $arg
* @return array completions
*
* Naively complete built-in function and constants
*/
public function complete($arg) {
$symbolLists = get_defined_functions();
$symbolLists['constants'] = array_keys(get_defined_constants());
$candidates = array();
if ($result = $this->_parser->parse($arg)) {
if ($result->stmt != '') {
foreach ($symbolLists as $symbols) {
$candidates += array_filter($symbols, function($symbol) use ($result, &$candidates) {
return (strpos($symbol, $result->stmt) === 0);
});
}
}
}
return $candidates;
}

/**
* Clear the input buffer.
*/
Expand Down
21 changes: 15 additions & 6 deletions lib/Boris/ShallowParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ public function __construct() {
* @return array
*/
public function statements($buffer) {
$result = $this->parse($buffer);

if (!empty($result->statements) && trim($result->stmt) === '' && strlen($result->buffer) == 0) {
$this->_combineStatements($result);
$this->_prepareForDebug($result);
return $result->statements;
}
}

/**
* @param $buffer
*/
public function parse($buffer)
{
$result = $this->_createResult($buffer);

while (strlen($result->buffer) > 0) {
Expand All @@ -59,12 +73,7 @@ public function statements($buffer) {
break;
}
}

if (!empty($result->statements) && trim($result->stmt) === '' && strlen($result->buffer) == 0) {
$this->_combineStatements($result);
$this->_prepareForDebug($result);
return $result->statements;
}
return $result;
}

public function quote($token) {
Expand Down