Adds basic support for the Swift programming language

Text only lexer for now until Pygments catches up
This commit is contained in:
Andy Lindeman
2014-06-02 15:51:44 -04:00
parent 4cd35c1f33
commit 8f251e6756
45 changed files with 636 additions and 3 deletions

View File

@@ -2046,6 +2046,12 @@ SuperCollider:
extensions:
- .scd
Swift:
type: programming
lexer: Text only
extensions:
- .swift
SystemVerilog:
type: programming
color: "#343761"

View File

@@ -581,6 +581,9 @@
"SuperCollider": [
".scd"
],
"Swift": [
".swift"
],
"SystemVerilog": [
".sv",
".svh",
@@ -723,8 +726,8 @@
".gemrc"
]
},
"tokens_total": 611474,
"languages_total": 755,
"tokens_total": 612602,
"languages_total": 798,
"tokens": {
"ABAP": {
"*/**": 1,
@@ -61881,6 +61884,260 @@
"wait": 1,
".fork": 1
},
"Swift": {
"let": 43,
"apples": 1,
"oranges": 1,
"appleSummary": 1,
"fruitSummary": 1,
"var": 42,
"shoppingList": 3,
"[": 18,
"]": 18,
"occupations": 2,
"emptyArray": 1,
"String": 27,
"(": 89,
")": 89,
"emptyDictionary": 1,
"Dictionary": 1,
"<String,>": 1,
"Float": 1,
"//": 1,
"Went": 1,
"shopping": 1,
"and": 1,
"bought": 1,
"everything.": 1,
"individualScores": 2,
"teamScore": 4,
"for": 10,
"score": 2,
"in": 11,
"{": 77,
"if": 6,
"+": 15,
"}": 77,
"else": 1,
"optionalString": 2,
"nil": 1,
"optionalName": 2,
"greeting": 2,
"name": 21,
"vegetable": 2,
"switch": 4,
"case": 21,
"vegetableComment": 4,
"x": 1,
"where": 2,
"x.hasSuffix": 1,
"default": 2,
"interestingNumbers": 2,
"largest": 4,
"kind": 1,
"numbers": 6,
"number": 13,
"n": 5,
"while": 2,
"<": 4,
"*": 7,
"m": 5,
"do": 1,
"firstForLoop": 3,
"i": 6,
"secondForLoop": 3,
";": 2,
"println": 1,
"func": 24,
"greet": 2,
"day": 1,
"-": 21,
"return": 30,
"getGasPrices": 2,
"Double": 11,
"sumOf": 3,
"Int...": 1,
"Int": 19,
"sum": 3,
"returnFifteen": 2,
"y": 3,
"add": 2,
"makeIncrementer": 2,
"addOne": 2,
"increment": 2,
"hasAnyMatches": 2,
"list": 2,
"condition": 2,
"Bool": 4,
"item": 4,
"true": 2,
"false": 2,
"lessThanTen": 2,
"numbers.map": 2,
"result": 5,
"sort": 1,
"class": 7,
"Shape": 2,
"numberOfSides": 4,
"simpleDescription": 14,
"myVariable": 2,
"myConstant": 1,
"shape": 1,
"shape.numberOfSides": 1,
"shapeDescription": 1,
"shape.simpleDescription": 1,
"NamedShape": 3,
"init": 4,
"self.name": 1,
"Square": 7,
"sideLength": 17,
"self.sideLength": 2,
"super.init": 2,
"area": 1,
"override": 2,
"test": 1,
"test.area": 1,
"test.simpleDescription": 1,
"EquilateralTriangle": 4,
"perimeter": 1,
"get": 2,
"set": 1,
"newValue": 1,
"/": 1,
"triangle": 3,
"triangle.perimeter": 2,
"triangle.sideLength": 2,
"TriangleAndSquare": 2,
"willSet": 2,
"square.sideLength": 1,
"newValue.sideLength": 2,
"square": 2,
"size": 4,
"triangleAndSquare": 1,
"triangleAndSquare.square.sideLength": 1,
"triangleAndSquare.triangle.sideLength": 2,
"triangleAndSquare.square": 1,
"Counter": 2,
"count": 2,
"incrementBy": 1,
"amount": 2,
"numberOfTimes": 2,
"times": 4,
"counter": 1,
"counter.incrementBy": 1,
"optionalSquare": 2,
".sideLength": 1,
"enum": 4,
"Rank": 2,
"Ace": 1,
"Two": 1,
"Three": 1,
"Four": 1,
"Five": 1,
"Six": 1,
"Seven": 1,
"Eight": 1,
"Nine": 1,
"Ten": 1,
"Jack": 1,
"Queen": 1,
"King": 1,
"self": 3,
".Ace": 1,
".Jack": 1,
".Queen": 1,
".King": 1,
"self.toRaw": 1,
"ace": 1,
"Rank.Ace": 1,
"aceRawValue": 1,
"ace.toRaw": 1,
"convertedRank": 1,
"Rank.fromRaw": 1,
"threeDescription": 1,
"convertedRank.simpleDescription": 1,
"Suit": 2,
"Spades": 1,
"Hearts": 1,
"Diamonds": 1,
"Clubs": 1,
".Spades": 2,
".Hearts": 1,
".Diamonds": 1,
".Clubs": 1,
"hearts": 1,
"Suit.Hearts": 1,
"heartsDescription": 1,
"hearts.simpleDescription": 1,
"implicitInteger": 1,
"implicitDouble": 1,
"explicitDouble": 1,
"struct": 2,
"Card": 2,
"rank": 2,
"suit": 2,
"threeOfSpades": 1,
".Three": 1,
"threeOfSpadesDescription": 1,
"threeOfSpades.simpleDescription": 1,
"ServerResponse": 1,
"Result": 1,
"Error": 1,
"success": 2,
"ServerResponse.Result": 1,
"failure": 1,
"ServerResponse.Error": 1,
".Result": 1,
"sunrise": 1,
"sunset": 1,
"serverResponse": 2,
".Error": 1,
"error": 1,
"protocol": 1,
"ExampleProtocol": 5,
"mutating": 3,
"adjust": 4,
"SimpleClass": 2,
"anotherProperty": 1,
"a": 2,
"a.adjust": 1,
"aDescription": 1,
"a.simpleDescription": 1,
"SimpleStructure": 2,
"b": 1,
"b.adjust": 1,
"bDescription": 1,
"b.simpleDescription": 1,
"extension": 1,
"protocolValue": 1,
"protocolValue.simpleDescription": 1,
"repeat": 2,
"<ItemType>": 1,
"ItemType": 3,
"OptionalValue": 2,
"<T>": 1,
"None": 1,
"Some": 1,
"T": 5,
"possibleInteger": 2,
"<Int>": 1,
".None": 1,
".Some": 1,
"anyCommonElements": 2,
"<T,>": 1,
"U": 4,
"Sequence": 2,
"GeneratorType": 3,
"Element": 3,
"Equatable": 1,
"lhs": 2,
"rhs": 2,
"lhsItem": 2,
"rhsItem": 2,
"label": 2,
"width": 2,
"widthLabel": 1
},
"SystemVerilog": {
"module": 3,
"endpoint_phy_wrapper": 2,
@@ -66478,6 +66735,7 @@
"Stata": 3133,
"Stylus": 76,
"SuperCollider": 133,
"Swift": 1128,
"SystemVerilog": 541,
"Tcl": 1133,
"Tea": 3,
@@ -66663,6 +66921,7 @@
"Stata": 7,
"Stylus": 1,
"SuperCollider": 1,
"Swift": 43,
"SystemVerilog": 4,
"Tcl": 2,
"Tea": 1,
@@ -66686,5 +66945,5 @@
"YAML": 2,
"Zephir": 2
},
"md5": "a7d557f9fecf06444c92c22e6f32daa7"
"md5": "e41e5530c6efe39b710ace7e1d5d318c"
}

View File

@@ -0,0 +1,4 @@
let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit."

View File

@@ -0,0 +1,8 @@
var shoppingList = ["catfish", "water", "tulips", "blue paint"]
shoppingList[1] = "bottle of water"
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"

View File

@@ -0,0 +1,2 @@
let emptyArray = String[]()
let emptyDictionary = Dictionary<String, Float>()

View File

@@ -0,0 +1 @@
shoppingList = [] // Went shopping and bought everything.

View File

@@ -0,0 +1,10 @@
let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores {
if score > 50 {
teamScore += 3
} else {
teamScore += 1
}
}
teamScore

View File

@@ -0,0 +1,8 @@
var optionalString: String? = "Hello"
optionalString == nil
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \(name)"
}

View File

@@ -0,0 +1,11 @@
let vegetable = "red pepper"
switch vegetable {
case "celery":
let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
let vegetableComment = "That would make a good tea sandwich."
case let x where x.hasSuffix("pepper"):
let vegetableComment = "Is it a spicy \(x)?"
default:
let vegetableComment = "Everything tastes good in soup."
}

View File

@@ -0,0 +1,14 @@
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]
var largest = 0
for (kind, numbers) in interestingNumbers {
for number in numbers {
if number > largest {
largest = number
}
}
}
largest

View File

@@ -0,0 +1,11 @@
var n = 2
while n < 100 {
n = n * 2
}
n
var m = 2
do {
m = m * 2
} while m < 100
m

View File

@@ -0,0 +1,11 @@
var firstForLoop = 0
for i in 0..3 {
firstForLoop += i
}
firstForLoop
var secondForLoop = 0
for var i = 0; i < 3; ++i {
secondForLoop += 1
}
secondForLoop

View File

@@ -0,0 +1 @@
println("Hello, world")

View File

@@ -0,0 +1,4 @@
func greet(name: String, day: String) -> String {
return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")

View File

@@ -0,0 +1,4 @@
func getGasPrices() -> (Double, Double, Double) {
return (3.59, 3.69, 3.79)
}
getGasPrices()

View File

@@ -0,0 +1,9 @@
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sumOf()
sumOf(42, 597, 12)

View File

@@ -0,0 +1,9 @@
func returnFifteen() -> Int {
var y = 10
func add() {
y += 5
}
add()
return y
}
returnFifteen()

View File

@@ -0,0 +1,8 @@
func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)

View File

@@ -0,0 +1,13 @@
func hasAnyMatches(list: Int[], condition: Int -> Bool) -> Bool {
for item in list {
if condition(item) {
return true
}
}
return false
}
func lessThanTen(number: Int) -> Bool {
return number < 10
}
var numbers = [20, 19, 7, 12]
hasAnyMatches(numbers, lessThanTen)

View File

@@ -0,0 +1,5 @@
numbers.map({
(number: Int) -> Int in
let result = 3 * number
return result
})

View File

@@ -0,0 +1 @@
numbers.map({ number in 3 * number })

View File

@@ -0,0 +1 @@
sort([1, 5, 3, 12, 2]) { $0 > $1 }

View File

@@ -0,0 +1,6 @@
class Shape {
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}

View File

@@ -0,0 +1,3 @@
var myVariable = 42
myVariable = 50
let myConstant = 42

View File

@@ -0,0 +1,3 @@
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()

View File

@@ -0,0 +1,12 @@
class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}

View File

@@ -0,0 +1,20 @@
class Square: NamedShape {
var sideLength: Double
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
override func simpleDescription() -> String {
return "A square with sides of length \(sideLength)."
}
}
let test = Square(sideLength: 5.2, name: "my test square")
test.area()
test.simpleDescription()

View File

@@ -0,0 +1,26 @@
class EquilateralTriangle: NamedShape {
var sideLength: Double = 0.0
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 3
}
var perimeter: Double {
get {
return 3.0 * sideLength
}
set {
sideLength = newValue / 3.0
}
}
override func simpleDescription() -> String {
return "An equilateral triagle with sides of length \(sideLength)."
}
}
var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")
triangle.perimeter
triangle.perimeter = 9.9
triangle.sideLength

View File

@@ -0,0 +1,21 @@
class TriangleAndSquare {
var triangle: EquilateralTriangle {
willSet {
square.sideLength = newValue.sideLength
}
}
var square: Square {
willSet {
triangle.sideLength = newValue.sideLength
}
}
init(size: Double, name: String) {
square = Square(sideLength: size, name: name)
triangle = EquilateralTriangle(sideLength: size, name: name)
}
}
var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape")
triangleAndSquare.square.sideLength
triangleAndSquare.triangle.sideLength
triangleAndSquare.square = Square(sideLength: 50, name: "larger square")
triangleAndSquare.triangle.sideLength

View File

@@ -0,0 +1,8 @@
class Counter {
var count: Int = 0
func incrementBy(amount: Int, numberOfTimes times: Int) {
count += amount * times
}
}
var counter = Counter()
counter.incrementBy(2, numberOfTimes: 7)

View File

@@ -0,0 +1,2 @@
let optionalSquare: Square? = Square(sideLength: 2.5, name: "optional square")
let sideLength = optionalSquare?.sideLength

View File

@@ -0,0 +1,21 @@
enum Rank: Int {
case Ace = 1
case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
case Jack, Queen, King
func simpleDescription() -> String {
switch self {
case .Ace:
return "ace"
case .Jack:
return "jack"
case .Queen:
return "queen"
case .King:
return "king"
default:
return String(self.toRaw())
}
}
}
let ace = Rank.Ace
let aceRawValue = ace.toRaw()

View File

@@ -0,0 +1,3 @@
if let convertedRank = Rank.fromRaw(3) {
let threeDescription = convertedRank.simpleDescription()
}

View File

@@ -0,0 +1,17 @@
enum Suit {
case Spades, Hearts, Diamonds, Clubs
func simpleDescription() -> String {
switch self {
case .Spades:
return "spades"
case .Hearts:
return "hearts"
case .Diamonds:
return "diamonds"
case .Clubs:
return "clubs"
}
}
}
let hearts = Suit.Hearts
let heartsDescription = hearts.simpleDescription()

View File

@@ -0,0 +1,3 @@
let implicitInteger = 70
let implicitDouble = 70.0
let explicitDouble: Double = 70

View File

@@ -0,0 +1,9 @@
struct Card {
var rank: Rank
var suit: Suit
func simpleDescription() -> String {
return "The \(rank.simpleDescription()) of \(suit.simpleDescription())"
}
}
let threeOfSpades = Card(rank: .Three, suit: .Spades)
let threeOfSpadesDescription = threeOfSpades.simpleDescription()

View File

@@ -0,0 +1,14 @@
enum ServerResponse {
case Result(String, String)
case Error(String)
}
let success = ServerResponse.Result("6:00 am", "8:09 pm")
let failure = ServerResponse.Error("Out of cheese.")
switch success {
case let .Result(sunrise, sunset):
let serverResponse = "Sunrise is at \(sunrise) and sunset is at \(sunset)."
case let .Error(error):
let serverResponse = "Failure... \(error)"
}

View File

@@ -0,0 +1,4 @@
protocol ExampleProtocol {
var simpleDescription: String { get }
mutating func adjust()
}

View File

@@ -0,0 +1,20 @@
class SimpleClass: ExampleProtocol {
var simpleDescription: String = "A very simple class."
var anotherProperty: Int = 69105
func adjust() {
simpleDescription += " Now 100% adjusted."
}
}
var a = SimpleClass()
a.adjust()
let aDescription = a.simpleDescription
struct SimpleStructure: ExampleProtocol {
var simpleDescription: String = "A simple structure"
mutating func adjust() {
simpleDescription += " (adjusted)"
}
}
var b = SimpleStructure()
b.adjust()
let bDescription = b.simpleDescription

View File

@@ -0,0 +1,9 @@
extension Int: ExampleProtocol {
var simpleDescription: String {
return "The number \(self)"
}
mutating func adjust() {
self += 42
}
}
7.simpleDescription

View File

@@ -0,0 +1,3 @@
let protocolValue: ExampleProtocol = a
protocolValue.simpleDescription
// protocolValue.anotherProperty // Uncomment to see the error

View File

@@ -0,0 +1,8 @@
func repeat<ItemType>(item: ItemType, times: Int) -> ItemType[] {
var result = ItemType[]()
for i in 0..times {
result += item
}
return result
}
repeat("knock", 4)

View File

@@ -0,0 +1,7 @@
// Reimplement the Swift standard library's optional type
enum OptionalValue<T> {
case None
case Some(T)
}
var possibleInteger: OptionalValue<Int> = .None
possibleInteger = .Some(100)

View File

@@ -0,0 +1,11 @@
func anyCommonElements <T, U where T: Sequence, U: Sequence, T.GeneratorType.Element: Equatable, T.GeneratorType.Element == U.GeneratorType.Element> (lhs: T, rhs: U) -> Bool {
for lhsItem in lhs {
for rhsItem in rhs {
if lhsItem == rhsItem {
return true
}
}
}
return false
}
anyCommonElements([1, 2, 3], [3])

View File

@@ -0,0 +1,3 @@
let label = "The width is "
let width = 94
let widthLabel = label + String(width)