@@ -101,6 +101,8 @@ public struct Parser {
101101 /// When this nesting level is exceeded, the parser should stop parsing.
102102 let maximumNestingLevel : Int
103103
104+ let parseTransition : IncrementalParseTransition ?
105+
104106 /// A default maximum nesting level that is used if the client didn't
105107 /// explicitly specify one. Debug builds of the parser comume a lot more stack
106108 /// space and thus have a lower default maximum nesting level.
@@ -111,7 +113,7 @@ public struct Parser {
111113 #endif
112114
113115 /// Initializes a ``Parser`` from the given string.
114- public init ( _ input: String , maximumNestingLevel: Int ? = nil ) {
116+ public init ( _ input: String , maximumNestingLevel: Int ? = nil , parseTransition : IncrementalParseTransition ? = nil ) {
115117 self . maximumNestingLevel = maximumNestingLevel ?? Self . defaultMaximumNestingLevel
116118
117119 self . arena = ParsingSyntaxArena (
@@ -124,6 +126,7 @@ public struct Parser {
124126 return arena. internSourceBuffer ( buffer)
125127 }
126128
129+ self . parseTransition = parseTransition
127130 self . lexemes = Lexer . tokenize ( interned)
128131 self . currentToken = self . lexemes. advance ( )
129132 }
@@ -142,7 +145,7 @@ public struct Parser {
142145 /// arena is created automatically, and `input` copied into the
143146 /// arena. If non-`nil`, `input` must be within its registered
144147 /// source buffer or allocator.
145- public init ( _ input: UnsafeBufferPointer < UInt8 > , maximumNestingLevel: Int ? = nil , arena: ParsingSyntaxArena ? = nil ) {
148+ public init ( _ input: UnsafeBufferPointer < UInt8 > , maximumNestingLevel: Int ? = nil , arena: ParsingSyntaxArena ? = nil , parseTransition : IncrementalParseTransition ? = nil ) {
146149 self . maximumNestingLevel = maximumNestingLevel ?? Self . defaultMaximumNestingLevel
147150
148151 var sourceBuffer : UnsafeBufferPointer < UInt8 >
@@ -157,6 +160,7 @@ public struct Parser {
157160 sourceBuffer = self . arena. internSourceBuffer ( input)
158161 }
159162
163+ self . parseTransition = parseTransition
160164 self . lexemes = Lexer . tokenize ( sourceBuffer)
161165 self . currentToken = self . lexemes. advance ( )
162166 }
@@ -621,3 +625,19 @@ extension Parser {
621625 )
622626 }
623627}
628+
629+ // MARK: Incremental Parsing
630+ extension Parser {
631+ mutating func loadCurrentSyntaxNodeFromCache( for kind: SyntaxKind ) -> Syntax ? {
632+ guard let parseTransition = self . parseTransition else { return nil }
633+
634+ var lookUpHelper = IncrementalParseLookup ( transition: parseTransition)
635+ let currentOffset = self . lexemes. getOffsetToStart ( self . currentToken)
636+ if let node = lookUpHelper. lookUp ( currentOffset, kind: kind) {
637+ self . lexemes. advance ( by: node. byteSize, currentToken: & self . currentToken)
638+ return node
639+ }
640+
641+ return nil
642+ }
643+ }
0 commit comments