Decouple parser creation from input selection (#6533)
authorAndres Noetzli <andres.noetzli@gmail.com>
Fri, 14 May 2021 23:17:54 +0000 (16:17 -0700)
committerGitHub <noreply@github.com>
Fri, 14 May 2021 23:17:54 +0000 (23:17 +0000)
This commit decouples the creation of a `Parser` instance from creating
an `Input` and setting the `Input` on the parser. This is a first step
in refactoring the parser infrastructure. A future PR will split the
parser class into three classes: `Parser`, `ParserState`, and
`InputParser`. The `Parser` and `InputParser` classes will be the
public-facing classes. The new `Parser` class will have methods to
create `InputParser`s from files, streams, and strings. `InputParser`s
will have methods to get commands/exprs from a given input. The
`ParserState` class will keep track of the state of the parser and will
be the internal interface for the parsers. The current `Parser` class is
used both publicly and internally, which is messy.

15 files changed:
src/main/driver_unified.cpp
src/main/interactive_shell.cpp
src/parser/cvc/cvc.h
src/parser/parser.cpp
src/parser/parser.h
src/parser/parser_builder.cpp
src/parser/parser_builder.h
src/parser/smt2/smt2.cpp
src/parser/smt2/smt2.h
src/parser/tptp/tptp.cpp
src/parser/tptp/tptp.h
test/api/ouroborous.cpp
test/api/smt2_compliance.cpp
test/unit/parser/parser_black.cpp
test/unit/parser/parser_builder_black.cpp

index f27bf03f0e190748272c49d52a36a5fd94908000..a26ee3b73f8771a4c21b4d8462af28e0eaa4f0f3 100644 (file)
@@ -259,16 +259,20 @@ int runCvc5(int argc, char* argv[], Options& opts)
 
       ParserBuilder parserBuilder(pExecutor->getSolver(),
                                   pExecutor->getSymbolManager(),
-                                  filename,
                                   opts);
-
+      std::unique_ptr<Parser> parser(parserBuilder.build());
       if( inputFromStdin ) {
-        parserBuilder.withStreamInput(cin);
+        parser->setInput(
+            Input::newStreamInput(opts.getInputLanguage(), cin, filename));
+      }
+      else
+      {
+        parser->setInput(Input::newFileInput(
+            opts.getInputLanguage(), filename, opts.getMemoryMap()));
       }
 
       vector< vector<Command*> > allCommands;
       allCommands.push_back(vector<Command*>());
-      std::unique_ptr<Parser> parser(parserBuilder.build());
       int needReset = 0;
       // true if one of the commands was interrupted
       bool interrupted = false;
@@ -411,14 +415,18 @@ int runCvc5(int argc, char* argv[], Options& opts)
 
       ParserBuilder parserBuilder(pExecutor->getSolver(),
                                   pExecutor->getSymbolManager(),
-                                  filename,
                                   opts);
-
+      std::unique_ptr<Parser> parser(parserBuilder.build());
       if( inputFromStdin ) {
-        parserBuilder.withStreamInput(cin);
+        parser->setInput(
+            Input::newStreamInput(opts.getInputLanguage(), cin, filename));
+      }
+      else
+      {
+        parser->setInput(Input::newFileInput(
+            opts.getInputLanguage(), filename, opts.getMemoryMap()));
       }
 
-      std::unique_ptr<Parser> parser(parserBuilder.build());
       bool interrupted = false;
       while (status)
       {
index 516f9e6213292c4f5e2af542d9963cd71aee7b5c..8ca10799fb1a7a683ea31de0e8235119298edc78 100644 (file)
@@ -91,9 +91,9 @@ InteractiveShell::InteractiveShell(api::Solver* solver, SymbolManager* sm)
       d_out(*d_options.getOutConst()),
       d_quit(false)
 {
-  ParserBuilder parserBuilder(solver, sm, INPUT_FILENAME, d_options);
+  ParserBuilder parserBuilder(solver, sm, d_options);
   /* Create parser with bogus input. */
-  d_parser = parserBuilder.withStringInput("").build();
+  d_parser = parserBuilder.build();
   if(d_options.wasSetByUserForceLogicString()) {
     LogicInfo tmp(d_options.getForceLogicString());
     d_parser->forceLogic(tmp.getLogicString());
index e14f1bc7a3d6deab05a84bf6bf3eba7f31f9d1a4..e20e041ccd33a37580f6b61173ecd367ad9de497 100644 (file)
@@ -38,10 +38,9 @@ class Cvc : public Parser
  protected:
   Cvc(api::Solver* solver,
       SymbolManager* sm,
-      Input* input,
       bool strictMode = false,
       bool parseOnly = false)
-      : Parser(solver, sm, input, strictMode, parseOnly)
+      : Parser(solver, sm, strictMode, parseOnly)
   {
   }
 };
index 31f8517cd33d9100745c6fd3bb3880f6308b982e..f3a0d5c83226a26a7457acf4e5c0dfa09e1d43f1 100644 (file)
@@ -39,15 +39,13 @@ namespace parser {
 
 Parser::Parser(api::Solver* solver,
                SymbolManager* sm,
-               Input* input,
                bool strictMode,
                bool parseOnly)
-    : d_input(input),
-      d_symman(sm),
+    : d_symman(sm),
       d_symtab(sm->getSymbolTable()),
       d_assertionLevel(0),
       d_anonymousFunctionCount(0),
-      d_done(false),
+      d_done(true),
       d_checksEnabled(true),
       d_strictMode(strictMode),
       d_parseOnly(parseOnly),
@@ -56,7 +54,6 @@ Parser::Parser(api::Solver* solver,
       d_forcedLogic(),
       d_solver(solver)
 {
-  d_input->setParser(*this);
 }
 
 Parser::~Parser() {
@@ -66,7 +63,6 @@ Parser::~Parser() {
     delete command;
   }
   d_commandQueue.clear();
-  delete d_input;
 }
 
 api::Solver* Parser::getSolver() const { return d_solver; }
index 42baf98cdaab0fb46c2156926243ba4c565b9a08..b127221d735250ff83f549c1fcbe56b851a5c0d5 100644 (file)
@@ -19,6 +19,7 @@
 #define CVC5__PARSER__PARSER_H
 
 #include <list>
+#include <memory>
 #include <set>
 #include <string>
 
@@ -107,7 +108,7 @@ class CVC5_EXPORT Parser
 private:
 
  /** The input that we're parsing. */
Input* d_input;
std::unique_ptr<Input> d_input;
 
  /**
   * Reference to the symbol manager, which manages the symbol table used by
@@ -207,7 +208,6 @@ protected:
   */
  Parser(api::Solver* solver,
         SymbolManager* sm,
-        Input* input,
         bool strictMode = false,
         bool parseOnly = false);
 
@@ -219,17 +219,14 @@ public:
   api::Solver* getSolver() const;
 
   /** Get the associated input. */
-  inline Input* getInput() const {
-    return d_input;
-  }
+  Input* getInput() const { return d_input.get(); }
 
   /** Get unresolved sorts */
   inline std::set<api::Sort>& getUnresolvedSorts() { return d_unresolved; }
 
   /** Deletes and replaces the current parser input. */
   void setInput(Input* input)  {
-    delete d_input;
-    d_input = input;
+    d_input.reset(input);
     d_input->setParser(*this);
     d_done = false;
   }
index 220edb9d5cb4c969edcee0fcdd876e550bbc3892..26a867f9544a05f88ad49e4f9996d6f19e749f9f 100644 (file)
 namespace cvc5 {
 namespace parser {
 
-ParserBuilder::ParserBuilder(api::Solver* solver,
-                             SymbolManager* sm,
-                             const std::string& filename)
-    : d_filename(filename), d_solver(solver), d_symman(sm)
+ParserBuilder::ParserBuilder(api::Solver* solver, SymbolManager* sm)
+    : d_solver(solver), d_symman(sm)
 {
-  init(solver, sm, filename);
+  init(solver, sm);
 }
 
 ParserBuilder::ParserBuilder(api::Solver* solver,
                              SymbolManager* sm,
-                             const std::string& filename,
                              const Options& options)
-    : d_filename(filename), d_solver(solver), d_symman(sm)
+    : d_solver(solver), d_symman(sm)
 {
-  init(solver, sm, filename);
+  init(solver, sm);
   withOptions(options);
 }
 
-void ParserBuilder::init(api::Solver* solver,
-                         SymbolManager* sm,
-                         const std::string& filename)
+void ParserBuilder::init(api::Solver* solver, SymbolManager* sm)
 {
-  d_inputType = FILE_INPUT;
   d_lang = language::input::LANG_AUTO;
-  d_filename = filename;
-  d_streamInput = NULL;
   d_solver = solver;
   d_symman = sm;
   d_checksEnabled = true;
   d_strictMode = false;
   d_canIncludeFile = true;
-  d_mmap = false;
   d_parseOnly = false;
   d_logicIsForced = false;
   d_forcedLogic = "";
@@ -70,39 +61,23 @@ void ParserBuilder::init(api::Solver* solver,
 
 Parser* ParserBuilder::build()
 {
-  Input* input = NULL;
-  switch( d_inputType ) {
-  case FILE_INPUT:
-    input = Input::newFileInput(d_lang, d_filename, d_mmap);
-    break;
-  case STREAM_INPUT:
-    Assert(d_streamInput != NULL);
-    input = Input::newStreamInput(d_lang, *d_streamInput, d_filename);
-    break;
-  case STRING_INPUT:
-    input = Input::newStringInput(d_lang, d_stringInput, d_filename);
-    break;
-  }
-
-  Assert(input != NULL);
-
   Parser* parser = NULL;
   switch (d_lang)
   {
     case language::input::LANG_SYGUS_V2:
-      parser = new Smt2(d_solver, d_symman, input, d_strictMode, d_parseOnly);
+      parser = new Smt2(d_solver, d_symman, d_strictMode, d_parseOnly);
       break;
     case language::input::LANG_TPTP:
-      parser = new Tptp(d_solver, d_symman, input, d_strictMode, d_parseOnly);
+      parser = new Tptp(d_solver, d_symman, d_strictMode, d_parseOnly);
       break;
     default:
       if (language::isInputLang_smt2(d_lang))
       {
-        parser = new Smt2(d_solver, d_symman, input, d_strictMode, d_parseOnly);
+        parser = new Smt2(d_solver, d_symman, d_strictMode, d_parseOnly);
       }
       else
       {
-        parser = new Cvc(d_solver, d_symman, input, d_strictMode, d_parseOnly);
+        parser = new Cvc(d_solver, d_symman, d_strictMode, d_parseOnly);
       }
       break;
   }
@@ -131,32 +106,11 @@ ParserBuilder& ParserBuilder::withChecks(bool flag) {
   return *this;
 }
 
-ParserBuilder& ParserBuilder::withSolver(api::Solver* solver)
-{
-  d_solver = solver;
-  return *this;
-}
-
-ParserBuilder& ParserBuilder::withFileInput() {
-  d_inputType = FILE_INPUT;
-  return *this;
-}
-
-ParserBuilder& ParserBuilder::withFilename(const std::string& filename) {
-  d_filename = filename;
-  return *this;
-}
-
 ParserBuilder& ParserBuilder::withInputLanguage(InputLanguage lang) {
   d_lang = lang;
   return *this;
 }
 
-ParserBuilder& ParserBuilder::withMmap(bool flag) {
-  d_mmap = flag;
-  return *this;
-}
-
 ParserBuilder& ParserBuilder::withParseOnly(bool flag) {
   d_parseOnly = flag;
   return *this;
@@ -166,7 +120,6 @@ ParserBuilder& ParserBuilder::withOptions(const Options& options) {
   ParserBuilder& retval = *this;
   retval =
       retval.withInputLanguage(options.getInputLanguage())
-      .withMmap(options.getMemoryMap())
       .withChecks(options.getSemanticChecks())
       .withStrictMode(options.getStrictParsing())
       .withParseOnly(options.getParseOnly())
@@ -194,17 +147,5 @@ ParserBuilder& ParserBuilder::withForcedLogic(const std::string& logic) {
   return *this;
 }
 
-ParserBuilder& ParserBuilder::withStreamInput(std::istream& input) {
-  d_inputType = STREAM_INPUT;
-  d_streamInput = &input;
-  return *this;
-}
-
-ParserBuilder& ParserBuilder::withStringInput(const std::string& input) {
-  d_inputType = STRING_INPUT;
-  d_stringInput = input;
-  return *this;
-}
-
 }  // namespace parser
 }  // namespace cvc5
index b43da3548e93063e96a3ed247954d94f65c05d1b..992ca408a9ba0f100fa89bda1ba3b67f5851bfbf 100644 (file)
@@ -44,27 +44,9 @@ class Parser;
  */
 class CVC5_EXPORT ParserBuilder
 {
-  enum InputType {
-    FILE_INPUT,
-    STREAM_INPUT,
-    STRING_INPUT
-  };
-
-  /** The input type. */
-  InputType d_inputType;
-
   /** The input language */
   InputLanguage d_lang;
 
-  /** The file name (may not exist) */
-  std::string d_filename;
-
-  /** The string input, if any. */
-  std::string d_stringInput;
-
-  /** The stream input, if any. */
-  std::istream* d_streamInput;
-
   /** The API Solver object. */
   api::Solver* d_solver;
 
@@ -80,9 +62,6 @@ class CVC5_EXPORT ParserBuilder
   /** Should we allow include-file commands? */
   bool d_canIncludeFile;
 
-  /** Should we memory-map a file input? */
-  bool d_mmap;
-
   /** Are we parsing only? */
   bool d_parseOnly;
 
@@ -93,19 +72,14 @@ class CVC5_EXPORT ParserBuilder
   std::string d_forcedLogic;
 
   /** Initialize this parser builder */
-  void init(api::Solver* solver,
-            SymbolManager* sm,
-            const std::string& filename);
+  void init(api::Solver* solver, SymbolManager* sm);
 
  public:
   /** Create a parser builder using the given Solver and filename. */
-  ParserBuilder(api::Solver* solver,
-                SymbolManager* sm,
-                const std::string& filename);
+  ParserBuilder(api::Solver* solver, SymbolManager* sm);
 
   ParserBuilder(api::Solver* solver,
                 SymbolManager* sm,
-                const std::string& filename,
                 const Options& options);
 
   /** Build the parser, using the current settings. */
@@ -114,20 +88,6 @@ class CVC5_EXPORT ParserBuilder
   /** Should semantic checks be enabled in the parser? (Default: yes) */
   ParserBuilder& withChecks(bool flag = true);
 
-  /** Set the Solver to use with the parser. */
-  ParserBuilder& withSolver(api::Solver* solver);
-
-  /** Set the parser to read a file for its input. (Default) */
-  ParserBuilder& withFileInput();
-
-  /**
-   * Set the filename for use by the parser. If file input is used,
-   * this file will be opened and read by the parser. Otherwise, the
-   * filename string (possibly a non-existent path) will only be used
-   * in error messages.
-   */
-  ParserBuilder& withFilename(const std::string& filename);
-
   /**
    * Set the input language to be used by the parser.
    *
@@ -135,14 +95,6 @@ class CVC5_EXPORT ParserBuilder
    */
   ParserBuilder& withInputLanguage(InputLanguage lang);
 
-  /**
-   * Should the parser memory-map its input? This is only relevant if
-   * the parser will have a file input.
-   *
-   * (Default: no)
-   */
-  ParserBuilder& withMmap(bool flag = true);
-
   /**
    * Are we only parsing, or doing something with the resulting
    * commands and expressions?  This setting affects whether the
@@ -173,12 +125,6 @@ class CVC5_EXPORT ParserBuilder
    */
   ParserBuilder& withIncludeFile(bool flag = true);
 
-  /** Set the parser to use the given stream for its input. */
-  ParserBuilder& withStreamInput(std::istream& input);
-
-  /** Set the parser to use the given string for its input. */
-  ParserBuilder& withStringInput(const std::string& input);
-
   /** Set the parser to use the given logic string. */
   ParserBuilder& withForcedLogic(const std::string& logic);
 }; /* class ParserBuilder */
index 2f19d16f0582d3f0cafb0390654be9baa7111f83..a91c713d6ed2459beaca418242faa05d1703d48f 100644 (file)
@@ -31,10 +31,9 @@ namespace parser {
 
 Smt2::Smt2(api::Solver* solver,
            SymbolManager* sm,
-           Input* input,
            bool strictMode,
            bool parseOnly)
-    : Parser(solver, sm, input, strictMode, parseOnly),
+    : Parser(solver, sm, strictMode, parseOnly),
       d_logicSet(false),
       d_seenSetLogic(false)
 {
index e28ef955c125b9fdb38cec9d4ede2af00c07e9d0..2dd4bf66370fd2c765ebad59eee42abd7dbc8474 100644 (file)
@@ -66,7 +66,6 @@ class Smt2 : public Parser
  protected:
   Smt2(api::Solver* solver,
        SymbolManager* sm,
-       Input* input,
        bool strictMode = false,
        bool parseOnly = false);
 
index 01b4c1165c82b101a4a64dd37b59560acfc8bb17..156f2e1e6d80cf8818281b23fd0b875abdae99d2 100644 (file)
@@ -34,12 +34,9 @@ namespace parser {
 
 Tptp::Tptp(api::Solver* solver,
            SymbolManager* sm,
-           Input* input,
            bool strictMode,
            bool parseOnly)
-    : Parser(solver, sm, input, strictMode, parseOnly),
-      d_cnf(false),
-      d_fof(false)
+    : Parser(solver, sm, strictMode, parseOnly), d_cnf(false), d_fof(false)
 {
   addTheory(Tptp::THEORY_CORE);
 
index 0be74c79d417ef97d1c32faafad54f108d63e271..964c0bdfb3e7af903a8b72286d7185814e52e727 100644 (file)
@@ -90,7 +90,6 @@ class Tptp : public Parser {
  protected:
   Tptp(api::Solver* solver,
        SymbolManager* sm,
-       Input* input,
        bool strictMode = false,
        bool parseOnly = false);
 
index b51982d59bf693d91b9316624689b4b9d76e93ac..c226da13b63fe36773367a685966ddd27fa9ec88 100644 (file)
@@ -102,10 +102,10 @@ std::string parse(std::string instr,
   solver.setOption("input-language", input_language);
   solver.setOption("output-language", output_language);
   SymbolManager symman(&solver);
-  Parser* parser = ParserBuilder(&solver, &symman, "internal-buffer")
-                       .withStringInput(declarations)
-                       .withInputLanguage(ilang)
-                       .build();
+  std::unique_ptr<Parser> parser(
+      ParserBuilder(&solver, &symman).withInputLanguage(ilang).build());
+  parser->setInput(
+      Input::newStringInput(ilang, declarations, "internal-buffer"));
   // we don't need to execute the commands, but we DO need to parse them to
   // get the declarations
   while (Command* c = parser->nextCommand())
@@ -117,7 +117,6 @@ std::string parse(std::string instr,
   api::Term e = parser->nextExpression();
   std::string s = e.toString();
   assert(parser->nextExpression().isNull());  // next expr should be null
-  delete parser;
   return s;
 }
 
index 431bde7d6659adbad3cd914e40a8c6efffa09973..04b366cf0b4cdfe487d5aa5e1632752ae9c642ea 100644 (file)
@@ -60,8 +60,11 @@ void testGetInfo(api::Solver* solver, const char* s)
 {
   std::unique_ptr<SymbolManager> symman(new SymbolManager(solver));
 
-  ParserBuilder pb(solver, symman.get(), "<internal>", solver->getOptions());
-  Parser* p = pb.withStringInput(string("(get-info ") + s + ")").build();
+  std::unique_ptr<Parser> p(
+      ParserBuilder(solver, symman.get(), solver->getOptions()).build());
+  p->setInput(Input::newStringInput(language::input::LANG_SMTLIB_V2,
+                                    string("(get-info ") + s + ")",
+                                    "<internal>"));
   assert(p != NULL);
   Command* c = p->nextCommand();
   assert(c != NULL);
@@ -69,7 +72,6 @@ void testGetInfo(api::Solver* solver, const char* s)
   stringstream ss;
   c->invoke(solver, symman.get(), ss);
   assert(p->nextCommand() == NULL);
-  delete p;
   delete c;
   cout << ss.str() << endl << endl;
 }
index b6c433663001a6d44c4bea22eeb6f69fc59f17bc..78a67f6d0147b32dade0bb40bae1627bd2cdd203 100644 (file)
@@ -86,11 +86,11 @@ class TestParserBlackParser : public TestInternal
   void tryGoodInput(const std::string goodInput)
   {
     d_symman.reset(new SymbolManager(d_solver.get()));
-    Parser* parser = ParserBuilder(d_solver.get(), d_symman.get(), "test")
-                         .withStringInput(goodInput)
-                         .withOptions(d_options)
-                         .withInputLanguage(d_lang)
-                         .build();
+    std::unique_ptr<Parser> parser(ParserBuilder(d_solver.get(), d_symman.get())
+                                       .withOptions(d_options)
+                                       .withInputLanguage(d_lang)
+                                       .build());
+    parser->setInput(Input::newStringInput(d_lang, goodInput, "test"));
     ASSERT_FALSE(parser->done());
     Command* cmd;
     while ((cmd = parser->nextCommand()) != NULL)
@@ -100,18 +100,17 @@ class TestParserBlackParser : public TestInternal
     }
 
     ASSERT_TRUE(parser->done());
-    delete parser;
   }
 
   void tryBadInput(const std::string badInput, bool strictMode = false)
   {
     d_symman.reset(new SymbolManager(d_solver.get()));
-    Parser* parser = ParserBuilder(d_solver.get(), d_symman.get(), "test")
-                         .withStringInput(badInput)
-                         .withOptions(d_options)
-                         .withInputLanguage(d_lang)
-                         .withStrictMode(strictMode)
-                         .build();
+    std::unique_ptr<Parser> parser(ParserBuilder(d_solver.get(), d_symman.get())
+                                       .withOptions(d_options)
+                                       .withInputLanguage(d_lang)
+                                       .withStrictMode(strictMode)
+                                       .build());
+    parser->setInput(Input::newStringInput(d_lang, badInput, "test"));
     ASSERT_THROW(
         {
           Command* cmd;
@@ -123,23 +122,21 @@ class TestParserBlackParser : public TestInternal
           std::cout << "\nBad input succeeded:\n" << badInput << std::endl;
         },
         ParserException);
-    delete parser;
   }
 
   void tryGoodExpr(const std::string goodExpr)
   {
     d_symman.reset(new SymbolManager(d_solver.get()));
-    Parser* parser = ParserBuilder(d_solver.get(), d_symman.get(), "test")
-                         .withStringInput(goodExpr)
-                         .withOptions(d_options)
-                         .withInputLanguage(d_lang)
-                         .build();
-
+    std::unique_ptr<Parser> parser(ParserBuilder(d_solver.get(), d_symman.get())
+                                       .withOptions(d_options)
+                                       .withInputLanguage(d_lang)
+                                       .build());
+    parser->setInput(Input::newStringInput(d_lang, goodExpr, "test"));
     if (d_lang == LANG_SMTLIB_V2)
     {
       /* Use QF_LIA to make multiplication ("*") available */
       std::unique_ptr<Command> cmd(
-          static_cast<Smt2*>(parser)->setLogic("QF_LIA"));
+          static_cast<Smt2*>(parser.get())->setLogic("QF_LIA"));
     }
 
     ASSERT_FALSE(parser->done());
@@ -150,7 +147,6 @@ class TestParserBlackParser : public TestInternal
     e = parser->nextExpression();
     ASSERT_TRUE(parser->done());
     ASSERT_TRUE(e.isNull());
-    delete parser;
   }
 
   /**
@@ -165,12 +161,12 @@ class TestParserBlackParser : public TestInternal
   void tryBadExpr(const std::string badExpr, bool strictMode = false)
   {
     d_symman.reset(new SymbolManager(d_solver.get()));
-    Parser* parser = ParserBuilder(d_solver.get(), d_symman.get(), "test")
-                         .withStringInput(badExpr)
-                         .withOptions(d_options)
-                         .withInputLanguage(d_lang)
-                         .withStrictMode(strictMode)
-                         .build();
+    std::unique_ptr<Parser> parser(ParserBuilder(d_solver.get(), d_symman.get())
+                                       .withOptions(d_options)
+                                       .withInputLanguage(d_lang)
+                                       .withStrictMode(strictMode)
+                                       .build());
+    parser->setInput(Input::newStringInput(d_lang, badExpr, "test"));
     setupContext(*parser);
     ASSERT_FALSE(parser->done());
     ASSERT_THROW(api::Term e = parser->nextExpression();
@@ -179,7 +175,6 @@ class TestParserBlackParser : public TestInternal
                            << "Input: <<" << badExpr << ">>" << std::endl
                            << "Output: <<" << e << ">>" << std::endl;
                  , ParserException);
-    delete parser;
   }
 
   Options d_options;
index 4ed036202ac64e4d10f8267a90d7a438a8d75a43..fa532f6b6d2b77f16ae339b9e11f65c803abe6b3 100644 (file)
@@ -40,28 +40,19 @@ class TestParseBlackParserBuilder : public TestApi
  protected:
   void SetUp() override { d_symman.reset(new SymbolManager(&d_solver)); }
 
-  void checkEmptyInput(ParserBuilder& builder)
+  void checkEmptyInput(Parser* parser)
   {
-    Parser* parser = builder.build();
-    ASSERT_NE(parser, nullptr);
-
     api::Term e = parser->nextExpression();
     ASSERT_TRUE(e.isNull());
-
-    delete parser;
   }
 
-  void checkTrueInput(ParserBuilder& builder)
+  void checkTrueInput(Parser* parser)
   {
-    Parser* parser = builder.build();
-    ASSERT_NE(parser, nullptr);
-
     api::Term e = parser->nextExpression();
     ASSERT_EQ(e, d_solver.mkTrue());
 
     e = parser->nextExpression();
     ASSERT_TRUE(e.isNull());
-    delete parser;
   }
 
   char* mkTemp()
@@ -80,8 +71,11 @@ TEST_F(TestParseBlackParserBuilder, empty_file_input)
   char* filename = mkTemp();
   ASSERT_NE(filename, nullptr);
 
-  checkEmptyInput(ParserBuilder(&d_solver, d_symman.get(), filename)
-                      .withInputLanguage(LANG_CVC));
+  std::unique_ptr<Parser> parser(ParserBuilder(&d_solver, d_symman.get())
+                                     .withInputLanguage(LANG_CVC)
+                                     .build());
+  parser->setInput(Input::newFileInput(LANG_CVC, filename, false));
+  checkEmptyInput(parser.get());
 
   remove(filename);
   free(filename);
@@ -95,8 +89,11 @@ TEST_F(TestParseBlackParserBuilder, simple_file_input)
   fs << "TRUE" << std::endl;
   fs.close();
 
-  checkTrueInput(ParserBuilder(&d_solver, d_symman.get(), filename)
-                     .withInputLanguage(LANG_CVC));
+  std::unique_ptr<Parser> parser(ParserBuilder(&d_solver, d_symman.get())
+                                     .withInputLanguage(LANG_CVC)
+                                     .build());
+  parser->setInput(Input::newFileInput(LANG_CVC, filename, false));
+  checkTrueInput(parser.get());
 
   remove(filename);
   free(filename);
@@ -104,32 +101,40 @@ TEST_F(TestParseBlackParserBuilder, simple_file_input)
 
 TEST_F(TestParseBlackParserBuilder, empty_string_input)
 {
-  checkEmptyInput(ParserBuilder(&d_solver, d_symman.get(), "foo")
-                      .withInputLanguage(LANG_CVC)
-                      .withStringInput(""));
+  std::unique_ptr<Parser> parser(ParserBuilder(&d_solver, d_symman.get())
+                                     .withInputLanguage(LANG_CVC)
+                                     .build());
+  parser->setInput(Input::newStringInput(LANG_CVC, "", "foo"));
+  checkEmptyInput(parser.get());
 }
 
 TEST_F(TestParseBlackParserBuilder, true_string_input)
 {
-  checkTrueInput(ParserBuilder(&d_solver, d_symman.get(), "foo")
-                     .withInputLanguage(LANG_CVC)
-                     .withStringInput("TRUE"));
+  std::unique_ptr<Parser> parser(ParserBuilder(&d_solver, d_symman.get())
+                                     .withInputLanguage(LANG_CVC)
+                                     .build());
+  parser->setInput(Input::newStringInput(LANG_CVC, "TRUE", "foo"));
+  checkTrueInput(parser.get());
 }
 
 TEST_F(TestParseBlackParserBuilder, empty_stream_input)
 {
   std::stringstream ss("", std::ios_base::in);
-  checkEmptyInput(ParserBuilder(&d_solver, d_symman.get(), "foo")
-                      .withInputLanguage(LANG_CVC)
-                      .withStreamInput(ss));
+  std::unique_ptr<Parser> parser(ParserBuilder(&d_solver, d_symman.get())
+                                     .withInputLanguage(LANG_CVC)
+                                     .build());
+  parser->setInput(Input::newStreamInput(LANG_CVC, ss, "foo"));
+  checkEmptyInput(parser.get());
 }
 
 TEST_F(TestParseBlackParserBuilder, true_stream_input)
 {
   std::stringstream ss("TRUE", std::ios_base::in);
-  checkTrueInput(ParserBuilder(&d_solver, d_symman.get(), "foo")
-                     .withInputLanguage(LANG_CVC)
-                     .withStreamInput(ss));
+  std::unique_ptr<Parser> parser(ParserBuilder(&d_solver, d_symman.get())
+                                     .withInputLanguage(LANG_CVC)
+                                     .build());
+  parser->setInput(Input::newStreamInput(LANG_CVC, ss, "foo"));
+  checkTrueInput(parser.get());
 }
 
 }  // namespace test