Documentation.php

<?php

namespace Tlf\Lexer\Test;

/**
 * This class is for writing documentation as code
 * So tests should be extremely clean & not seek to be unit tests, but seek to confirm broad functionality
 */
class Documentation extends \Tlf\Lexer\Test\Tester {

    /**
     * Just an example of how to run the php grammar
     * @tests nothing
     */
    public function testRunPhpGrammar(){
        // whatever code to parse
        $input = 'const blm = "yes";';

        // the starting ast ... php grammar uses the head AST's type to determine certain paths, so this is very important for unit tests
        $ast = new \Tlf\Lexer\Ast('class_body');

        $phpGram = new \Tlf\Lexer\PhpGrammarNew();
        // or pass `true` to addGrammar() & let php grammar's onGrammarAdded() setup its directives
        $phpGram->directives = array_merge(
            $phpGram->_string_directives,
            $phpGram->_core_directives,
        );
        $lexer = new \Tlf\Lexer();
        $lexer->debug = true;
        // pass `($phpGram, null, true)` (the default) to invoke `onGrammarAdded()`
        $lexer->addGrammar($phpGram, null, false);

        // adds directives to the top of the stack
        foreach ($phpGram->getDirectives(':php_code') as $directive){
            $lexer->addDirective($directive);
        }
        // alternate starting directive, if you're expecting a php open tag before php code
        // $lexer->addDirective($phpGram->getDirectives(':php_open')['php_open']);

        // runs the lexer with $ast as the head
        $ast = $lexer->lex($input, $ast);

        $tree = $ast->getTree();

        echo "\n\n\n-----------\n\n";

        print_r($tree);

        $expect = 
        [
            'type'=>'class_body',
            'const'=>[
                0=>[
                    'type'=>'const',
                    'name'=>'blm',
                    'value'=>'"yes"',
                    'declaration'=>'const blm = "yes";',
                ],
            ]
        ];
        $this->compare($expect,
            $tree
        );
    }
}