/** * Copyright 2017 Matt Acosta * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { INode } from './INode'; import { ISyntaxNode, ISyntaxNodeOrList } from '../syntax/ISyntaxNode'; import { NodeFlags } from './NodeFlags'; import { SyntaxDiagnostic } from '../../diagnostics/SyntaxDiagnostic'; /** * Provides a base class for all nodes in a tree (both terminal and non-terminal). */ export declare abstract class NodeBase implements INode { /** * A map of all diagnostics attached to nodes. * * Since nodes are more likely to not contain any diagnostics, this saves * memory by not adding a diagnostic field to every node. Diagnostics are * instead maintained using a weak reference to the associated node, so that * they can still be reclaimed automatically when the node is no longer * referenced. */ protected static readonly DiagnosticWeakMap: WeakMap>; /** * An empty diagnostic array. */ protected static readonly EmptyDiagnosticList: ReadonlyArray; /** * A bit field storing information about this node. */ protected abstract _flags: NodeFlags; /** * The width of the token or token collection, with trivia. */ protected abstract _fullWidth: number; /** * Stores the hash code of the node. * * This prevents recalculating the hash code of every child on every lookup. * * Fun fact: The .NET CLR saves space by storing an object's hash code in * what is usually an unused chunk of memory that was already allocated as * part of the object's overhead. */ protected abstract hash: number; /** * Constructs a `NodeBase` object. * * @param {ReadonlyArray} diagnostics * A list of diagnostics associated with the token or token collection. */ constructor(diagnostics: ReadonlyArray); /** * @inheritDoc */ get containsDiagnostics(): boolean; /** * @inheritDoc */ get containsSkippedText(): boolean; /** * @inheritDoc */ get count(): number; /** * @inheritDoc */ get diagnostics(): ReadonlyArray; /** * Gets the flags used to store metadata about this node and its children. */ abstract get flags(): NodeFlags; /** * @inheritDoc */ abstract get fullWidth(): number; /** * @inheritDoc */ get isMissing(): boolean; /** * @inheritDoc */ get isList(): boolean; /** * @inheritDoc */ get isToken(): boolean; /** * @inheritDoc */ get isTrivia(): boolean; /** * @inheritDoc */ get leadingTrivia(): INode | null; /** * @inheritDoc */ get leadingTriviaWidth(): number; /** * @inheritDoc */ get width(): number; /** * @inheritDoc */ abstract childAt(index: number): INode | null; /** * @inheritDoc */ abstract createSyntaxNode(parent: ISyntaxNode | null, offset: number): ISyntaxNodeOrList; /** * Determines if the current node is equal to a given node. */ abstract equals(value: NodeBase): boolean; /** * @inheritDoc */ abstract hashCode(): number; /** * @inheritDoc */ indexAtOffset(relativeOffset: number): number; /** * @inheritDoc */ offsetAt(index: number): number; /** * @inheritDoc */ abstract withDiagnostics(diagnostics: ReadonlyArray): INode; /** * Calculates the hash code of the node. */ protected abstract computeHashCode(): number; /** * Updates the flags and width of the current node given the flags and width * of a child node. */ protected abstract updateFlagsAndWidth(flags: NodeFlags, fullWidth: number): void; /** * Gets the first node that does not have any children. */ private getFirstToken; /** * Gets the last node that does not have any children. */ private getLastToken; }