bugo / scss-parser
Standalone SCSS, Sass and CSS syntax parser
Requires
- php: ^8.2
- ext-ctype: *
Requires (Dev)
- ext-tokenizer: *
- pestphp/pest: ^3|^4|^5
- phpstan/phpstan: ^2.2
- rector/rector: ^2.6
- slevomat/coding-standard: ^8.32
- symfony/process: ^7|^8
- symplify/easy-coding-standard: ^13.3
- vimeo/psalm: ^6.16
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A syntax parser for SCSS, indented Sass, and plain CSS. It returns a syntax tree, normalized text, and source positions.
<?php declare(strict_types=1); use Bugo\SCSS\Parser; use Bugo\SCSS\Parser\ParseOptions; use Bugo\SCSS\Parser\Syntax; require __DIR__ . '/vendor/autoload.php'; $parser = new Parser(); $stylesheet = $parser->parse(".a\n width: 2px", new ParseOptions(Syntax::SASS)); // The normalized source in $stylesheet->source contains this CSS rule. // .a { // width: 2px; // } $expression = $parser->parseInlineExpression('2px'); // The AST node represents this CSS value. // 2px
Public API for the first release
Entry points, options, and results
| Contract | Behavior |
|---|---|
StylesheetParserInterface |
parse(string $source, ParseOptions $options = new ParseOptions()): ParsedStylesheet |
ExpressionParserInterface |
parseInlineExpression(string $expr, ParseOptions $options = new ParseOptions()): AstNode |
Parser\Syntax |
SCSS, SASS, and CSS have the string values scss, sass, and css |
Parser\ParseOptions |
final readonly. Defaults to Syntax::SCSS, trackSourceLocations: true, and sourceTracking: null |
Parser\ParsedStylesheet |
final readonly. Exposes ast: RootNode, normalized source: string, and originalSource: ?string. Parser sets originalSource to the input text in Syntax::SASS mode |
Parser |
final readonly. Implements both parser interfaces |
Select the syntax for each call. Inline expressions bypass normalization and default to SCSS independently of earlier calls. Nested parsing retains the mode and any source span available for its argument. Each call returns a separate tree.
AST and source positions
The public AST includes the syntax classes under Bugo\SCSS\Nodes\ in the parser manifest, including AstNode, StatementNode, RootNode, and MapPair. The contract covers class names, constructors, public fields and methods, and the children returned by getChildren(). Consumers can read and construct the AST. The HasChildren, HasSourcePosition, and HasLayoutPosition traits implement these methods internally.
The source-location API includes Nodes\SourcePosition, Parser\SourceTracking, and Parser\SourceSpan. SourceTracking provides a file name, a record(AstNode, SourceSpan): void callback, and an optional positionAt(int): SourcePosition callback. SourceSpan exposes text, file, reparsed, and the position(), end(), and slice() operations. Line numbers and byte columns start at one. With trackSourceLocations enabled, Sass AST and syntax errors refer to the original input before normalization.
Syntax errors
Parser\Exceptions exposes SyntaxException and its subclasses UnexpectedTokenException, DirectiveOrderException, and SassSyntaxException. The public contract includes their constructors, messages, and sourceLine and sourceColumn properties. Message factories are internal.
Syntax helpers
The following members of Bugo\SCSS\Parser\Support\ are part of the public API.
| Type | Supported members |
|---|---|
CssColorLiteral |
toHex() |
CssNamedColors |
NAMED_HEX |
NameHelper |
cssFunctionName(), hasNamespace(), isSpecialCssFunctionName(), splitNamespacedName(), splitQualifiedName() |
StringEscapeDecoder |
decodeLiteral(), encodeQuotedContent(), encodeUnquotedContent(), hexToUtf8(), protectHashes(), restoreHashes(), skipInterpolation(), skipQuotedChunk() |
StringHelper |
consumeQuotedChar(), isQuoted(), parseNumberPrefix(), trimPreservingEscapeTerminator(), unquote() |
Implementation details
Instantiate the standard parser with new Parser(). To substitute an implementation, implement both parsing interfaces. The lexer, tokens, ParsingSession, specialized parsers and their internal interfaces, InlineValueParserInterface callbacks, normalizers, and SassSourcePositions are implementation details. The lexing and normalization dependencies of the Parser constructor belong to internal wiring.
Tests
Install dependencies and run tests from the package root.
composer install
composer test
Pass a path to composer test to run a single file.
composer test -- tests/Unit/ParserTest.php
The smoke test loads all manifest types and checks parsing without the Composer autoloader.
php -d auto_prepend_file= -d auto_append_file= tests/Support/ParserCoreSmoke.php
Code checks
Run ECS, Rector, PHPStan and Psalm from the package root.
composer cs composer check composer phpstan composer psalm
Run composer fix to apply ECS formatting and composer rector to apply Rector changes.