BashGrammar.php

<?php

namespace Tlf\Lexer\Test;

class BashGrammar extends \Tlf\Tester {

    protected $thingies = [
        'CommentsWithAttributes'=>[
            'start'=>'comment',
            'input'=>"var=\"abc\"\n#Black Lives Matter @see(local_police_data)\nvarb=\"def\"",
            'expect'=>[
                "comments"=>[
                    0=>[
                        'type'=>'comment',
                        'src'=>'#Black Lives Matter @see(local_police_data)',
                        'description'=> "Black Lives Matter ",
                        'attributes'=>[
                                [ "type"=>'attribute',
                                'src'=>'@see(local_police_data)',
                                'name'=>'see',
                                'args'=>['local_police_data'],
                                ]
                            ]
                        ]
                    ],
                ],
            ],
        'Comments'=>[
            'start'=>'comment',
            'input'=>"var=\"abc\"\n#I am a comment\nvarb=\"def\"",
            'expect'=>[
                "comments"=>[
                    0=>[
                        'type'=>'comment',
                        'src'=>'#I am a comment',
                        'description'=> "I am a comment",
                    ]
                ],
            ],
        ],
    ];

    public function testBashStuff(){
        $this->disable();
        foreach ($this->thingies as $description=>$test){
            $startingDirective=$test['start'];
            $inputString=$test['input']??file_get_contents($test['file']);
            $expect=$test['expect'];
            $this->test($description);
            if (!isset($this->options['run'])||$this->options['run']==$description){
                $output = $this->parse($inputString, $startingDirective);
                $this->compare($expect, $output);
            } else {
                echo "\n  '-run' specified for a different test";
            }
        }
    }

    public function testBashComment(){
        $this->disable();
        $description="I am a comment";
        $src = "#$description";
        $input = "var=\"abc\"\n$src\nvarb=\"def\"";
        $expect = [
            "comments"=>[
                0=>$description,
            ],
        ];

        $this->compare(
            $expect,
            $this->parse($input, "comment"),
        );
    }

    protected function parse($stringInput, $startingDirective){
        $lexer = new \Tlf\Lexer();
        $lexer->debug = true;
        $bashGrammar=new \Tlf\Lexer\BashGrammar();
        $lexer->addGrammar($bashGrammar);

        // print_r($bashGrammar->getDirectives(':comment')['comment']);
        // exit;
        //
        $startingDirectives = $bashGrammar->getDirectives(':comment');
        foreach ($startingDirectives as $se){
            $lexer->addDirective($se);
        }

        $ast = $lexer->lexStr($stringInput);

        $tree = $ast->getTree();
        unset($tree['type'], $tree['src']);

        return $tree;
    }
}