Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_Reader.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef TEUCHOS_READER_HPP
11#define TEUCHOS_READER_HPP
12
17#include <iosfwd>
18#include <functional>
19
21#include <Teuchos_any.hpp>
22
23namespace Teuchos {
24
53class Reader {
54 public:
55
57 virtual ~Reader() {}
58
67
78 void read_stream(any& result, std::istream& stream, std::string const& stream_name_in);
79
84 void read_string(any& result, std::string const& string, std::string const& string_name);
85
90 void read_file(any& result, std::string const& file_name);
91
92 protected:
93
114 virtual void at_shift(any& result, int token, std::string& text);
115
139 virtual void at_reduce(any& result, int production, std::vector<any>& rhs);
140
141 protected: // variables for normal Context Free Grammar parsing and error reporting
142
143 ReaderTablesPtr tables;
144 Parser const& parser;
145 FiniteAutomaton const& lexer;
146 GrammarPtr grammar;
147 std::size_t line;
148 std::size_t column;
149 int lexer_state;
150 std::string lexer_text;
151 std::string line_text;
152 int lexer_token;
153 std::size_t last_lexer_accept;
154 std::size_t last_lexer_accept_line;
155 std::size_t last_lexer_accept_column;
156 std::string last_lexer_accept_line_text;
157 int parser_state;
158 std::vector<int> parser_stack;
159 std::vector<any> value_stack;
160 std::vector<any> reduction_rhs;
161 std::string stream_name;
162 bool did_accept;
163
164 protected: // variables for indentation-sensitive language parsing
165
166 bool sensing_indent;
167 std::string indent_text;
168 struct IndentStackEntry {
169 std::size_t line;
170 std::size_t start_length;
171 std::size_t end_length;
172 IndentStackEntry(std::size_t l, std::size_t s, std::size_t e);
173 };
174 // this is the stack that shows, for the current leading indentation
175 // characters, which subset of them came from each nested increase
176 // in indentation
177 std::vector<IndentStackEntry> indent_stack;
178 // this stack notes, for each symbol in the pushdown automaton
179 // stack, how many characters indent the line that that symbol
180 // starts on
181 std::vector<std::size_t> symbol_indentation_stack;
182
183 private: // helper methods
184
185 void at_token(std::istream& stream);
186 void indent_mismatch();
187 void at_token_indent(std::istream& stream);
188 void at_lexer_end(std::istream& stream);
189 void backtrack_to_last_accept(std::istream& stream);
190 void reset_lexer_state();
191 void update_position(char c);
192 void error_print_line(std::istream& is, std::ostream& os);
193};
194
195class DebugReader : public Reader {
196 public:
197 DebugReader(ReaderTablesPtr tables_in, std::ostream& os_in);
198 virtual ~DebugReader() {}
199 protected:
200 virtual void at_shift(any& result, int token, std::string& text);
201 virtual void at_reduce(any& result, int token, std::vector<any>& rhs);
202 private:
203 std::ostream& os;
204};
205
206}
207
208#endif
Declares Teuchos::ReaderTables.
Modified boost::any class for holding a templated value.
The main class for users to read text using TeuchosParser.
virtual ~Reader()
Virtual destructor, allows polymorphism.
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.
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.
void read_stream(any &result, std::istream &stream, std::string const &stream_name_in)
The main method for reading a stream of text.
Modified boost::any class, which is a container for a templated value.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
RCP< const ReaderTables > ReaderTablesPtr
an RCP to a const ReaderTables