Back to index

supertuxkart  0.5+dfsg1
Public Member Functions | Private Member Functions | Private Attributes
lisp::Parser Class Reference

#include <parser.hpp>

Collaboration diagram for lisp::Parser:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Parser ()
 ~Parser ()
Lispparse (const std::string &filename)
Lispparse (std::istream &stream)

Private Member Functions

Lispread ()
LispreadList ()

Private Attributes

Lexerm_lexer
Lexer::TokenType m_token

Detailed Description

Definition at line 31 of file parser.hpp.


Constructor & Destructor Documentation

Definition at line 34 of file parser.cpp.

            : m_lexer(0)
    {}

Definition at line 40 of file parser.cpp.

    {
        delete m_lexer;
    }

Member Function Documentation

Lisp * lisp::Parser::parse ( const std::string &  filename)

Definition at line 48 of file parser.cpp.

    {
        std::ifstream in(filename.c_str());
        if(!in.good())
        {
            char msg[MAX_ERROR_MESSAGE_LENGTH];
            snprintf(msg, sizeof(msg), 
                     "Couldn't open file '%s'.",
                     filename.c_str());
            throw std::runtime_error(msg);
        }
        return parse(in);
    }

Here is the caller graph for this function:

Lisp * lisp::Parser::parse ( std::istream &  stream)

Definition at line 65 of file parser.cpp.

    {
        delete m_lexer;
        m_lexer = new Lexer(m_stream);

        m_token = m_lexer->getNextToken();
        Lisp* result = new Lisp(Lisp::TYPE_CONS);
        result->m_v.m_cons.m_car = 0;
        result->m_v.m_cons.m_cdr = readList();

        delete m_lexer;
        m_lexer = 0;

        return result;
    }

Here is the call graph for this function:

Lisp * lisp::Parser::read ( ) [private]

Definition at line 84 of file parser.cpp.

    {
        Lisp* result;
        switch(m_token)
        {
        case Lexer::TOKEN_EOF:
            {
                char msg[MAX_ERROR_MESSAGE_LENGTH];
                snprintf(msg, sizeof(msg), 
                         "Parse Error at line %d: Unexpected EOF.",
                         m_lexer->getLineNumber());
                throw std::runtime_error(msg);
            }
        case Lexer::TOKEN_CLOSE_PAREN:
            {
                char msg[MAX_ERROR_MESSAGE_LENGTH];
                snprintf(msg, sizeof(msg), 
                         "Parse Error at line %d: Unexpected ')'.",
                         m_lexer->getLineNumber());
                throw std::runtime_error(msg);
            }
        case Lexer::TOKEN_TRANSLATION:
           {
                   result = new Lisp(Lisp::TYPE_STRING);
                m_token = m_lexer->getNextToken();
                Lisp* next=read();
                if(next->getType()!=Lisp::TYPE_STRING)
                {
                    char msg[MAX_ERROR_MESSAGE_LENGTH];
                    snprintf(msg, sizeof(msg), 
                        "Parse Error at line %d: No string inside translation.",
                        m_lexer->getLineNumber());
                    throw std::runtime_error(msg);
                }
                const char* trans=_(next->m_v.m_string);
                const size_t LEN = strlen(trans) + 1;
                result->m_v.m_string = new char[LEN];
                memcpy(result->m_v.m_string, trans, LEN);
                delete next;
                break;
           }
        case Lexer::TOKEN_OPEN_PAREN:
            result = new Lisp(Lisp::TYPE_CONS);

            m_token = m_lexer->getNextToken();

            result->m_v.m_cons.m_car = read();
            result->m_v.m_cons.m_cdr = readList();

            if(m_token != Lexer::TOKEN_CLOSE_PAREN)
            {
                char msg[MAX_ERROR_MESSAGE_LENGTH];
                snprintf(msg, sizeof(msg), 
                         "Parse Error at line %d: Expected ')'.",
                         m_lexer->getLineNumber());
                throw std::runtime_error(msg);
            }
            break;
        case Lexer::TOKEN_SYMBOL:
            {
                result = new Lisp(Lisp::TYPE_SYMBOL);
                const size_t LEN = strlen(m_lexer->getString()) + 1;
                result->m_v.m_string = new char[LEN];
                memcpy(result->m_v.m_string, m_lexer->getString(), LEN);
                break;
            }
        case Lexer::TOKEN_STRING:
            {
                result = new Lisp(Lisp::TYPE_STRING);
                const size_t LEN = strlen(m_lexer->getString()) + 1;
                result->m_v.m_string = new char[LEN];
                memcpy(result->m_v.m_string, m_lexer->getString(), LEN);
                break;
            }
        case Lexer::TOKEN_INTEGER:
            result = new Lisp(Lisp::TYPE_INTEGER);
            sscanf(m_lexer->getString(), "%d", &result->m_v.m_integer);
            break;
        case Lexer::TOKEN_REAL:
            result = new Lisp(Lisp::TYPE_REAL);
            sscanf(m_lexer->getString(), "%f", &result->m_v.m_real);
            break;
        case Lexer::TOKEN_TRUE:
            result = new Lisp(Lisp::TYPE_BOOLEAN);
            result->m_v.m_boolean = true;
            break;
        case Lexer::TOKEN_FALSE:
            result = new Lisp(Lisp::TYPE_BOOLEAN);
            result->m_v.m_boolean = false;
            break;

        default:
            // this should never happen
            assert(false);
        }

        m_token = m_lexer->getNextToken();
        return result;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Lisp * lisp::Parser::readList ( ) [private]

Definition at line 187 of file parser.cpp.

    {
        Lisp* result = 0;

        while(m_token != Lexer::TOKEN_CLOSE_PAREN && m_token != Lexer::TOKEN_EOF)
        {
            Lisp* newlisp = new Lisp(Lisp::TYPE_CONS);
            newlisp->m_v.m_cons.m_car = read();
            newlisp->m_v.m_cons.m_cdr = result;
            result = newlisp;
        }

        return result;
    }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 44 of file parser.hpp.

Definition at line 45 of file parser.hpp.


The documentation for this class was generated from the following files: