Skip to content

Commit

Permalink
Added “Swift” Language
Browse files Browse the repository at this point in the history
  • Loading branch information
JRaspass committed Jan 17, 2020
1 parent 7638e21 commit 93d765b
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 7 deletions.
5 changes: 3 additions & 2 deletions assets/common.css
Expand Up @@ -350,7 +350,7 @@ thead th {
#matrix {
display: grid;
grid-gap: .25rem;
grid-template-columns: repeat(auto-fit, minmax(7rem, 1fr));
grid-template-columns: repeat(auto-fit, minmax(6rem, 1fr));
}

#matrix a {
Expand Down Expand Up @@ -378,7 +378,8 @@ thead th {
#matrix input.python:not(:checked) ~ .python,
#matrix input.raku:not(:checked) ~ .raku,
#matrix input.ruby:not(:checked) ~ .ruby,
#matrix input.rust:not(:checked) ~ .rust { display: none }
#matrix input.rust:not(:checked) ~ .rust,
#matrix input.swift:not(:checked) ~ .swift { display: none }

#matrix input:checked + label,
#matrix label:hover {
Expand Down
1 change: 1 addition & 0 deletions assets/hole.js
Expand Up @@ -16,6 +16,7 @@
/* include vendor/codemirror-raku.js */
/* include vendor/codemirror-ruby.js */
/* include vendor/codemirror-rust.js */
/* include vendor/codemirror-swift.js */
/* include vendor/codemirror-xml.js */

const chars = document.querySelector('#chars');
Expand Down
216 changes: 216 additions & 0 deletions assets/includes/vendor/codemirror-swift.js
@@ -0,0 +1,216 @@
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: https://codemirror.net/LICENSE

// Swift mode created by Michael Kaminsky https://github.com/mkaminsky11

(function(mod) {
mod(CodeMirror)
})(function(CodeMirror) {
"use strict"

function wordSet(words) {
var set = {}
for (var i = 0; i < words.length; i++) set[words[i]] = true
return set
}

var keywords = wordSet(["_","var","let","class","enum","extension","import","protocol","struct","func","typealias","associatedtype",
"open","public","internal","fileprivate","private","deinit","init","new","override","self","subscript","super",
"convenience","dynamic","final","indirect","lazy","required","static","unowned","unowned(safe)","unowned(unsafe)","weak","as","is",
"break","case","continue","default","else","fallthrough","for","guard","if","in","repeat","switch","where","while",
"defer","return","inout","mutating","nonmutating","catch","do","rethrows","throw","throws","try","didSet","get","set","willSet",
"assignment","associativity","infix","left","none","operator","postfix","precedence","precedencegroup","prefix","right",
"Any","AnyObject","Type","dynamicType","Self","Protocol","__COLUMN__","__FILE__","__FUNCTION__","__LINE__"])
var definingKeywords = wordSet(["var","let","class","enum","extension","import","protocol","struct","func","typealias","associatedtype","for"])
var atoms = wordSet(["true","false","nil","self","super","_"])
var types = wordSet(["Array","Bool","Character","Dictionary","Double","Float","Int","Int8","Int16","Int32","Int64","Never","Optional","Set","String",
"UInt8","UInt16","UInt32","UInt64","Void"])
var operators = "+-/*%=|&<>~^?!"
var punc = ":;,.(){}[]"
var binary = /^\-?0b[01][01_]*/
var octal = /^\-?0o[0-7][0-7_]*/
var hexadecimal = /^\-?0x[\dA-Fa-f][\dA-Fa-f_]*(?:(?:\.[\dA-Fa-f][\dA-Fa-f_]*)?[Pp]\-?\d[\d_]*)?/
var decimal = /^\-?\d[\d_]*(?:\.\d[\d_]*)?(?:[Ee]\-?\d[\d_]*)?/
var identifier = /^\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1/
var property = /^\.(?:\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1)/
var instruction = /^\#[A-Za-z]+/
var attribute = /^@(?:\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1)/
//var regexp = /^\/(?!\s)(?:\/\/)?(?:\\.|[^\/])+\//

function tokenBase(stream, state, prev) {
if (stream.sol()) state.indented = stream.indentation()
if (stream.eatSpace()) return null

var ch = stream.peek()
if (ch == "/") {
if (stream.match("//")) {
stream.skipToEnd()
return "comment"
}
if (stream.match("/*")) {
state.tokenize.push(tokenComment)
return tokenComment(stream, state)
}
}
if (stream.match(instruction)) return "builtin"
if (stream.match(attribute)) return "attribute"
if (stream.match(binary)) return "number"
if (stream.match(octal)) return "number"
if (stream.match(hexadecimal)) return "number"
if (stream.match(decimal)) return "number"
if (stream.match(property)) return "property"
if (operators.indexOf(ch) > -1) {
stream.next()
return "operator"
}
if (punc.indexOf(ch) > -1) {
stream.next()
stream.match("..")
return "punctuation"
}
var stringMatch
if (stringMatch = stream.match(/("""|"|')/)) {
var tokenize = tokenString.bind(null, stringMatch[0])
state.tokenize.push(tokenize)
return tokenize(stream, state)
}

if (stream.match(identifier)) {
var ident = stream.current()
if (types.hasOwnProperty(ident)) return "variable-2"
if (atoms.hasOwnProperty(ident)) return "atom"
if (keywords.hasOwnProperty(ident)) {
if (definingKeywords.hasOwnProperty(ident))
state.prev = "define"
return "keyword"
}
if (prev == "define") return "def"
return "variable"
}

stream.next()
return null
}

function tokenUntilClosingParen() {
var depth = 0
return function(stream, state, prev) {
var inner = tokenBase(stream, state, prev)
if (inner == "punctuation") {
if (stream.current() == "(") ++depth
else if (stream.current() == ")") {
if (depth == 0) {
stream.backUp(1)
state.tokenize.pop()
return state.tokenize[state.tokenize.length - 1](stream, state)
}
else --depth
}
}
return inner
}
}

function tokenString(openQuote, stream, state) {
var singleLine = openQuote.length == 1
var ch, escaped = false
while (ch = stream.peek()) {
if (escaped) {
stream.next()
if (ch == "(") {
state.tokenize.push(tokenUntilClosingParen())
return "string"
}
escaped = false
} else if (stream.match(openQuote)) {
state.tokenize.pop()
return "string"
} else {
stream.next()
escaped = ch == "\\"
}
}
if (singleLine) {
state.tokenize.pop()
}
return "string"
}

function tokenComment(stream, state) {
var ch
while (true) {
stream.match(/^[^/*]+/, true)
ch = stream.next()
if (!ch) break
if (ch === "/" && stream.eat("*")) {
state.tokenize.push(tokenComment)
} else if (ch === "*" && stream.eat("/")) {
state.tokenize.pop()
}
}
return "comment"
}

function Context(prev, align, indented) {
this.prev = prev
this.align = align
this.indented = indented
}

function pushContext(state, stream) {
var align = stream.match(/^\s*($|\/[\/\*])/, false) ? null : stream.column() + 1
state.context = new Context(state.context, align, state.indented)
}

function popContext(state) {
if (state.context) {
state.indented = state.context.indented
state.context = state.context.prev
}
}

CodeMirror.defineMode("swift", function(config) {
return {
startState: function() {
return {
prev: null,
context: null,
indented: 0,
tokenize: []
}
},

token: function(stream, state) {
var prev = state.prev
state.prev = null
var tokenize = state.tokenize[state.tokenize.length - 1] || tokenBase
var style = tokenize(stream, state, prev)
if (!style || style == "comment") state.prev = prev
else if (!state.prev) state.prev = style

if (style == "punctuation") {
var bracket = /[\(\[\{]|([\]\)\}])/.exec(stream.current())
if (bracket) (bracket[1] ? popContext : pushContext)(state, stream)
}

return style
},

indent: function(state, textAfter) {
var cx = state.context
if (!cx) return 0
var closing = /^[\]\}\)]/.test(textAfter)
if (cx.align != null) return cx.align - (closing ? 1 : 0)
return cx.indented + (closing ? 0 : config.indentUnit)
},

electricInput: /^\s*[\)\}\]]$/,

lineComment: "//",
blockCommentStart: "/*",
blockCommentEnd: "*/",
fold: "brace",
closeBrackets: "()[]{}''\"\"``"
}
})
});
6 changes: 4 additions & 2 deletions build-langs
Expand Up @@ -14,7 +14,7 @@ declare -A urls=(
["Haskell"]="//www.haskell.org/ghc/"
["J"]="http://jsoftware.com"
["JavaScript"]="//v8.dev"
["Julia"]="//julialang.org"
#["Julia"]="//julialang.org"
["Lisp"]="//clisp.sourceforge.io"
["Lua"]="//www.lua.org"
["Nim"]="//nim-lang.org"
Expand All @@ -24,6 +24,7 @@ declare -A urls=(
["Raku"]="//raku.org"
["Ruby"]="//www.ruby-lang.org"
["Rust"]="//www.rust-lang.org"
["Swift"]="//swift.org"
)

IFS=$'\n'
Expand Down Expand Up @@ -55,10 +56,11 @@ for name in "${sorted_names[@]}"; do
fi

# Remove some crap from version strings.
ver=${ver#JavaScript-C}
ver=${ver#Lua }
ver=${ver#Swift }
ver=${ver#The Glorious }
ver=${ver#This is }
ver=${ver#JavaScript-C}
ver=${ver#rustc }
ver=${ver/ation/er}
ver=${ver/built /}
Expand Down
2 changes: 1 addition & 1 deletion db/0.schema.sql
Expand Up @@ -14,7 +14,7 @@ CREATE TYPE hole AS ENUM (

CREATE TYPE lang AS ENUM (
'bash', 'brainfuck', 'c', 'haskell', 'j', 'javascript', 'julia', 'lisp',
'lua', 'nim', 'perl', 'php', 'python', 'raku', 'ruby', 'rust'
'lua', 'nim', 'perl', 'php', 'python', 'raku', 'ruby', 'rust', 'swift'
);

CREATE TYPE trophy AS ENUM (
Expand Down
1 change: 1 addition & 0 deletions langs/swift/.dockerignore
@@ -0,0 +1 @@
*
3 changes: 3 additions & 0 deletions langs/swift/Dockerfile
@@ -0,0 +1,3 @@
FROM swift:5.1.3 as builder

ENTRYPOINT [ "swift", "--version" ]
4 changes: 4 additions & 0 deletions latest-langs
Expand Up @@ -42,6 +42,10 @@ constant @langs = (
'Rust',
'https://www.rust-lang.org',
/ 'Version ' (<[\d.]>+) /,

'Swift',
'https://swift.org/download/',
/ 'Swift ' (<[\d.]>+) /,
);

constant $len = @langs[0, 3*.chars.max;
Expand Down
1 change: 1 addition & 0 deletions routes/types.go
Expand Up @@ -36,6 +36,7 @@ var langs = []Lang{
{"raku", "Raku"},
{"ruby", "Ruby"},
{"rust", "Rust"},
{"swift", "Swift"},
}

var trophies = []Trophy{
Expand Down
4 changes: 2 additions & 2 deletions routes/versions.go
Expand Up @@ -7,7 +7,6 @@ const versionTable = "" +
"<tr><th class=haskell>Haskell<td>Glasgow Haskell Compiler 8.4.3<td class=wide><a href=//www.haskell.org/ghc/>website</a>" +
"<tr><th class=j>J<td>8.07.07<td class=wide><a href=http://jsoftware.com>website</a>" +
"<tr><th class=javascript>JavaScript<td>V8 7.4.51<td class=wide><a href=//v8.dev>website</a>" +
"<tr><th class=julia>Julia<td>1.3.0<td class=wide><a href=//julialang.org>website</a>" +
"<tr><th class=lisp>Lisp<td>GNU CLISP 2.49.92<td class=wide><a href=//clisp.sourceforge.io>website</a>" +
"<tr><th class=lua>Lua<td>5.3.5<td class=wide><a href=//www.lua.org>website</a>" +
"<tr><th class=nim>Nim<td>1.0.4<td class=wide><a href=//nim-lang.org>website</a>" +
Expand All @@ -16,4 +15,5 @@ const versionTable = "" +
"<tr><th class=python>Python<td>3.8.1<td class=wide><a href=//www.python.org>website</a>" +
"<tr><th class=raku>Raku<td>Rakudo 2019.11 on MoarVM 2019.11 implementing Perl 6.d<td class=wide><a href=//raku.org>website</a>" +
"<tr><th class=ruby>Ruby<td>2.7.0<td class=wide><a href=//www.ruby-lang.org>website</a>" +
"<tr><th class=rust>Rust<td>1.40.0<td class=wide><a href=//www.rust-lang.org>website</a>"
"<tr><th class=rust>Rust<td>1.40.0<td class=wide><a href=//www.rust-lang.org>website</a>" +
"<tr><th class=swift>Swift<td>5.1.3<td class=wide><a href=//swift.org>website</a>"
1 change: 1 addition & 0 deletions views/swift.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 93d765b

Please sign in to comment.