aboutsummaryrefslogtreecommitdiff
path: root/build2/test/script/parser
blob: b5c77fe6a6005accbb52f2f37e0c13695386dbb9 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// file      : build2/test/script/parser -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD2_TEST_SCRIPT_PARSER
#define BUILD2_TEST_SCRIPT_PARSER

#include <build2/types>
#include <build2/utility>

#include <build2/parser>
#include <build2/diagnostics>

#include <build2/test/script/token>
#include <build2/test/script/script>

namespace build2
{
  namespace test
  {
    namespace script
    {
      class lexer;
      class runner;

      class parser: protected build2::parser
      {
      public:
        // Issue diagnostics and throw failed in case of an error.
        //
        void
        pre_parse (istream&, const path& name, script&);

        void
        parse (const path& name, script& s, runner& r)
        {
          parse (s, name, s, r);
        }

        // Recursive descent parser.
        //
        // Each parse function receives the token/type from which it should
        // start consuming. On return the token/type should contain the first
        // token that has not been consumed. For *_line() functions this is
        // the newline.
        //
      protected:
        void
        parse (scope&, const path& name, script&, runner&);

        token
        pre_parse_scope_body ();

        void
        parse_scope_body ();

        description
        pre_parse_description (token&, token_type&);

        optional<description>
        pre_parse_line (token&, token_type&,
                        optional<description>&&,
                        lines* = nullptr);

        bool
        parse_variable_line (token&, token_type&);

        pair<bool, optional<description>>
        parse_command_line (token&, token_type&, line_type, size_t);

        command_exit
        parse_command_exit (token&, token_type&);

        string
        parse_here_document (token&, token_type&, const string&, bool);

        // Customization hooks.
        //
      protected:
        virtual lookup
        lookup_variable (name&&, string&&, const location&) override;

        // Number of quoted tokens since last reset. Note that this includes
        // the peeked token, if any.
        //
      protected:
        size_t
        quoted () const;

        void
        reset_quoted (token& current);

        size_t replay_quoted_;

        // Insert id into the id map checking for duplicates.
        //
      protected:
        const string&
        insert_id (string, location);

      protected:
        using base_parser = build2::parser;

        lexer* lexer_;
        script* script_;
        runner* runner_;

        // Pre-parse state.
        //
        using id_map = std::unordered_map<string, location>;

        group* group_;
        id_map* id_map_;

        // Parse state.
        //
        scope* scope_;
      };
    }
  }
}

#endif // BUILD2_TEST_SCRIPT_PARSER