From 9d7ce06140b79b59123f022ec33c4344e667ded3 Mon Sep 17 00:00:00 2001 From: John Fairhurst Date: Tue, 4 Jul 2023 08:57:41 +0100 Subject: [PATCH] Swift 5.8 and 5.9 updates (#1948) * 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 --- lib/rouge/lexers/swift.rb | 48 +++++++++++++++++++---------- spec/visual/samples/swift | 64 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 93 insertions(+), 19 deletions(-) diff --git a/lib/rouge/lexers/swift.rb b/lib/rouge/lexers/swift.rb index f4ad79f7b5..96b85c1cd2 100644 --- a/lib/rouge/lexers/swift.rb +++ b/lib/rouge/lexers/swift.rb @@ -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 @@ -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).*(?!&|^.~]+), Operator rule %r/@?"/, Str, :dq rule %r/'(\\.|.)'/, Str::Char @@ -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' @@ -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 @@ -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 @@ -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 diff --git a/spec/visual/samples/swift b/spec/visual/samples/swift index aaa507b0ab..e4e0ef56fa 100644 --- a/spec/visual/samples/swift +++ b/spec/visual/samples/swift @@ -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 @@ -399,7 +400,7 @@ typealias StringDictionary = Dictionary @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) { } @@ -452,7 +453,7 @@ func test(t: Types?) -> Bool { } } -actor AnActor { +distributed actor AnActor { nonisolated func funcA() {} func funcB(otherActor: isolated AnActor) async { await otherActor.funcB() @@ -460,4 +461,61 @@ actor AnActor { } } +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]*(?( + firsts first: repeat each First, + seconds second: repeat each Second +) -> (repeat Pair) { + return (repeat Pair(each first, each second)) +} + +@freestanding(expression) +public macro stringify(_ value: T) -> (T, String) = #externalMacro(module: "m", type: "t") + +let p = #stringify(x + y) + foo() // end-of-file comment