summaryrefslogtreecommitdiff
path: root/cli/cli/parser.hxx
blob: 960e74b9eaa9c8a01089e5793f509d95aab7ebe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// file      : cli/parser.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// license   : MIT; see accompanying LICENSE file

#ifndef CLI_PARSER_HXX
#define CLI_PARSER_HXX

#include <map>
#include <string>
#include <vector>
#include <memory>  // unique_ptr
#include <cstddef> // size_t
#include <istream>

#include <cli/semantics/elements.hxx>
#include <cli/semantics/unit.hxx>

class token;
class lexer;

class parser
{
public:
  typedef std::vector<semantics::path> paths;

  parser (paths const& include_paths, bool collect_dependencies)
      : include_paths_ (include_paths),
        collect_dependencies_ (collect_dependencies) {}

  struct invalid_input {};

  struct parse_result
  {
    std::unique_ptr<semantics::cli_unit> unit;

    // Normalized paths of the main CLI file and files it includes and sources
    // recursively, with the duplicates suppressed.
    //
    paths dependencies;
  };

  parse_result
  parse (std::istream& is, semantics::path const& path);

private:
  struct error {};

  void
  def_unit ();

  void
  source_decl ();

  void
  include_decl ();

  bool
  decl (token&);

  void
  scope_doc (token&);

  void
  namespace_def ();

  void
  class_def ();

  bool
  option_def (token&);

  std::string
  doc_string (const char*, std::size_t);

  bool
  qualified_name (token&, std::string& name);

  bool
  fundamental_type (token&, std::string& name);

private:
  void
  recover (token& t);

private:
  paths const include_paths_;
  bool collect_dependencies_;

  bool valid_;
  semantics::path const* path_;

  lexer* lexer_;

  semantics::cli_unit* root_;
  semantics::cli_unit* cur_;
  semantics::scope* scope_;

  std::size_t doc_count_; // Scope doc counter, see scope_doc() for details.

  // If the entry's value is NULL, then the key refers to a sourced file.
  //
  typedef std::map<semantics::path, semantics::cli_unit*> include_map;
  include_map include_map_;

  paths dependencies_;
};

#endif // CLI_PARSER_HXX