@@ -115,7 +115,7 @@ public struct Parser {
115
115
public internal( set) var lookaheadRanges = LookaheadRanges ( )
116
116
117
117
/// Parser should own a ``LookaheadTracker`` so that we can share one `furthestOffset` in a parse.
118
- let lookaheadTrackerOwner = LookaheadTrackerOwner ( )
118
+ let lookaheadTrackerOwner : LookaheadTrackerOwner
119
119
120
120
/// A default maximum nesting level that is used if the client didn't
121
121
/// explicitly specify one. Debug builds of the parser consume a lot more stack
@@ -126,25 +126,46 @@ public struct Parser {
126
126
static let defaultMaximumNestingLevel = 256
127
127
#endif
128
128
129
- /// Initializes a ``Parser`` from the given string.
130
- public init (
131
- _ input: String ,
132
- maximumNestingLevel: Int ? = nil ,
133
- parseTransition: IncrementalParseTransition ? = nil
129
+ /// The delegated initializer for the parser.
130
+ ///
131
+ /// - Parameters
132
+ /// - input: An input buffer containing Swift source text. If a non-`nil`
133
+ /// arena is provided, the buffer must be present in it. Otherwise
134
+ /// the buffer is copied into a new arena and can thus be freed
135
+ /// after the initializer has been called.
136
+ /// - maximumNestingLevel: To avoid overflowing the stack, the parser will
137
+ /// stop if a nesting level greater than this value
138
+ /// is reached. The nesting level is increased
139
+ /// whenever a bracketed expression like `(` or `{`
140
+ /// is started. `defaultMaximumNestingLevel` is used
141
+ /// if this is `nil`.
142
+ /// - parseTransition: The previously recorded state for an incremental
143
+ /// parse, or `nil`.
144
+ /// - arena: Arena the parsing syntax are made into. If it's `nil`, a new
145
+ /// arena is created automatically, and `input` copied into the
146
+ /// arena. If non-`nil`, `input` must be within its registered
147
+ /// source buffer or allocator.
148
+ private init (
149
+ input: UnsafeBufferPointer < UInt8 > ,
150
+ maximumNestingLevel: Int ? ,
151
+ parseTransition: IncrementalParseTransition ? ,
152
+ arena: ParsingSyntaxArena ?
134
153
) {
135
- self . maximumNestingLevel = maximumNestingLevel ?? Self . defaultMaximumNestingLevel
136
-
137
- self . arena = ParsingSyntaxArena (
138
- parseTriviaFunction: TriviaParser . parseTrivia ( _: position: )
139
- )
140
-
141
154
var input = input
142
- input. makeContiguousUTF8 ( )
143
- let interned = input. withUTF8 { [ arena] buffer in
144
- return arena. internSourceBuffer ( buffer)
155
+ if let arena {
156
+ self . arena = arena
157
+ precondition ( arena. contains ( text: SyntaxText ( baseAddress: input. baseAddress, count: input. count) ) )
158
+ } else {
159
+ self . arena = ParsingSyntaxArena (
160
+ parseTriviaFunction: TriviaParser . parseTrivia ( _: position: )
161
+ )
162
+ input = self . arena. internSourceBuffer ( input)
145
163
}
146
164
147
- self . lexemes = Lexer . tokenize ( interned, lookaheadTracker: lookaheadTrackerOwner. lookaheadTracker)
165
+ self . maximumNestingLevel = maximumNestingLevel ?? Self . defaultMaximumNestingLevel
166
+ self . lookaheadTrackerOwner = LookaheadTrackerOwner ( )
167
+
168
+ self . lexemes = Lexer . tokenize ( input, lookaheadTracker: lookaheadTrackerOwner. lookaheadTracker)
148
169
self . currentToken = self . lexemes. advance ( )
149
170
if let parseTransition {
150
171
self . parseLookup = IncrementalParseLookup ( transition: parseTransition)
@@ -153,16 +174,39 @@ public struct Parser {
153
174
}
154
175
}
155
176
177
+ /// Initializes a ``Parser`` from the given string.
178
+ public init (
179
+ _ input: String ,
180
+ maximumNestingLevel: Int ? = nil ,
181
+ parseTransition: IncrementalParseTransition ? = nil
182
+ ) {
183
+ var input = input
184
+ input. makeContiguousUTF8 ( )
185
+ self = input. withUTF8 { buffer in
186
+ Parser (
187
+ input: buffer,
188
+ maximumNestingLevel: maximumNestingLevel,
189
+ parseTransition: parseTransition,
190
+ arena: nil
191
+ )
192
+ }
193
+ }
194
+
156
195
/// Initializes a ``Parser`` from the given input buffer.
157
196
///
158
197
/// - Parameters
159
- /// - input: An input buffer containing Swift source text.
198
+ /// - input: An input buffer containing Swift source text. If a non-`nil`
199
+ /// arena is provided, the buffer must be present in it. Otherwise
200
+ /// the buffer is copied into a new arena and can thus be freed
201
+ /// after the initializer has been called.
160
202
/// - maximumNestingLevel: To avoid overflowing the stack, the parser will
161
203
/// stop if a nesting level greater than this value
162
204
/// is reached. The nesting level is increased
163
205
/// whenever a bracketed expression like `(` or `{`
164
206
/// is started. `defaultMaximumNestingLevel` is used
165
207
/// if this is `nil`.
208
+ /// - parseTransition: The previously recorded state for an incremental
209
+ /// parse, or `nil`.
166
210
/// - arena: Arena the parsing syntax are made into. If it's `nil`, a new
167
211
/// arena is created automatically, and `input` copied into the
168
212
/// arena. If non-`nil`, `input` must be within its registered
@@ -173,27 +217,12 @@ public struct Parser {
173
217
parseTransition: IncrementalParseTransition ? = nil ,
174
218
arena: ParsingSyntaxArena ? = nil
175
219
) {
176
- self . maximumNestingLevel = maximumNestingLevel ?? Self . defaultMaximumNestingLevel
177
-
178
- var sourceBuffer : UnsafeBufferPointer < UInt8 >
179
- if let arena {
180
- self . arena = arena
181
- sourceBuffer = input
182
- precondition ( arena. contains ( text: SyntaxText ( baseAddress: input. baseAddress, count: input. count) ) )
183
- } else {
184
- self . arena = ParsingSyntaxArena (
185
- parseTriviaFunction: TriviaParser . parseTrivia ( _: position: )
186
- )
187
- sourceBuffer = self . arena. internSourceBuffer ( input)
188
- }
189
-
190
- self . lexemes = Lexer . tokenize ( sourceBuffer, lookaheadTracker: lookaheadTrackerOwner. lookaheadTracker)
191
- self . currentToken = self . lexemes. advance ( )
192
- if let parseTransition {
193
- self . parseLookup = IncrementalParseLookup ( transition: parseTransition)
194
- } else {
195
- self . parseLookup = nil
196
- }
220
+ self . init (
221
+ input: input,
222
+ maximumNestingLevel: maximumNestingLevel,
223
+ parseTransition: parseTransition,
224
+ arena: arena
225
+ )
197
226
}
198
227
199
228
mutating func missingToken( _ kind: RawTokenKind , text: SyntaxText ? = nil ) -> RawTokenSyntax {
0 commit comments