-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for annotating until we reach a certain end
- Loading branch information
1 parent
2adf8dc
commit 004eea4
Showing
12 changed files
with
346 additions
and
60 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
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
105 changes: 105 additions & 0 deletions
105
Sources/Syntax/Implementations/Structure/AnnotatedUntil.swift
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,105 @@ | ||
|
||
import Foundation | ||
import SyntaxTree | ||
|
||
public struct AnnotatedUntil<Content : Parser, End: Parser>: Parser { | ||
private enum AnnotationValue { | ||
case content(Content.Output) | ||
case end(End.Output) | ||
} | ||
|
||
let id = UUID() | ||
|
||
let content: InternalParser | ||
let end: InternalParser | ||
|
||
public init(@ParserBuilder content: () -> Content, @ParserBuilder end: () -> End) { | ||
self.content = content().map(AnnotationValue.content) | ||
self.end = end().map(AnnotationValue.end) | ||
} | ||
|
||
public var body: AnyParser<(AnnotatedString<Content.Output>, End.Output)> { | ||
return neverBody() | ||
} | ||
} | ||
|
||
extension AnnotatedUntil: InternalParser { | ||
|
||
func prefixes() -> Set<String> { | ||
return [] | ||
} | ||
|
||
func parse(using scanner: Scanner) throws { | ||
scanner.enterNode() | ||
scanner.beginScanning(in: scanner.range, clipToLast: true, for: AnnotationValue.self) | ||
|
||
while (true) { | ||
let nextContentRange = try scanner.range(for: content) | ||
let nextEndRange = try scanner.range(for: end) | ||
|
||
switch (nextContentRange, nextEndRange) { | ||
|
||
case (.none, .none), (.none, .some): | ||
break | ||
|
||
case (.some(let nextContentRange), .some(let nextEndRange)): | ||
if nextContentRange.lowerBound >= nextEndRange.lowerBound { | ||
break | ||
} else { | ||
fallthrough | ||
} | ||
|
||
default: | ||
scanner.begin() | ||
try content.parse(using: scanner) | ||
try scanner.commit() | ||
continue | ||
} | ||
|
||
break | ||
} | ||
|
||
scanner.begin() | ||
try end.parse(using: scanner) | ||
try scanner.commit() | ||
|
||
try scanner.commit() | ||
|
||
let annotatedString = try scanner.pop(of: AnnotatedString<AnnotationValue>.self) | ||
let last = annotatedString.annotations.last! | ||
|
||
let text = annotatedString.text[..<last.range.lowerBound] | ||
|
||
guard case .end(let end) = last.value else { fatalError() } | ||
|
||
let annotations = annotatedString.annotations.dropLast().map { annotation -> AnnotatedString<Content.Output>.Annotation in | ||
guard case .content(let content) = annotation.value else { fatalError() } | ||
return AnnotatedString<Content.Output>.Annotation(range: annotation.range, value: content) | ||
} | ||
|
||
let newString = AnnotatedString(text: text, annotations: annotations) | ||
scanner.store(value: (newString, end)) | ||
scanner.exitNode() | ||
} | ||
|
||
} | ||
|
||
extension Scanner { | ||
|
||
fileprivate func range(for parser: InternalParser) throws -> Range<Location>? { | ||
begin() | ||
enterNode() | ||
do { | ||
try parser.parse(using: self) | ||
exitNode() | ||
} catch { | ||
try rollback() | ||
return nil | ||
} | ||
|
||
let range = locationOfNode() | ||
try rollback() | ||
return range | ||
} | ||
|
||
} |
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
Oops, something went wrong.