Introducing a new way to review your PRs.

Read the announcement

less / less.js

Less. The dynamic stylesheet language.

View on GitHub

17 documents · Updated Sep 7, 2026

docs / parser-and-syntax-tree

Parser and syntax tree

Less turns source text into a tree of typed nodes before it applies Less semantics or emits CSS. The parser keeps the structure of rules, selectors, declarations, values, and expressions in this tree so later stages can resolve scope, imports, interpolation, operations, and output while retaining the source file and position of each meaningful node.

Parse entry point

The parse entry point prepares the source and the context that belongs to one compilation. It applies optional pre-processing and injected variables, records the normalized source for imports, then creates a parser that returns a root Ruleset containing the complete input. A caller can receive this tree through the callback or the Promise form of the parse API. packages/…/parse.js• L7-84

The parser can also process imported source as part of the parse lifecycle. The root tree is passed to the import visitor unless import processing is disabled, so the returned parse result represents the source structure together with the import work that the next compiler stage can evaluate. packages/…/parser.js• L323-394

References: packages/…/parse.js• L7-84 packages/…/parser.js• L323-394

Source consumption

The parser is a predictive, single-pass parser. It does not create a separate token stream. Instead, grammar rules ask a shared input reader to match strings or regular expressions, move the current position, and restore a saved position when a possible construct does not fit. packages/…/parser.js• L35-66

The input reader skips whitespace and collects comments while it moves. It reads the source in chunks for speed, tracks the furthest point reached during backtracking, and reports the best available error location when the whole source does not parse. Balanced braces, brackets, parentheses, and quoted text are treated as groups while the parser scans permissive values. packages/…/parser-input.js• L36-115 packages/…/parser-input.js• L204-334

When a rule fails, the parser restores the input state rather than consuming part of a different construct. This lets the top-level grammar try the alternatives that Less allows at the same position, such as a declaration, mixin call, ruleset, variable call, value entity, or at-rule. packages/…/parser.js• L441-481

References: packages/…/parser.js• L35-66 packages/…/parser.js• L441-481 packages/…/parser-input.js• L36-115 packages/…/parser-input.js• L204-334

Tree model

The syntax tree is a hierarchy of Node objects. A node carries its semantic type, child values, a parent link, source index, and file information. These common fields let later visitors walk or replace parts of the tree while errors, debug output, and source maps can point back to the originating Less source. packages/…/node.js• L69-127

The tree catalog maps grammar concepts to node types. The parser uses these types as the shared contract with evaluation and output stages rather than passing raw source fragments between stages. packages/…/index.js• L1-55

Block structure

Ruleset is the main container for nested Less structure. It pairs selectors with child rules and also serves as the root container, where the root flag prevents an extra CSS wrapper. A block parses its contents through the same primary rule used at the top level, which gives nested rules and declarations the same grammar inside {}. packages/…/parser.js• L1698-1714 packages/…/parser.js• L1751-1774 packages/…/ruleset.js• L39-89

References: packages/…/parser.js• L1698-1714 packages/…/parser.js• L1751-1774 packages/…/ruleset.js• L39-89

Selector structure

Selector represents a selector as an ordered set of Element nodes. Each element holds a selector value and its combinator, so nesting, parent references, interpolation, attributes, and selector extensions remain structured until selector processing joins them. A selector may also carry an extend list or a guard condition. packages/…/parser.js• L1508-1555 packages/…/parser.js• L1566-1642 packages/…/selector.js• L10-52 packages/…/element.js• L7-45

References: packages/…/parser.js• L1508-1555 packages/…/parser.js• L1566-1642 packages/…/selector.js• L10-52 packages/…/element.js• L7-45

Declaration structure

Declaration connects a property name to a parsed value. Its name can be a plain string or a sequence of nodes when interpolation or property merging is present. Its value is kept as a tree value even when the parser first receives text, and the node records whether it defines a variable, carries !important, or represents an inline call argument. During evaluation, interpolated names become property names and values resolve in the current Less context; a detached ruleset cannot become an ordinary property value. This distinction lets later stages treat variable definitions as scope data while treating ordinary declarations as CSS properties. packages/…/parser.js• L1775-1854 packages/…/declaration.js• L27-139

References: packages/…/parser.js• L1775-1854 packages/…/declaration.js• L27-139

At-rule structure

AtRule represents CSS at-rules and Less-aware nested at-rules with a name, a prelude value, and either declarations or nested rules. The parser uses the at-rule name to select the broad grammar for identifiers, expressions, blocks, and unknown forms. This preserves enough structure for later stages to decide whether the at-rule stays rooted, nests, bubbles, or becomes a simple declaration block. packages/…/parser.js• L2395-2524 packages/…/atrule.js• L24-109

References: packages/…/parser.js• L2395-2524 packages/…/atrule.js• L24-109

Value structure

Less separates the structure of a declaration value from the property that owns it. A Value is a comma-delimited list of Expression nodes. Each expression is a space-delimited sequence of entities, or an operation tree when Less math applies. This model keeps list separators, spacing, and computation boundaries available to evaluation and CSS generation. packages/…/parser.js• L2526-2549 packages/…/parser.js• L2873-2911 packages/…/value.js• L5-26 packages/…/expression.js• L9-28

The smallest value entities include dimensions, colors, keywords, quoted strings, URLs, variables, property accessors, function calls, JavaScript expressions, and anonymous text. The parser creates a typed node when later stages need to resolve or evaluate the value. It keeps text anonymous when the value can pass through without finer Less meaning. packages/…/parser.js• L496-707 packages/…/parser.js• L1457-1466

Operations form a binary tree with an operator and two operands. The parser records multiplication and addition precedence, whether the source used spacing, and whether parentheses affect the operation. Evaluation later decides whether the operation performs math or remains CSS text. packages/…/parser.js• L2556-2651 packages/…/operation.js• L9-22

References: packages/…/parser.js• L496-707 packages/…/parser.js• L1457-1466 packages/…/parser.js• L2526-2549 packages/…/parser.js• L2556-2651 packages/…/parser.js• L2873-2911 packages/…/value.js• L5-26 packages/…/expression.js• L9-28 packages/…/operation.js• L9-22

Less-specific constructs

The parser represents Less extensions as first-class nodes instead of flattening them into CSS text. Variables and property accessors remain references. Mixin definitions retain their parameter rules and guard. Mixin calls retain their selector, arguments, and optional lookup. Detached rulesets retain a block that can be passed as a value. These forms give evaluation the information it needs to resolve scope and expand reusable rules. packages/…/parser.js• L790-853 packages/…/parser.js• L1098-1181 packages/…/parser.js• L1716-1746

Mixin definitions use a ruleset-like node because their body is a block and their selector-like name participates in lookup. A mixin call later searches the active ruleset frames and expands the matching definition into rules. The parser's job is to keep the call and definition separate so argument matching and guards remain a later semantic step. packages/…/mixin-definition.js• L29-76 packages/…/mixin-call.js• L25-47 packages/…/mixin-call.js• L163-239

Interpolation can appear in values, selectors, property names, at-rule preludes, and URLs. The parser keeps interpolated references as nodes where possible and uses permissive text groups where the grammar must preserve CSS syntax that Less does not interpret. Lookup chains such as @map[key] remain attached to variable-call or namespace nodes for later resolution. packages/…/parser.js• L815-853 packages/…/parser.js• L1856-1919 packages/…/parser.js• L2913-2981

Conditions are separate nodes used by guarded selectors, mixin guards, and conditional function forms. They hold operands and a comparison or logical operator. This allows the same parsed condition to be evaluated against the current Less context rather than being decided while source text is read. packages/…/parser.js• L2653-2844 packages/…/condition.js• L5-22

References: packages/…/parser.js• L790-853 packages/…/parser.js• L1098-1181 packages/…/parser.js• L1716-1746 packages/…/parser.js• L1856-1919 packages/…/parser.js• L2653-2844 packages/…/parser.js• L2913-2981 packages/…/mixin-definition.js• L29-76 packages/…/mixin-call.js• L25-47 packages/…/mixin-call.js• L163-239 packages/…/condition.js• L5-22

Import nodes

An import is first represented as an Import node with its path, media or feature data, and import options. The node keeps the request in the tree while the import system fetches and parses the referenced source. Later evaluation can replace the node with the imported rules or keep it as a CSS import, depending on its options and resolved path. packages/…/import.js• L33-82 packages/…/import.js• L99-109

Imported Less source is parsed into its own tree and attached to the import node before evaluation incorporates it. This separation keeps file lookup and source loading in the import system while preserving a normal tree shape for the compiler stages that follow. See Imports and file resolution for the file and queue lifecycle.

References: packages/…/import.js• L33-82 packages/…/import.js• L99-109 packages/…/import.js• L162-220

Source context

Every node that comes from source records an input index and file information, or inherits them from its parent. The index identifies the source position used for diagnostics. File information identifies the source file and carries path context needed by imports, URL handling, and source maps. packages/…/node.js• L69-127

The parser creates source-aware nodes with the current file and an index adjusted for the source being parsed. It also stores comments as nodes, so comment text and placement can survive long enough for output rules to decide whether to emit it. When requested, rulesets and at-rules also receive line information for debug output. packages/…/parser.js• L225-231 packages/…/parser.js• L484-490 packages/…/comment.js• L7-24

The same context travels into later tree processing. ParseTree transforms the root and wraps errors with import context. If source maps are enabled, CSS output receives node file and index data while it writes the final result. See Evaluation and CSS generation for the stages that consume this context. packages/…/parse-tree.js• L5-20 packages/…/parse-tree.js• L36-119

References: packages/…/parser.js• L225-231 packages/…/parser.js• L484-490 packages/…/node.js• L69-127 packages/…/comment.js• L7-24 packages/…/parse-tree.js• L5-20 packages/…/parse-tree.js• L36-119

Tree handoff

Parsing ends with a root Ruleset rather than CSS text. The root is marked as a special container, and each child node keeps the structure needed for evaluation. Visitors then traverse this tree to resolve Less meaning, join selectors, apply extensions, mark visibility, and prepare CSS output. packages/…/parser.js• L323-335 packages/…/transform-tree.js• L44-101

The visitor contract follows node ownership. A node accepts a visitor for its child fields, while a replacing visitor may return a new node or expand an array of nodes. This lets semantic stages change tree shape without requiring the parser to know how a mixin expands, how an import resolves, or how a selector joins with its parent. packages/…/node.js• L159-169 packages/…/visitor.js• L45-128

See Less compiler pipeline for the complete stage order. The parser and tree stage supplies the structured input that evaluation and CSS generation consume.

References: packages/…/parser.js• L323-335 packages/…/transform-tree.js• L44-101 packages/…/node.js• L159-169 packages/…/visitor.js• L45-128

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep parser alternatives and node constructors aligned. A grammar change that returns a different node shape can break evaluation without causing a parse error.
  • Keep source index and file-info arguments aligned when adding a node or reparsing a fragment. Diagnostics and source maps depend on both values.
  • Keep Value, Expression, and Operation nesting consistent with comma, space, parenthesis, and math rules. CSS output and Less evaluation read these boundaries differently.
  • Keep declaration name and value handling aligned with variable scope and property output. Interpolated names must not become new variable definitions after evaluation.
  • Keep import node options aligned with import resolution. The same node must support both inlined Less content and a CSS import that remains in output.
  • Keep visitor child traversal aligned with new node fields. A field that is not visited can retain unresolved source nodes after evaluation.

Important failure modes

TriggerConsequenceGuard
A parser rule consumes input but does not finish its required delimiterThe parser reports a parse error at the furthest useful position instead of returning a partial treeUse saved input state for alternatives and require the matching block, bracket, or terminator
A value is grouped as anonymous text when it contains Less referencesVariables or interpolation pass through as literal textUse the structured value path when the source contains entities that later stages must resolve
A reparsed selector or fragment loses its starting index or file infoErrors and source mappings point to the wrong source locationPass the original node context into the parser's fragment entry point
An interpolated declaration name is treated as a variable definition after evaluationA generated property name enters Less scope instead of CSS outputPreserve the distinction between source variable names and evaluated property names
A detached ruleset is used as an ordinary property valueCSS generation fails instead of emitting a property with an invalid object valueReject ruleset values for non-variable declarations during declaration evaluation
A new node child is omitted from acceptVisitors leave part of the tree unprocessedVisit every node-valued field that can contain Less syntax
An import is treated as plain output before its options are resolvedLess content may remain unexpanded or a CSS import may be inlined incorrectlyPreserve an Import node until import evaluation decides its mode

Key files

FileWhy to read it
packages/…/parse.jsShows the public parse setup and the context passed into the parser.
packages/…/parser.jsDefines the grammar entry points and the mapping from Less constructs to tree nodes.
packages/…/parser-input.jsExplains matching, whitespace and comment handling, backtracking, grouping, and parse errors.
packages/…/node.jsDefines the shared node contract and source-context behavior.
packages/…/index.jsLists the node types that form the syntax tree model.
packages/…/ruleset.jsShows the main nested container and root tree behavior.
packages/…/declaration.jsShows how property and variable declarations carry parsed values and preserve their evaluation context.
packages/…/selector.jsShows how selector elements, guards, and extensions remain structured.
packages/…/value.js and packages/…/expression.jsShow the list and expression layers used for declaration values.
packages/…/import.jsShows how an import remains a tree node until import evaluation.
packages/…/parse-tree.jsShows how the parsed root is handed to transformation and CSS output.