Merge pull request #22 from JohnSundell/custom-grammars

Make it easier to build custom grammars
This commit is contained in:
John Sundell 2018-09-28 19:03:27 +02:00 committed by GitHub
commit 1dfa17227f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 5 deletions

View File

@ -9,8 +9,8 @@ import Foundation
/// Grammar for the Swift language. Use this implementation when
/// highlighting Swift code. This is the default grammar.
public struct SwiftGrammar: Grammar {
public let delimiters: CharacterSet
public let syntaxRules: [SyntaxRule]
public var delimiters: CharacterSet
public var syntaxRules: [SyntaxRule]
public init() {
var delimiters = CharacterSet.alphanumerics.inverted

View File

@ -33,7 +33,7 @@ public extension HTMLOutputFormat {
}
public mutating func addToken(_ token: String, ofType type: TokenType) {
html.append("<span class=\"\(classPrefix)\(type.rawValue)\">\(token.escaped)</span>")
html.append("<span class=\"\(classPrefix)\(type.string)\">\(token.escaped)</span>")
}
public mutating func addPlainText(_ text: String) {

View File

@ -7,7 +7,7 @@
import Foundation
/// Enum defining the possible types of tokens that can be highlighted
public enum TokenType: String, Equatable {
public enum TokenType: Hashable {
/// A keyword, such as `if`, `class` or `let`
case keyword
/// A token that is part of a string literal
@ -26,4 +26,17 @@ public enum TokenType: String, Equatable {
case dotAccess
/// A preprocessing symbol, such as `#if` or `@available`
case preprocessing
/// A custom token type, containing an arbitrary string
case custom(String)
}
public extension TokenType {
/// Return a string value representing the token type
var string: String {
if case .custom(let type) = self {
return type
}
return "\(self)"
}
}

View File

@ -18,7 +18,7 @@ extension TokenizerOutputFormat {
private var components = [String]()
mutating func addToken(_ token: String, ofType type: TokenType) {
components.append("\(type.rawValue.capitalized) token: \(token)")
components.append("\(type.string.capitalized) token: \(token)")
}
mutating func addPlainText(_ text: String) {

View File

@ -0,0 +1,32 @@
/**
* Splash
* Copyright (c) John Sundell 2018
* MIT license - see LICENSE.md
*/
import Foundation
import XCTest
import Splash
final class TokenTypeTests: SplashTestCase {
func testConvertingToString() {
let standardType = TokenType.comment
XCTAssertEqual(standardType.string, "comment")
let customType = TokenType.custom("MyCustomType")
XCTAssertEqual(customType.string, "MyCustomType")
}
func testAllTestsRunOnLinux() {
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
}
}
extension TokenTypeTests {
static var allTests: [(String, TestClosure<TokenTypeTests>)] {
return [
("testConvertingToString", testConvertingToString)
]
}
}