Class SymbolTable


  • public final class SymbolTable
    extends java.lang.Object
    The SymbolTable class implements a symbol table.
    • Constructor Summary

      Constructors 
      Constructor Description
      SymbolTable()
      Creates a symbol table with a global scope.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void define​(Symbol symbol)
      Defines the specified symbol in the current scope.
      void defineReserved​(java.lang.Enum<?> kind, java.lang.String[] names)
      Defines reserved keywords by placing corresponding symbols into the global scope.
      boolean isReserved​(java.lang.String name)
      Checks whether the specified named was registered as reserved keyword.
      SymbolScope peek()
      Returns the current scope.
      void pop()
      Discards the current scope and replaces it with its outer scope.
      void push()
      Starts a new scope and sets it as the current scope.
      void push​(java.util.List<SymbolScope> scopes)
      Sets the specified scope list as the current scope.
      void push​(SymbolScope scope)
      Sets the specified scope as the current scope.
      Symbol resolve​(java.lang.String name)
      Searches for a symbol by its name in the current scope and its outer scopes.
      Symbol resolveMember​(java.lang.String name)
      Searches for a symbol by its name in the current scope only.
      Symbol resolveNested​(java.lang.String... names)
      Searches for a symbol described by an array containing its name preceded with names of the scopes the symbol is nested into.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SymbolTable

        public SymbolTable()
        Creates a symbol table with a global scope.
    • Method Detail

      • defineReserved

        public void defineReserved​(java.lang.Enum<?> kind,
                                   java.lang.String[] names)
        Defines reserved keywords by placing corresponding symbols into the global scope.
        Parameters:
        kind - Symbol kind for reserved keywords.
        names - Collection of keywords to be registered.
      • isReserved

        public boolean isReserved​(java.lang.String name)
        Checks whether the specified named was registered as reserved keyword.
        Parameters:
        name - Name to be checked.
        Returns:
        true if it is a reserved keyword of false otherwise.
      • peek

        public SymbolScope peek()
        Returns the current scope.
        Returns:
        Current scope.
      • push

        public void push()
        Starts a new scope and sets it as the current scope. The new scope will be nested into the current scope.
      • push

        public void push​(SymbolScope scope)
        Sets the specified scope as the current scope. The new scope must be nested in the old scope.
        Parameters:
        scope - Scope to be set as the current scope.
        Throws:
        java.lang.IllegalArgumentException - if scope is null; if scope is not nested into the current scope.
      • push

        public void push​(java.util.List<SymbolScope> scopes)
        Sets the specified scope list as the current scope. The new scope must be nested in the old scope.
        Parameters:
        scopes - List of scopes to be set as the current scope.
        Throws:
        java.lang.IllegalArgumentException - if scopes is null or empty; if scopes are not nested into the current scope.
      • pop

        public void pop()
        Discards the current scope and replaces it with its outer scope.
        Throws:
        java.lang.IllegalStateException - if the current scope is the global scope which cannot be discarded.
      • define

        public void define​(Symbol symbol)
        Defines the specified symbol in the current scope.
        Parameters:
        symbol - Symbol to be defined.
      • resolve

        public Symbol resolve​(java.lang.String name)
        Searches for a symbol by its name in the current scope and its outer scopes. If no such symbol is found, null is returned.
        Parameters:
        name - Symbol name.
        Returns:
        Symbol or null if it is not defined.
      • resolveMember

        public Symbol resolveMember​(java.lang.String name)
        Searches for a symbol by its name in the current scope only. Outer scopes are not searched. If no such symbol is found, null is returned.
        Parameters:
        name - Symbol name.
        Returns:
        Symbol or null if it is not defined.
      • resolveNested

        public Symbol resolveNested​(java.lang.String... names)
        Searches for a symbol described by an array containing its name preceded with names of the scopes the symbol is nested into. Search starts in the current scope and goes to outer scopes until the first nesting scope is found. Then the search is continued in that scope. If no such symbol is found, null is returned.
        Parameters:
        names - Array of names.
        Returns:
        Symbol or null if it is not defined.