forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_stream_adapter.cc
50 lines (44 loc) · 1.81 KB
/
token_stream_adapter.cc
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
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/lexer/token_stream_adapter.h"
#include "absl/status/status.h"
#include "common/lexer/lexer.h"
#include "common/lexer/token_generator.h"
#include "common/text/token_info.h"
namespace verible {
TokenGenerator MakeTokenGenerator(Lexer* l) {
return [=]() { return l->DoNextToken(); };
}
absl::Status MakeTokenSequence(
Lexer* lexer, absl::string_view text, TokenSequence* tokens,
const std::function<void(const TokenInfo&)>& error_token_handler) {
// TODO(fangism): provide a Lexer interface to grab all tokens en masse,
// which would save virtual function dispatch overhead.
lexer->Restart(text);
do {
const auto& new_token = lexer->DoNextToken();
tokens->push_back(new_token);
if (lexer->TokenIsError(new_token)) { // one more virtual function call
error_token_handler(new_token);
// Stop-on-first-error.
return absl::InvalidArgumentError("Lexical error.");
}
} while (!tokens->back().isEOF());
// Final token is EOF.
// Force EOF token's text range to be empty, pointing to end of original
// string. Otherwise, its range ends up overlapping with the previous token.
tokens->back() = TokenInfo::EOFToken(text);
return absl::OkStatus();
}
} // namespace verible