1 /********************* */
4 ** Original author: cconway
5 ** Major contributors: mdeters
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009, 2010, 2011 The Analysis of Computer Systems Group (ACSys)
9 ** Courant Institute of Mathematical Sciences
10 ** New York University
11 ** See the file COPYING in the top-level source directory for licensing
12 ** information.\endverbatim
14 ** \brief Base for parser inputs.
16 ** Base for parser inputs.
19 #include "cvc4parser_public.h"
21 #ifndef __CVC4__PARSER__INPUT_H
22 #define __CVC4__PARSER__INPUT_H
29 #include "expr/expr.h"
30 #include "expr/expr_manager.h"
31 #include "parser/parser_exception.h"
32 #include "util/Assert.h"
33 #include "util/language.h"
43 class CVC4_PUBLIC InputStreamException
: public Exception
{
46 InputStreamException(const std::string
& msg
);
47 virtual ~InputStreamException() throw() { }
50 /** Wrapper around an input stream. */
53 /** The name of this input stream. */
56 /** Indicates whether the input file is a temporary that we should
58 bool d_fileIsTemporary
;
62 /** Initialize the input stream with a name. */
63 InputStream(std::string name
, bool isTemporary
=false) :
65 d_fileIsTemporary(isTemporary
) {
71 virtual ~InputStream() {
72 if( d_fileIsTemporary
) {
73 remove(d_name
.c_str());
77 /** Get the name of this input stream. */
78 const std::string
getName() const;
84 * An input to be parsed. The static factory methods in this class (e.g.,
85 * <code>newFileInput</code>, <code>newStringInput</code>) create a parser
86 * for the given input language and attach it to an input source of the
89 class CVC4_PUBLIC Input
{
90 friend class Parser
; // for parseError, parseCommand, parseExpr
91 friend class ParserBuilder
;
93 /** The input stream. */
94 InputStream
*d_inputStream
;
96 /* Since we own d_inputStream and it needs to be freed, we need to prevent
97 * copy construction and assignment. Mark them private and do not define
100 Input(const Input
& input
) CVC4_UNUSED
;
101 Input
& operator=(const Input
& input
) CVC4_UNUSED
;
105 /** Create an input for the given file.
107 * @param lang the input language
108 * @param filename the input filename
109 * @param useMmap true if the parser should use memory-mapped I/O (default: false)
111 static Input
* newFileInput(InputLanguage lang
,
112 const std::string
& filename
,
113 bool useMmap
= false)
114 throw (InputStreamException
, AssertionException
);
116 /** Create an input for the given stream.
118 * @param lang the input language
119 * @param input the input stream
120 * @param name the name of the stream, for use in error messages
122 static Input
* newStreamInput(InputLanguage lang
,
124 const std::string
& name
,
125 bool lineBuffered
= false)
126 throw (InputStreamException
, AssertionException
);
128 /** Create an input for the given string
130 * @param lang the input language
131 * @param input the input string
132 * @param name the name of the stream, for use in error messages
134 static Input
* newStringInput(InputLanguage lang
,
135 const std::string
& input
,
136 const std::string
& name
)
137 throw (InputStreamException
, AssertionException
);
140 /** Destructor. Frees the input stream and closes the input. */
143 /** Retrieve the remaining text in this input. */
144 virtual std::string
getUnparsedText() = 0;
146 /** Get the language that this Input is reading. */
147 virtual InputLanguage
getLanguage() const throw() = 0;
153 * @param inputStream the input stream
155 Input(InputStream
& inputStream
);
157 /** Retrieve the input stream for this parser. */
158 InputStream
*getInputStream();
160 /** Parse a command from the input by invoking the
161 * implementation-specific parsing method. Returns
162 * <code>NULL</code> if there is no command there to parse.
164 * @throws ParserException if an error is encountered during parsing.
166 virtual Command
* parseCommand()
167 throw (ParserException
, TypeCheckingException
, AssertionException
) = 0;
170 * Throws a <code>ParserException</code> with the given message.
172 virtual void parseError(const std::string
& msg
)
173 throw (ParserException
, AssertionException
) = 0;
175 /** Parse an expression from the input by invoking the
176 * implementation-specific parsing method. Returns a null
177 * <code>Expr</code> if there is no expression there to parse.
179 * @throws ParserException if an error is encountered during parsing.
181 virtual Expr
parseExpr()
182 throw (ParserException
, TypeCheckingException
, AssertionException
) = 0;
184 /** Set the Parser object for this input. */
185 virtual void setParser(Parser
& parser
) = 0;
189 }/* CVC4::parser namespace */
190 }/* CVC4 namespace */
192 #endif /* __CVC4__PARSER__ANTLR_INPUT_H */