splash/Sources/SplashTokenizer/TokenizerOutputFormat.swift
John Sundell 4c6f71c0de Support custom token types
This change makes it possible to implement custom token types, which is
really useful when building specialized grammars that use non-standard
token types.
2018-09-28 14:02:42 +02:00

37 lines
839 B
Swift

/**
* Splash
* Copyright (c) John Sundell 2018
* MIT license - see LICENSE.md
*/
import Foundation
import Splash
struct TokenizerOutputFormat: OutputFormat {
func makeBuilder() -> Builder {
return Builder()
}
}
extension TokenizerOutputFormat {
struct Builder: OutputBuilder {
private var components = [String]()
mutating func addToken(_ token: String, ofType type: TokenType) {
components.append("\(type.string.capitalized) token: \(token)")
}
mutating func addPlainText(_ text: String) {
components.append("Plain text: \(text)")
}
mutating func addWhitespace(_ whitespace: String) {
// Ignore whitespace
}
func build() -> String {
return components.joined(separator: "\n")
}
}
}