Words.php

<?php

namespace Tlf\Lexer\PhpNew;

trait Words {

    public function unhandled_wd($lexer, $xpn, $ast, $word){
        if ($ast->type=='var_name'||$ast->type=='property_name'||$ast->type=='arg_name'){
            $ast->set('value', $word);
            $lexer->popHead();
        } 
        else if ($ast->type == 'method' && !isset($ast->name) 
            || $ast->type == 'function' && !isset($ast->name)){
            $ast->set('name', $word);
            $xpn->words = [];
            $xpn->last_word = null;
        } else if ($ast->type=='return_types'){
            $ast->push('value', $word);
        }  else if ($ast->type == 'class' && !isset($ast->name)){
            $ast->set('name',$word);
            if (isset($ast->fqn))$ast->fqn .= $word;
            $xpn->words = [];
        } else if ($ast->type=='class_extends'){
            $ast->value = $word;
            $lexer->popHead();
        } else if ($ast->type == 'trait' && !isset($ast->name)){
            $ast->set('name',$word);
            if (isset($ast->fqn))$ast->fqn .= $word;
            $xpn->words = [];
        }else if ($ast->type=='const_name'){
            $ast->value = $word;
            $lexer->popHead();
        }
    }

    public function wd_function($lexer, $xpn, $ast){
        if ($ast->type == 'class_body'){
            // its a method
            $method = new \Tlf\Lexer\Ast('method');
            $method->args = [];
            $this->docblock($method);
            $words = $xpn->words;
            array_pop($words); // remove 'function' 
            $method->modifiers = $words;
            $xpn->words = [];
            $lexer->setHead($method);
            // $ast->push('methods', $ast->get('type'));
            $ast->push('methods', $method);
        } else if (($ast->type=='file'||$ast->type=='namespace')
            &&( is_null($xpn->last_op)
                || $xpn->last_op == 'terminate'
                ||$xpn->last_op == 'block_end'
                ||$xpn->last_op == 'block_start'
            )

        ){

            $method = new \Tlf\Lexer\Ast('function');
            $method->args = [];
            $this->docblock($method);
            // $words = $xpn->words;
            // array_pop($words); // remove 'function'
            // $method->modifiers = $words;
            $xpn->words = [];
            $lexer->setHead($method);
            // $ast->push('methods', $ast->get('type'));
            $ast->push('functions', $method);



            // // var_dump($xpn->last_op);
            // // var_dump($xpn);
            // // exit;
            // // its a method
            // $function = new \Tlf\Lexer\Ast('function');
            // $function->args = [];
            // $this->docblock($function);
            // // $words = $xpn->words;
            // // array_pop($words); // remove 'function'
            // // $function->modifiers = $words;
            // $xpn->words = [];
            // $lexer->setHead($function);
            // // $ast->push('methods', $ast->get('type'));
            // $ast->push('functions', $function);
            // // $ast->set('body', new \Tlf\Lexer\StringAst('function_body'));
        }

    }
    
    public function wd_const($lexer,$xpn, $ast){
        if ($ast->type!='class_body')return;

        $const = new \Tlf\Lexer\Ast('const');
        $const->name = new \Tlf\Lexer\StringAst('const_name');
        $ast->push('const',$const);
        $lexer->setHead($const);
        $lexer->setHead($const->name);
        if (count($xpn->words)>1){
            $const->modifiers = array_slice($xpn->words,0,-1);
        }
        $xpn->words = [];

    }

    public function wd_namespace($lexer, $xpn, $ast){
        if ($ast->type!='file' && $ast->type != 'str')return;

        $ns = new \Tlf\Lexer\Ast('namespace');
        $this->docblock($ns);
        $ast->set('namespace', $ns);
        $lexer->setHead($ns);
        $xpn->words = [];
    }

    public function wd_use($lexer, $xpn, $ast){
        if ($ast->type!='file' && $ast->type != 'str' && $ast->type == 'namespace')return;

        $trait = new \Tlf\Lexer\Ast('use_trait');
        $this->docblock($trait);
        $ast->push('traits', $trait);
        $lexer->setHead($trait);
        $xpn->words = [];
    }

    public function wd_class($lexer, $xpn, $ast){
        if ($ast->type!='file'&&$ast->type!='namespace')return;
        // var_dump($xpn);
        // exit;

        $good_op =( is_null($xpn->last_op)
                || $xpn->last_op == 'terminate'
                ||$xpn->last_op == 'block_end'
                ||$xpn->last_op == 'block_start');
        if (!$good_op)return;

        $class = new \Tlf\Lexer\Ast('class');
        $this->docblock($class);
        // echo 'zeep';
        // exit;

        if (count($xpn->words)>1){
            $class->modifiers = array_slice($xpn->words,0,-1);
        }
        if ($ast->type=='namespace'&&$ast->name!=''){
            $class->namespace = $ast->name;
            $class->fqn = $ast->name.'\\';
        } else {
            $class->fqn = '';
            $class->namespace = '';
        }
        $xpn->words = [];
        $lexer->setHead($class);
        $ast->add('class', $class);
    }

    public function wd_trait($lexer, $xpn, $ast){
        if ($ast->type!='file'&&$ast->type!='namespace')return;

        $trait = new \Tlf\Lexer\Ast('trait');
        if ($ast->type=='namespace'&&$ast->name!=''){
            $trait->fqn = $ast->name.'\\';
            $trait->namespace = $ast->name;
        } else {
            $trait->fqn = '';
            $trait->namespace = '';
        }
        $xpn->words = [];
        $lexer->setHead($trait);
        $ast->add('trait', $trait);

    }

    public function wd_extends($lexer, $xpn, $ast){
        if ($ast->type!='class')return;

        $extends = new \Tlf\Lexer\StringAst('class_extends');
        $ast->set('extends', $extends);
        $lexer->setHead($extends);
    }

}