GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/parser/input.h Lines: 7 9 77.8 %
Date: 2021-03-23 Branches: 1 2 50.0 %

Line Exec Source
1
/*********************                                                        */
2
/*! \file input.h
3
 ** \verbatim
4
 ** Top contributors (to current version):
5
 **   Christopher L. Conway, Morgan Deters, Tim King
6
 ** This file is part of the CVC4 project.
7
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 ** in the top-level source directory and their institutional affiliations.
9
 ** All rights reserved.  See the file COPYING in the top-level source
10
 ** directory for licensing information.\endverbatim
11
 **
12
 ** \brief Base for parser inputs.
13
 **
14
 ** Base for parser inputs.
15
 **/
16
17
#include "cvc4parser_public.h"
18
19
#ifndef CVC4__PARSER__INPUT_H
20
#define CVC4__PARSER__INPUT_H
21
22
#include <stdio.h>
23
24
#include <iostream>
25
#include <string>
26
#include <vector>
27
28
#include "api/cvc4cpp.h"
29
#include "cvc4_export.h"
30
#include "options/language.h"
31
#include "parser/parser_exception.h"
32
33
namespace CVC4 {
34
35
class Command;
36
37
namespace parser {
38
39
class InputStreamException : public Exception
40
{
41
 public:
42
  InputStreamException(const std::string& msg);
43
};
44
45
/** Wrapper around an input stream. */
46
class InputStream
47
{
48
  /** The name of this input stream. */
49
  std::string d_name;
50
51
  /** Indicates whether the input file is a temporary that we should
52
    * delete on exit. */
53
  bool d_fileIsTemporary;
54
55
 protected:
56
  /** Initialize the input stream with a name. */
57
5766
  InputStream(std::string name, bool isTemporary=false) :
58
    d_name(name),
59
5766
    d_fileIsTemporary(isTemporary) {
60
5766
  }
61
62
 public:
63
  /** Destructor. */
64
11532
  virtual ~InputStream() {
65
5766
    if( d_fileIsTemporary ) {
66
      remove(d_name.c_str());
67
    }
68
5766
  }
69
70
  /** Get the name of this input stream. */
71
  const std::string getName() const;
72
}; /* class InputStream */
73
74
class Parser;
75
76
/**
77
 * An input to be parsed. The static factory methods in this class (e.g.,
78
 * <code>newFileInput</code>, <code>newStringInput</code>) create a parser
79
 * for the given input language and attach it to an input source of the
80
 * appropriate type.
81
 */
82
class CVC4_EXPORT Input
83
{
84
  friend class Parser; // for parseError, parseCommand, parseExpr
85
  friend class ParserBuilder;
86
87
  /** The input stream. */
88
  InputStream *d_inputStream;
89
90
  /* Since we own d_inputStream and it needs to be freed, we need to prevent
91
   * copy construction and assignment.  Mark them private and do not define
92
   * them.
93
   */
94
  Input(const Input& input) = delete;
95
  Input& operator=(const Input& input) = delete;
96
97
 public:
98
  /** Create an input for the given file.
99
    *
100
    * @param lang the input language
101
    * @param filename the input filename
102
    * @param useMmap true if the parser should use memory-mapped I/O (default: false)
103
    */
104
  static Input* newFileInput(InputLanguage lang,
105
                             const std::string& filename,
106
                             bool useMmap = false);
107
108
  /** Create an input for the given stream.
109
   *
110
   * @param lang the input language
111
   * @param input the input stream
112
   * @param name the name of the stream, for use in error messages
113
   * @param lineBuffered whether this Input should be line-buffered
114
   * (false, the default, means that the entire Input might be read
115
   * before being lexed and parsed)
116
   */
117
  static Input* newStreamInput(InputLanguage lang,
118
                               std::istream& input,
119
                               const std::string& name,
120
                               bool lineBuffered = false);
121
122
  /** Create an input for the given string
123
   *
124
   * @param lang the input language
125
   * @param input the input string
126
   * @param name the name of the stream, for use in error messages
127
   */
128
  static Input* newStringInput(InputLanguage lang,
129
                               const std::string& input,
130
                               const std::string& name);
131
132
  /** Destructor. Frees the input stream and closes the input. */
133
  virtual ~Input();
134
135
  /** Retrieve the name of the input stream */
136
49
  const std::string getInputStreamName() { return getInputStream()->getName(); }
137
 protected:
138
  /** Create an input.
139
   *
140
   * @param inputStream the input stream
141
   */
142
  Input(InputStream& inputStream);
143
144
  /** Retrieve the input stream for this parser. */
145
  InputStream *getInputStream();
146
147
  /** Parse a command from the input by invoking the
148
   * implementation-specific parsing method.  Returns
149
   * <code>NULL</code> if there is no command there to parse.
150
   *
151
   * @throws ParserException if an error is encountered during parsing.
152
   */
153
  virtual Command* parseCommand() = 0;
154
155
  /**
156
   * Issue a warning to the user, with source file, line, and column info.
157
   */
158
  virtual void warning(const std::string& msg) = 0;
159
160
  /**
161
   * Throws a <code>ParserException</code> with the given message.
162
   */
163
  virtual void parseError(const std::string& msg,
164
                          bool eofException = false) = 0;
165
166
  /** Parse an expression from the input by invoking the
167
   * implementation-specific parsing method. Returns a null
168
   * <code>api::Term</code> if there is no expression there to parse.
169
   *
170
   * @throws ParserException if an error is encountered during parsing.
171
   */
172
  virtual api::Term parseExpr() = 0;
173
174
  /** Set the Parser object for this input. */
175
  virtual void setParser(Parser& parser) = 0;
176
177
}; /* class Input */
178
179
}/* CVC4::parser namespace */
180
}/* CVC4 namespace */
181
182
#endif /* CVC4__PARSER__ANTLR_INPUT_H */