mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
libSyntax nodes don't maintain absolute source location on each individual node. Instead, the absolute locations are calculated on demand with a given root by accumulating the length of all the other nodes before the target node. This bridging is important for issuing diagnostics from libSyntax entities. With the observation that our current implementation of the source location calculation has multiple bugs, this patch re-implemented this bridging by using the newly-added syntax visitor. Also, we moved the function from RawSyntax to Syntax for better visibility. To test this source location calculation, we added a new action in swift-syntax-test. This action parses a given file as a SourceFileSyntax, calculates the absolute location of the EOF token in the SourceFileSyntax, and dump the buffer from the start of the input file to the absolute location of the EOF. Finally, we compare the dump with the original input to ensure they are identical.
49 lines
1.5 KiB
C++
49 lines
1.5 KiB
C++
%{
|
|
# -*- mode: C++ -*-
|
|
from gyb_syntax_support import *
|
|
NODE_MAP = create_node_map()
|
|
# Ignore the following admonition; it applies to the resulting .cpp file only
|
|
}%
|
|
//// Automatically Generated From SyntaxVisitor.cpp.gyb.
|
|
//// Do Not Edit Directly!
|
|
//===------------- SyntaxVisitor.cpp - Syntax Visitor definitions ---------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "swift/Syntax/SyntaxVisitor.h"
|
|
#include "swift/Basic/Defer.h"
|
|
|
|
% for node in SYNTAX_NODES:
|
|
% if is_visitable(node):
|
|
void swift::syntax::SyntaxVisitor::visit(${node.name} node) {
|
|
visitChildren(node);
|
|
}
|
|
% end
|
|
% end
|
|
|
|
void swift::syntax::SyntaxVisitor::visit(Syntax node) {
|
|
visitPre(node);
|
|
SWIFT_DEFER { visitPost(node); };
|
|
switch (node.getKind()) {
|
|
case SyntaxKind::Token: visit(make<TokenSyntax>(node.getRaw())); return;
|
|
% for node in SYNTAX_NODES:
|
|
% if is_visitable(node):
|
|
case SyntaxKind::${node.syntax_kind}: visit(make<${node.name}>(node.getRaw())); return;
|
|
% end
|
|
% end
|
|
default: visitChildren(node); return;
|
|
}
|
|
}
|
|
|
|
void swift::syntax::Syntax::accept(SyntaxVisitor &visitor) {
|
|
visitor.visit(*this);
|
|
}
|