Skip to content

Commit

Permalink
Remove some conditionals used for regex literals (#17)
Browse files Browse the repository at this point in the history
Relates to #16 / cc
@slevithan

The conditional negative lookaheads were conditional based on absence of
`#` in the opening delimiter. This can be simplified by including `/` in
the negative lookaheads and moving them before the opening delimiter.

(The comment rule seems like it is not actually necessary since
`#comment` is given a higher priority, but I kept it for good measure.)
  • Loading branch information
jtbandes authored Dec 30, 2024
1 parent 860efac commit d0ccac7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Swift.tmLanguage.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Swift.tmLanguage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1824,9 +1824,9 @@ repository:
- name: string.regexp.line.swift
match: |-
(?x)
(?!/\s) # non-extended regex literals may not start with a space or tab
(?!//) # disambiguation with line comments (redundant since comment rules occur earlier)
(((\#+)?)/) # (1) for captures, (2) for matching end, (3) for conditionals
(?(3)|(?!/)) # is not a comment
(?(3)|(?!\s)) # does not start with a space or tab
(\\\s)? # (4) may start with an escaped space or tab
(?<guts>
(?> # no backtracking, avoids issues with negative lookbehind at end
Expand Down
4 changes: 2 additions & 2 deletions Syntaxes/Swift.tmLanguage

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions grammar-test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ let r = /\Q/1/ //x
let r = /\ \Q/1/ //x
let r = #/\Q/1/# //x
let r = ##/\Q/1/# /## //x
let quote = /\Q/\E/ // FIXME? xcode doesn't handle this either?
let nested = /\Q^[xy])+$\E/ // FIXME: xcode does not parse this as a regex because of the `)` (??)
let nested = /[a\Q]\E]/
let nested = /ab[^a^~~b--c&&d\Qa\E\f] [\w--\d] foo [:a] foo [:a:] [-a-] [a-c-d]/
Expand Down Expand Up @@ -770,6 +771,10 @@ let r = ^^(/x/)


//// A regex literal may not start or end with a space or tab.
let initialSpace = / x/
let finalSpace = /x /
let initialSpace = #/ x/#
let finalSpace = #/x /#

// Unapplied '/' in a call to 'reduce':
let x = array.reduce(1, /) / 5
Expand Down

0 comments on commit d0ccac7

Please sign in to comment.