mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
- Frontend: Implicitly import `_StringProcessing` when frontend flag `-enable-experimental-string-processing` is set. - Type checker: Set a regex literal expression's type as `_StringProcessing.Regex<(Substring, DynamicCaptures)>`. `(Substring, DynamicCaptures)` is a temporary `Match` type that will help get us to an end-to-end working system. This will be replaced by actual type inference based a regex's pattern in a follow-up patch (soon). - SILGen: Lower a regex literal expression to a call to `_StringProcessing.Regex.init(_regexString:)`. - String processing runtime: Add `Regex`, `DynamicCaptures` (matching actual APIs in apple/swift-experimental-string-processing), and `Regex(_regexString:)`. Upcoming: - Build `_MatchingEngine` and `_StringProcessing` modules with sources from apple/swift-experimental-string-processing. - Replace `DynamicCaptures` with inferred capture types.
60 lines
2.1 KiB
C++
60 lines
2.1 KiB
C++
//===--- ParseRegex.cpp - Regular expression literal parsing --------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2021 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Regular expression literal parsing
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "swift/Parse/Parser.h"
|
|
#include "swift/AST/DiagnosticsParse.h"
|
|
#include "swift/Parse/ParsedSyntaxRecorder.h"
|
|
#include "swift/Parse/SyntaxParsingContext.h"
|
|
#include "swift/Syntax/SyntaxKind.h"
|
|
|
|
// Regex parser delivered via libSwift
|
|
#include "swift/Parse/ExperimentalRegexBridging.h"
|
|
static ParseRegexStrawperson parseRegexStrawperson = nullptr;
|
|
void Parser_registerParseRegexStrawperson(ParseRegexStrawperson fn) {
|
|
parseRegexStrawperson = fn;
|
|
}
|
|
// Exposes the presence of the regex parsing function to the lexer.
|
|
bool Parser_hasParseRegexStrawperson() {
|
|
return parseRegexStrawperson != nullptr;
|
|
}
|
|
|
|
using namespace swift;
|
|
using namespace swift::syntax;
|
|
|
|
ParserResult<Expr> Parser::parseExprRegexLiteral() {
|
|
assert(Tok.is(tok::regex_literal));
|
|
assert(parseRegexStrawperson);
|
|
|
|
SyntaxParsingContext LocalContext(SyntaxContext,
|
|
SyntaxKind::RegexLiteralExpr);
|
|
// Strip off delimiters.
|
|
auto rawText = Tok.getText();
|
|
assert(rawText.front() == '\'' && rawText.back() == '\'');
|
|
auto regexText = rawText.slice(1, rawText.size() - 1);
|
|
|
|
// Let the Swift library parse the contents, returning an error, or null if
|
|
// successful.
|
|
// TODO: We need to be able to pass back a source location to emit the error
|
|
// at.
|
|
auto *errorStr = parseRegexStrawperson(regexText.str().c_str());
|
|
if (errorStr)
|
|
diagnose(Tok, diag::regex_literal_parsing_error, errorStr);
|
|
|
|
auto loc = consumeToken();
|
|
return makeParserResult(
|
|
RegexLiteralExpr::createParsed(Context, loc, regexText));
|
|
}
|