Skip to content

Commit 7e2721b

Browse files
committed
[SwiftSyntax] Add an API to enable or disable bare slash regex literals from SwiftSyntax
This adds C functions that can be called from SwiftSyntax to enable or disable bare slash regex parsing. It also adds a function to set the Swift language version - while this is not of top priority at the moment it will become important once we introduce a new language mode again. rdar://93750821
1 parent 34c2eb2 commit 7e2721b

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

include/swift-c/SyntaxParser/SwiftSyntaxParser.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ swiftparse_parser_create(void);
9393
SWIFTPARSE_PUBLIC void
9494
swiftparse_parser_dispose(swiftparse_parser_t);
9595

96+
/// Set the language version that should be used to parse the Swift source file.
97+
SWIFTPARSE_PUBLIC void
98+
swiftparse_parser_set_language_version(swiftparse_parser_t c_parser,
99+
const char *version);
100+
101+
/// Set whether bare slash regex literals are enabled.
102+
SWIFTPARSE_PUBLIC void swiftparse_parser_set_enable_bare_slash_regex_literal(
103+
swiftparse_parser_t c_parser, bool enabled);
104+
96105
/// Invoked by the parser when a syntax node is parsed. The client should
97106
/// return a pointer to associate with that particular node.
98107
typedef swiftparse_client_node_t

tools/libSwiftSyntaxParser/libSwiftSyntaxParser.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/Module.h"
2020
#include "swift/Basic/LangOptions.h"
2121
#include "swift/Basic/SourceManager.h"
22+
#include "swift/Basic/Version.h"
2223
#include "swift/Parse/Parser.h"
2324
#include "swift/Parse/SyntaxParseActions.h"
2425
#include "swift/Parse/SyntaxRegexFallbackLexing.h"
@@ -58,6 +59,13 @@ class SynParser {
5859
swiftparse_node_handler_t NodeHandler = nullptr;
5960
swiftparse_node_lookup_t NodeLookup = nullptr;
6061
swiftparse_diagnostic_handler_t DiagHandler = nullptr;
62+
/// The language version that should be used to parse the Swift source file.
63+
/// If \c None this is the default langauge version specified in LangOptions.h
64+
Optional<version::Version> EffectiveLanguageVersion;
65+
66+
/// Whether bare slash regex literals are enabled.
67+
/// If \c None this is the default specified in LangOptions.h
68+
Optional<bool> EnableBareSlashRegexLiteral;
6169

6270
public:
6371
swiftparse_node_handler_t getNodeHandler() const {
@@ -90,6 +98,17 @@ class SynParser {
9098
Block_release(prevBlk);
9199
}
92100

101+
void setLanguageVersion(const char *versionString) {
102+
if (auto version = version::Version::parseVersionString(
103+
versionString, SourceLoc(), /*Diags=*/nullptr)) {
104+
this->EffectiveLanguageVersion = version;
105+
}
106+
}
107+
108+
void setEnableBareSlashRegexLiteral(bool EnableBareSlashRegexLiteral) {
109+
this->EnableBareSlashRegexLiteral = EnableBareSlashRegexLiteral;
110+
}
111+
93112
~SynParser() {
94113
setNodeHandler(nullptr);
95114
setNodeLookup(nullptr);
@@ -493,7 +512,12 @@ swiftparse_client_node_t SynParser::parse(const char *source, size_t len) {
493512

494513
// Always enable bare /.../ regex literal in syntax parser.
495514
langOpts.EnableExperimentalStringProcessing = true;
496-
langOpts.EnableBareSlashRegexLiterals = true;
515+
if (EnableBareSlashRegexLiteral && *EnableBareSlashRegexLiteral) {
516+
langOpts.EnableBareSlashRegexLiterals = true;
517+
}
518+
if (EffectiveLanguageVersion) {
519+
langOpts.EffectiveLanguageVersion = *EffectiveLanguageVersion;
520+
}
497521

498522
auto parseActions =
499523
std::make_shared<CLibParseActions>(*this, SM, bufID);
@@ -517,6 +541,18 @@ swiftparse_parser_create(void) {
517541
return new SynParser();
518542
}
519543

544+
void swiftparse_parser_set_language_version(swiftparse_parser_t c_parser,
545+
const char *version) {
546+
SynParser *parser = static_cast<SynParser *>(c_parser);
547+
parser->setLanguageVersion(version);
548+
}
549+
550+
void swiftparse_parser_set_enable_bare_slash_regex_literal(
551+
swiftparse_parser_t c_parser, bool enabled) {
552+
SynParser *parser = static_cast<SynParser *>(c_parser);
553+
parser->setEnableBareSlashRegexLiteral(enabled);
554+
}
555+
520556
void
521557
swiftparse_parser_dispose(swiftparse_parser_t c_parser) {
522558
SynParser *parser = static_cast<SynParser*>(c_parser);

tools/libSwiftSyntaxParser/libSwiftSyntaxParser.exports

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
swiftparse_parse_string
22
swiftparse_parser_create
3+
swiftparse_parser_set_language_version
4+
swiftparse_parser_set_enable_bare_slash_regex_literal
35
swiftparse_parser_dispose
46
swiftparse_parser_set_node_handler
57
swiftparse_parser_set_node_lookup

0 commit comments

Comments
 (0)