// file      : build/parser -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD_PARSER
#define BUILD_PARSER

#include <string>
#include <iosfwd>
#include <utility> // move()

#include <build/types>
#include <build/token>
#include <build/spec>
#include <build/variable>    // list_value
#include <build/diagnostics>

namespace build
{
  class scope;
  class target;
  class lexer;

  class parser
  {
  public:
    parser (): fail (&path_) {}

    // Issue diagnostics and throw failed in case of an error.
    //
    void
    parse_buildfile (std::istream&, const path&, scope& root, scope& base);

    buildspec
    parse_buildspec (std::istream&, const std::string& name);

    token
    parse_variable (lexer&, scope&, std::string name, token_type kind);

    list_value
    export_value ()
    {
      list_value r (std::move (export_value_));
      export_value_.clear (); // Empty state.
      return r;
    }

    // Recursive descent parser.
    //
  private:
    typedef build::names names_type;

    void
    clause (token&, token_type&);

    void
    print (token&, token_type&);

    void
    source (token&, token_type&);

    void
    include (token&, token_type&);

    void
    import (token&, token_type&);

    void
    export_ (token&, token_type&);

    void
    using_ (token&, token_type&);

    void
    variable (token&, token_type&, std::string name, token_type kind);

    std::string
    variable_name (names_type&&, const location&);

    names_type
    names (token& t, token_type& tt)
    {
      names_type ns;
      names (t, tt, ns, 0, nullptr, nullptr, nullptr);
      return ns;
    }

    void
    names (token&, token_type&,
           names_type&,
           std::size_t pair,
           const std::string* prj,
           const dir_path* dir,
           const std::string* type);

    // Buildspec.
    //
    buildspec
    buildspec_clause (token&, token_type&, token_type end);

    // Utilities.
    //
  private:
    // Switch to new root scope and return the previous one.
    //
    scope*
    switch_root (scope*);

    void
    process_default_target (token&);

    // Lexer.
    //
  private:
    token_type
    next (token&, token_type&);

    token_type
    peek ();

    const token&
    peeked () const
    {
      assert (peeked_);
      return peek_;
    }

    // Diagnostics.
    //
  private:
    const fail_mark<failed> fail;

  private:
    const std::string* path_; // Path processed by diag_relative().
    lexer* lexer_;
    target* target_; // Current target, if any.
    scope* scope_;   // Current base scope (out_base).
    scope* root_;    // Current root scope (out_root).
    const dir_path* out_root_;
    const dir_path* src_root_;
    target* default_target_;
    list_value export_value_;

    token peek_ {token_type::eos, false, 0, 0};
    bool peeked_ {false};
  };
}

#endif // BUILD_PARSER