From 64cd1a98f06470a01aadcf5f77bc488e450796d9 Mon Sep 17 00:00:00 2001 From: Mathias Quintero Date: Sun, 7 Feb 2021 15:09:23 +0100 Subject: [PATCH] Fixing node updates during scanning of annotated strings --- Sources/Syntax/Internals/ScannerState.swift | 13 +---------- Tests/SyntaxTests/HTML/HTMLNode.swift | 4 ++-- Tests/SyntaxTests/HTML/HTMLNodeParser.swift | 25 +++++++++++++++++++-- Tests/SyntaxTests/HTMLSyntaxTests.swift | 10 ++++++++- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Sources/Syntax/Internals/ScannerState.swift b/Sources/Syntax/Internals/ScannerState.swift index c982c2d..8320897 100644 --- a/Sources/Syntax/Internals/ScannerState.swift +++ b/Sources/Syntax/Internals/ScannerState.swift @@ -416,18 +416,6 @@ private final class ScanningStorage: ScannerStateStorage, ScanningStorageProt throw state.error(reason: .failedToMatch(expression)) } - // Upon a match start with in place storage -// let nextState = ScannerState(range: result.range.lowerBound..: 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.. + public let contents: AnnotatedString? - init(tag: Substring, attributes: [Attribute], contents: AnnotatedString) { + init(tag: Substring, attributes: [Attribute], contents: AnnotatedString?) { self.tag = tag self.attributes = attributes self.contents = contents diff --git a/Tests/SyntaxTests/HTML/HTMLNodeParser.swift b/Tests/SyntaxTests/HTML/HTMLNodeParser.swift index 56c59a4..c183e07 100644 --- a/Tests/SyntaxTests/HTML/HTMLNodeParser.swift +++ b/Tests/SyntaxTests/HTML/HTMLNodeParser.swift @@ -10,13 +10,34 @@ struct HTMLNodeParser: Parser { var body: AnyParser { Recursive { parser in - InternalHTMLNodeParser(parser: parser) + Either { + HTMLNodeWithChildrenParser(parser: parser) + SingleHTMLNodeParser() + } + } + } + +} + +private struct SingleHTMLNodeParser: Parser { + var body: AnyParser { + 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 var body: AnyParser { diff --git a/Tests/SyntaxTests/HTMLSyntaxTests.swift b/Tests/SyntaxTests/HTMLSyntaxTests.swift index a207b04..7ee9fc1 100644 --- a/Tests/SyntaxTests/HTMLSyntaxTests.swift +++ b/Tests/SyntaxTests/HTMLSyntaxTests.swift @@ -5,7 +5,15 @@ final class HTMLSyntaxTests: XCTestCase { func testParagraph() { let text = """ -

Hello World: link

+

+ + + Swift Package Manager + + + Twitter: @nerdsupremacist + +

""" let html = try! HTMLNodeParser().parse(text)