LOOS  v2.3.2
ParserDriver.hpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 
24 
25 #if !defined(LOOS_PARSERDRIVER_HPP)
26 #define LOOS_PARSERDRIVER_HPP
27 
28 #include <string>
29 #include <vector>
30 
31 #include <FlexLexer.h>
32 
33 #include "exceptions.hpp"
34 #include "Kernel.hpp"
35 
36 namespace loos { struct ParserDriver; }
37 
38 #include "grammar.hh"
39 #include "LoosLexer.hpp"
40 
41 
42 namespace loos {
43 
47  struct ParserDriver {
48  parser *pparser;
49  LoosLexer *lexer;
50  Kernel& kern;
51  std::istringstream *isp;
52 
53 
55  explicit ParserDriver(Kernel& k) : pparser(0), lexer(0), kern(k), isp(0) { }
56 
58  ParserDriver(const std::string s, Kernel& k) : pparser(0), lexer(0), kern(k), isp(0) {
59  if (isp)
60  delete isp;
61  isp = new std::istringstream(s);
62 
63  if (lexer)
64  delete lexer;
65 
66  lexer = new LoosLexer(isp);
67  parse();
68  }
69 
70  ~ParserDriver() { delete pparser; delete lexer; delete isp; }
71 
73 
77  void parse(const std::string& s) {
78  if (isp)
79  delete isp;
80  isp = new std::istringstream(s);
81 
82  if (lexer)
83  delete lexer;
84  lexer = new LoosLexer(isp);
85 
86  parse();
87  }
88 
90  void parse(void) {
91  if (!lexer)
92  throw(LOOSError("Attempting to parse sans lexer"));
93 
94  if (!pparser)
95  pparser = new parser(*this);
96  if (pparser->parse())
97  throw(ParseError("Parse error"));
98  }
99 
100  };
101 
102 
103 }
104 
105 
106 #endif
void parse(void)
Calls the Bison parser.
void parse(const std::string &s)
Parse the passed string...
Generic LOOS exception.
Definition: exceptions.hpp:40
ParserDriver(Kernel &k)
For future parsing...
Exception when parsing input data.
Definition: exceptions.hpp:64
Namespace for most things not already encapsulated within a class.
The Kernel (virtual machine) for compiling and executing user-defined atom selections.
Definition: Kernel.hpp:42
ParserDriver(const std::string s, Kernel &k)
For parsing a string...