aboutsummaryrefslogtreecommitdiff
blob: 50ca7a6cce2e42fdf18d7b0eab307541cdf097e1 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
   Please use git log for copyright holder and year information

   This file is part of libbash.

   libbash 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 2 of the License, or
   (at your option) any later version.

   libbash 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 libbash.  If not, see <http://www.gnu.org/licenses/>.
*/
///
/// \file bash_ast.h
/// \brief a class that helps interpret from istream and string
///

#ifndef LIBBASH_CORE_PARSER_BUILDER_H_
#define LIBBASH_CORE_PARSER_BUILDER_H_

#include <type_traits>
#include <functional>
#include <istream>
#include <memory>
#include <string>
#include <vector>

#include <antlr3.h>
#include <boost/utility.hpp>

struct libbashLexer_Ctx_struct;
struct libbashParser_Ctx_struct;
struct libbashWalker_Ctx_struct;
class interpreter;

/// \class antlr_pointer
/// \brief customized unique_ptr for antlr objects.
template<typename T>
class antlr_pointer: public std::unique_ptr<T, std::function<void(T*)>>
{
  /// the constructor of base class
  typedef std::unique_ptr<T, std::function<void(T*)>> parent;
public:
  /// \brief constructor of antlr_pointer
  /// \param p the pointer to the antlr objects, it should provide a method called 'free'
  ///          to free the memory
  antlr_pointer(T* p = 0) : parent(p, [](T* to_delete) { to_delete->free(to_delete); }) {};
};

/// \class bash_ast
/// \brief a wrapper class that helps interpret from istream and string
class bash_ast: public boost::noncopyable
{
  antlr_pointer<ANTLR3_INPUT_STREAM_struct> input;
  std::string script;
  antlr_pointer<libbashLexer_Ctx_struct> lexer;
  antlr_pointer<ANTLR3_COMMON_TOKEN_STREAM_struct> token_stream;
  antlr_pointer<libbashParser_Ctx_struct> parser;
  pANTLR3_BASE_TREE ast;
  std::function<pANTLR3_BASE_TREE(libbashParser_Ctx_struct*)> parse;

  typedef std::unique_ptr<libbashWalker_Ctx_struct, std::function<void(libbashWalker_Ctx_struct*)>> walker_pointer;

  void read_script(const std::istream& source);
  void init_parser(const std::string& script_path);
  walker_pointer create_walker(interpreter& walker,
                               antlr_pointer<ANTLR3_COMMON_TREE_NODE_STREAM_struct>& nodes);

public:
  /// \brief build AST from istream
  /// \param source input source
  /// \param p the parser rule for building the AST
  bash_ast(const std::istream& source,
           std::function<pANTLR3_BASE_TREE(libbashParser_Ctx_struct*)> p=parser_start);

  /// \brief build AST from string
  /// \param script_path input source
  /// \param p the parser rule for building the AST
  bash_ast(const std::string& script_path,
           std::function<pANTLR3_BASE_TREE(libbashParser_Ctx_struct*)> p=parser_start);

  /// \brief the functor for walker start rule
  /// \param tree_parser the pointer to the tree_parser
  static void walker_start(libbashWalker_Ctx_struct* tree_parser);

  /// \brief the functor for walker arithmetics rule
  /// \param tree_parser the pointer to the tree_parser
  static long walker_arithmetics(libbashWalker_Ctx_struct* tree_parser);

  /// \brief the functor for walker string_expr rule
  /// \param tree_parser the pointer to the tree_parser
  static std::string walker_string_expr(libbashWalker_Ctx_struct* tree_parser);

  /// \brief call a function that is defined in the AST
  /// \param tree_parser the pointer to the tree_parser
  /// \param index the function index
  static void call_function(libbashWalker_Ctx_struct* tree_parser,
                            ANTLR3_MARKER index);

  /// \brief the functor for parser start rule
  /// \param parser the pointer to the parser
  static pANTLR3_BASE_TREE parser_start(libbashParser_Ctx_struct* parser);

  /// \brief the functor for parser arithmetics rule
  /// \param parser the pointer to the parser
  static pANTLR3_BASE_TREE parser_arithmetics(libbashParser_Ctx_struct* parser);

  /// \brief the functor for parser all_expansions rule
  /// \param parser the pointer to the parser
  static pANTLR3_BASE_TREE parser_all_expansions(libbashParser_Ctx_struct* parser);

  /// \brief the functor for parser builtin_variable_definitions rule
  /// \param parser the pointer to the parser
  /// \param local whether to define the variables in local scope
  static pANTLR3_BASE_TREE parser_builtin_variable_definitions(libbashParser_Ctx_struct* parser, bool local);

  ///
  /// \brief interpret the script with a given interpreter
  /// \param walker the interpreter object
  /// \param walk the walker rule to evaluate the AST
  /// \return the interpreted result
  template<typename Functor>
  typename std::result_of<Functor(libbashWalker_Ctx_struct*)>::type
  interpret_with(interpreter& walker, Functor walk)
  {
    antlr_pointer<ANTLR3_COMMON_TREE_NODE_STREAM_struct> nodes(
      antlr3CommonTreeNodeStreamNewTree(ast, ANTLR3_SIZE_HINT));
    walker_pointer p_tree_parser = create_walker(walker, nodes);
    return walk(p_tree_parser.get());
  }

  ///
  /// \brief use the start rule to interpret the script with a given interpreter
  /// \param walker the interpreter object
  void interpret_with(interpreter& walker)
  {
    interpret_with(walker, walker_start);
  }

  /// \brief get the dot graph for the AST
  /// \return the dot graph
  std::string get_dot_graph();

  /// \brief get the string tree for the AST
  /// \return the string tree
  std::string get_string_tree();

  /// \brief get parser tokens from a token stream
  /// \param token_stream the token stream
  /// \param token_mapper function that translates token numbers to token names
  /// \return the parser tokens
  static std::string get_parser_tokens(antlr_pointer<ANTLR3_COMMON_TOKEN_STREAM_struct>& token_stream,
                                       std::function<std::string(ANTLR3_UINT32)> token_mapper);

  /// \brief get walker tokens from current AST
  /// \param token_mapper function that translates token numbers to token names
  /// \return the walker tokens
  std::string get_walker_tokens(std::function<std::string(ANTLR3_UINT32)> token_mapper);
};

#endif