1+ /// Result returned from `CodeParser.parse` containing the AST, token stream and
2+ /// any parsing errors.
13public 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.
1325public 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 ( )
0 commit comments