forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_verifier.h
82 lines (66 loc) · 2.77 KB
/
parser_verifier.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
70
71
72
73
74
75
76
77
78
79
80
81
82
// 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.
#ifndef VERIBLE_COMMON_TEXT_PARSER_VERIFIER_H_
#define VERIBLE_COMMON_TEXT_PARSER_VERIFIER_H_
#include <vector>
#include "common/text/concrete_syntax_leaf.h"
#include "common/text/concrete_syntax_tree.h"
#include "common/text/symbol.h"
#include "common/text/token_info.h"
#include "common/text/token_stream_view.h"
#include "common/text/tree_compare.h"
#include "common/text/visitors.h"
namespace verible {
// ParserVerifier is used to compare a parse tree and a token stream view. It
// Iterates through both, and checks that every token appearing in the view
// also appears in the same order in the tree.
// Unmatched tokens are reported
//
// Usage:
// const Symbol& root = ...
// const TokenStreamView& view = ...
// ParserVerifier verifier(root, view);
// auto unmatched_tokens = verifier.Verify();
// ... process unmatched tokens ...
class ParserVerifier : public TreeVisitorRecursive {
public:
ParserVerifier(const Symbol& root, const TokenStreamView& view)
: root_(root), view_(view), view_iterator_(view.begin()) {}
ParserVerifier(const Symbol& root, const TokenStreamView& view,
const TokenComparator& token_comparator)
: root_(root),
view_(view),
view_iterator_(view.begin()),
token_comparator_(token_comparator) {}
// Iterates through tree and stream view provided in constructor
std::vector<TokenInfo> Verify();
// Do call these methods directly. Use Verify instead
// TODO(jeremycs): changed these to protected and make SyntaxTreeLeaf
// and SyntaxTreeNode friend classes
void Visit(const SyntaxTreeLeaf& leaf) final;
void Visit(const SyntaxTreeNode& node) final{};
private:
const Symbol& root_;
const TokenStreamView& view_;
// Current position in view. Ensures visit once behavior for each token
TokenStreamView::const_iterator view_iterator_;
// List of tokens contained in view that were not found in tree
std::vector<TokenInfo> unmatched_tokens_;
TokenComparator token_comparator_ = default_comparator;
static bool default_comparator(const TokenInfo& t1, const TokenInfo& t2) {
return t1 == t2;
}
};
} // namespace verible
#endif // VERIBLE_COMMON_TEXT_PARSER_VERIFIER_H_