aboutsummaryrefslogtreecommitdiff
blob: 23571ef2a48583d97abe5627cbde76b81f585fa7 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
   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.cpp
/// \brief a wrapper class that helps interpret from istream and string
///
#include "core/bash_ast.h"

#include <fstream>
#include <sstream>
#include <thread>

#include <boost/algorithm/string/erase.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/numeric/conversion/cast.hpp>

#include "core/interpreter.h"
#include "exceptions.h"
#include "libbashLexer.h"
#include "libbashParser.h"
#include "libbashWalker.h"

void bash_ast::read_script(const std::istream& source, bool trim)
{
  std::stringstream stream;
  stream << source.rdbuf();
  script = stream.str();
  boost::algorithm::erase_all(script, "\\\n");
  if(trim)
    boost::trim_if(script, boost::is_any_of(" \t\n"));
}

bash_ast::bash_ast(const std::istream& source,
                   std::function<pANTLR3_BASE_TREE(plibbashParser)> p,
                   bool trim): parse(p)
{
  read_script(source, trim);
  init_parser("unknown source");
}

bash_ast::bash_ast(const std::string& script_path,
                   std::function<pANTLR3_BASE_TREE(plibbashParser)> p,
                   bool trim): parse(p)
{
  std::stringstream stream;
  std::ifstream file_stream(script_path);
  if(!file_stream)
    throw libbash::parse_exception(script_path + " can't be read");

  read_script(file_stream, trim);
  init_parser(script_path);
}

namespace
{
  std::mutex string_mutex;

  pANTLR3_STRING locked_newRaw8(pANTLR3_STRING_FACTORY factory)
  {
    std::lock_guard<std::mutex> l(string_mutex);
    pANTLR3_STRING_FACTORY pristine = antlr3StringFactoryNew();
    pANTLR3_STRING result = pristine->newRaw(factory);
    pristine->close(pristine);
    return result;
  }

  void locked_destroy(pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string)
  {
    std::lock_guard<std::mutex> l(string_mutex);
    pANTLR3_STRING_FACTORY pristine = antlr3StringFactoryNew();
    pristine->destroy(factory, string);
    pristine->close(pristine);
  }
}

void bash_ast::init_parser(const std::string& script_path)
{
  input.reset(antlr3NewAsciiStringInPlaceStream(
    reinterpret_cast<pANTLR3_UINT8>(const_cast<char*>(script.c_str())),
    // We do not support strings longer than the max value of ANTLR3_UNIT32
    boost::numeric_cast<ANTLR3_UINT32>(script.size()),
    NULL));

  if(!input)
    throw libbash::parse_exception("Unable to open file " + script + " due to malloc() failure");

  input->fileName = input->strFactory->newStr(
      input->strFactory,
      reinterpret_cast<pANTLR3_UINT8>(const_cast<char*>(script_path.c_str())));

  lexer.reset(libbashLexerNew(input.get()));
  if(!lexer)
    throw libbash::parse_exception("Unable to create the lexer due to malloc() failure");

  token_stream.reset(antlr3CommonTokenStreamSourceNew(
      ANTLR3_SIZE_HINT, lexer->pLexer->rec->state->tokSource));
  if(!token_stream)
    throw libbash::parse_exception("Out of memory trying to allocate token stream");

  parser.reset(libbashParserNew(token_stream.get()));
  if(!parser)
    throw libbash::parse_exception("Out of memory trying to allocate parser");

  ast = parse(parser.get());
  ast->strFactory->newRaw = &locked_newRaw8;
  ast->strFactory->destroy = &locked_destroy;
  if(parser->pParser->rec->getNumberOfSyntaxErrors(parser->pParser->rec))
    throw libbash::parse_exception("Something wrong happened while parsing");
}

std::string bash_ast::get_dot_graph()
{
  antlr_pointer<ANTLR3_COMMON_TREE_NODE_STREAM_struct> nodes(
    antlr3CommonTreeNodeStreamNewTree(ast, ANTLR3_SIZE_HINT));
  pANTLR3_STRING graph = nodes->adaptor->makeDot(nodes->adaptor, ast);
  return std::string(reinterpret_cast<char*>(graph->chars));
}

std::string bash_ast::get_string_tree()
{
  return std::string(reinterpret_cast<char*>(ast->toStringTree(ast)->chars));
}

namespace
{
  void print_line_counter(std::stringstream& result,
                          pANTLR3_COMMON_TOKEN token,
                          int& line_counter,
                          int pos)
  {
    char* text = reinterpret_cast<char*>(token->getText(token)->chars);
    for(int i = pos; text[i] == '\n'; ++i)
      result << '\n' << line_counter++ << "\t";
  }
}

std::string bash_ast::get_parser_tokens(antlr_pointer<ANTLR3_COMMON_TOKEN_STREAM_struct>& token_stream,
                                        std::function<std::string(ANTLR3_UINT32)> token_map)
{
  std::stringstream result;
  int line_counter = 1;

  // output line number for the first line
  result << line_counter++ << "\t";

  pANTLR3_VECTOR token_list = token_stream->getTokens(token_stream.get());
  unsigned token_size = token_list->size(token_list);

  for(unsigned i = 0u; i != token_size; ++i)
  {
    pANTLR3_COMMON_TOKEN token = reinterpret_cast<pANTLR3_COMMON_TOKEN>
      (token_list->get(token_list, i));
    std::string tokenName = token_map(token->getType(token));

    if(tokenName != "EOL" && tokenName != "COMMENT" && tokenName != "CONTINUE_LINE")
    {
      result << tokenName << " ";
    }
    // Output \n and line number before each COMMENT token for better readability
    else if(tokenName == "COMMENT")
    {
      print_line_counter(result, token, line_counter, 0);
      result << tokenName;
    }
    // Output \n and line number after each CONTINUE_LINE/EOL token for better readability
    // omit the last \n and line number
    else if(i + 1 != token_size)
    {
      result << tokenName;
      print_line_counter(result, token, line_counter, tokenName == "CONTINUE_LINE"? 1 : 0);
    }
  }
  return result.str();
}

std::string bash_ast::get_walker_tokens(std::function<std::string(ANTLR3_UINT32)> token_map)
{
  std::stringstream result;
  antlr_pointer<ANTLR3_COMMON_TREE_NODE_STREAM_struct> nodes(
    antlr3CommonTreeNodeStreamNewTree(ast, ANTLR3_SIZE_HINT));
  pANTLR3_INT_STREAM istream = nodes->tnstream->istream;
  auto istream_size = istream->size(istream);

  for(ANTLR3_UINT32 i = 1; i <= istream_size; ++i)
  {
    ANTLR3_UINT32 token = istream->_LA(istream, boost::numeric_cast<ANTLR3_INT32>(i));
    if(token == 2)
      result << "DOWN ";
    else if(token == 3)
      result << "UP ";
    else
      result << token_map(istream->_LA(istream, boost::numeric_cast<ANTLR3_INT32>(i))) << " ";
  }
  result << std::endl;

  return result.str();
}

void bash_ast::walker_start(plibbashWalker tree_parser)
{
  tree_parser->start(tree_parser);
}

long bash_ast::walker_arithmetics(plibbashWalker tree_parser)
{
  return tree_parser->arithmetics(tree_parser);
}

std::string bash_ast::walker_string_expr(libbashWalker_Ctx_struct* tree_parser)
{
  return tree_parser->string_expr(tree_parser).libbash_value;
}

pANTLR3_BASE_TREE bash_ast::parser_start(plibbashParser parser)
{
  return parser->start(parser).tree;
}

pANTLR3_BASE_TREE bash_ast::parser_arithmetics(plibbashParser parser)
{
  return parser->arithmetics(parser).tree;
}

pANTLR3_BASE_TREE bash_ast::parser_all_expansions(libbashParser_Ctx_struct* parser)
{
  return parser->all_expansions(parser).tree;
}

pANTLR3_BASE_TREE bash_ast::parser_builtin_variable_definitions(libbashParser_Ctx_struct* parser, bool local)
{
  return parser->builtin_variable_definitions(parser, local).tree;
}

void bash_ast::call_function(plibbashWalker ctx,
                             ANTLR3_MARKER index)
{
  auto INPUT = ctx->pTreeParser->ctnstream;

  // Initialize the input stream
  auto ISTREAM = ctx->pTreeParser->ctnstream->tnstream->istream;
  ISTREAM->size(ISTREAM);

  // Push function index into INPUT
  // The actual type of ANTLR3_MARKER is ANTLR3_INT32
  INPUT->push(INPUT, boost::numeric_cast<ANTLR3_INT32>(index));
  // Execute function body
  ctx->compound_command(ctx);
}

bash_ast::walker_pointer bash_ast::create_walker(interpreter& walker,
                                                 antlr_pointer<ANTLR3_COMMON_TREE_NODE_STREAM_struct>& nodes)
{
    set_interpreter(&walker);
    walker.push_current_ast(this);

    auto deleter = [&](plibbashWalker tree_parser)
    {
      tree_parser->free(tree_parser);
      walker.pop_current_ast();
    };

    return walker_pointer(libbashWalkerNew(nodes.get()), deleter);
}