|
cparse
1.1.0
A small hand-written recursive-descent parser core for C++
|
A read-only position in a character stream, with line and column tracking and a save/restore mechanism. More...
#include <Cursor.hh>
Public Types | |
| using | stream_type = std::istream |
The underlying stream type (std::istream). | |
Public Member Functions | |
| Cursor (std::string sourceName="") | |
| Construct over a file, opened internally for reading. More... | |
| Cursor (std::string sourceName, std::shared_ptr< stream_type > externalStream) | |
| Construct over a caller-supplied stream. More... | |
| Cursor (Cursor const &object) | |
| Copy constructor. Copies the position; shares the stream. More... | |
| Cursor (Cursor &&object)=delete | |
| Move construction is deleted. | |
| virtual | ~Cursor () |
Destructor. Does not close a caller-supplied stream; the internally-opened ifstream is closed when its last owner drops. More... | |
| Cursor & | operator= (Cursor const &object) |
| Copy assignment. Copies the position; shares the stream. More... | |
| Cursor & | operator= (Cursor &&object)=delete |
| Move assignment is deleted. | |
| bool | operator== (Cursor const &object) const |
| Equality by location: same filename pointer, line and column. The stream contents are not compared. More... | |
| void | rewind () |
| Seek back to the start of the stream and reset the line and column counters to zero. More... | |
| void | store () noexcept |
Record the current stream offset as the saved position. If the stream is at or past end-of-input, a flag is set so a later restore() seeks to the end. More... | |
| void | restore () |
Seek back to the position recorded by the last store(). More... | |
| void | safeRestore () noexcept |
| Clear stream error flags, restore to the saved position, and put the exception mask back. Never throws. More... | |
| bool | isValid () const noexcept |
| Whether the stream exists and is readable (not EOF, not bad). More... | |
| stream_type::char_type | get () |
| Consume and return the next character; advance line/column. More... | |
| stream_type::char_type | safeGet () noexcept |
Non-throwing get(): returns ‘’\032'` at end-of-input instead of throwing. Line/column still advance. More... | |
| stream_type::char_type | peek () |
| Return the next character without consuming it. More... | |
| stream_type::char_type | safePeek () noexcept |
Non-throwing peek(): returns ‘’\032'` at end-of-input instead of throwing, and leaves the stream usable. More... | |
| std::shared_ptr< std::string > | getLocation () const noexcept |
| Shared pointer to the source-name string. More... | |
| std::string const & | getFilename () const noexcept |
| The source name. More... | |
| unsigned long int | getLineNumber () const noexcept |
| The current line number. More... | |
| unsigned long int | getColumnNumber () const noexcept |
| The current column number. More... | |
A read-only position in a character stream, with line and column tracking and a save/restore mechanism.
Cursor wraps a std::istream (an internally-opened std::ifstream when constructed from a filename, or a caller-supplied stream) and adds:
get() / safeGet(). Position starts at line 0, column 0 ("before any character"); the first read moves it to line 1, column 1.store() records tellg(), restore() seeks back to it. Used by Parser for backtracking.get() / peek() throw std::ios_base::failure at end-of-input (the exception mask is set in the constructor); safeGet() / safePeek() return the sentinel ‘’\032'` (ASCII SUB) instead.The stream is shared (std::shared_ptr); copying a Cursor copies the position but shares the underlying stream. Move construction and move assignment are deleted.
|
explicit |
Construct over a file, opened internally for reading.
| sourceName | Path to open. An empty string yields a cursor whose stream never becomes valid (isValid() stays false). |
skipws is cleared so whitespace is visible to the grammar. The exception mask is set to failbit | eofbit | badbit. References store().
Here is the call graph for this function:| fedem::parser::Cursor::Cursor | ( | std::string | sourceName, |
| std::shared_ptr< stream_type > | externalStream | ||
| ) |
Construct over a caller-supplied stream.
| sourceName | Name used for diagnostics and getFilename(); need not be a real path (e.g. "<memory>"). |
| externalStream | Stream to read from. The cursor shares ownership and sets the exception mask and skipws on it. |
References store().
Here is the call graph for this function:| fedem::parser::Cursor::Cursor | ( | Cursor const & | object | ) |
Copy constructor. Copies the position; shares the stream.
| object | Cursor to copy from. |
|
virtual |
Destructor. Does not close a caller-supplied stream; the internally-opened ifstream is closed when its last owner drops.
Copy assignment. Copies the position; shares the stream.
| object | Cursor to copy from. |
*this. | bool fedem::parser::Cursor::operator== | ( | Cursor const & | object | ) | const |
Equality by location: same filename pointer, line and column. The stream contents are not compared.
| object | Cursor to compare against. |
true if both cursors are at the same location. References getColumnNumber(), and getLocation().
Here is the call graph for this function:| void fedem::parser::Cursor::rewind | ( | ) |
Seek back to the start of the stream and reset the line and column counters to zero.
References store().
Here is the call graph for this function:
|
noexcept |
Record the current stream offset as the saved position. If the stream is at or past end-of-input, a flag is set so a later restore() seeks to the end.
Referenced by Cursor(), rewind(), and fedem::parser::Parser::storeCursor().
Here is the caller graph for this function:| void fedem::parser::Cursor::restore | ( | ) |
Seek back to the position recorded by the last store().
safeRestore() to recover from that. References get().
Referenced by fedem::parser::Parser::restoreCursor(), and safeRestore().
Here is the call graph for this function:
Here is the caller graph for this function:
|
noexcept |
Clear stream error flags, restore to the saved position, and put the exception mask back. Never throws.
References restore().
Here is the call graph for this function:
|
noexcept |
Whether the stream exists and is readable (not EOF, not bad).
true while more input can be read. | Cursor::stream_type::char_type fedem::parser::Cursor::get | ( | ) |
Consume and return the next character; advance line/column.
| std::runtime_error | if the internal stream pointer is null. |
| std::ios_base::failure | at end-of-input or on stream error — this is the path Parser::parse() uses to detect EOF. |
Referenced by restore().
Here is the caller graph for this function:
|
noexcept |
Non-throwing get(): returns ‘’\032'` at end-of-input instead of throwing. Line/column still advance.
| Cursor::stream_type::char_type fedem::parser::Cursor::peek | ( | ) |
Return the next character without consuming it.
| std::runtime_error | if the internal stream pointer is null. |
| std::ios_base::failure | at end-of-input or on stream error. |
|
noexcept |
Non-throwing peek(): returns ‘’\032'` at end-of-input instead of throwing, and leaves the stream usable.
|
inlinenoexcept |
Shared pointer to the source-name string.
std::string holding the source name; may be null. Referenced by operator==().
Here is the caller graph for this function:
|
inlinenoexcept |
The source name.
|
inlinenoexcept |
The current line number.
|
inlinenoexcept |
The current column number.
Referenced by operator==().
Here is the caller graph for this function: