helmich / typo3-typoscript-parser
Parser for the TYPO3 configuration language TypoScript.
Fund package maintenance!
martin-helmich
donate.helmich.me
Installs: 2 678 072
Dependents: 2
Suggesters: 0
Security: 0
Stars: 34
Watchers: 5
Forks: 11
Open Issues: 8
Requires
- php: >=8.1
- symfony/config: ^5.4 || ^6.4 || ^7.0
- symfony/console: ^5.4 || ^6.4 || ^7.0
- symfony/dependency-injection: ^5.4 || ^6.4 || ^7.0
- symfony/yaml: ^5.4 || ^6.4 || ^7.0
Requires (Dev)
- php-vfs/php-vfs: ^1.4.2
- phpspec/prophecy-phpunit: ^2.1.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^10.5 || ^11.5
- symfony/phpunit-bridge: ^5.4 || ^6.4 || ^7.0
- dev-master
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.0
- v2.4.1
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.1
- v2.2.0
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.1
- v2.0.0
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-bugfix/issue-111
- dev-bugfix/issue-104
- dev-bugfix/condition-keep-end-statement
- dev-chore/php-8.4
- dev-chore/fix-psalm-compat
- dev-bugfix/fix-psalm-errors
- dev-dependabot/add-v2-config-file
- dev-task/appease-codeclimate
- dev-bugfix/fix-typechecker-issues
- dev-task/maint2019
- dev-bugfix/allow-expr-conditions
- dev-chore/upgrade-php72
This package is auto-updated.
Last update: 2024-12-16 16:27:25 UTC
README
Author
Martin Helmich (typo3 at martin-helmich dot de)
Synopsis
This package contains a library offering a tokenizer and a parser for TYPO3's configuration language, "TypoScript".
Why?
Just as typoscript-lint, this project started of as a simple programming excercise. Tokenizer and parser could probably implemented in a better way (it's open source, go for it!).
Usage
Parsing TypoScript
You can use the Helmich\TypoScriptParser\Parser\Parser
class to generate a syntax
tree from source code input. The class requires an instance of the Helmich\TypoScriptParser\Tokenizer\Tokenizer
class as dependency. When using the Symfony DependencyInjection component, you can
simply use the service parser
for this.
use Helmich\TypoScriptParser\Parser\Parser, Helmich\TypoScriptParser\Tokenizer\Tokenizer; $typoscript = file_get_contents('path/to/typoscript.ts'); $parser = new Parser(new Tokenizer()); $statements = $parser->parse($typoscript);
Analyzing TypoScript
You can analyze the generated syntax tree by implementing visitors. For example, let's implement a check that checks for non-CGL-compliant variable names (there's probably no use case for that, just as a simple example):
First, we need the respective visitor implementation:
use Helmich\TypoScriptParser\Parser\Traverser\Visitor, Helmich\TypoScriptParser\Parser\AST\Statement, Helmich\TypoScriptParser\Parser\AST\Operator\Assignment, Helmich\TypoScriptParser\Parser\AST\NestedAssignment; class VariableNamingCheckVisitor implements Visitor { public function enterTree(array $statements) {} public function enterNode(Statement $statement) { if ($statement instanceof Assignment || $statement instanceof NestedAssignment) { if (!preg_match(',^[0-9]+$,', $statement->object->relativePath)) { throw new \Exception('Variable names must be numbers only!'); } } } public function exitNode(Statement $statement) {} public function exitTree(array $statements) {} }
Then traverse the syntax tree:
use Helmich\TypoScriptParser\Parser\Traverser\Traverser; $traverser = new Traverser($statements); $traverser->addVisitor(new VariableNamingCheckVisitor()); $traverser->walk();