| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* Copyright 2025 Jonathan S. Arney | ||
| 2 | * | ||
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | * you may not use this file except in compliance with the License. | ||
| 5 | * You may obtain a copy of the License at | ||
| 6 | * | ||
| 7 | * https://github.com/jarney/gyoji/blob/master/LICENSE | ||
| 8 | * | ||
| 9 | * Unless required by applicable law or agreed to in writing, software | ||
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | * See the License for the specific language governing permissions and | ||
| 13 | * limitations under the License. | ||
| 14 | */ | ||
| 15 | #include <gyoji-context/token-stream.hpp> | ||
| 16 | |||
| 17 | using namespace Gyoji::context; | ||
| 18 | |||
| 19 | static const std::string internal_filename("internal"); | ||
| 20 | static const SourceReference zero_source_ref(internal_filename, 1, 0, 0); | ||
| 21 | |||
| 22 | const SourceReference & | ||
| 23 | ✗ | TokenStream::get_zero_source_ref() | |
| 24 | { | ||
| 25 | ✗ | return zero_source_ref; | |
| 26 | } | ||
| 27 | |||
| 28 | 296 | TokenStream::TokenStream() | |
| 29 | 296 | {} | |
| 30 | |||
| 31 | 296 | TokenStream::~TokenStream() | |
| 32 | 296 | {} | |
| 33 | |||
| 34 | const std::vector<Gyoji::owned<Token>> & | ||
| 35 | 48 | TokenStream::get_tokens() const | |
| 36 | { | ||
| 37 | 48 | return tokens; | |
| 38 | } | ||
| 39 | |||
| 40 | /** | ||
| 41 | * Returns the most recent source reference found. | ||
| 42 | * If no prior source reference was found, this must | ||
| 43 | * be the first token in the file and we will return | ||
| 44 | * the most recent one. | ||
| 45 | */ | ||
| 46 | const SourceReference & | ||
| 47 | 8248 | TokenStream::get_current_source_ref() const | |
| 48 | { | ||
| 49 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8248 times.
|
8248 | if (tokens.size() == 0) { |
| 50 | ✗ | return zero_source_ref; | |
| 51 | } | ||
| 52 | 8248 | return tokens.back()->get_source_ref(); | |
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | 72 | std::string TokenStream::get_line(size_t _line) const | |
| 57 | { | ||
| 58 | 72 | std::string msg; | |
| 59 | 72 | auto it = tokens_by_lineno.find(_line); | |
| 60 |
2/2✓ Branch 2 taken 50 times.
✓ Branch 3 taken 22 times.
|
72 | if (it == tokens_by_lineno.end()) { |
| 61 | 50 | return msg; | |
| 62 | } | ||
| 63 | 22 | const std::vector<Token *> & tokens = it->second; | |
| 64 |
2/2✓ Branch 5 taken 28 times.
✓ Branch 6 taken 22 times.
|
50 | for (Token* token : tokens) { |
| 65 | 28 | msg += token->get_value(); | |
| 66 | } | ||
| 67 | 22 | return msg; | |
| 68 | } | ||
| 69 | |||
| 70 | std::vector<std::pair<size_t, std::string>> | ||
| 71 | 22 | TokenStream::context(size_t line_start, size_t line_end) const | |
| 72 | { | ||
| 73 | 22 | std::vector<std::pair<size_t, std::string>> ret; | |
| 74 | 22 | line_start = (unsigned long)std::max((long)line_start, (long)0); | |
| 75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (line_end < line_start) { |
| 76 | ✗ | return ret; | |
| 77 | } | ||
| 78 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 22 times.
|
94 | for (size_t i = line_start; i <= line_end; i++) { |
| 79 | 72 | std::string msg(get_line(i)); | |
| 80 | 72 | ret.push_back(std::pair<size_t, std::string>((size_t)i, msg)); | |
| 81 | 72 | } | |
| 82 | 22 | return ret; | |
| 83 | } | ||
| 84 | |||
| 85 | const Token & | ||
| 86 | 58888 | TokenStream::add_token( | |
| 87 | TokenID _typestr, | ||
| 88 | std::string _value, | ||
| 89 | const std::string & _filename, | ||
| 90 | size_t _line, | ||
| 91 | size_t _column | ||
| 92 | ) | ||
| 93 | { | ||
| 94 | 58888 | Gyoji::owned<Token> token = std::make_unique<Token>(_typestr, _value, _filename, _line, _column, _value.size()); | |
| 95 | 58888 | const Token & token_ref = *token; | |
| 96 | 58888 | tokens_by_lineno[_line].push_back(token.get()); | |
| 97 | 58888 | tokens.push_back(std::move(token)); | |
| 98 | 117776 | return token_ref; | |
| 99 | 58888 | } | |
| 100 | |||
| 101 | void | ||
| 102 | 240 | TokenStream::append_token(std::string _value) | |
| 103 | { | ||
| 104 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
|
240 | if (tokens.empty()) return; |
| 105 | 240 | const auto & it = tokens.back(); | |
| 106 | 240 | it->append(_value); | |
| 107 | } | ||
| 108 | |||
| 109 | 58888 | Token::Token( | |
| 110 | TokenID _typestr, | ||
| 111 | std::string _value, | ||
| 112 | const std::string & _filename, | ||
| 113 | size_t _line, | ||
| 114 | size_t _column, | ||
| 115 | size_t _length | ||
| 116 | 58888 | ) | |
| 117 | 58888 | : typestr(_typestr) | |
| 118 | 58888 | , value(_value) | |
| 119 | 58888 | , src_ref(_filename, _line, _column, _length) | |
| 120 | 58888 | {} | |
| 121 | |||
| 122 | 58888 | Token::~Token() | |
| 123 | 58888 | {} | |
| 124 | |||
| 125 | static std::string no_type_string("No-type"); | ||
| 126 | |||
| 127 | const TokenID & | ||
| 128 | 48204 | Token::get_type() const | |
| 129 | 48204 | { return typestr; } | |
| 130 | |||
| 131 | const std::string & | ||
| 132 | 46374 | Token::get_value() const | |
| 133 | 46374 | { return value; } | |
| 134 | |||
| 135 | void | ||
| 136 | 240 | Token::append(std::string & _value) | |
| 137 | { | ||
| 138 | 240 | value += _value; | |
| 139 | 240 | } | |
| 140 | const SourceReference & | ||
| 141 | 40538 | Token::get_source_ref() const | |
| 142 | 40538 | { return src_ref; } | |
| 143 |