
// Copyright {Jagger Software Limited} 2003

#include "csharp/source.hpp"

#include "csharp/harvest.hpp"
#include "csharp/syntactic_grammar.hpp"
#include "csharp/syntactic_grammar_source_parser_visitor.hpp"

namespace csharp // source::lexical_type 
{            
    source::lexical_type::~lexical_type()
    {
        delete tree;
    }
    
    source::lexical_type::iterator source::lexical_type::begin() const
    {
        return characters.begin();
    }
    
    source::lexical_type::iterator source::lexical_type::end() const
    {
        return characters.end();
    }
           
    source::lexical_type::iterator source::lexical_type::position() const
    {
        return parse_end_position;
    }

    const source::lexical_type::symbol_type * source::lexical_type::input() const
    {
        return tree;
    }
}

namespace csharp // source::syntactic_type 
{

    source::syntactic_type::syntactic_type(const lexical_type::symbol_type * root)
        : tokens()
        , tree(0)
        , parse_end_position()
    {
        harvest(root, tokens);
        
        const iterator begin = tokens.begin();
        const iterator end   = tokens.end();

        grammar_tools::tree_building_parseable_source<container_type::const_iterator> 
            code(skip_lexical_ws(begin, end), end);
            
        csharp::syntactic_grammar_source_parser_visitor<container_type::const_iterator> code_visitor(code);
        
        const bool parsed = code.parse(syntactic_grammar::instance().compilation_unit, code_visitor);
        
        parse_end_position = skip_lexical_ws(code.position(), end);
        
        tree = code.extract_tree();           
    }


    source::syntactic_type::~syntactic_type()
    {
        delete tree;
    }
    
    source::syntactic_type::iterator source::syntactic_type::begin() const
    {
        return tokens.begin();
    }
    
    source::syntactic_type::iterator source::syntactic_type::end() const
    {
        return tokens.end();
    }
        
    source::syntactic_type::iterator source::syntactic_type::position() const
    {
        return parse_end_position;
    }
    
    const source::syntactic_type::symbol_type * source::syntactic_type::compilation_unit() const
    {
        return tree;
    }
}

namespace csharp // source
{
    source::~source()
    {
    }
    
    const source::lexical_type & source::lexical() const
    {
        return lexical_source;
    }
    
    const source::syntactic_type & source::syntactic() const
    {
        return syntactic_source;
    }
}
