mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Instead of creating syntax nodes directly, modify the parser to invoke an abstract interface 'SyntaxParseActions' while it is parsing the source code. This decouples the act of parsing from the act of forming a syntax tree representation. 'SyntaxTreeCreator' is an implementation of SyntaxParseActions that handles the logic of creating a syntax tree. To enforce the layering separation of parsing and syntax tree creation, a static library swiftSyntaxParse is introduced to compose the two. This decoupling is important for introducing a syntax parser library for SwiftSyntax to directly access parsing.
65 lines
2.0 KiB
C++
65 lines
2.0 KiB
C++
//===--- SyntaxTreeCreator.h - Syntax Tree Creation ------------*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2019 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_SYNTAX_PARSE_SYNTAXTREECREATOR_H
|
|
#define SWIFT_SYNTAX_PARSE_SYNTAXTREECREATOR_H
|
|
|
|
#include "swift/Parse/SyntaxParseActions.h"
|
|
#include "swift/Syntax/References.h"
|
|
|
|
namespace swift {
|
|
class RawSyntaxTokenCache;
|
|
class SyntaxParsingCache;
|
|
class SourceFile;
|
|
|
|
namespace syntax {
|
|
class SyntaxArena;
|
|
}
|
|
|
|
class SyntaxTreeCreator: public SyntaxParseActions {
|
|
RC<syntax::SyntaxArena> Arena;
|
|
|
|
/// A cache of nodes that can be reused when creating the current syntax
|
|
/// tree.
|
|
SyntaxParsingCache *SyntaxCache;
|
|
|
|
/// Tokens nodes that have already been created and may be reused in other
|
|
/// parts of the syntax tree.
|
|
std::unique_ptr<RawSyntaxTokenCache> TokenCache;
|
|
|
|
public:
|
|
SyntaxTreeCreator(SyntaxParsingCache *syntaxCache,
|
|
RC<syntax::SyntaxArena> arena);
|
|
~SyntaxTreeCreator();
|
|
|
|
void acceptSyntaxRoot(OpaqueSyntaxNode root, SourceFile &SF);
|
|
|
|
private:
|
|
OpaqueSyntaxNode recordToken(const Token &tok,
|
|
const syntax::Trivia &leadingTrivia,
|
|
const syntax::Trivia &trailingTrivia,
|
|
CharSourceRange range) override;
|
|
|
|
OpaqueSyntaxNode recordMissingToken(tok tokenKind, SourceLoc loc) override;
|
|
|
|
OpaqueSyntaxNode recordRawSyntax(syntax::SyntaxKind kind,
|
|
ArrayRef<OpaqueSyntaxNode> elements,
|
|
CharSourceRange range) override;
|
|
|
|
std::pair<size_t, OpaqueSyntaxNode>
|
|
lookupNode(size_t lexerOffset, syntax::SyntaxKind kind) override;
|
|
};
|
|
|
|
} // end namespace swift
|
|
|
|
#endif
|