summaryrefslogtreecommitdiff
path: root/src/core/common/SourceContextReader.hpp
blob: cd2988055b3a43852aabcaf217c1539852d10191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file SourceContextReader.hpp
 *
 * The SourceContextReader class is used to read a SourceContext struct from
 * a SourcePosition instance and an input stream.
 *
 * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
 */

#ifndef _OUSIA_SOURCE_CONTEXT_READER_HPP_
#define _OUSIA_SOURCE_CONTEXT_READER_HPP_

#include <string>
#include <vector>
#include <limits>

#include "Location.hpp"

namespace ousia {

// Forward declarations
class CharReader;

/**
 * The SourceContextReader can read SourceContext structures given a
 * SourcePosition or SourceRange and a char reader. It is capable of managing
 * a line number cache which speeds up repeated context lookups.
 */
class SourceContextReader {
private:
	/**
	 * Cache containing the byte offset of each line break.
	 */
	std::vector<SourceOffset> cache;

public:
	/**
	 * Maximum context size. Used to indicate that the context should have an
	 * unlimited size.
	 */
	static constexpr size_t MAX_MAX_CONTEXT_LENGTH =
	    std::numeric_limits<ssize_t>::max();

	/**
	 * Default constructor. Initializes the internal lineNumberCache with a
	 * single zero entry.
	 */
	SourceContextReader();

	/**
	 * Returns the context for the char reader and the given SourceRange.
	 * Returns an invalid source context if either the given range is invalid
	 * or the byte offset described in the SourceRange cannot be reached because
	 * the CharReader cannot be seeked back to this position.
	 *
	 * @param reader is the CharReader instance from which the context should be
	 * read.
	 * @param range describes the Range within the source file for which the
	 * context should be extraced.
	 * @param filename is the filename that should be stored in the returned
	 * context.
	 * @param maxContextLength is the maximum number of characters that should
	 * be stored in the returned context.
	 * @return a SourceContext instance describing the
	 */
	SourceContext readContext(CharReader &reader, const SourceRange &range,
	                          size_t maxContextLength,
	                          const std::string &filename = "");

	/**
	 * Returns the context for the char reader and the given SourceRange.
	 * Returns an invalid source context if either the given range is invalid
	 * or the byte offset described in the SourceRange cannot be reached because
	 * the CharReader cannot be seeked back to this position. Does not limit
	 * the output context.
	 *
	 * @param reader is the CharReader instance from which the context should be
	 * read.
	 * @param range describes the Range within the source file for which the
	 * context should be extraced.
	 * @param filename is the filename that should be stored in the returned
	 * context.
	 * @return a SourceContext instance describing the
	 */
	SourceContext readContext(CharReader &reader, const SourceRange &range,
	                          const std::string &filename = "");
};
}

#endif /* _OUSIA_SOURCE_CONTEXT_READER_HPP_ */