Search


Search something to see results

Arena

Arena memory manager.

The node data is stored column-wise in typed arrays allocated for the capacity of the arena (getCapacity, 62 bytes per node). The arena grows by doubling its capacity and never frees memory by itself: detached nodes keep their memory and their ids stay valid. release, compact, trim and compactStrings reclaim the memory, compact() and trim() renumber the nodes. See the Arena Memory guide (files/arena.md).

Hierarchy (View Summary)

Index

Accessors

Constructors

Methods

node.Arena.allocatenode.Arena.appendChildnode.Arena.appendChildListnode.Arena.appendChildrennode.Arena.appendFieldnode.Arena.appendTokennode.Arena.appendTokenListnode.Arena.appendTokensnode.Arena.childCountnode.Arena.childrennode.Arena.compactnode.Arena.compactStringsnode.Arena.copySpannode.Arena.detachnode.Arena.equalsIgnoreCasenode.Arena.exportPropertiesnode.Arena.exportTransferablesnode.Arena.fieldnode.Arena.fieldListnode.Arena.fieldTokensnode.Arena.findnode.Arena.findAllnode.Arena.findByValuenode.Arena.findChildnode.Arena.findLastnode.Arena.findTokennode.Arena.findValuenode.Arena.getCapacitynode.Arena.getChildAtnode.Arena.getColorTypenode.Arena.getErrorsnode.Arena.getFirstChildnode.Arena.getFirstTokennode.Arena.getFlagsnode.Arena.getLastChildnode.Arena.getLastIdnode.Arena.getLastTokennode.Arena.getLeftnode.Arena.getNamenode.Arena.getNextSiblingnode.Arena.getNumericValuenode.Arena.getOpnode.Arena.getOp2node.Arena.getParentnode.Arena.getPreviousSiblingnode.Arena.getRightnode.Arena.getSourceIdnode.Arena.getSpanEndnode.Arena.getSpanStartnode.Arena.getStringCountnode.Arena.getTokenAtnode.Arena.getTypenode.Arena.getUnitnode.Arena.getValuenode.Arena.getValueOwnernode.Arena.getValueParentnode.Arena.hasFieldnode.Arena.hasFlagsnode.Arena.importPropertiesnode.Arena.internStringnode.Arena.isMarkednode.Arena.marknode.Arena.moveChildrennode.Arena.prependTokensnode.Arena.recyclenode.Arena.releasenode.Arena.resizenode.Arena.resolveStringnode.Arena.setnode.Arena.setChildAtnode.Arena.setChildListnode.Arena.setChildrennode.Arena.setColorTypenode.Arena.setErrorsnode.Arena.setFieldnode.Arena.setFirstChildnode.Arena.setFirstTokennode.Arena.setFlagsnode.Arena.setLastChildnode.Arena.setLeftnode.Arena.setNamenode.Arena.setNextSiblingnode.Arena.setNumericValuenode.Arena.setOpnode.Arena.setOp2node.Arena.setParentnode.Arena.setPreviousSiblingnode.Arena.setRightnode.Arena.setSpannode.Arena.setSpandStartnode.Arena.setSpanEndnode.Arena.setTokenListnode.Arena.setTokensnode.Arena.setTypenode.Arena.setUnitnode.Arena.setValuenode.Arena.siblingsnode.Arena.startsWithnode.Arena.subTokensnode.Arena.tokenCountnode.Arena.tokensnode.Arena.trimnode.Arena.typeOfnode.Arena.visitValuesnode.Arena.walknode.Arena.walkValuesnode.Arena.fromTransferableArenaObjectsnode.Arena.toTransferableArenaObject

Properties

  • get transferred(): boolean

    true when the column buffers have been transferred to another thread: the arena can no longer be used

    Returns boolean

Parameters

  • size: number = 4096

Returns Arena

Returns number

append a single child node. faster than appendChildren()

Parameters

  • id: number
  • child: number

Returns void

append children. same as appendChildren with an array

Parameters

  • id: number
  • children: number[]

Returns void

Parameters

  • id: number
  • …children: number[]

Returns void

append tokens to a structured token field

Parameters

Returns void

append a single token. faster than appendTokens()

Parameters

  • id: number
  • token: number

Returns void

append tokens. same as appendTokens with an array

Parameters

  • id: number
  • children: number[]

Returns void

Parameters

  • id: number
  • …children: number[]

Returns void

number of children

Parameters

  • id: number

Returns number

child node ids

Parameters

  • id: number
  • Optionalfilter: (arena: this, id: number) => boolean

    called with the arena and the node id

Returns number[]

drop the nodes that are not reachable from the root and renumber the remaining nodes in document order. the node ids change: ids obtained before the call (from visitors for instance) are no longer valid. the unused strings are dropped too

Parameters

  • Optionalerrors: ErrorDescription[] | null

    error descriptions whose node is remapped

Returns Uint32Array

mapping of the old ids to the new ids (0 when the node was dropped)

drop the interned strings that are no longer referenced by any node

Returns void

copy the packed location of a node

Parameters

  • target: number
  • source: number

Returns void

detach a node from its parent

Parameters

  • id: number

Returns void

Parameters

  • node: number
  • key: "val" | "nam"
  • string: string

Returns boolean

structured-cloneable properties of the arena

Returns ArenaProperties

column buffers, in the order of arenaColumns. the spans buffer is shared with the spans32 view

Returns ArenaTransferables

first token of a structured token field (single token fields: l and r of binary expressions, op, ...)

Parameters

Returns number

list node holding the tokens of a structured token field. created when missing

Parameters

Returns number

tokens of a structured token field

Parameters

Returns number[]

Search the ast subtree and return the first match, in document order

// find the first declaration which name is 'aspect-ratio'
import { EnumToken, transform } from "@tbela99/css-parser";

const result = await transform(css);
const arena = result.arena;

const node = arena.find(result.ast, (arena, node) =>
arena.getType(node) == EnumToken.DeclarationNodeType && arena.getName(node) == 'aspect-ratio');

console.log(node == null ? null : arena.toObject(node));

Parameters

  • node: number

    node id

  • matcher: AstNodeMatcher

    called with the arena and the node id

Returns number | null

the node id or null

Search the ast subtree and return all matches, in document order

Parameters

  • node: number

    node id

  • matcher: AstNodeMatcher

    called with the arena and the node id

Returns number[]

Search the value tokens of the ast subtree and return the first match: the node that owns the token and the token

// find the first node which contains the length token '30px'
import { EnumToken, transform } from "@tbela99/css-parser";

const result = await transform(css);
const {node, value} = result.arena.findByValue(result.ast, (arena, value) =>
arena.getType(value) == EnumToken.LengthTokenType && arena.getNumericValue(value) == 30 && arena.getUnit(value) == 'px') ?? {};

Parameters

  • node: number

    node id

  • matcher: AstValueMatcher

    called with the arena and the token id

Returns { node: number; value: number } | null

Parameters

  • id: number
  • predicate: (id: number) => boolean

Returns number | null

Search the ast subtree in reverse order and return the first match (the last node in document order)

Parameters

  • node: number

    node id

  • matcher: AstNodeMatcher

    called with the arena and the node id

Returns number | null

find a token in the token list

Parameters

  • id: number
  • predicate: (arena: this, id: number) => boolean

    called with the arena and the token id

Returns number | null

Search a token and its descendants, or the value tokens of a node and their descendants, and return the first match

Parameters

  • node: number

    node or token id

  • matcher: AstValueMatcher

    called with the arena and the token id

Returns number | null

the token id or null

number of nodes the columns can hold before the arena grows. see getLastId for the last allocated id

Returns number

Parameters

  • id: number
  • index: number

Returns number | null

Parameters

  • id: number

Returns number

Parameters

  • id: number

Returns ErrorDescription[] | null

first child of a node. 0 when the node has no children

Parameters

  • id: number

Returns number

first token of the token list of a node or of a token (declaration value, selector / prelude tokens, function arguments). 0 when the list is empty

Parameters

  • id: number

Returns number

Parameters

  • id: number

Returns number

Parameters

  • id: number

Returns number

last allocated node id. the ids of the arena are in the range 1 ... getLastId(), the detached nodes included

Returns number

last token of the token list of a node or of a token. 0 when the list is empty

Parameters

  • id: number

Returns number

Parameters

  • id: number

Returns number[] | null

Parameters

  • id: number

Returns string

next sibling of a node or of a token. 0 for the last node / token of the list

Parameters

  • id: number

Returns number

Parameters

  • id: number

Returns number

Parameters

  • id: number

Returns number

Parameters

  • id: number

Returns number

Parameters

  • id: number

Returns number

previous sibling of a node or of a token. 0 for the first node / token of the list

Parameters

  • id: number

Returns number

Parameters

  • id: number

Returns number[] | null

Parameters

  • id: number

Returns number

Parameters

  • id: number

Returns number

Parameters

  • id: number

Returns number

number of interned strings

Returns number

token at the given position of the token list

Parameters

  • id: number
  • index: number

Returns number | null

Parameters

  • id: number

Returns EnumToken

Parameters

  • id: number

Returns string

Parameters

  • id: number

Returns string

node that owns a value token: the declaration, the rule or the at-rule. 0 when the token is detached

Parameters

  • id: number

Returns number

parent of a value token: the node or the token that owns the token list, or the structured token that owns the field (the field list token is skipped). 0 when the token is detached

Parameters

  • id: number

Returns number

true if the field exists and is not empty

Parameters

Returns boolean

Parameters

  • id: number
  • flag: number

Returns boolean

initialize the arena from exported properties and column buffers

Parameters

Returns void

Interns a string and returns its unique ID. If the string has already been interned, it returns the existing ID.

Parameters

  • str: string

Returns number

true if the node is marked, see mark

Parameters

  • id: number

Returns boolean

mark nodes to be released or trimmed later, see release and trim. the library never releases nor trims the arena by itself

Parameters

  • …ids: number[]

Returns void

move the children of a node into another node, in order

Parameters

  • from: number
  • to: number
  • before: number = 0

    the children are inserted before this child of to. they are appended when 0

Returns void

prepend tokens to the token list

Parameters

  • id: number
  • …tokens: number[]

Returns void

recycle a detached leaf node (no parent, no children, no tokens) that is no longer referenced: its id is reused by the next allocation. the record is zeroed, so a stale reference reads an empty node

Parameters

  • id: number

Returns void

release nodes: detach them and drop their data (interned strings, errors). node ids are never reused: the memory is reclaimed by trim

Parameters

  • Optionalid: number

    the node to release. every marked node (and its descendants) is released when omitted

  • deep: boolean = false

    release the children and the tokens too

Returns void

Parameters

  • newSize: number

Returns void

Parameters

  • id: number

Returns string

set node data for a given id. The first argument is the id, the second is the type, the third is the string value, the fourth is the numeric value, and the fifth is the unit.

Parameters

Returns void

set node data for a given id. The first argument is the id, the second is the type, the third is the string value, the fourth is the numeric value, and the fifth is the unit.

Parameters

  • id: number
  • type: EnumToken
  • stringValue: number

Returns void

set node data for a given id. The first argument is the id, the second is the type, the third is the string value, and the fourth is the numeric value.

Parameters

  • id: number
  • type: EnumToken
  • stringValue: number
  • numericValue: number

Returns void

set node data for a given id. The first argument is the id, the second is the type, the third is the string value, the fourth is the numeric value, and the fifth is the unit.

Parameters

  • id: number
  • type: EnumToken
  • stringValue: number
  • numericValue: number
  • unit: number

Returns void

replace the child at the given index. the node is appended when the index is out of range

Parameters

  • id: number
  • index: number
  • child: number

Returns void

replace the children. same as setChildren with an array

Parameters

  • id: number
  • children: number[]

Returns void

Parameters

  • id: number
  • …children: number[]

Returns void

Parameters

  • id: number
  • colorType: number

Returns void

Parameters

Returns void

set the tokens of a structured token field

Parameters

Returns void

replace the first child

Parameters

  • id: number
  • firstChildId: number

Returns void

replace the first token

Parameters

  • id: number
  • firstTokenId: number

Returns void

Parameters

  • id: number
  • flags: number

Returns void

replace the last child

Parameters

  • id: number
  • lastChildId: number

Returns void

Parameters

  • id: number
  • …children: number[]

Returns void

Parameters

  • id: number
  • nameId: number

Returns void

insert a node after another one

Parameters

  • id: number
  • nextSiblingId: number

Returns void

Parameters

  • id: number
  • numericValue: number

Returns void

Parameters

  • id: number
  • opId: number

Returns void

Parameters

  • id: number
  • op2Id: number

Returns void

Parameters

  • id: number
  • parentId: number

Returns void

insert a node before another one

Parameters

  • id: number
  • previousSiblingId: number

Returns void

Parameters

  • id: number
  • rightId: null

Returns void

Parameters

  • id: number
  • …rightId: number[]

Returns void

Parameters

  • id: number
  • sourceId: number
  • start: number
  • end: number

Returns void

Parameters

  • id: number
  • start: number

Returns void

Parameters

  • id: number
  • end: number

Returns void

replace the token list. same as setTokens with an array

Parameters

  • id: number
  • tokens: number[]

Returns void

Parameters

  • id: number
  • …children: number[]

Returns void

Parameters

Returns void

Parameters

  • id: number
  • unit: number

Returns void

Parameters

  • id: number
  • valueId: number

Returns void

the children of the parent of a node, the node included

Parameters

  • id: number
  • Optionalfilter: (arena: this, id: number) => boolean

    called with the arena and the node id

Returns number[]

Parameters

  • node: number
  • key: "val" | "nam"
  • string: string

Returns boolean

direct value tokens of a node or a token: the token list, then the tokens of the structured fields and the child tokens (fractions, unary expressions)

Parameters

  • id: number

Returns number[]

number of tokens in the token list

Parameters

  • id: number

Returns number

token ids of the token list: the declaration values, the selector / prelude tokens, the function arguments

Parameters

  • id: number
  • Optionalfilter: (arena: this, id: number) => boolean

    called with the arena and the token id

Returns number[]

reclaim the memory: release the marked nodes, then drop the nodes that are not reachable from the root and renumber the remaining ones, see compact. the ids obtained before the call are no longer valid

Parameters

  • Optionalerrors: ErrorDescription[] | null

    error descriptions whose node is remapped

Returns Uint32Array

mapping of the old ids to the new ids (0 when the node was dropped)

type of a node or a token. null when the id is null

Parameters

  • id: number | null | undefined

Returns EnumToken | null

Internal

call the visitor for the tokens and their children, in the order of walkValues (the children of a token are read after the visitor returns). no iterator or stack is created: use it when the visitor is not a closure

Parameters

  • tokens: number[]
  • visitor: (arena: this, token: number) => void

Returns void

Walk the ast nodes: the node and its descendants, in document order. The iterator returns node ids (and value tokens when attributes is set). The children of a node are read when the next node is requested: the current node can be modified or detached by the caller.

import {EnumToken, transform, WalkerOptionEnum} from '@tbela99/css-parser';

const result = await transform(css);
const arena = result.arena;

for (const node of arena.walk(result.ast)) {

console.log(EnumToken[arena.getType(node)], arena.getParent(node));
}

// the filter controls the walk, see WalkerOptionEnum
function filter(arena: AstArena, node: number) {

if (arena.getType(node) == EnumToken.RuleNodeType && arena.getSelector(node).includes('html')) {

// skip the children of the current node
return WalkerOptionEnum.IgnoreChildren;
}
}

for (const node of arena.walk(result.ast, filter)) {

console.log(EnumToken[arena.getType(node)]);
}

Parameters

  • node: number

    initial node id (or value token)

  • Optionalfilter: WalkerFilter | WalkerOptions | null

    filter function or walker options

  • Optionalreverse: boolean

    walk in reverse order

Returns IterableIterator<number>

Walk value tokens and their descendants (function arguments, structured token fields, ...), in document order. The iterator returns token ids. The children of a token are read when the next token is requested: the current token can be modified or replaced by the caller.

import {EnumToken, transform} from '@tbela99/css-parser';

const css = `
body { color: color(from var(--base-color) display-p3 r calc(g + 0.24) calc(b + 0.15)); }
`;

const result = await transform(css);
const arena = result.arena;
const declaration = arena.getFirstChild(arena.getFirstChild(result.ast));

// walk the value tokens of the declaration in reverse order
for (const value of arena.walkValues(declaration, null, true)) {

console.error([EnumToken[arena.getType(value)], arena.getValue(value)]);
}

Parameters

  • tokens: number | number[]

    token ids, or a node / token id: its value tokens are walked

  • Optionalfilter: WalkerValueFilter | WalkerValueOptions | null

    filter function, called with the arena, the token id and the event, or filter options

  • Optionalreverse: boolean

    walk in reverse order

Returns IterableIterator<number>

Create an arena from the objects produced by Arena.toTransferableArenaObject. The column buffers are used as is (zero copy): the typed arrays are views on the given buffers.

Type Parameters

Parameters

Returns T

an Arena or an AstArena depending on the class the method is called on

Export the arena as a structured-cloneable object and the list of its column buffers, to send an arena to a worker (or back to the main thread) without copying the node data.

The properties (strings, errors, ...) are cloned by postMessage(), the column buffers are moved when they are passed as the transfer list: the source arena must not be used afterwards (its columns are detached). The buffers are copied instead when the transfer list is omitted.

// main thread
const [properties, transferables] = AstArena.toTransferableArenaObject(result.arena);

worker.postMessage({ ast: result.ast, properties, transferables }, transferables);

// worker
const { ast, properties, transferables } = event.data;
const arena = AstArena.fromTransferableArenaObjects(properties, transferables);

Parameters

Returns [ArenaProperties, ArenaTransferables]

the arena properties and the column buffers, see ArenaProperties and ArenaTransferables

colorType: Uint8Array

color type. previously use as (node as ColorToken).kin

current: number = 1

last allocated node id. the next node is allocated with the id current + 1 (unless a recycled id is available). 0 is never allocated: it means "no node"

dataTypes: Uint8Array

node EnumToken type array

errors: Map<number, ErrorDescription[]> = ...

error cache

firstChild: Uint32Array

first child node id array

firstToken: Uint32Array

first token node id array

flags: Uint32Array

node flags

freeList: number = 0

head of the list of recycled node ids. the list is linked through the nextSibling column, see recycle

lastChild: Uint32Array

last child node id array

lastToken: Uint32Array

last token node id array

name: Uint32Array

node name string interner id array. used to store (node as AstRule | AstDeclaration | AstKeyframesAtRule).nam and (node as AstRule | AstKeyframesRule).sel

nextSibling: Uint32Array

next sibling node id array

numericValue: Float64Array

node numeric value array. previously used as (node as NumberToken | DimensionToken).val

parents: Uint32Array

parent node id array

previousSibling: Uint32Array

previous sibling node id array

root: number = 1

root node

spans: BigUint64Array

token spans. 8 bits for source type. 28 bits for start position, 28 bits for end position. This allows us to store the start and end positions of tokens in a single 64-bit integer, which is more efficient than storing them separately.

spans32: Uint32Array

32 bits view of the spans: the low word holds the end position and the 4 low bits of the start position, the high word holds the 24 high bits of the start position and the source

stringIds: Map<string, number> = ...

string interner identifiers

strings: string[] = []

string interner values

stringValue: Uint32Array

node string value index array.

unit: Uint32Array

node unit string interner id array. previously used as (node as DimensionToken).unit