Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports back references #5

Closed
wants to merge 11 commits into from
Closed

Conversation

Lessica
Copy link

@Lessica Lessica commented Feb 20, 2021

Fix some caveats mentioned here: soffes#18

Copy link
Owner

@alehed alehed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the code looks good. Here are a few comments.

// MARK: - Initializers

init(reference: String, in repository: Repository? = nil, parent: Pattern?, manager: BundleManager) {
init(reference: String, in repository: Repository? = nil, parent: Pattern?, manager: BundleManager /* not used */) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

manager can be removed if it is not used.

I guess this is because the language references are resolved eagerly in Language.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its parent Pattern uses this parameter.

type = .resolved
}

func resolveExternalReference(from thisLanguage: Language, in languages: [String: Language], baseName: String?) {
func resolveExternalReference(from thisLanguage: Language /* not used */, in languages: [String: Language], baseName: String?) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unused argument can also be removed.

// SyntaxKit
//
// Created by Rachel on 2021/2/19.
// Copyright © 2021 Sam Soffes. All rights reserved.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copyright should be changed here.

}

// Expand a back-referenced regex string with original content and matches
func removingBackReferencePlaceholders(content: String, matches: NSTextCheckingResult) -> String {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use character literals here. Hexadecimal is not super readable. Also there might be some convenience function like isDigit or something like that.

XCTAssertFalse("title: Hello World\ncomments: 24\nposts: \"12\"zz\n".hasBackReferencePlaceholder)
XCTAssert("title: Hello World\ncomments: 24\nposts: \"12\\3\"zz\n".hasBackReferencePlaceholder)

XCTAssertEqual("title: Hello World\ncomments: \\24\nposts: \"12\\3\"zz\n".convertToICUBackReferencedRegex(), "title: Hello World\ncomments: $24\nposts: \"12$3\"zz\n")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you could just write the literals once and check that converting it back produces the original string.

return _expression?.firstMatch(in: string, options: NSRegularExpression.MatchingOptions(rawValue: options.rawValue), range: range)
}

func rangeOfFirstMatch(in string: String, options: RegularExpression.MatchingOptions = [], range: NSRange) -> NSRange {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be clearer if written as

// guard expression
return _expression.rangeOfFirstMatch(in: string, options: NSRegularExpression.MatchingOptions(rawValue: options.rawValue), range: range)

(since the rangeOfFirstMatch will never return nil). That way we know that the interface is not changed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general we want to keep the exact same interface as NSRegularExpression.

@@ -278,9 +281,9 @@ open class Parser {
if range.location == NSNotFound {
continue
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace needs removal

self._options = options
self._isTemplate = pattern.hasBackReferencePlaceholder
if !self._isTemplate {
self._expression = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options(rawValue: options.rawValue))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to add expandBackReferences to this class and not expose the expression at all. It would also be good to have the methods NSRegularExpression methods below fail if expression is nil.

/// - parameter begin: The match result of the beginning
///
/// - returns: The regular expression string with its back reference placeholders expanded.
private func expandBackReferences(from regex: RegularExpression, beginResults begin: ResultSet?) -> RegularExpression {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed as commented before.

@Lessica
Copy link
Author

Lessica commented Mar 10, 2021

Close this in order to open another

@Lessica Lessica closed this Mar 10, 2021
@Lessica Lessica deleted the feature-back-refs branch March 10, 2021 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants