1 /********************* */
4 ** Original author: cconway
5 ** Major contributors: none
6 ** Minor contributors (to current version): dejan, mdeters
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009, 2010 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 A collection of state for use by parser implementations.
16 ** A collection of state for use by parser implementations.
19 #include "cvc4parser_public.h"
21 #ifndef __CVC4__PARSER__PARSER_STATE_H
22 #define __CVC4__PARSER__PARSER_STATE_H
28 #include "parser_exception.h"
29 #include "parser_options.h"
30 #include "expr/declaration_scope.h"
31 #include "expr/expr.h"
32 #include "expr/kind.h"
33 #include "util/Assert.h"
37 // Forward declarations
47 /** Types of check for the symols */
48 enum DeclarationCheck
{
49 /** Enforce that the symbol has been declared */
51 /** Enforce that the symbol has not been declared */
53 /** Don't check anything */
57 /** Returns a string representation of the given object (for
59 inline std::string
toString(DeclarationCheck check
) {
64 return "CHECK_DECLARED";
65 case CHECK_UNDECLARED
:
66 return "CHECK_UNDECLARED";
73 * Types of symbols. Used to define namespaces.
82 /** Returns a string representation of the given object (for
84 inline std::string
toString(SymbolType type
) {
87 return "SYM_VARIABLE";
96 * This class encapsulates all of the state of a parser, including the
97 * name of the file, line number and column information, and in-scope
100 class CVC4_PUBLIC Parser
{
101 friend class ParserBuilder
;
103 /** The expression manager */
104 ExprManager
*d_exprManager
;
106 /** The input that we're parsing. */
109 /** The symbol table */
110 DeclarationScope d_declScope
;
112 /** The name of the input file. */
113 // std::string d_filename;
118 /** Are semantic checks enabled during parsing? */
119 bool d_checksEnabled
;
121 /** Are we parsing in strict mode? */
124 /** The set of operators available in the current logic. */
125 std::set
<Kind
> d_logicOperators
;
127 /** Lookup a symbol in the given namespace. */
128 Expr
getSymbol(const std::string
& var_name
, SymbolType type
);
131 /** Create a parser state. NOTE: The parser takes "ownership" of the given
132 * input and will delete it on destruction.
134 * @param exprManager the expression manager to use when creating expressions
135 * @param input the parser input
137 Parser(ExprManager
* exprManager
, Input
* input
, bool strictMode
= false);
145 /** Get the associated <code>ExprManager</code>. */
146 inline ExprManager
* getExprManager() const {
147 return d_exprManager
;
150 /** Get the associated input. */
151 inline Input
* getInput() const {
155 /** Set the declaration scope manager for this input. NOTE: This should <em>only</me> be
156 * called before parsing begins. Otherwise, previous declarations will be lost. */
157 /* inline void setDeclarationScope(DeclarationScope declScope) {
158 d_declScope = declScope;
162 * Check if we are done -- either the end of input has been reached, or some
163 * error has been encountered.
164 * @return true if parser is done
166 inline bool done() const {
170 /** Sets the done flag */
171 inline void setDone(bool done
= true) {
175 /** Enable semantic checks during parsing. */
176 void enableChecks() { d_checksEnabled
= true; }
178 /** Disable semantic checks during parsing. Disabling checks may lead to crashes on bad inputs. */
179 void disableChecks() { d_checksEnabled
= false; }
181 /** Enable strict parsing, according to the language standards. */
182 void enableStrictMode() { d_strictMode
= true; }
184 /** Disable strict parsing. Allows certain syntactic infelicities to pass without comment. */
185 void disableStrictMode() { d_strictMode
= false; }
187 bool strictModeEnabled() { return d_strictMode
; }
190 * Returns a variable, given a name.
192 * @param name the name of the variable
193 * @return the variable expression
195 Expr
getVariable(const std::string
& name
);
198 * Returns a function, given a name.
200 * @param var_name the name of the variable
201 * @return the variable expression
203 Expr
getFunction(const std::string
& name
);
206 * Returns a sort, given a name.
208 Type
getSort(const std::string
& sort_name
);
211 * Returns a (parameterized) sort, given a name and args.
213 Type
getSort(const std::string
& sort_name
,
214 const std::vector
<Type
>& params
);
217 * Checks if a symbol has been declared.
218 * @param name the symbol name
219 * @param type the symbol type
220 * @return true iff the symbol has been declared with the given type
222 bool isDeclared(const std::string
& name
, SymbolType type
= SYM_VARIABLE
);
225 * Checks if the declaration policy we want to enforce holds
226 * for the given symbol.
227 * @param name the symbol to check
228 * @param check the kind of check to perform
229 * @param type the type of the symbol
230 * @throws ParserException if checks are enabled and the check fails
232 void checkDeclaration(const std::string
& name
, DeclarationCheck check
,
233 SymbolType type
= SYM_VARIABLE
) throw (ParserException
);
236 * Checks whether the given name is bound to a function.
237 * @param name the name to check
238 * @throws ParserException if checks are enabled and name is not
239 * bound to a function
241 void checkFunction(const std::string
& name
) throw (ParserException
);
244 * Check that <code>kind</code> can accept <code>numArgs</code> arguments.
245 * @param kind the built-in operator to check
246 * @param numArgs the number of actual arguments
247 * @throws ParserException if checks are enabled and the operator
248 * <code>kind</code> cannot be applied to <code>numArgs</code>
251 void checkArity(Kind kind
, unsigned int numArgs
) throw (ParserException
);
254 * Check that <code>kind</code> is a legal operator in the current
255 * logic and that it can accept <code>numArgs</code> arguments.
257 * @param kind the built-in operator to check
258 * @param numArgs the number of actual arguments
259 * @throws ParserException if the parser mode is strict and the
260 * operator <code>kind</kind> has not been enabled
262 void checkOperator(Kind kind
, unsigned int numArgs
) throw (ParserException
);
265 * Returns the type for the variable with the given name.
267 * @param var_name the symbol to lookup
268 * @param type the (namespace) type of the symbol
270 Type
getType(const std::string
& var_name
, SymbolType type
= SYM_VARIABLE
);
272 /** Create a new CVC4 variable expression of the given type. */
273 Expr
mkVar(const std::string
& name
, const Type
& type
);
276 * Create a set of new CVC4 variable expressions of the given type.
278 const std::vector
<Expr
>
279 mkVars(const std::vector
<std::string
> names
, const Type
& type
);
281 /** Create a new CVC4 function expression of the given type. */
282 Expr
mkFunction(const std::string
& name
, const Type
& type
);
284 /** Create a new variable definition (e.g., from a let binding). */
285 void defineVar(const std::string
& name
, const Expr
& val
);
287 /** Create a new function definition (e.g., from a define-fun). */
288 void defineFunction(const std::string
& name
, const Expr
& val
);
290 /** Create a new type definition. */
291 void defineType(const std::string
& name
, const Type
& type
);
293 /** Create a new (parameterized) type definition. */
294 void defineType(const std::string
& name
,
295 const std::vector
<Type
>& params
, const Type
& type
);
297 /** Create a new type definition (e.g., from an SMT-LIBv2 define-sort). */
298 void defineParameterizedType(const std::string
& name
,
299 const std::vector
<Type
>& params
,
303 * Creates a new sort with the given name.
305 Type
mkSort(const std::string
& name
);
308 * Creates a new sort constructor with the given name and arity.
310 Type
mkSortConstructor(const std::string
& name
, size_t arity
);
313 * Creates new sorts with the given names (all of arity 0).
315 const std::vector
<Type
>
316 mkSorts(const std::vector
<std::string
>& names
);
318 /** Add an operator to the current legal set.
320 * @param kind the built-in operator to add
322 void addOperator(Kind kind
);
324 /** Is the symbol bound to a boolean variable? */
325 bool isBoolean(const std::string
& name
);
327 /** Is the symbol bound to a function? */
328 bool isFunction(const std::string
& name
);
330 /** Is the symbol bound to a defined function? */
331 bool isDefinedFunction(const std::string
& name
);
333 /** Is the symbol bound to a predicate? */
334 bool isPredicate(const std::string
& name
);
336 Command
* nextCommand() throw(ParserException
);
337 Expr
nextExpression() throw(ParserException
);
339 inline void parseError(const std::string
& msg
) throw (ParserException
) {
340 d_input
->parseError(msg
);
343 inline void pushScope() { d_declScope
.pushScope(); }
344 inline void popScope() { d_declScope
.popScope(); }
347 } // namespace parser
351 #endif /* __CVC4__PARSER__PARSER_STATE_H */