Files
swift-mirror/lib/Parse/ParseRegex.cpp
Arnold Schwaighofer 9511994e52 Revert "Merge pull request #40595 from hamishknight/straw-bales"
This reverts commit a67a0436f7, reversing
changes made to 9965df76d0.

This commit or the earlier commit this commit is based on (#40531) broke the
incremental bot.
2021-12-18 11:02:37 -08:00

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));
}