Line | Branch | Exec | Source |
---|---|---|---|
1 | #include <gyoji-context/token-stream.hpp> | ||
2 | |||
3 | using namespace Gyoji::context; | ||
4 | |||
5 | static const std::string internal_filename("internal"); | ||
6 | static const SourceReference zero_source_ref(internal_filename, 1, 0, 0); | ||
7 | |||
8 | const SourceReference & | ||
9 | ✗ | TokenStream::get_zero_source_ref() | |
10 | { | ||
11 | ✗ | return zero_source_ref; | |
12 | } | ||
13 | |||
14 | 298 | TokenStream::TokenStream() | |
15 | 298 | {} | |
16 | |||
17 | 298 | TokenStream::~TokenStream() | |
18 | 298 | {} | |
19 | |||
20 | const std::vector<Gyoji::owned<Token>> & | ||
21 | 48 | TokenStream::get_tokens() const | |
22 | { | ||
23 | 48 | return tokens; | |
24 | } | ||
25 | |||
26 | /** | ||
27 | * Returns the most recent source reference found. | ||
28 | * If no prior source reference was found, this must | ||
29 | * be the first token in the file and we will return | ||
30 | * the most recent one. | ||
31 | */ | ||
32 | const SourceReference & | ||
33 | 7934 | TokenStream::get_current_source_ref() const | |
34 | { | ||
35 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7934 times.
|
7934 | if (tokens.size() == 0) { |
36 | ✗ | return zero_source_ref; | |
37 | } | ||
38 | 7934 | return tokens.back()->get_source_ref(); | |
39 | } | ||
40 | |||
41 | |||
42 | 72 | std::string TokenStream::get_line(size_t _line) const | |
43 | { | ||
44 | 72 | std::string msg; | |
45 | 72 | auto it = tokens_by_lineno.find(_line); | |
46 |
2/2✓ Branch 2 taken 50 times.
✓ Branch 3 taken 22 times.
|
72 | if (it == tokens_by_lineno.end()) { |
47 | 50 | return msg; | |
48 | } | ||
49 | 22 | const std::vector<Token *> & tokens = it->second; | |
50 |
2/2✓ Branch 5 taken 28 times.
✓ Branch 6 taken 22 times.
|
50 | for (Token* token : tokens) { |
51 | 28 | msg += token->get_value(); | |
52 | } | ||
53 | 22 | return msg; | |
54 | } | ||
55 | |||
56 | std::vector<std::pair<size_t, std::string>> | ||
57 | 22 | TokenStream::context(size_t line_start, size_t line_end) const | |
58 | { | ||
59 | 22 | std::vector<std::pair<size_t, std::string>> ret; | |
60 | 22 | line_start = (unsigned long)std::max((long)line_start, (long)0); | |
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (line_end < line_start) { |
62 | ✗ | return ret; | |
63 | } | ||
64 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 22 times.
|
94 | for (size_t i = line_start; i <= line_end; i++) { |
65 | 72 | std::string msg(get_line(i)); | |
66 | 72 | ret.push_back(std::pair<size_t, std::string>((size_t)i, msg)); | |
67 | 72 | } | |
68 | 22 | return ret; | |
69 | } | ||
70 | |||
71 | const Token & | ||
72 | 58548 | TokenStream::add_token( | |
73 | TokenID _typestr, | ||
74 | std::string _value, | ||
75 | const std::string & _filename, | ||
76 | size_t _line, | ||
77 | size_t _column | ||
78 | ) | ||
79 | { | ||
80 | 58548 | Gyoji::owned<Token> token = std::make_unique<Token>(_typestr, _value, _filename, _line, _column, _value.size()); | |
81 | 58548 | const Token & token_ref = *token; | |
82 | 58548 | tokens_by_lineno[_line].push_back(token.get()); | |
83 | 58548 | tokens.push_back(std::move(token)); | |
84 | 117096 | return token_ref; | |
85 | 58548 | } | |
86 | |||
87 | void | ||
88 | 240 | TokenStream::append_token(std::string _value) | |
89 | { | ||
90 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
|
240 | if (tokens.empty()) return; |
91 | 240 | const auto & it = tokens.back(); | |
92 | 240 | it->append(_value); | |
93 | } | ||
94 | |||
95 | 58548 | Token::Token( | |
96 | TokenID _typestr, | ||
97 | std::string _value, | ||
98 | const std::string & _filename, | ||
99 | size_t _line, | ||
100 | size_t _column, | ||
101 | size_t _length | ||
102 | 58548 | ) | |
103 | 58548 | : typestr(_typestr) | |
104 | 58548 | , value(_value) | |
105 | 58548 | , src_ref(_filename, _line, _column, _length) | |
106 | 58548 | {} | |
107 | |||
108 | 58548 | Token::~Token() | |
109 | 58548 | {} | |
110 | |||
111 | static std::string no_type_string("No-type"); | ||
112 | |||
113 | const TokenID & | ||
114 | 48164 | Token::get_type() const | |
115 | 48164 | { return typestr; } | |
116 | |||
117 | const std::string & | ||
118 | 42796 | Token::get_value() const | |
119 | 42796 | { return value; } | |
120 | |||
121 | void | ||
122 | 240 | Token::append(std::string & _value) | |
123 | { | ||
124 | 240 | value += _value; | |
125 | 240 | } | |
126 | const SourceReference & | ||
127 | 40122 | Token::get_source_ref() const | |
128 | 40122 | { return src_ref; } | |
129 |