/*
 * Copyright (C) 2012 David Anderson
 *
 * This file is part of JITCraft.
 *
 * JITCraft 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.
 * 
 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY 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
 * JITCraft. If not, see http://www.gnu.org/licenses/.
 */
#ifndef _include_jitcraft_parser_h_
#define _include_jitcraft_parser_h_

#include <stdio.h>
#include "AST.h"
#include "Scanner.h"
#include "Scopes.h"
#include "Strings.h"

namespace ke {

class Parser
{
    Zone *zone_;
    CompileContext &cc_;
    PoolAllocator &pool_;
    Scanner scanner_;
    Scope *scope_;
    PoolList<NameProxy *> *unbound_;
    bool encounteredReturn_;

  private:
    void redeclarationError(BoundName *name);
    bool match(TokenKind kind);
    bool expect(TokenKind kind);
    bool matchSemicolon();

    Scope *enterScope(Scope::Kind kind);

    VariableDeclaration *variableDeclaration();
    bool matchName(String **name, TokenKind kind = TOK_NAME);
    String *expectName();
    NameProxy *bind(Handle<String> name);
    Expression *primitive();
    Expression *call(Expression *callee);
    Expression *index(Expression *left);
    Expression *prefix();
    Expression *primary();
    Expression *unary();
    Expression *multiplication();
    Expression *addition();
    Expression *shift();
    Expression *bitand_();
    Expression *bitxor();
    Expression *bitor_();
    Expression *relational();
    Expression *equals();
    Expression *and_();
    Expression *or_();
    Expression *ternary();
    Expression *assignment();
    Expression *expression();

    Statement *enum_();
    Statement *if_();
    Statement *block(Scope *scope = NULL);
    Statement *function(TokenKind tok);
    Statement *expressionStatement();
    Statement *while_();
    Statement *do_();
    Statement *forStatement();
    Statement *localStatement();
    Statement *globalStatement();
    Statement *returnStatement();

  public:
    Parser(Zone *zone, CompileContext &cc, const char *stream, size_t length);

    ParseTree *parse();
};

}

#endif // _include_jitcraft_parser_h_
