# -*- 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 scans a source stream using a computer
#  language specification. """

from Library.Parse.Scan.Scanners import AScanner
from Library.Parse.Scan.Tokens import AToken
from Library.Parse.Lex.Languages import TokenType

## A scanner for a source stream. 
class ASourceScanner(AScanner):
    ## Constructor
    def __init__(Self, ThisStream, ThisLanguage):
        AScanner.__init__(Self, ThisStream)
        
        Self._Language = ThisLanguage
    
    ## Initializer    
    def init(Self):
        result = AScanner.init(Self)
        
        if result:
            # Establish private attributes
            # The language used by the scanner
            Self._Language = None
            
        return result
        
    ## Peek at the next token from the source stream
    def PeekToken(Self):
        Result = None
        
        if Self._Source is not None:
            # Fetch the token
            Result = AToken.fromSource(Self._Source, Self._Language)
            
            # Put back the token
            if Result is not None:
                Result.rewind(Self._Source)
                
        return Result        
        
    ## Retrieve the next token from the source stream.
    def NextToken(Self):
        Result = None
        
        if Self._Source is not None:
            Result = AToken.fromSource(Self._Source, Self._Language)
            
            if Result.code() == TokenType.endOfLine:
                Self._line += 1
            
        return Result
        
    # Properties ---------------------------------------------------------------
    ## Retrieve the language definition used by the scanner.
    def Language(Self):
        return Self._Language
    
