Skip to content

Commit de96d78

Browse files
authored
Add documentation comments and update guide (#57)
1 parent 30cd9c5 commit de96d78

File tree

7 files changed

+66
-1
lines changed

7 files changed

+66
-1
lines changed

MARKDOWN_PARSER.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,12 @@ class MarkdownPluginManager {
602602
- Add documentation comments for public APIs
603603
- Keep functions focused and single-purpose
604604
605+
### Documentation
606+
The codebase now contains detailed Swift documentation comments explaining the
607+
responsibilities of core types such as `CodeParser`, `CodeConstructor` and the
608+
inline parser. These comments can be viewed in Xcode Quick Help or rendered by
609+
documentation tools.
610+
605611
### Testing Requirements
606612
- All new features must include comprehensive tests
607613
- Maintain test coverage above 90%
@@ -653,4 +659,4 @@ This project is licensed under the MIT License - see the LICENSE file for detail
653659
654660
---
655661
656-
*Last updated: 2025-07-20*
662+
*Last updated: 2025-07-21*

Sources/SwiftParser/Core/CodeConstructor.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,27 @@
55
// Created by Dongyu Zhao on 7/21/25.
66
//
77

8+
/// Consumes a list of tokens to build an AST using registered node builders.
89
public class CodeConstructor<Node, Token> where Node: CodeNodeElement, Token: CodeTokenElement {
10+
/// Ordered collection of node builders that attempt to consume tokens.
911
private let builders: [any CodeNodeBuilder<Node, Token>]
12+
/// Factory that provides initial construction state for each parse run.
1013
private var state: () -> (any CodeConstructState<Node, Token>)?
1114

15+
/// Create a new constructor
16+
/// - Parameters:
17+
/// - builders: The node builders responsible for producing AST nodes.
18+
/// - state: Factory returning the initial parsing state object.
1219
public init(builders: [any CodeNodeBuilder<Node, Token>], state: @escaping () -> (any CodeConstructState<Node, Token>)?) {
1320
self.builders = builders
1421
self.state = state
1522
}
1623

24+
/// Build an AST from a token stream
25+
/// - Parameters:
26+
/// - tokens: Token list to consume.
27+
/// - root: Root node that will receive parsed children.
28+
/// - Returns: The populated root node and any construction errors.
1729
public func parse(_ tokens: [any CodeToken<Token>], root: CodeNode<Node>) -> (CodeNode<Node>, [CodeError]) {
1830
var context = CodeConstructContext(current: root, tokens: tokens, state: state())
1931

Sources/SwiftParser/Core/CodeError.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import Foundation
22

3+
/// Represents a parsing error encountered during tokenization or AST building.
34
public struct CodeError: Error {
5+
/// Human readable error message.
46
public let message: String
7+
/// Range in the original source where the error occurred, if available.
58
public let range: Range<String.Index>?
9+
10+
/// Create a new error instance.
11+
/// - Parameters:
12+
/// - message: Description of the problem.
13+
/// - range: Optional source range that triggered the error.
614
public init(_ message: String, range: Range<String.Index>? = nil) {
715
self.message = message
816
self.range = range

Sources/SwiftParser/Core/CodeParser.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1+
/// Result returned from `CodeParser.parse` containing the AST, token stream and
2+
/// any parsing errors.
13
public struct CodeParseResult<Node: CodeNodeElement, Token: CodeTokenElement> {
24
public let root: CodeNode<Node>
35
public let tokens: [any CodeToken<Token>]
46
public let errors: [CodeError]
57

8+
/// Create a result object
9+
/// - Parameters:
10+
/// - root: The constructed root node of the AST.
11+
/// - tokens: Token stream produced while parsing.
12+
/// - errors: Any errors that occurred during tokenization or AST
13+
/// construction.
614
public init(root: CodeNode<Node>, tokens: [any CodeToken<Token>], errors: [CodeError] = []) {
715
self.root = root
816
self.tokens = tokens
917
self.errors = errors
1018
}
1119
}
1220

21+
/// High level parser that orchestrates tokenization and AST construction.
22+
///
23+
/// `CodeParser` uses the provided `CodeLanguage` implementation to tokenize the
24+
/// source text and then build an AST using the registered node builders.
1325
public class CodeParser<Node: CodeNodeElement, Token: CodeTokenElement> where Node: CodeNodeElement, Token: CodeTokenElement {
1426
private let language: any CodeLanguage<Node, Token>
1527

@@ -22,6 +34,14 @@ public class CodeParser<Node: CodeNodeElement, Token: CodeTokenElement> where No
2234
self.constructor = CodeConstructor(builders: language.nodes, state: language.state)
2335
}
2436

37+
/// Parse a source string using the supplied language.
38+
///
39+
/// This method first tokenizes the input and, if tokenization succeeds,
40+
/// constructs the AST using the language's node builders.
41+
/// - Parameter source: The raw text to parse.
42+
/// - Parameter language: The language definition to use for parsing.
43+
/// - Returns: A `CodeParseResult` containing the root node, tokens and any
44+
/// errors encountered.
2545
public func parse(_ source: String, language: any CodeLanguage<Node, Token>) -> CodeParseResult<Node, Token> {
2646
let normalized = normalize(source)
2747
let root = language.root()

Sources/SwiftParser/Markdown/MarkdownLanguage.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import Foundation
22

33
// MARK: - Markdown Language Implementation
4+
/// Default Markdown language implementation following CommonMark with optional
5+
/// extensions.
6+
///
7+
/// The language exposes a set of token and node builders that together
8+
/// understand Markdown syntax. The initializer allows callers to supply a
9+
/// custom list of builders to enable or disable features.
410
public class MarkdownLanguage: CodeLanguage {
511
public typealias Node = MarkdownNodeElement
612
public typealias Token = MarkdownTokenElement
@@ -11,6 +17,11 @@ public class MarkdownLanguage: CodeLanguage {
1117

1218

1319
// MARK: - Initialization
20+
/// Create a Markdown language with the provided builders.
21+
///
22+
/// - Parameter consumers: Node builders to be used when constructing the
23+
/// document AST. Passing a custom set allows features to be enabled or
24+
/// disabled.
1425
public init(
1526
consumers: [any CodeNodeBuilder<MarkdownNodeElement, MarkdownTokenElement>] = [
1627
MarkdownReferenceDefinitionBuilder(),

Sources/SwiftParser/Markdown/MarkdownNodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class MarkdownNodeBase: CodeNode<MarkdownNodeElement> {
2424
}
2525

2626
// MARK: - Document Structure
27+
/// Root node representing an entire Markdown document.
2728
public class DocumentNode: MarkdownNodeBase {
2829
public var title: String?
2930
public var metadata: [String: Any] = [:]

Sources/SwiftParser/Markdown/Nodes/MarkdownInlineParser.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import Foundation
22

3+
/// Simple inline parser used by block builders to parse inline Markdown syntax.
4+
/// Handles emphasis, links, images, inline code and other span level elements.
35
struct MarkdownInlineParser {
6+
/// Parse inline content until one of the `stopAt` tokens is encountered.
7+
/// - Parameters:
8+
/// - context: Construction context providing tokens and current state.
9+
/// - stopAt: Tokens that terminate inline parsing.
10+
/// - Returns: Array of parsed inline nodes.
411
static func parseInline(
512
_ context: inout CodeConstructContext<MarkdownNodeElement, MarkdownTokenElement>,
613
stopAt: Set<MarkdownTokenElement> = [.newline, .eof]

0 commit comments

Comments
 (0)