-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec2687f
commit 1687136
Showing
5 changed files
with
274 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,59 @@ | ||
import Foundation | ||
|
||
/// The model of the Lowlight language. | ||
public struct Language: Codable, Hashable, Sendable { | ||
public let keywords: Set<String> | ||
public let symbols: Set<String> | ||
public struct Pattern: Hashable, Sendable { | ||
public enum Behavior: Hashable, Sendable { | ||
case exact(String) | ||
case toEndOfLine(String) | ||
|
||
public func matches(_ character: Character, at offset: Int) -> Bool { | ||
switch self { | ||
case let .exact(string): | ||
let patternIdx = string.index(string.startIndex, offsetBy: offset) | ||
|
||
public init(keywords: Set<String> = [], symbols: Set<String> = []) { | ||
self.keywords = keywords | ||
self.symbols = symbols | ||
return string[patternIdx] == character | ||
case let .toEndOfLine(string): | ||
let patternIdx = string.index(string.startIndex, offsetBy: offset) | ||
|
||
return string[patternIdx] == character | ||
} | ||
} | ||
} | ||
|
||
var keywordPattern: String { | ||
"(" + keywords.joined(separator: "|") + ")" | ||
public enum Element: Hashable, Sendable { | ||
case keyword | ||
case comment | ||
|
||
public var treeSitterHighlightName: String { | ||
switch self { | ||
case .comment: | ||
"comment" | ||
case .keyword: | ||
"keyword" | ||
} | ||
} | ||
} | ||
|
||
public let element: Element | ||
public let match: Behavior | ||
|
||
public init(element: Element, match: Behavior) { | ||
self.element = element | ||
self.match = match | ||
} | ||
} | ||
|
||
/// The model of the Lowlight language. | ||
public struct Language: Hashable, Sendable { | ||
public let patterns: Set<Pattern> | ||
|
||
public init(patterns: Set<Pattern>) { | ||
self.patterns = patterns | ||
} | ||
|
||
public init(keywords: Set<String>, lineComment: String) { | ||
let keywordPatterns = keywords.map({ Pattern(element: .keyword, match: .exact($0)) }) | ||
let patterns = keywordPatterns + [.init(element: .comment, match: .toEndOfLine(lineComment))] | ||
|
||
var symbolsPattern: String { | ||
"(" + symbols.joined(separator: "|") + ")" | ||
self.init(patterns: Set(patterns)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import XCTest | ||
import Lowlight | ||
|
||
final class ProcessorTests: XCTestCase { | ||
func testLineComments() throws { | ||
let language = Language(patterns: [Pattern(element: .comment, match: .toEndOfLine("//"))]) | ||
let processor = Processor(language: language) | ||
|
||
let input = """ | ||
// leading comment | ||
// after whitespace | ||
abc def // trailing comment | ||
// comment inside // comment | ||
""" | ||
|
||
let tokens = processor.processTokens(for: input) | ||
let expected = [ | ||
Token(.comment, range: NSRange(0..<18)), | ||
Token(.comment, range: NSRange(22..<41)), | ||
Token(.comment, range: NSRange(50..<69)), | ||
Token(.comment, range: NSRange(70..<98)), | ||
] | ||
|
||
XCTAssertEqual(tokens, expected) | ||
} | ||
|
||
func testSingleExactMatch() throws { | ||
let language = Language(patterns: [Pattern(element: .keyword, match: .exact("abc"))]) | ||
let processor = Processor(language: language) | ||
|
||
let tokens = processor.processTokens(for: "abc ") | ||
|
||
let expected = [ | ||
Token(.keyword, range: NSRange(0..<3)), | ||
] | ||
|
||
XCTAssertEqual(tokens, expected) | ||
} | ||
|
||
func testSingleExactMatchEOF() throws { | ||
let language = Language(patterns: [Pattern(element: .keyword, match: .exact("abc"))]) | ||
let processor = Processor(language: language) | ||
|
||
let tokens = processor.processTokens(for: "abc") | ||
|
||
let expected = [ | ||
Token(.keyword, range: NSRange(0..<3)), | ||
] | ||
|
||
XCTAssertEqual(tokens, expected) | ||
} | ||
|
||
func testExactMatches() throws { | ||
let language = Language(patterns: [Pattern(element: .keyword, match: .exact("abc"))]) | ||
let processor = Processor(language: language) | ||
|
||
let input = """ | ||
abc | ||
dabc abc | ||
abc | ||
abcabc | ||
""" | ||
|
||
let tokens = processor.processTokens(for: input) | ||
let expected = [ | ||
Token(.keyword, range: NSRange(0..<3)), | ||
Token(.keyword, range: NSRange(9..<12)), | ||
Token(.keyword, range: NSRange(14..<17)), | ||
] | ||
|
||
XCTAssertEqual(tokens, expected) | ||
} | ||
|
||
func testEndOfLineFollowedByNewlineThenExact() throws { | ||
let language = Language(keywords: ["abc"], lineComment: "--") | ||
let processor = Processor(language: language) | ||
|
||
let input = """ | ||
-- comment | ||
abc def | ||
""" | ||
|
||
let tokens = processor.processTokens(for: input) | ||
let expected = [ | ||
Token(.comment, range: NSRange(0..<10)), | ||
Token(.keyword, range: NSRange(12..<15)), | ||
] | ||
|
||
XCTAssertEqual(tokens, expected) | ||
} | ||
} |