Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief This file contains a class that represents textual input.
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #ifndef ELEKTRA_PLUGIN_YAMBI_INPUT_HPP
10 : #define ELEKTRA_PLUGIN_YAMBI_INPUT_HPP
11 :
12 : // -- Imports ------------------------------------------------------------------
13 :
14 : #include <fstream>
15 :
16 : // -- Class --------------------------------------------------------------------
17 :
18 : /**
19 : * @brief This class provides methods for a lexer to analyze textual input.
20 : */
21 126 : class Input
22 : {
23 :
24 : /** This variable stores the input represented by this class. */
25 : std::u32string input;
26 :
27 : /** This variable stores the current position inside `input`. */
28 : size_t position = 0;
29 :
30 : public:
31 : /**
32 : * @brief This constructor creates an input from the given stream.
33 : *
34 : * @param stream This parameter stores the text this object operates on.
35 : */
36 : Input (std::ifstream const & stream);
37 :
38 : /**
39 : * @brief This function returns a character that was not consumed yet.
40 : *
41 : * @param offset This variable specifies the index of the character
42 : * this method should retrieve as offset to the last consumed
43 : * character.
44 : *
45 : * @return A character which is `offset` positions away from the last
46 : * consumed character
47 : */
48 : size_t LA (size_t const offset) const;
49 :
50 : /**
51 : * @brief This method consumes the next character of `input`.
52 : */
53 : void consume ();
54 :
55 : /**
56 : * @brief Retrieve the current position inside the input.
57 : *
58 : * @return The current position in number of characters
59 : */
60 : size_t index () const;
61 :
62 : /**
63 : * @brief This method retrieves the text between `start` (inclusive) and the
64 : * current position (exclusive).
65 : *
66 : * @param start This parameter specifies the start index of the string this
67 : * functions returns.
68 : *
69 : * @return A UTF-8 encoded substring of input starting at `start` and ending
70 : * one character before the current position in the input
71 : */
72 : std::string getText (size_t const start) const;
73 :
74 : /**
75 : * @brief This method retrieves the whole text represented by this input object.
76 : *
77 : * @return A UTF-8 encoded string that stores the data of the input
78 : */
79 : std::string toString () const;
80 : };
81 :
82 : #endif // ELEKTRA_PLUGIN_YAMBI_INPUT_HPP
|