Skip to content

Commit

Permalink
Swift 5.8 and 5.9 updates (#1948)
Browse files Browse the repository at this point in the history
* Swift 5.8 updates

Add new keywords and tidy up the overall list.
Support property wrapper projection syntax

* Swift regex literals

* Tidy Swift keywords lists

* Swift 5.9 updates
  • Loading branch information
johnfairh committed Jul 4, 2023
1 parent 339ed08 commit 9d7ce06
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 19 deletions.
48 changes: 32 additions & 16 deletions lib/rouge/lexers/swift.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,29 @@ class Swift < RegexLexer
id = /#{id_head}#{id_rest}*/

keywords = Set.new %w(
await break case continue default do else fallthrough if in for return switch where while try catch throw guard defer repeat
autoreleasepool await break case catch consume continue default defer discard do each else fallthrough guard if in for repeat return switch throw try where while

as dynamicType is new super self Self Type __COLUMN__ __FILE__ __FUNCTION__ __LINE__
as dynamicType is new super self Self Type

associativity async didSet get infix inout isolated mutating none nonmutating operator override postfix precedence prefix set unowned weak willSet throws rethrows precedencegroup

#available #colorLiteral #column #else #elseif #endif #error #file #fileLiteral #function #if #imageLiteral #line #selector #sourceLocation #warning
associativity async didSet get infix inout isolated left mutating none nonmutating operator override postfix precedence precedencegroup prefix rethrows right set throws unowned weak willSet
)

declarations = Set.new %w(
actor class deinit enum convenience extension final func import init internal lazy let nonisolated optional private protocol public required static struct subscript typealias var dynamic indirect associatedtype open fileprivate some
actor any associatedtype borrowing class consuming deinit distributed dynamic enum convenience extension fileprivate final func import indirect init internal lazy let macro nonisolated open optional package private protocol public required some static struct subscript typealias var
)

constants = Set.new %w(
true false nil
)

start { push :bol }
start do
push :bol
@re_delim = "" # multi-line regex delimiter
end

# beginning of line
state :bol do
rule %r/#.*/, Comment::Preproc
rule %r/#(?![#"\/]).*/, Comment::Preproc

mixin :inline_whitespace

Expand Down Expand Up @@ -69,8 +70,11 @@ class Swift < RegexLexer
mixin :whitespace

rule %r/\$(([1-9]\d*)?\d)/, Name::Variable
rule %r/\$#{id}/, Name
rule %r/~Copyable\b/, Keyword::Type

rule %r{[()\[\]{}:;,?\\]}, Punctuation
rule %r{(#*)/(?!\s).*(?<![\s\\])/\1}, Str::Regex
rule %r([-/=+*%<>!&|^.~]+), Operator
rule %r/@?"/, Str, :dq
rule %r/'(\\.|.)'/, Str::Char
Expand All @@ -82,6 +86,7 @@ class Swift < RegexLexer
rule %r{[\d]+(?:_\d+)*}, Num::Integer

rule %r/@#{id}/, Keyword::Declaration
rule %r/##{id}/, Keyword

rule %r/(private|internal)(\([ ]*)(\w+)([ ]*\))/ do |m|
if m[3] == 'set'
Expand All @@ -99,14 +104,6 @@ class Swift < RegexLexer
end
end

rule %r/#available\([^)]+\)/, Keyword::Declaration

rule %r/(#(?:selector|keyPath)\()([^)]+?(?:[(].*?[)])?)(\))/ do
groups Keyword::Declaration, Name::Function, Keyword::Declaration
end

rule %r/#(line|file|column|function|dsohandle)/, Keyword::Declaration

rule %r/(let|var)\b(\s*)(#{id})/ do
groups Keyword, Text, Name::Variable
end
Expand Down Expand Up @@ -148,6 +145,12 @@ class Swift < RegexLexer
rule %r/(`)(#{id})(`)/ do
groups Punctuation, Name::Variable, Punctuation
end

rule %r{(#+)/\n} do |m|
@re_delim = m[1]
token Str::Regex
push :re_multi
end
end

state :tuple do
Expand Down Expand Up @@ -181,6 +184,19 @@ class Swift < RegexLexer
rule %r/[)]/, Punctuation, :pop!
mixin :root
end

state :re_multi do
rule %r{^\s*/#+} do |m|
token Str::Regex
if m[0].end_with?("/#{@re_delim}")
@re_delim = ""
pop!
end
end

rule %r/#.*/, Comment::Single
rule %r/./m, Str::Regex
end
end
end
end
64 changes: 61 additions & 3 deletions spec/visual/samples/swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ repeat {
if #available(iOS 8.0, OSX 10.10, *) {
// Use Handoff APIs when available.
let activity = NSUserActivity(activityType:"com.example.ShoppingList.view")
} else {
} else if #unavailable(linux) {
// Fall back when Handoff APIs not available.
} else {
}

//MARK: Classes
Expand Down Expand Up @@ -399,7 +400,7 @@ typealias StringDictionary<T> = Dictionary<String, T>
@available(*, unavailable, renamed: "MyRenamedProtocol")
@discardableResult func f() -> T {}

#sourceLocation(file: "foo", line: 42)
_ = #sourceLocation(file: "foo", line: 42)

@available(swift, obsoleted: 5.0.0, renamed: "foo2(file:line:)")
func foo(_ file: StaticString = #file, line: UInt = #line) { }
Expand Down Expand Up @@ -452,12 +453,69 @@ func test(t: Types?) -> Bool {
}
}

actor AnActor {
distributed actor AnActor {
nonisolated func funcA() {}
func funcB(otherActor: isolated AnActor) async {
await otherActor.funcB()
async let v = funcA()
}
}

func existential(arg: any Proto1) -> some Proto2 {
autoreleasepool {
}
}

func pwrap() -> Bool {
@SmallNum var myNum: Int = 8
self.$myOwnNum = 8
return $myNum
}

let res = [
// comment not a regex
#//#, // empty regex
##//##, // another
##//#, // unbalanced #
#//##, // unbalanced #
/ not a regex /,
/a regex/,
/not a single-line
regex/,
/re with \/ escaped/,
##/usr/lib/#modules/vmlinuz/##, // unescaped / with ##
/(#*)/(?!\s)[^\n]*(?<![\s\\])/\1/,

#/
multiline regex
/#,

##/
multiline # comment
/#
/###
still going
/##
]

package struct FileDescriptor: ~Copyable {
private var fd: Int32

consuming func close() throws {
discard self
}
}

func makePairs<each First, each Second>(
firsts first: repeat each First,
seconds second: repeat each Second
) -> (repeat Pair<each First, each Second>) {
return (repeat Pair(each first, each second))
}

@freestanding(expression)
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "m", type: "t")

let p = #stringify(x + y)

foo() // end-of-file comment

0 comments on commit 9d7ce06

Please sign in to comment.