Skip to content

Commit

Permalink
Fixing node updates during scanning of annotated strings
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdsupremacist committed Feb 7, 2021
1 parent 004eea4 commit 64cd1a9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
13 changes: 1 addition & 12 deletions Sources/Syntax/Internals/ScannerState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -416,18 +416,6 @@ private final class ScanningStorage<T>: ScannerStateStorage, ScanningStorageProt
throw state.error(reason: .failedToMatch(expression))
}

// Upon a match start with in place storage
// let nextState = ScannerState(range: result.range.lowerBound..<state.range.upperBound, parent: state, storage: InPlaceStorage())
// nextState.range = result.range.upperBound..<state.range.upperBound
//
// let (firstNode, rest) = state.node.backToFirst()
//
// state.node = firstNode
// if let rest = rest {
// rest.update(from: state.range.lowerBound, to: result.range.lowerBound)
// nextState.node = rest
// }

rangeMatchStart = result.range.lowerBound
return ExpressionScanResult(match: result, state: state)
}
Expand Down Expand Up @@ -552,6 +540,7 @@ private class StackedScanningStateStorage<T>: ScannerStateStorage {

let result = try current.take(expression: expression, in: text, for: state)
isInPlace = true
state.node.update(from: state.range.lowerBound, to: result.match.range.lowerBound)
state.range = result.match.range.upperBound..<state.range.upperBound
return result
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/SyntaxTests/HTML/HTMLNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class HTMLNode {

public let tag: Substring
public let attributes: [Attribute]
public let contents: AnnotatedString<HTMLNode>
public let contents: AnnotatedString<HTMLNode>?

init(tag: Substring, attributes: [Attribute], contents: AnnotatedString<HTMLNode>) {
init(tag: Substring, attributes: [Attribute], contents: AnnotatedString<HTMLNode>?) {
self.tag = tag
self.attributes = attributes
self.contents = contents
Expand Down
25 changes: 23 additions & 2 deletions Tests/SyntaxTests/HTML/HTMLNodeParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,34 @@ struct HTMLNodeParser: Parser {

var body: AnyParser<HTMLNode> {
Recursive { parser in
InternalHTMLNodeParser(parser: parser)
Either {
HTMLNodeWithChildrenParser(parser: parser)
SingleHTMLNodeParser()
}
}
}

}

private struct SingleHTMLNodeParser: Parser {
var body: AnyParser<HTMLNode> {
return Group {
"<"

RegularExpression("[a-zA-Z][-a-zA-Z0-9]*")

Repeat {
AttributeParser()
}

"/>"
}
.map { HTMLNode(tag: $0.text, attributes: $1, contents: nil) }
}

}

private struct InternalHTMLNodeParser: Parser {
private struct HTMLNodeWithChildrenParser: Parser {
let parser: AnyParser<HTMLNode>

var body: AnyParser<HTMLNode> {
Expand Down
10 changes: 9 additions & 1 deletion Tests/SyntaxTests/HTMLSyntaxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ final class HTMLSyntaxTests: XCTestCase {

func testParagraph() {
let text = """
<p>Hello World: <a href="link">link</a></p>
<p align="center">
<img src="https://img.shields.io/badge/Swift-5.3-orange.svg" />
<a href="https://swift.org/package-manager">
<img src="https://img.shields.io/badge/swiftpm-compatible-brightgreen.svg?style=flat" alt="Swift Package Manager" />
</a>
<a href="https://twitter.com/nerdsupremacist">
<img src="https://img.shields.io/badge/[email protected]?style=flat" alt="Twitter: @nerdsupremacist" />
</a>
</p>
"""

let html = try! HTMLNodeParser().parse(text)
Expand Down

0 comments on commit 64cd1a9

Please sign in to comment.