# -*- coding: UTF-8 -*-
#    This file is part of arena.
#   
#    arena is free software: you can redistribute it and/or modify it
#    under the terms of the GNU General Public License as published by the
#    Free Software Foundation; either version 3 of the License or, at your
#    option, any later version.
#
#    arena is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABLITY
#    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
#    License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with arena.  If not, see <http://www.gnu.org/licenses/>. 

## @file
#  This file defines a class that parses a source stream.  The class constructs
#  its own scanner and symbol tables for that purpose.

from Library.Parse.Parsers import AParser
from Library.Parse.Semantic.Statements import AStatement
from Library.Parse.Lex.Languages import TokenType

# Source parser errors
## The error message used when one token is found where another was expected
sperrMissingToken = "expected {} where {} was found"

## A parser that parses source code. 
class ASourceParser(AParser):
    ## Construct a new parser instance.
    #
    #  In addition to the parameters expected by AParser.__init__(), this
    #  routine expects that \p params will contain the following items:
    #  - Language -- A reference to the language definition to use when parsing
    #                the specified source.
    def __init__(Self, **params):
        AParser.__init__(Self, **params)
        
        if "Language" in Self._params:
            Self._Language = Self._params["Language"]
            del Self._params["Language"]
    
    ## Initializer
    def init(Self):
        result = AParser.init(Self)
        
        if result:
            # Establish private attributes
            # The language used by the parser
            Self._Language = None
            # The symbol tables used by the parser
            Self._Symbols = {}
            # The parser's current scope (the symbol table currently in use)
            Self._Scope = None
            
        return result
    
    ## Destructor
    def __del__(Self):
        # Free the symbol tables
        del Self._Symbols
        
        AParser.__del__(Self)
        
    # Properties ---------------------------------------------------------------
    ## Retrieve the language definition used by the parser
    def Language(Self):
        return Self._Language
        
    ## Retrieve the symbol tables used by the parser. 
    def Symbols(Self):
        return Self._Symbols
        
    ## Retrieve the current symbol table being used by the parser.
    def Scope(Self):
        return Self._Scope
