Class ASymbolTable

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type ASymbolTable = class(AStringTree)

Description

This class represents a symbol table, which matches literal token values to an instance of ASymbol or one of its descendants. Symbol tables are used by parsers to collect and recall symbols declared in a source file. Such symbols typically nclude identifiers (variable, custom type, and function names) as well as string literals and numeric constants.

Instances of ASymbolTable are designed to be used at parse time, when it is more convenient to look up symbols by name. Once the source has been parsed in its entirety and is error-free, however, it is faster to look up symbols by index. For this reason, instances of ASymbolTable do not provide a readFrom method to allow them to be rebuilt from a binary stream; instead, you should create an instance of ASymbolVector to do this.

Although instances of ASymbolTable make use of the Parent property inherited from ANode, symbol tables are not normally collected into hierarchical lists but are instead kept in a vector to make symbol lookup as fast as possible. The Parent property allows the parent scope to be searched for a given symbol, in case that symbol is not found in the immediate scope. This allows global symbols to be found even when dealing with local scopes, and it also allows local symbols with the same name as a global variable to override their counterparts.

Hierarchy

Overview

Methods

Public constructor asChildOf(const ThisParent: ASymbolTable); reintroduce; virtual;
Public function hasSymbolNamed(const thisName: string): boolean; virtual;
Public function SymbolNamed(const thisName: string): ASymbol; virtual;
Public function addSymbol(const ThisSymbol: ASymbol; const freeOnFailure: boolean = true): boolean; virtual;
Public function InsertSymbol(const ThisSymbol: ASymbol): ASymbol; virtual;
Public function ParentTable: ASymbolTable; virtual;

Description

Methods

Public constructor asChildOf(const ThisParent: ASymbolTable); reintroduce; virtual;

Construct a new symbol table that will be a child of the specified table.

ThisParent will become the parent table in the sense that its symbols will be searched if a specific symbol is not found in the new table. This behavior allows variables from the parent scope to be "present" within the child scope. By following the chain of references returned by each successive call to ASymbolTable.ParentTable, one should eventually arrive at the symbol table which represents the global scope. However, symbol tables are not actually organized into hierarchical trees, but rather into a vector to make symbol lookup as fast as possible.

Public function hasSymbolNamed(const thisName: string): boolean; virtual;

Determine whether or not the table has the named symbol.

This method simply calls ABinaryTree.hasLeafWithKey and returns the result. It searches only the current symbol table.

Public function SymbolNamed(const thisName: string): ASymbol; virtual;

Fetch the named symbol.

This method first calls ABinaryTree.FetchLeaf to search the current symbol table. If a match is not found and expandSearch is True, then the method calls ABinaryTree.FetchLeaf on ASymbolTable.ParentTable.

If a match is found in the current symbol table or its parent scope, a reference to the symbol is returned. Otherwise, this routine returns Nil.

Public function addSymbol(const ThisSymbol: ASymbol; const freeOnFailure: boolean = true): boolean; virtual;

Add the specified symbol to the table.

This method simply calls ABinaryTree.addLeaf and returns the result.

Public function InsertSymbol(const ThisSymbol: ASymbol): ASymbol; virtual;

Insert the specified symbol into the table.

This method simply calls ABinaryTree.InsertLeaf and then casts the return value as an instance of ASymbol.

Public function ParentTable: ASymbolTable; virtual;

Retrieve a reference to the parent symbol table, if any. The parent table represents the scope which encloses the scope represented by the current table.

The reference returned by this routine should NOT be freed by the caller.


Generated by PasDoc 0.13.0 on 2015-01-10 17:13:18