keys.php

<?php
return array (
  'Commands.NamespaceTargets' => '$namespaceTargets = [
    \'lexer\'=>$this,
    \'token\'=>$this->token,
    \'ast\'=>$this->getHead(),
];
$grammarTargets = $this->grammars;
$grammarTargets[\'this\'] = $directive->_grammar ?? null;',
  'Commands.SwitchCase' => 'switch ($command){
    ///
    // comands for debugging
    ///
    case "debug.die":
    case "die":
        var_dump($args);
        exit;
        break;
    case "debug.print":
    case "print":
        var_dump($args);
        break;
    /**
     * Run commands of another directive. Does not run \'match\' by default
     *
     * @arg :directive.isn
     * @arg literal string \'match\' to keep the match command from the inherited directive
     *
     * @example directive.inherit :varchars.start
     */
    case "directive.inherit":
    case "inherit":
        $arg2 = $args[1]??\'\';
        $parts = explode(\'.\', $arg1);
        $name = $parts[0];
        $isn = $parts[1];
        $directives = $directive->_grammar->getDirectives($name);
        foreach ($directives as $d){
            if ($arg2!==\'match\'){
                unset($d->$isn[\'match\']);
            }
            // print_r($d
            $this->processInstructions($d, $isn, $directiveList);
            if ($arg2===\'match\'&&isset($d->$isn[\'_matches\'])){
                $directive->$isn[\'_matches\'] = $d->$isn[\'_matches\'];
            }
        }
        echo "\\n\\033[0;32mContinue ".$directive->_name."\\033[0m";
        break;
    //
    // commands with non-namespaced shorthands
    //
    case "directive.start":
    case "start":
        $this->directiveStarted($directive);
        break;
    case "directive.stop":
    case "stop":
        $this->directiveStopped($directive);
        break;
    case "token.rewind":
    case "rewind":
        $token->rewind($arg1);
        break;
    case "token.forward":
    case "forward":
        $token->forward($arg1);
        break;
    /**
     * Halt execution of current directive (don\'t run its following instructions). Useful for preventing overrides from being executed
     */
    case "directive.halt":
    case "halt":
        $this->haltInstructions();
        break;
    case "halt.all":
        $this->haltAll();
        break;
    //
    // namespaced commands
    //
    case "previous.append":
        if (!is_array($arg1))$arg1 = [$arg1];
        foreach ($arg1 as $index=>$keyForPrevious){
            $this->appendToPrevious($keyForPrevious, $token->buffer());
        }
        break;
    case "previous.set":
        $value = $args[1] ?? $token->buffer();
        if ($value ===true)$value = $token->buffer();
        $this->setPrevious($arg1, $value);
        break;
    case "directive.stop_others":
        foreach ($directiveList[\'started\'] as $started){
            if ($started!=$directive){
                $this->directiveStopped($started, $list);
            }
        }
        break;
    case "directive.pop":
        $arg1 = (int)$arg1;
        if ($arg1===0)echo "\\n    --no directives popped.";
        while ($arg1-- > 0){
            $this->popDirectivesLayer();
        }
        break;

    //
    // buffer commands
    //
    case "buffer.clear":
        $token->clearBuffer();
        break;
    case "buffer.clearNext":
        $amount = (int)$arg1;
        $remove = 0;
        while ($amount-->0){
            if ($token->next())$remove++;
        }
        $token->setBuffer(substr($token->buffer(),0,-$remove));
        break;
    case "buffer.appendChar":
        $token->setBuffer($token->buffer() . $arg1);
        break;
    //
    // ast commands
    //
    case "ast.pop":
        $this->popHead();
        break;
    case "ast.set":
        if (isset($args[1])){
            $value = $this->executeMethodString($args[1]);
        } else $value = $token->buffer();
        $this->getHead()->set($arg1, $value);
        break;
    /**
     * Save the currrent buffer to the given key
     * @arg key to push to
     */
    case "ast.push":
        $key = $arg1;
        $toPush = $token->buffer();
        $ast = $this->getHead();
        $ast->add($key,$toPush);
        break;
    case "ast.append":
        $key = $arg1;
        if (isset($args[1])&&is_string($args[1])){
            var_dump($args[1]);
            $value = $this->executeMethodString($args[1]);
        } else $value = $token->buffer();
        $ast = $this->getHead();
        $src = $ast->get($key);
        $new = $src . $value;
        $ast->set($key, $new);
        break;

    default:
        throw new \\Exception("\\nAction \'$command\' not handled yet. Maybe it needs to be a callable. Prepend `this:` to call a method on your grammar.");
}',
  'Commands.MethodMap' => '[
    \'directive.then\'=>\'cmdThen\',
    \'then\'=>\'cmdThen\',
    \'then.pop\'=>\'cmdThenPop\',

    \'match\'=>\'cmdMatch\',
    \'buffer.match\'=>\'cmdMatch\',

    \'buffer.notin\'=>\'cmdBuffer_notin\',

    \'ast.new\'=>\'cmdAst_new\',
];',
  'DocBlock.onLexerStart' => 'Called before any characters are added to the token

@usage Set placeholder values
@param Tlf\\Lexer $lexer the lexer
@param Tlf\\Lexer\\Ast $ast The root ast. Usually a \'file\' type ast with \'ext\', \'name\', and \'path\' set
@param Tlf\\Lexer\\Token $token a token with an empty buffer',
  'ParsingJson' => '$json = \'["Cool",["yes","please"],"okay"]\';
$lexer = new \\Tlf\\Lexer();
// $lexer->debug = true;
// $lexer->useCache = false; // useCache only matters when lexing a file
$lexer->addGrammar($jsonGrammar = new \\Tlf\\Lexer\\JsonGrammar());

//this is the root ast
$ast = new \\Tlf\\Lexer\\JsonAst("json"); //"json" is the type
$ast->source = $json; //Not required, but I like keeping source in the ast root.
$ast = $lexer->lexAst($ast, $json);

// $tree = $ast->getTree(); // not what we normally want with json
$data = $ast->getJsonData(); // custom method on the JsonAst class',
  'Internal.prompt_choose_function' => '    # prompt_choose_function \\
        # "# bent [command]" \\
            # "run help" "help" "View all functions and extended help" \\
            # "run core save" "save" "Save & upload your project" \\
            # "run core upload" "upload" "upload your project (doesn\'t save first)" \\
            # "run core update" "update" "Download all changes" \\
            # "\'" \\
            # "run core tag" "tag" "Create a numbered release of your project" \\
            # "run core revert" "revert" "Rollback to a previous save of your project" \\
            # "run core merge" "merge" "Merge current branch into another branch" \\
            # "run core check" "check" "Check the status of your project" \\
            # "run core url" "url" "Get special urls to git hosts" \\
            # "run core ignore" "ignore" "Download a .gitignore file from Github\'s gitignore repo" \\
        # \\
    # ;
#',
  'Test.Doc.LexString' => '$lexer = new \\Tlf\\Lexer();
$docGrammar = new \\Tlf\\Lexer\\DocblockGrammar();
$lexer->addGrammar($docGrammar);
$lexer->addDirective($docGrammar->getDirectives(\':/*\')[\'/*\']);
$str = "/** I am docblock */";
$ast = $lexer->lex($str);

$tree = $ast->getTree();
$actual = $tree[\'docblock\'][0];
$expect = [
    \'type\'=>\'docblock\',
    \'description\'=>\'I am docblock\',
];',
  'Test.Doc.LexAst' => '$lexer = new \\Tlf\\Lexer();
$phpGrammar = new \\Tlf\\Lexer\\PhpGrammar();
$lexer->addGrammar($phpGrammar);

// set up the ast
$ast = new \\Tlf\\Lexer\\Ast(\'code\');
$ast->set (\'language\', \'php\');
$code = \'<?php class Abc extends Alphabet {}\';
$ast->set(\'src\', $code);


$ast = $lexer->lex($code, $ast);
$actual = $ast->getTree();
$expect = [
    \'type\'=>\'code\',
    \'language\'=>\'php\',
    \'src\'=>$code,
    \'class\'=>[
        0=>[
            \'type\'=>\'class\',
            \'docblock\'=>\'\',
            \'namespace\'=>\'\',
            \'name\'=>\'Abc\',
            \'declaration\'=>\'class Abc extends Alphabet \',
        ],
    ],
];',
  'Test.Doc.LexFile' => '$lexer = new \\Tlf\\Lexer();
$lexer->useCache = false; // cache is disabled only for testing
$lexer->addGrammar($phpGrammar = new \\Tlf\\Lexer\\PhpGrammar());

$ast = $lexer->lexFile(dirname(__DIR__).\'/php/SampleClass.php\');

// An array detailing the file 
$tree = $ast->getTree(); ',
)
?>