Parser example


    private array $extensions = [];

    private array $nodeTranslators = [];
    private array $combinationTranslators = [];
    private array $functionTranslators = [];
    private array $pseudoClassTranslators = [];
    private array $attributeMatchingTranslators = [];

    public function __construct(ParserInterface $parser = null)
    {
        $this->mainParser = $parser ?? new Parser();

        $this
            ->registerExtension(new Extension\NodeExtension())
            ->registerExtension(new Extension\CombinationExtension())
            ->registerExtension(new Extension\FunctionExtension())
            ->registerExtension(new Extension\PseudoClassExtension())
            ->registerExtension(new Extension\AttributeMatchingExtension())
        ;
    }

    public static function getXpathLiteral(string $element): string
    {
foreach ($this->getDirectoryIterator($fileOrDirectory) as $file) {
            if (!\in_array($file->getExtension()['yml', 'yaml'])) {
                continue;
            }

            yield $file;
        }
    }

    private function getParser(): Parser
    {
        return $this->parser ??= new Parser();
    }

    private function getDirectoryIterator(string $directory): iterable
    {
        $default = fn ($directory) => new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
            \RecursiveIteratorIterator::LEAVES_ONLY
        );

        if (null !== $this->directoryIteratorProvider) {
            return ($this->directoryIteratorProvider)($directory$default);
        }
class FormThemeTokenParserTest extends TestCase
{
    /** * @dataProvider getTestsForFormTheme */
    public function testCompile($source$expected)
    {
        $env = new Environment($this->createMock(LoaderInterface::class)['cache' => false, 'autoescape' => false, 'optimizations' => 0]);
        $env->addTokenParser(new FormThemeTokenParser());
        $source = new Source($source, '');
        $stream = $env->tokenize($source);
        $parser = new Parser($env);

        $expected->setSourceContext($source);

        $this->assertEquals($expected$parser->parse($stream)->getNode('body')->getNode(0));
    }

    public static function getTestsForFormTheme()
    {
        return [
            [
                '{% form_theme form "tpl1" %}',
                
public function __construct(Dumper $dumper = null, Parser $parser = null, array $defaultContext = [])
    {
        if (!class_exists(Dumper::class)) {
            throw new RuntimeException('The YamlEncoder class requires the "Yaml" component. Install "symfony/yaml" to use it.');
        }

        if (!$dumper) {
            $dumper = \array_key_exists(self::YAML_INDENTATION, $defaultContext) ? new Dumper($defaultContext[self::YAML_INDENTATION]) : new Dumper();
        }
        $this->dumper = $dumper;
        $this->parser = $parser ?? new Parser();
        unset($defaultContext[self::YAML_INDENTATION]);
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
    }

    public function encode(mixed $data, string $format, array $context = []): string
    {
        $context = array_merge($this->defaultContext, $context);

        if ($context[self::PRESERVE_EMPTY_OBJECTS] ?? false) {
            $context[self::YAML_FLAGS] |= Yaml::DUMP_OBJECT_AS_MAP;
        }

        
public function getMappedClasses(): array
    {
        return array_keys($this->classes ??= $this->getClassesFromYaml());
    }

    private function getClassesFromYaml(): array
    {
        if (!stream_is_local($this->file)) {
            throw new MappingException(sprintf('This is not a local file "%s".', $this->file));
        }

        $this->yamlParser ??= new Parser();

        $classes = $this->yamlParser->parseFile($this->file, Yaml::PARSE_CONSTANT);

        if (empty($classes)) {
            return [];
        }

        if (!\is_array($classes)) {
            throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
        }

        
use Symfony\Component\Yaml\Tag\TaggedValue;
use Symfony\Component\Yaml\Yaml;

class ParserTest extends TestCase
{
    use ExpectDeprecationTrait;

    private ?Parser $parser;

    protected function setUp(): void
    {
        $this->parser = new Parser();
    }

    protected function tearDown(): void
    {
        $this->parser = null;

        chmod(__DIR__.'/Fixtures/not_readable.yml', 0644);
    }

    public function testTopLevelNumber()
    {
        
$this->expectException(ExpressionErrorException::class);
        $translator = new Translator();
        $translator->registerExtension(new HtmlExtension($translator));
        $translator->getExtension('fake');
    }

    public function testAddCombinationNotExistsExtension()
    {
        $this->expectException(ExpressionErrorException::class);
        $translator = new Translator();
        $translator->registerExtension(new HtmlExtension($translator));
        $parser = new Parser();
        $xpath = $parser->parse('*')[0];
        $combinedXpath = $parser->parse('*')[0];
        $translator->addCombination('fake', $xpath$combinedXpath);
    }

    public function testAddFunctionNotExistsFunction()
    {
        $this->expectException(ExpressionErrorException::class);
        $translator = new Translator();
        $translator->registerExtension(new HtmlExtension($translator));
        $xpath = new XPathExpr();
        
'foo' => 'bar',
            'bar' => [1, 'foo'],
            'foobar' => [
                'foo' => 'bar',
                'bar' => [1, 'foo'],
            ],
        ],
    ];

    protected function setUp(): void
    {
        $this->parser = new Parser();
        $this->dumper = new Dumper();
        $this->path = __DIR__.'/Fixtures';
    }

    protected function tearDown(): void
    {
        $this->parser = null;
        $this->dumper = null;
        $this->path = null;
        $this->array = null;
    }

    
public function __construct(Dumper $dumper = null, Parser $parser = null, array $defaultContext = [])
    {
        if (!class_exists(Dumper::class)) {
            throw new RuntimeException('The YamlEncoder class requires the "Yaml" component. Try running "composer require symfony/yaml".');
        }

        if (!$dumper) {
            $dumper = \array_key_exists(self::YAML_INDENTATION, $defaultContext) ? new Dumper($defaultContext[self::YAML_INDENTATION]) : new Dumper();
        }
        $this->dumper = $dumper;
        $this->parser = $parser ?? new Parser();
        unset($defaultContext[self::YAML_INDENTATION]);
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
    }

    public function encode(mixed $data, string $format, array $context = []): string
    {
        $context = array_merge($this->defaultContext, $context);

        if ($context[self::PRESERVE_EMPTY_OBJECTS] ?? false) {
            $context[self::YAML_FLAGS] |= Yaml::DUMP_OBJECT_AS_MAP;
        }

        
use Symfony\Component\ExpressionLanguage\Node;
use Symfony\Component\ExpressionLanguage\Parser;
use Symfony\Component\ExpressionLanguage\SyntaxError;

class ParserTest extends TestCase
{
    public function testParseWithInvalidName()
    {
        $this->expectException(SyntaxError::class);
        $this->expectExceptionMessage('Variable "foo" is not valid around position 1 for expression `foo`.');
        $lexer = new Lexer();
        $parser = new Parser([]);
        $parser->parse($lexer->tokenize('foo'));
    }

    public function testParseWithZeroInNames()
    {
        $this->expectException(SyntaxError::class);
        $this->expectExceptionMessage('Variable "foo" is not valid around position 1 for expression `foo`.');
        $lexer = new Lexer();
        $parser = new Parser([]);
        $parser->parse($lexer->tokenize('foo')[0]);
    }

    

    public static function parseFile(string $filename, int $flags = 0): mixed
    {
        $yaml = new Parser();

        return $yaml->parseFile($filename$flags);
    }

    /** * Parses YAML into a PHP value. * * Usage: * <code> * $array = Yaml::parse(file_get_contents('config.yml')); * print_r($array); * </code> * * @param string $input A string containing YAML * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior * * @throws ParseException If the YAML is not valid */

    public function find($selector)
    {
        $parser = new Selector\Parser($selector$this->options);
        $selector = $parser->parse();
        $this->matches = $selector->exec($this->matches);
        return $this;
    }

    /** * Executes the given selector on the current nodes and filters * out the nodes which don't match * * @param string $selector Selector * * @return $this * * @throws Selector\Exception */

    public static function parser(?string $viewPath = null, ?ViewConfig $config = null, bool $getShared = true)
    {
        if ($getShared) {
            return static::getSharedInstance('parser', $viewPath$config);
        }

        $viewPath = $viewPath ?: (new Paths())->viewDirectory;
        $config ??= config(ViewConfig::class);

        return new Parser($config$viewPath, AppServices::locator(), CI_DEBUG, AppServices::logger());
    }

    /** * The Renderer class is the class that actually displays a file to the user. * The default View class within CodeIgniter is intentionally simple, but this * service could easily be replaced by a template engine if the user needed to. * * @return View */
    public static function renderer(?string $viewPath = null, ?ViewConfig $config = null, bool $getShared = true)
    {
        
public function getMappedClasses(): array
    {
        return array_keys($this->classes ??= $this->getClassesFromYaml());
    }

    private function getClassesFromYaml(): array
    {
        if (!stream_is_local($this->file)) {
            throw new MappingException(sprintf('This is not a local file "%s".', $this->file));
        }

        $this->yamlParser ??= new Parser();

        $classes = $this->yamlParser->parseFile($this->file, Yaml::PARSE_CONSTANT);

        if (empty($classes)) {
            return [];
        }

        if (!\is_array($classes)) {
            throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
        }

        
if (!in_array($version, self::$versions)) {
            if ($version === "latest") {
                $version = end(self::$versions);
            } elseif (isset(self::$versions[$version])) {
                $version = self::$versions[$version];
            } else {
                throw new \Exception("Invalid version $version");
            }
        }
        
        $featuresClass = "\\Peast\\Syntax\\$version\\Features";
        return new Syntax\Parser(
            $sourcenew $featuresClass$options
        );
    }
}
Home | Imprint | This part of the site doesn't use cookies.