Skip to content

Commit b85f6d9

Browse files
authored
libSyntax: add a C++ side read-only syntax visitor to facilitate verification. NFC (#13882)
1 parent cd7600c commit b85f6d9

File tree

7 files changed

+111
-3
lines changed

7 files changed

+111
-3
lines changed

include/swift/Syntax/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ set(generated_include_sources
55
SyntaxKind.h.gyb
66
SyntaxNodes.h.gyb
77
SyntaxBuilders.h.gyb
8-
SyntaxFactory.h.gyb)
8+
SyntaxFactory.h.gyb
9+
SyntaxVisitor.h.gyb)
910

1011
add_gyb_target(swift-syntax-generated-headers
1112
"${generated_include_sources}")

include/swift/Syntax/Syntax.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class SyntaxASTMap;
3737

3838
namespace syntax {
3939

40+
struct SyntaxVisitor;
41+
4042
template <typename SyntaxNode>
4143
SyntaxNode make(RC<RawSyntax> Raw) {
4244
auto Data = SyntaxData::make(Raw);
@@ -172,6 +174,10 @@ class Syntax {
172174
// Trivially true.
173175
return true;
174176
}
177+
178+
/// Recursively visit this node.
179+
void accept(SyntaxVisitor &Visitor);
180+
175181
// TODO: hasSameStructureAs ?
176182
};
177183

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
%{
2+
# -*- mode: C++ -*-
3+
from gyb_syntax_support import *
4+
NODE_MAP = create_node_map()
5+
# Ignore the following admonition; it applies to the resulting .h file only
6+
}%
7+
//// Automatically Generated From SyntaxVisitor.h.gyb.
8+
//// Do Not Edit Directly!
9+
//===---------------- SyntaxVisitor.h - SyntaxVisitor definitions ---------===//
10+
//
11+
// This source file is part of the Swift.org open source project
12+
//
13+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
14+
// Licensed under Apache License v2.0 with Runtime Library Exception
15+
//
16+
// See https://swift.org/LICENSE.txt for license information
17+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
#ifndef SWIFT_SYNTAX_VISITOR_H
22+
#define SWIFT_SYNTAX_VISITOR_H
23+
24+
#include "swift/Syntax/Syntax.h"
25+
#include "swift/Syntax/SyntaxCollection.h"
26+
#include "swift/Syntax/TokenSyntax.h"
27+
#include "swift/Syntax/UnknownSyntax.h"
28+
#include "swift/Syntax/SyntaxNodes.h"
29+
30+
namespace swift {
31+
namespace syntax {
32+
struct SyntaxVisitor {
33+
virtual ~SyntaxVisitor() {}
34+
35+
% for node in SYNTAX_NODES:
36+
% if is_visitable(node):
37+
virtual void visit(${node.name} node);
38+
% end
39+
% end
40+
41+
virtual void visit(TokenSyntax token) {}
42+
43+
void visit(Syntax node);
44+
45+
void visitChildren(Syntax node) {
46+
for (auto C: node.getRaw()->Layout)
47+
visit(make<Syntax>(C));
48+
}
49+
};
50+
}
51+
}
52+
53+
#endif // SWIFT_SYNTAX_VISITOR_H

lib/Syntax/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_swift_library(swiftSyntax STATIC
66
SyntaxBuilders.cpp.gyb
77
SyntaxKind.cpp.gyb
88
SyntaxFactory.cpp.gyb
9+
SyntaxVisitor.cpp.gyb
910
Trivia.cpp
1011
RawSyntax.cpp
1112
RawTokenSyntax.cpp

lib/Syntax/SyntaxVisitor.cpp.gyb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
%{
2+
# -*- mode: C++ -*-
3+
from gyb_syntax_support import *
4+
NODE_MAP = create_node_map()
5+
# Ignore the following admonition; it applies to the resulting .cpp file only
6+
}%
7+
//// Automatically Generated From SyntaxVisitor.cpp.gyb.
8+
//// Do Not Edit Directly!
9+
//===------------- SyntaxVisitor.cpp - Syntax Visitor definitions ---------===//
10+
//
11+
// This source file is part of the Swift.org open source project
12+
//
13+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
14+
// Licensed under Apache License v2.0 with Runtime Library Exception
15+
//
16+
// See https://swift.org/LICENSE.txt for license information
17+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
#include "swift/Syntax/SyntaxVisitor.h"
22+
23+
% for node in SYNTAX_NODES:
24+
% if is_visitable(node):
25+
void swift::syntax::SyntaxVisitor::visit(${node.name} node) {
26+
visitChildren(node);
27+
}
28+
% end
29+
% end
30+
31+
void swift::syntax::SyntaxVisitor::visit(Syntax node) {
32+
switch (node.getKind()) {
33+
case SyntaxKind::Token: visit(make<TokenSyntax>(node.getRaw())); return;
34+
% for node in SYNTAX_NODES:
35+
% if is_visitable(node):
36+
case SyntaxKind::${node.syntax_kind}: visit(make<${node.name}>(node.getRaw())); return;
37+
% end
38+
% end
39+
default: visitChildren(node); return;
40+
}
41+
}
42+
43+
void swift::syntax::Syntax::accept(SyntaxVisitor &visitor) {
44+
visitor.visit(*this);
45+
}

tools/SwiftSyntax/SyntaxRewriter.swift.gyb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from gyb_syntax_support import *
33
# -*- mode: Swift -*-
44
# Ignore the following admonition it applies to the resulting .swift file only
5-
def is_visitable(node):
6-
return not node.is_base() and not node.collection_element
75
}%
86
//// Automatically Generated From SyntaxFactory.swift.gyb.
97
//// Do Not Edit Directly!

utils/gyb_syntax_support/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,7 @@ def create_node_map():
9797
Creates a lookup table to find nodes by their kind.
9898
"""
9999
return {node.syntax_kind: node for node in SYNTAX_NODES}
100+
101+
102+
def is_visitable(node):
103+
return not node.is_base() and not node.collection_element

0 commit comments

Comments
 (0)