1 /********************* */
3 ** Original author: cconway
4 ** Major contributors: none
5 ** Minor contributors (to current version): mdeters
6 ** This file is part of the CVC4 prototype.
7 ** Copyright (c) 2009, 2010 The Analysis of Computer Systems Group (ACSys)
8 ** Courant Institute of Mathematical Sciences
10 ** See the file COPYING in the top-level source directory for licensing
13 ** A collection of state for use by parser implementations.
16 #include "cvc4parser_private.h"
18 #ifndef __CVC4__PARSER__PARSER_STATE_H
19 #define __CVC4__PARSER__PARSER_STATE_H
24 #include "parser_exception.h"
25 #include "parser_options.h"
26 #include "expr/declaration_scope.h"
27 #include "expr/expr.h"
28 #include "expr/kind.h"
29 #include "util/Assert.h"
33 // Forward declarations
43 /** Types of check for the symols */
44 enum DeclarationCheck
{
45 /** Enforce that the symbol has been declared */
47 /** Enforce that the symbol has not been declared */
49 /** Don't check anything */
53 /** Returns a string representation of the given object (for
55 inline std::string
toString(DeclarationCheck check
) {
60 return "CHECK_DECLARED";
61 case CHECK_UNDECLARED
:
62 return "CHECK_UNDECLARED";
69 * Types of symbols. Used to define namespaces.
78 /** Returns a string representation of the given object (for
80 inline std::string
toString(SymbolType type
) {
83 return "SYM_VARIABLE";
92 * This class encapsulates all of the state of a parser, including the
93 * name of the file, line number and column information, and in-scope
96 class CVC4_PUBLIC Parser
{
98 /** The expression manager */
99 ExprManager
*d_exprManager
;
101 /** The input that we're parsing. */
104 /** The symbol table */
105 DeclarationScope d_declScope
;
107 /** The name of the input file. */
108 // std::string d_filename;
113 /** Are semantic checks enabled during parsing? */
114 bool d_checksEnabled
;
117 /** Lookup a symbol in the given namespace. */
118 Expr
getSymbol(const std::string
& var_name
, SymbolType type
);
121 /** Create a parser state.
123 * @param exprManager the expression manager to use when creating expressions
124 * @param input the parser input
126 Parser(ExprManager
* exprManager
, Input
* input
);
128 virtual ~Parser() { }
130 /** Get the associated <code>ExprManager</code>. */
131 inline ExprManager
* getExprManager() const {
132 return d_exprManager
;
135 /** Get the associated input. */
136 inline Input
* getInput() const {
140 /** Set the declaration scope manager for this input. NOTE: This should <em>only</me> be
141 * called before parsing begins. Otherwise, previous declarations will be lost. */
142 inline void setDeclarationScope(DeclarationScope declScope
) {
143 d_declScope
= declScope
;
147 * Check if we are done -- either the end of input has been reached, or some
148 * error has been encountered.
149 * @return true if parser is done
151 inline bool done() const {
155 /** Sets the done flag */
156 inline void setDone(bool done
= true) {
160 /** Enable semantic checks during parsing. */
163 /** Disable semantic checks during parsing. Disabling checks may lead to crashes on bad inputs. */
164 void disableChecks();
166 /** Get the name of the input file. */
168 inline const std::string getFilename() {
174 * Sets the logic for the current benchmark. Declares any logic symbols.
176 * @param name the name of the logic (e.g., QF_UF, AUFLIA)
178 void setLogic(const std::string
& name
);
181 * Returns a variable, given a name and a type.
182 * @param var_name the name of the variable
183 * @return the variable expression
185 Expr
getVariable(const std::string
& var_name
);
188 * Returns a sort, given a name
190 Type
getSort(const std::string
& sort_name
);
193 * Checks if a symbol has been declared.
194 * @param name the symbol name
195 * @param type the symbol type
196 * @return true iff the symbol has been declared with the given type
198 bool isDeclared(const std::string
& name
, SymbolType type
= SYM_VARIABLE
);
201 * Checks if the declaration policy we want to enforce holds
202 * for the given symbol.
203 * @param name the symbol to check
204 * @param check the kind of check to perform
205 * @param type the type of the symbol
206 * @throws ParserException if checks are enabled and the check fails
208 void checkDeclaration(const std::string
& name
, DeclarationCheck check
,
209 SymbolType type
= SYM_VARIABLE
) throw (ParserException
);
212 * Checks whether the given name is bound to a function.
213 * @param name the name to check
214 * @throws ParserException if checks are enabled and name is not bound to a function
216 void checkFunction(const std::string
& name
) throw (ParserException
);
219 * Check that <code>kind</code> can accept <code>numArgs</code> arguments.
220 * @param kind the built-in operator to check
221 * @param numArgs the number of actual arguments
222 * @throws ParserException if checks are enabled and the operator <code>kind</code> cannot be
223 * applied to <code>numArgs</code> arguments.
225 void checkArity(Kind kind
, unsigned int numArgs
) throw (ParserException
);
228 * Returns the type for the variable with the given name.
229 * @param var_name the symbol to lookup
230 * @param type the (namespace) type of the symbol
232 Type
getType(const std::string
& var_name
, SymbolType type
= SYM_VARIABLE
);
234 /** Create a new CVC4 variable expression of the given type. */
235 Expr
mkVar(const std::string
& name
, const Type
& type
);
237 /** Create a set of new CVC4 variable expressions of the given
239 const std::vector
<Expr
>
240 mkVars(const std::vector
<std::string
> names
, const Type
& type
);
242 /** Create a new variable definition (e.g., from a let binding). */
243 void defineVar(const std::string
& name
, const Expr
& val
);
245 /** Create a new type definition. */
246 void defineType(const std::string
& name
, const Type
& type
);
249 * Creates a new sort with the given name.
251 Type
mkSort(const std::string
& name
);
254 * Creates a new sorts with the given names.
256 const std::vector
<Type
>
257 mkSorts(const std::vector
<std::string
>& names
);
259 /** Is the symbol bound to a boolean variable? */
260 bool isBoolean(const std::string
& name
);
262 /** Is the symbol bound to a function? */
263 bool isFunction(const std::string
& name
);
265 /** Is the symbol bound to a predicate? */
266 bool isPredicate(const std::string
& name
);
268 Command
* nextCommand() throw(ParserException
);
269 Expr
nextExpression() throw(ParserException
);
271 inline void parseError(const std::string
& msg
) throw (ParserException
) {
272 d_input
->parseError(msg
);
275 inline void pushScope() { d_declScope
.pushScope(); }
276 inline void popScope() { d_declScope
.popScope(); }
279 } // namespace parser
283 #endif /* __CVC4__PARSER__PARSER_STATE_H */