From 8c34e380f61873bd27e191ee77cd729db14316e7 Mon Sep 17 00:00:00 2001 From: Caleb Hester Date: Tue, 14 May 2019 09:20:45 -0700 Subject: [PATCH] Add preliminary Swift Syntax HighLighting --- Packages/Swift/LICENSE.md | 12 + Packages/Swift/Swift.sublime-syntax | 652 ++++++++++++++++++++++++++++ 2 files changed, 664 insertions(+) create mode 100644 Packages/Swift/LICENSE.md create mode 100644 Packages/Swift/Swift.sublime-syntax diff --git a/Packages/Swift/LICENSE.md b/Packages/Swift/LICENSE.md new file mode 100644 index 0000000..ab027f4 --- /dev/null +++ b/Packages/Swift/LICENSE.md @@ -0,0 +1,12 @@ +## LICENSE + +The MIT License (MIT) + +Copyright (c) 2016 Gregory Berns-Leone + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Packages/Swift/Swift.sublime-syntax b/Packages/Swift/Swift.sublime-syntax new file mode 100644 index 0000000..f32454d --- /dev/null +++ b/Packages/Swift/Swift.sublime-syntax @@ -0,0 +1,652 @@ +%YAML 1.2 +--- +name: Swift +file_extensions: + - swift +scope: source.swift + +## Order largely from babel-sublime + +variables: + typeIdent: '[A-Z_][a-zA-Z0-9_]*' + ident: '[a-zA-Z_][a-zA-Z0-9_]*' + +contexts: + main: + - include: literal-for + - include: literal-switch + - include: expression + - include: literal-punctuation + + prototype: + - include: comments + + expression: + - include: generic + - include: support-class + + - include: literal-function + - include: literal-class + - include: literal-struct + - include: literal-protocol + - include: literal-enum + + - include: literal-keywords + + - include: literal-number + - include: literal-string + - include: literal-language-constant + - include: literal-language-variable + + - include: literal-method-call + - include: literal-function-call + - include: property-access + + - include: brackets + + - include: literal-operators + - include: literal-assignment + - include: literal-variable + + comments: + - match: //+ + scope: punctuation.definition.comment + push: line-comment + - match: /\* + scope: punctuation.definition.comment.begin + push: block-comment + + line-comment: + - meta_scope: comment.line + - match: ' *(MARK:)(.*)' + captures: + 1: punctuation.definition.comment + 2: meta.toc-list + pop: true + - match: .* + pop: true + + block-comment: + - meta_scope: comment.block + - match: \*/ + scope: punctuation.definition.comment.end + pop: true + + brackets: + - include: round-brackets + - include: square-brackets + - include: curly-brackets + + round-brackets: + - match: \( + scope: punctuation.section.parens.begin.swift + push: + - meta_scope: meta.parens.swift + - match: \) + scope: punctuation.section.parens.end.swift + pop: true + - include: expression + + square-brackets: + - match: \[ + scope: punctuation.section.brackets.begin.swift + push: + - meta_scope: meta.brackets.swift + - match: \] + scope: punctuation.section.brackets.end.swift + pop: true + - include: expression + + curly-brackets: + - match: '{' + scope: punctuation.section.braces.begin.swift + push: + - meta_scope: meta.braces.swift + - match: '}' + scope: punctuation.section.braces.end.swift + pop: true + - include: expression + + ##### Literals ##### + + literal-number: + - match: \b0b[01][01_]*\b + scope: constant.numeric.binary.swift + - match: \b0o[0-7][0-7_]*\b + scope: constant.numeric.octal.swift + - match: \b0x[a-fA-F0-9][a-fA-F0-9_]*\b + scope: constant.numeric.hex.swift + - match: '(?:\b[0-9][0-9_]*)?\.[0-9_]+\b' + scope: constant.numeric.float.swift + - match: '\b[0-9][0-9_]*\b' + scope: constant.numeric.integer.swift + + literal-language-constant: + - match: \btrue\b + scope: constant.language.boolean.true.swift + - match: \bfalse\b + scope: constant.language.boolean.false.swift + - match: \bnil\b + scope: constant.language.nil.swift + + literal-string: + - match: '"' + scope: punctuation.definition.string.begin.swift + push: string-double + + string-double: + - meta_include_prototype: false + - meta_scope: string.quoted.double.swift + - match: \\\( + scope: punctuation.section.embedded.begin.swift + set: embedded + - match: '"' + scope: punctuation.definition.string.end.swift + pop: true + - include: string-content + + embedded: + - match: \) + scope: punctuation.section.embedded.end.swift + set: string-double + - include: main + + string-content: + - match: \\n + scope: constant.character.escape.newline.swift + - match: \\[0tr"'\\] + scope: constant.character.escape.swift + + literal-language-variable: + - match: \bsuper\b + scope: variable.language.super.swift + - match: \bself\b + scope: variable.language.self.swift + + literal-operators: + - match: '[-+*/%]|&\*|&/|&%|&\+|&-' + scope: keyword.operator.arithmetic.swift + - match: ==|!=|===|!==|<=|>=|<|> + scope: keyword.operator.logical.comparison.swift + - match: '\|\||&&' + scope: keyword.operator.logical.swift + - match: '[!~](?=(?:[a-zA-Z]|{{ident}}))' + scope: keyword.operator.logical.swift + - match: '<<|>>|&|/|^' + scope: keyword.operator.bitwise.swift + - match: '~=' + scope: keyword.operator.match.swift + - match: '\?\?' + scope: keyword.operator.nilcoalescing.swift + - match: '\s+(\?)\s+(?=.*:)' + captures: + 1: keyword.control.conditional.ternary.swift + push: + - match: ':' + scope: keyword.control.conditional.ternary.swift + pop: true + - include: expression + + ##### KEYWORDS ##### + + literal-for: + - match: \bfor\b + scope: keyword.control.loop.swift + + literal-switch: + - match: (?if|else)\b + scope: keyword.control.conditional.swift + - match: (?continue|break|fallthrough)\b + scope: keyword.control.transfer.swift + - match: (?while|repeat)\b + scope: keyword.control.loop.swift + - match: \b(?>do|catch|throw|rethrow)\b + scope: keyword.control.exception.swift + - match: \b(try)(\?|!)?(?=\s) + captures: + 1: keyword.control.exception.swift + 2: keyword.operator.optional.swift + - match: \bimport\b + scope: keyword.control.import + - match: \b(?:as(?:(\?|!)?)|is)(?=\s) + scope: keyword.operator.cast.swift + - match: \bwhere\b + scope: keyword.control.where.swift + - match: \bin\b + scope: keyword.control.in.swift + - match: \bguard\b + scope: keyword.control.guard.swift + - include: declaration-attributes + - include: declaration-modifiers + - include: type-attributes + - include: access-attributes + - include: function-attributes + + declaration-attributes: + - match: (@)(?>available|discardableResult|GKInspectable|objc|nonobjc|NSApplicationMain|NSCopying|NSManaged|testable|UIApplicationMain|IBAction|IBOutlet|IBDesignable|IBInspectable) + scope: keyword.other.swift + captures: + 1: punctuation.definition.keyword.swift + + declaration-modifiers: + - match: (?>dynamic|final|lazy|optional|required|weak) + scope: storage.modifier.swift + + type-attributes: + - match: (?>autoclosure|convention|escaping) + scope: keyword.other.swift + + access-attributes: + - match: (?>open|public|internal|fileprivate|private) + scope: storage.modifier.swift + + function-attributes: + - match: override|convenience + scope: keyword.other.swift + + ##### CLASS DECLARATIONS ##### + + literal-class: + # e.g. class MyClass : OtherClass { } + - match: (?:|>|{)) + pop: true + + class_inherits: + - match: ':' + scope: punctuation.separator.swift + set: + - include: generic + - match: (?={) + pop: true + - match: ({{typeIdent}}) + scope: entity.other.inherited-class.swift + - match: "," + scope: punctuation.separator.swift + - match: (?={) + pop: true + + class_body: + - match: '{' + scope: punctuation.definition.class.body.begin.swift + set: + - match: '}' + scope: punctuation.definition.class.body.end.swift + pop: true + - include: main + + ##### GENERIC DECLARATION ##### + ## This could use work + generic: + - match: '(<)\s*(?={{typeIdent}}\s*(?:(?:[:,<]\s*{{typeIdent}})*\s*>))' + captures: + 1: punctuation.definition.generic.begin.swift + push: + - include: generic + - meta_scope: meta.generic.swift + - match: ',|:' + scope: punctuation.separator.swift + - match: ({{typeIdent}}) + scope: variable.other.generic.swift + - match: '>' + scope: punctuation.definition.generic.end.swift + pop: true + + ##### BUILTIN TYPES ##### + + ## Many more exist, these are just (by my estimation) the common ones. See: http://stackoverflow.com/questions/25754791/full-list-of-fundamental-swift-types + support-class: + - match: \b(?>Character|String|Int|Int8|Int16|Int32|Int64|UInt|UInt8|UInt16|UInt32|UInt64|Float|Float80|Double|Bool|AnyObject|Array|CollectionType|Comparable|Dictionary|Equatable|Hashable|MutableCollectionType|MutableSliceable|Printable|Range|RawByte|RawRepresentable|SequenceType|Sliceable|Slice|Streamable|Unmanaged|UnsafeMutablePointer|UnsafePointer) + scope: support.class.swift + + ##### PROTOCOL DECLARATIONS ##### + + literal-protocol: + # e.g. protocol MyProtocol { } + - match: (?:|>|{)) + pop: true + + protocol_inherits: + - match: ':' + scope: punctuation.separator.swift + set: + - match: (?={) + pop: true + - match: class + scope: keyword.other.swift + - match: ({{typeIdent}}) + scope: entity.other.inherited-class.swift + - match: "," + scope: punctuation.separator.swift + - match: (?={) + pop: true + + protocol_body: + - match: '{' + scope: punctuation.definition.protocol.body.begin.swift + set: + - match: '}' + scope: punctuation.definition.protocol.body.end.swift + pop: true + - include: protocol-properties + - include: protocol-method-definition + - include: protocol-method-storage + - include: brackets + + literal-struct: + # e.g. struct MyStruct { } + - match: (?:|>|{)) + pop: true + + struct_inherits: + - match: ':' + scope: punctuation.separator.swift + set: + - match: (?={) + pop: true + - match: ({{typeIdent}}) + scope: entity.other.inherited-class.swift + - match: "," + scope: punctuation.separator.swift + - match: (?={) + pop: true + + struct_body: + - match: '{' + scope: punctuation.definition.struct.body.begin.swift + set: + - match: '}' + scope: punctuation.definition.struct.body.end.swift + pop: true + - include: struct-properties + - include: struct-method-definition + - include: struct-method-storage + - include: brackets + + literal-enum: + # e.g. enum MyEnum { } + - match: (?:|>|{)) + pop: true + + enum_inherits: + - match: ':' + scope: punctuation.separator.swift + set: + - match: (?={) + pop: true + - match: ({{typeIdent}}) + scope: entity.other.inherited-class.swift + - match: "," + scope: punctuation.separator.swift + - match: (?={) + pop: true + + enum_body: + - match: '{' + scope: punctuation.definition.enum.body.begin.swift + set: + - match: '}' + scope: punctuation.definition.enum.body.end.swift + pop: true + - include: enum-cases + + ##### FUNCTION DECLARATION ##### + + literal-function: + - match: (?) + set: return-type + - match: \( + scope: punctuation.section.parens.begin.swift + set: parameter-list + - match: \) + scope: punctuation.section.parens.end.swift + - match: \{ + scope: punctuation.section.braces.begin.swift + push: function-body + - match: (?<=}) + pop: true + + func-name: + - match: ({{ident}}) + scope: entity.name.function.swift + pop: true + + parameter-list: + - meta_content_scope: meta.function.parameters.swift + - include: parameter + - match: (?=\)) + set: func-def + - match: (?=\{) + set: func-def + + parameter: + - match: _ + scope: entity.name.function.swift + - include: type + - match: '({{ident}})\s*(\:)' + captures: + 1: variable.parameter.swift + 2: punctuation.separator.swift + - match: '({{ident}}|_)\b' + scope: entity.name.function.swift + - match: ',' + scope: punctuation.separator.swift + + return-type: + - meta_content_scope: meta.function.return-type + - match: -> + scope: keyword.operator.return.swift + - include: type + - match: (?={) + set: func-def + + function-body: + - match: \} + scope: punctuation.section.braces.end.swift + pop: true + - include: main + + type: + - match: ({{typeIdent}})([?!]?) + captures: + 1: entity.name.type.swift + 2: keyword.operator.optional.swift + - match: (\[)({{typeIdent}})([?!]?)(\])([?!]?) + captures: + 1: punctuation.definition.array.begin.swift + 2: entity.name.type.swift + 3: keyword.operator.optional.swift + 4: punctuation.definition.array.end.swift + 5: keyword.operator.optional.swift + - match: (\[)({{typeIdent}})([?!]?)(\:)(\])([?!]?) + captures: + 1: punctuation.definition.array.begin.swift + 2: entity.name.type.swift + 3: keyword.operator.optional.swift + 4: punctuation.definition.array.end.swift + 5: keyword.operator.optional.swift\ + - include: generic + + property-access: + - match: (\.)({{ident}})([?!]?) + captures: + 1: punctuation.accessor.swift + 2: variable.other.member.swift + 3: keyword.operator.optional.swift + + literal-variable: + - match: \b({{typeIdent}})([?!]?) + captures: + 1: entity.name.class.swift + 2: keyword.operator.optional.swift + - match: \b({{ident}})([?!]?) + captures: + 1: variable.other.swift + 2: keyword.operator.optional.swift + - match: '=' + scope: keyword.operator.assignment.swift + + literal-function-call: + - match: ({{typeIdent}})(\() + scope: meta.function-call.swift + captures: + 1: entity.name.class.swift + 2: punctuation.section.parens.begin.swift + push: call-parameters + - match: (\.)?({{ident}})(\() + scope: meta.function-call.swift + captures: + 1: punctuation.accessor.swift + 2: variable.function.swift + 3: punctuation.section.parens.begin.swift + push: call-parameters + - match: \) + scope: punctuation.section.parens.end.swift meta.function-call.swift + + call-parameters: + - match: '({{ident}})\s*\:' + scope: variable.function.swift + - match: (?=\)) + pop: true + - include: expression + + literal-assignment: + - match: \blet\b + scope: storage.type.swift + push: assign-static + - match: \bvar\b + scope: storage.type.swift + push: assign-dynamic + + assign-static: + - include: type + - match: \b{{ident}}\b + scope: variable.other.constant.swift + - match: ':' + scope: punctuation.separator.swift + - match: '=' + scope: keyword.operator.assignment.swift + pop: true + - match: '$' + pop: true + - match: (?={) + pop: true + + assign-dynamic: + - include: type + - match: \b{{ident}}\b + scope: variable.other.readwrite.swift + - match: ':' + scope: punctuation.separator.swift + - match: '=' + scope: keyword.operator.assignment.swift + pop: true + - match: '$' + pop: true + - match: (?={) + pop: true