forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_stream_view.h
69 lines (52 loc) · 2.8 KB
/
token_stream_view.h
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
// 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.
// TokenStreamView is the interface to parsers.
#ifndef VERIBLE_COMMON_TEXT_TOKEN_STREAM_VIEW_H_
#define VERIBLE_COMMON_TEXT_TOKEN_STREAM_VIEW_H_
#include <functional>
#include <vector>
#include "absl/strings/string_view.h"
#include "common/text/token_info.h"
#include "common/util/iterator_range.h"
namespace verible {
// TODO(fangism): If we make this std::list, then we never have to worry
// about iterator invalidation due to insertion and deletion.
// Evaluate this decision later.
using TokenSequence = std::vector<TokenInfo>;
// Slice of tokens that works like a container.
using TokenRange = iterator_range<TokenSequence::const_iterator>;
// TokenStreamView is the type that is transformed and returned by filters.
using TokenStreamView = std::vector<TokenSequence::const_iterator>;
// TokenStreamReferenceView is TokenStreamView with writeable iterators.
// This should only be used in functions that intend to transform a token stream
// such as when applying context to alter token symbols.
using TokenStreamReferenceView = std::vector<TokenSequence::iterator>;
// Slice of TokenSequence iterators that works like a container
using TokenViewRange = iterator_range<TokenStreamView::const_iterator>;
// Tokens that evaluate to false with these predicates are removed.
using TokenFilterPredicate = std::function<bool(const TokenInfo &)>;
// Populates a TokenStreamView with every iterator of a TokenSequence.
void InitTokenStreamView(const TokenSequence &, TokenStreamView *);
// Create a new TokenStreamView with tokens conditionally omitted.
void FilterTokenStreamView(const TokenFilterPredicate &keep,
const TokenStreamView &src, TokenStreamView *dest);
// Remove tokens from a TokenStreamView according to a predicate.
void FilterTokenStreamViewInPlace(const TokenFilterPredicate &keep,
TokenStreamView *);
// Returns iterator range of TokenSequence iterators that span the given file
// offsets. The second iterator points 1-past-the-end of the range.
TokenViewRange TokenViewRangeSpanningOffsets(const TokenStreamView &view,
absl::string_view range);
} // namespace verible
#endif // VERIBLE_COMMON_TEXT_TOKEN_STREAM_VIEW_H_