1 /********************* */
2 /*! \file antlr_input.cpp
4 ** Original author: cconway
5 ** Major contributors: none
6 ** Minor contributors (to current version): mdeters
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 A super-class for ANTLR-generated input language parsers.
16 ** A super-class for ANTLR-generated input language parsers
23 #include "parser/antlr_input.h"
24 #include "parser/input.h"
25 #include "parser/bounded_token_buffer.h"
26 #include "parser/bounded_token_factory.h"
27 #include "parser/memory_mapped_input_buffer.h"
28 #include "parser/parser_exception.h"
29 #include "parser/parser.h"
31 #include "expr/command.h"
32 #include "expr/type.h"
33 #include "parser/cvc/cvc_input.h"
34 #include "parser/smt/smt_input.h"
35 #include "parser/smt2/smt2_input.h"
36 #include "util/output.h"
37 #include "util/Assert.h"
41 using namespace CVC4::parser
;
42 using namespace CVC4::kind
;
47 AntlrInputStream::AntlrInputStream(std::string name
,
48 pANTLR3_INPUT_STREAM input
,
49 bool fileIsTemporary
) :
50 InputStream(name
,fileIsTemporary
),
52 AlwaysAssert( input
!= NULL
);
55 AntlrInputStream::~AntlrInputStream() {
56 d_input
->free(d_input
);
59 pANTLR3_INPUT_STREAM
AntlrInputStream::getAntlr3InputStream() const {
64 AntlrInputStream::newFileInputStream(const std::string
& name
,
66 throw (InputStreamException
, AssertionException
) {
67 pANTLR3_INPUT_STREAM input
= NULL
;
69 input
= MemoryMappedInputBufferNew(name
);
71 // libantlr3c v3.2 isn't source-compatible with v3.4
72 #ifdef CVC4_ANTLR3_OLD_INPUT_STREAM
73 input
= antlr3AsciiFileStreamNew((pANTLR3_UINT8
) name
.c_str());
74 #else /* CVC4_ANTLR3_OLD_INPUT_STREAM */
75 input
= antlr3FileStreamNew((pANTLR3_UINT8
) name
.c_str(), ANTLR3_ENC_8BIT
);
76 #endif /* CVC4_ANTLR3_OLD_INPUT_STREAM */
79 throw InputStreamException("Couldn't open file: " + name
);
81 return new AntlrInputStream( name
, input
);
85 AntlrInputStream::newStreamInputStream(std::istream
& input
,
86 const std::string
& name
)
87 throw (InputStreamException
, AssertionException
) {
89 // Since these are all NULL on entry, realloc will be called
90 char *basep
= NULL
, *boundp
= NULL
, *cp
= NULL
;
91 /* 64KB seems like a reasonable default size. */
92 size_t bufSize
= 0x10000;
94 /* Keep going until we can't go no more. */
95 while( !input
.eof() && !input
.fail() ) {
98 /* We ran out of room in the buffer. Realloc at double the size. */
99 ptrdiff_t offset
= cp
- basep
;
100 basep
= (char *) realloc(basep
, bufSize
);
101 if( basep
== NULL
) {
102 throw InputStreamException("Failed buffering input stream: " + name
);
105 boundp
= basep
+ bufSize
;
109 /* Read as much as we have room for. */
110 input
.read( cp
, boundp
- cp
);
111 cp
+= input
.gcount();
114 /* Make sure the fail bit didn't get set. */
116 throw InputStreamException("Stream input failed: " + name
);
119 /* Create an ANTLR input backed by the buffer. */
120 #ifdef CVC4_ANTLR3_OLD_INPUT_STREAM
121 pANTLR3_INPUT_STREAM inputStream
=
122 antlr3NewAsciiStringInPlaceStream((pANTLR3_UINT8
) basep
,
124 (pANTLR3_UINT8
) strdup(name
.c_str()));
125 #else /* CVC4_ANTLR3_OLD_INPUT_STREAM */
126 pANTLR3_INPUT_STREAM inputStream
=
127 antlr3StringStreamNew((pANTLR3_UINT8
) basep
,
130 (pANTLR3_UINT8
) strdup(name
.c_str()));
131 #endif /* CVC4_ANTLR3_OLD_INPUT_STREAM */
132 if( inputStream
==NULL
) {
133 throw InputStreamException("Couldn't initialize input: " + name
);
135 return new AntlrInputStream( name
, inputStream
);
140 AntlrInputStream::newStringInputStream(const std::string
& input
,
141 const std::string
& name
)
142 throw (InputStreamException
, AssertionException
) {
143 char* inputStr
= strdup(input
.c_str());
144 char* nameStr
= strdup(name
.c_str());
145 AlwaysAssert( inputStr
!=NULL
&& nameStr
!=NULL
);
146 #ifdef CVC4_ANTLR3_OLD_INPUT_STREAM
147 pANTLR3_INPUT_STREAM inputStream
=
148 antlr3NewAsciiStringInPlaceStream((pANTLR3_UINT8
) inputStr
,
150 (pANTLR3_UINT8
) nameStr
);
151 #else /* CVC4_ANTLR3_OLD_INPUT_STREAM */
152 pANTLR3_INPUT_STREAM inputStream
=
153 antlr3StringStreamNew((pANTLR3_UINT8
) inputStr
,
156 (pANTLR3_UINT8
) nameStr
);
157 #endif /* CVC4_ANTLR3_OLD_INPUT_STREAM */
158 if( inputStream
==NULL
) {
159 throw InputStreamException("Couldn't initialize string input: '" + input
+ "'");
161 return new AntlrInputStream( name
, inputStream
);
164 AntlrInput
* AntlrInput::newInput(InputLanguage lang
, AntlrInputStream
& inputStream
) {
165 using namespace language::input
;
171 input
= new CvcInput(inputStream
);
175 input
= new SmtInput(inputStream
);
179 input
= new Smt2Input(inputStream
);
189 AntlrInput::AntlrInput(AntlrInputStream
& inputStream
, unsigned int lookahead
) :
191 d_lookahead(lookahead
),
194 d_antlr3InputStream( inputStream
.getAntlr3InputStream() ),
195 d_tokenBuffer(NULL
) {
199 AntlrParser::AntlrParser(ExprManager* exprManager, std::istream& input, const std::string& name, unsigned int lookahead)
200 Parser(exprManager,name),
201 d_lookahead(lookahead) {
207 AntlrInput::Input(ExprManager* exprManager, const std::string& input, const std::string& name, unsigned int lookahead) :
208 Input(exprManager,name),
209 d_lookahead(lookahead),
212 d_tokenStream(NULL) {
214 char* inputStr = strdup(input.c_str());
215 char* nameStr = strdup(name.c_str());
216 if( inputStr==NULL || nameStr==NULL ) {
217 throw ParserException("Couldn't initialize string input: '" + input + "'");
219 d_inputStream = antlr3NewAsciiStringInPlaceStream((pANTLR3_UINT8)inputStr,input.size(),(pANTLR3_UINT8)nameStr);
220 if( d_inputStream == NULL ) {
221 throw ParserException("Couldn't create input stream for string: '" + input + "'");
227 AntlrInput::~AntlrInput() {
228 BoundedTokenBufferFree(d_tokenBuffer
);
231 pANTLR3_COMMON_TOKEN_STREAM
AntlrInput::getTokenStream() {
232 return d_tokenBuffer
->commonTstream
;
235 void AntlrInput::lexerError(pANTLR3_BASE_RECOGNIZER recognizer
) {
236 pANTLR3_LEXER lexer
= (pANTLR3_LEXER
)(recognizer
->super
);
237 AlwaysAssert(lexer
!=NULL
);
238 Parser
*parser
= (Parser
*)(lexer
->super
);
239 AlwaysAssert(parser
!=NULL
);
240 AntlrInput
*input
= (AntlrInput
*) parser
->getInput();
241 AlwaysAssert(input
!=NULL
);
243 /* Call the error display routine *if* there's not already a
244 * parse error pending. If a parser error is pending, this
245 * error is probably less important, so we just drop it. */
246 if( input
->d_parser
->rec
->state
->error
== ANTLR3_FALSE
) {
247 input
->parseError("Error finding next token.");
251 void AntlrInput::parseError(const std::string
& message
)
252 throw (ParserException
, AssertionException
) {
253 Debug("parser") << "Throwing exception: "
254 << getInputStream()->getName() << ":"
255 << d_lexer
->getLine(d_lexer
) << "."
256 << d_lexer
->getCharPositionInLine(d_lexer
) << ": "
258 throw ParserException(message
, getInputStream()->getName(),
259 d_lexer
->getLine(d_lexer
),
260 d_lexer
->getCharPositionInLine(d_lexer
));
264 void AntlrInput::setAntlr3Lexer(pANTLR3_LEXER pLexer
) {
267 pANTLR3_TOKEN_FACTORY pTokenFactory
= d_lexer
->rec
->state
->tokFactory
;
268 if( pTokenFactory
!= NULL
) {
269 pTokenFactory
->close(pTokenFactory
);
272 /* 2*lookahead should be sufficient, but we give ourselves some breathing room. */
273 pTokenFactory
= BoundedTokenFactoryNew(d_antlr3InputStream
, 2*d_lookahead
);
274 if( pTokenFactory
== NULL
) {
275 throw ParserException("Couldn't create token factory.");
277 d_lexer
->rec
->state
->tokFactory
= pTokenFactory
;
279 pBOUNDED_TOKEN_BUFFER buffer
= BoundedTokenBufferSourceNew(d_lookahead
, d_lexer
->rec
->state
->tokSource
);
280 if( buffer
== NULL
) {
281 throw ParserException("Couldn't create token buffer.");
284 d_tokenBuffer
= buffer
;
286 // Override default lexer error reporting
287 d_lexer
->rec
->reportError
= &lexerError
;
288 // Override default nextToken function, just to prevent exceptions escaping.
289 d_lexer
->rec
->state
->tokSource
->nextToken
= &nextTokenStr
;
292 void AntlrInput::setParser(Parser
& parser
) {
293 // ANTLR isn't using super in the lexer or the parser, AFAICT.
294 // We could also use @lexer/parser::context to add a field to the generated
295 // objects, but then it would have to be declared separately in every
296 // language's grammar and we'd have to in the address of the field anyway.
297 d_lexer
->super
= &parser
;
298 d_parser
->super
= &parser
;
301 void AntlrInput::setAntlr3Parser(pANTLR3_PARSER pParser
) {
303 // d_parser->rec->match = &match;
304 d_parser
->rec
->reportError
= &reportError
;
305 /* Don't try to recover from a parse error. */
306 // [chris 4/5/2010] Not clear on why this cast is necessary, but I get an error if I remove it.
307 d_parser
->rec
->recoverFromMismatchedToken
=
308 (void* (*)(ANTLR3_BASE_RECOGNIZER_struct
*, ANTLR3_UINT32
, ANTLR3_BITSET_LIST_struct
*))
309 d_parser
->rec
->mismatch
;
313 }/* CVC4::parser namespace */
314 }/* CVC4 namespace */