Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * This file is part of the cvc5 project. 3 : : * 4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS 5 : : * in the top-level source directory and their institutional affiliations. 6 : : * All rights reserved. See the file COPYING in the top-level source 7 : : * directory for licensing information. 8 : : * **************************************************************************** 9 : : * 10 : : * Base class for parsing. 11 : : */ 12 : : 13 : : #include "parser/parser.h" 14 : : 15 : : #include <cvc5/cvc5_parser.h> 16 : : 17 : : #include "base/check.h" 18 : : #include "base/output.h" 19 : : #include "parser/commands.h" 20 : : #include "parser/lexer.h" 21 : : #include "parser/smt2/smt2_parser.h" 22 : : 23 : : namespace cvc5 { 24 : : namespace parser { 25 : : 26 : 25050 : Parser::Parser(Solver* solver, SymManager* sm) 27 : 25050 : : d_solver(solver), d_sm(sm), d_lex(nullptr), d_done(true) 28 : : { 29 : 25050 : } 30 : : 31 : 0 : void Parser::setLogic(CVC5_UNUSED const std::string& name) {} 32 : : 33 : 24773 : void Parser::setFileInput(const std::string& filename) 34 : : { 35 : 24773 : d_flexInput = Input::mkFileInput(filename); 36 : 24771 : initializeInput(filename); 37 : 24771 : } 38 : : 39 : 163 : void Parser::setStreamInput(std::istream& input, const std::string& name) 40 : : { 41 : 163 : d_flexInput = Input::mkStreamInput(input); 42 : 163 : initializeInput(name); 43 : 163 : } 44 : : 45 : 122 : void Parser::setStringInput(const std::string& input, const std::string& name) 46 : : { 47 : 122 : d_flexInput = Input::mkStringInput(input); 48 : 122 : initializeInput(name); 49 : 122 : } 50 : : 51 : 25056 : void Parser::initializeInput(const std::string& name) 52 : : { 53 : 25056 : d_done = false; 54 : 25056 : d_lex->initialize(d_flexInput.get(), name); 55 : 25056 : } 56 : : 57 : 218 : void Parser::warning(const std::string& msg) { d_lex->warning(msg); } 58 : : 59 : 77 : void Parser::parseError(const std::string& msg) { d_lex->parseError(msg); } 60 : : 61 : 0 : void Parser::unexpectedEOF(const std::string& msg) 62 : : { 63 : 0 : d_lex->parseError(msg, true); 64 : 0 : } 65 : : 66 : 708978 : std::unique_ptr<Cmd> Parser::nextCommand() 67 : : { 68 [ + - ]: 708978 : Trace("parser") << "nextCommand()" << std::endl; 69 : 708978 : std::unique_ptr<Cmd> cmd; 70 : : try 71 : : { 72 : 708978 : cmd = parseNextCommand(); 73 : 708903 : setDone(cmd == nullptr); 74 : : } 75 [ - + ][ + ]: 75 : catch (ParserException& e) 76 : : { 77 : 49 : setDone(); 78 : 49 : throw; 79 : 49 : } 80 : 26 : catch (std::exception& e) 81 : : { 82 : 26 : setDone(); 83 : 78 : parseError(e.what()); 84 : 26 : } 85 [ + - ]: 708903 : Trace("parser") << "nextCommand() => " << cmd.get() << std::endl; 86 : 708903 : return cmd; 87 : 75 : } 88 : : 89 : 139 : Term Parser::nextTerm() 90 : : { 91 [ + - ]: 139 : Trace("parser") << "nextTerm()" << std::endl; 92 : 139 : Term result; 93 [ + + ]: 139 : if (!d_done) 94 : : { 95 : : try 96 : : { 97 : 125 : result = parseNextTerm(); 98 : 102 : setDone(result.isNull()); 99 : : } 100 [ - + ][ + ]: 23 : catch (ParserException& e) 101 : : { 102 : 17 : setDone(); 103 : 17 : throw; 104 : 17 : } 105 : 6 : catch (std::exception& e) 106 : : { 107 : 6 : setDone(); 108 : 18 : parseError(e.what()); 109 : 6 : } 110 : : } 111 [ + - ]: 116 : Trace("parser") << "nextTerm() => " << result << std::endl; 112 : 116 : return result; 113 : 23 : } 114 : : 115 : 171 : bool Parser::done() const { return d_done; } 116 : : 117 : 25050 : std::unique_ptr<Parser> Parser::mkParser(modes::InputLanguage lang, 118 : : Solver* solver, 119 : : SymManager* sm) 120 : : { 121 : 25050 : std::unique_ptr<Parser> parser; 122 [ + + ]: 25050 : if (lang == modes::InputLanguage::SMT_LIB_2_6 123 [ + - ]: 963 : || lang == modes::InputLanguage::SYGUS_2_1) 124 : : { 125 : 25050 : bool isSygus = (lang == modes::InputLanguage::SYGUS_2_1); 126 : 25050 : ParsingMode parsingMode = ParsingMode::DEFAULT; 127 : 50100 : std::string mode = solver->getOption("parsing-mode"); 128 [ + + ]: 25050 : if (mode == "strict") 129 : : { 130 : 39 : parsingMode = ParsingMode::STRICT; 131 : : } 132 [ + + ]: 25011 : else if (mode == "lenient") 133 : : { 134 : 8 : parsingMode = ParsingMode::LENIENT; 135 : : } 136 : 25050 : parser.reset(new Smt2Parser(solver, sm, parsingMode, isSygus)); 137 : 25050 : } 138 : : else 139 : : { 140 : 0 : Unhandled() << "unable to detect input file format, try --lang"; 141 : : } 142 : 25050 : return parser; 143 : 0 : } 144 : : 145 : : } // namespace parser 146 : : } // namespace cvc5