Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_Reader.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2017) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_READER_HPP
43 #define TEUCHOS_READER_HPP
44 
49 #include <iosfwd>
50 #include <functional>
51 
52 #include <Teuchos_ReaderTables.hpp>
53 #include <Teuchos_any.hpp>
54 
55 namespace Teuchos {
56 
85 class Reader {
86  public:
87 
89  virtual ~Reader() {}
90 
98  Reader(ReaderTablesPtr tables_in);
99 
110  void read_stream(any& result, std::istream& stream, std::string const& stream_name_in);
111 
116  void read_string(any& result, std::string const& string, std::string const& string_name);
117 
122  void read_file(any& result, std::string const& file_name);
123 
124  protected:
125 
146  virtual void at_shift(any& result, int token, std::string& text);
147 
171  virtual void at_reduce(any& result, int production, std::vector<any>& rhs);
172 
173  protected: // variables for normal Context Free Grammar parsing and error reporting
174 
175  ReaderTablesPtr tables;
176  Parser const& parser;
177  FiniteAutomaton const& lexer;
178  GrammarPtr grammar;
179  std::size_t line;
180  std::size_t column;
181  int lexer_state;
182  std::string lexer_text;
183  std::string line_text;
184  int lexer_token;
185  std::size_t last_lexer_accept;
186  std::size_t last_lexer_accept_line;
187  std::size_t last_lexer_accept_column;
188  std::string last_lexer_accept_line_text;
189  int parser_state;
190  std::vector<int> parser_stack;
191  std::vector<any> value_stack;
192  std::vector<any> reduction_rhs;
193  std::string stream_name;
194  bool did_accept;
195 
196  protected: // variables for indentation-sensitive language parsing
197 
198  bool sensing_indent;
199  std::string indent_text;
200  struct IndentStackEntry {
201  std::size_t line;
202  std::size_t start_length;
203  std::size_t end_length;
204  IndentStackEntry(std::size_t l, std::size_t s, std::size_t e);
205  };
206  // this is the stack that shows, for the current leading indentation
207  // characters, which subset of them came from each nested increase
208  // in indentation
209  std::vector<IndentStackEntry> indent_stack;
210  // this stack notes, for each symbol in the pushdown automaton
211  // stack, how many characters indent the line that that symbol
212  // starts on
213  std::vector<std::size_t> symbol_indentation_stack;
214 
215  private: // helper methods
216 
217  void at_token(std::istream& stream);
218  void indent_mismatch();
219  void at_token_indent(std::istream& stream);
220  void at_lexer_end(std::istream& stream);
221  void backtrack_to_last_accept(std::istream& stream);
222  void reset_lexer_state();
223  void update_position(char c);
224  void error_print_line(std::istream& is, std::ostream& os);
225 };
226 
227 class DebugReader : public Reader {
228  public:
229  DebugReader(ReaderTablesPtr tables_in, std::ostream& os_in);
230  virtual ~DebugReader() {}
231  protected:
232  virtual void at_shift(any& result, int token, std::string& text);
233  virtual void at_reduce(any& result, int token, std::vector<any>& rhs);
234  private:
235  std::ostream& os;
236 };
237 
238 }
239 
240 #endif
Reader(ReaderTablesPtr tables_in)
Constructor: accepts an RCP to ReaderTables.
Modified boost::any class for holding a templated value.
void read_file(any &result, std::string const &file_name)
A convenience method for reading a file.
void read_string(any &result, std::string const &string, std::string const &string_name)
A convenience method for reading a string.
Modified boost::any class, which is a container for a templated value.
virtual ~Reader()
Virtual destructor, allows polymorphism.
The main class for users to read text using TeuchosParser.
Declares Teuchos::ReaderTables.
virtual void at_reduce(any &result, int production, std::vector< any > &rhs)
User-overridable REDUCE (production) method.
virtual void at_shift(any &result, int token, std::string &text)
User-overridable SHIFT (token) method.
RCP< const ReaderTables > ReaderTablesPtr
an RCP to a const ReaderTables
void read_stream(any &result, std::istream &stream, std::string const &stream_name_in)
The main method for reading a stream of text.