@@ -341,10 +341,6 @@ function token_last_byte(stream::ParseStream, i)
341341 stream. tokens[i]. next_byte - 1
342342end
343343
344- function token_span (stream:: ParseStream , i)
345- stream. tokens[i]. next_byte - stream. tokens[i- 1 ]. next_byte
346- end
347-
348344function lookahead_token_first_byte (stream, i)
349345 i == 1 ? _next_byte (stream) : stream. lookahead[i- 1 ]. next_byte
350346end
@@ -961,24 +957,25 @@ end
961957# API for extracting results from ParseStream
962958
963959"""
964- build_tree(:: Type{NodeType }, stream::ParseStream;
960+ build_tree(make_node::Function, :: Type{StackEntry }, stream::ParseStream;
965961 wrap_toplevel_as_kind=nothing, kws...)
966962
967- Construct a tree with `NodeType` nodes from a ParseStream using depth-first
968- traversal. `NodeType` must have the constructors
963+ Construct a tree from a ParseStream using depth-first traversal. `make_node`
964+ must have the signature
965+
966+ make_node(head::SyntaxHead, span::Integer, children)
969967
970- NodeType(head::SyntaxHead, span::Integer)
971- NodeType(head::SyntaxHead, span::Integer, children::Vector{NodeType})
968+ where `children` is either `nothing` for leaf nodes or an iterable of the
969+ children of type `StackEntry` for internal nodes. `StackEntry` may be a node
970+ type, but also may include other information required during building the tree.
972971
973972A single node which covers the input is expected, but if the ParseStream has
974973multiple nodes at the top level, `wrap_toplevel_as_kind` may be used to wrap
975974them in a single node.
976975
977- The tree here is constructed depth-first, but it would also be possible to use
978- a bottom-up tree builder interface similar to rust-analyzer. (In that case we'd
979- traverse the list of ranges backward rather than forward.)
976+ The tree here is constructed depth-first in postorder.
980977"""
981- function build_tree (:: Type{NodeType} , stream:: ParseStream ;
978+ function build_tree (make_node :: Function , :: Type{NodeType} , stream:: ParseStream ;
982979 wrap_toplevel_as_kind= nothing , kws... ) where NodeType
983980 stack = Vector {NamedTuple{(:first_token,:node),Tuple{Int,NodeType}}} ()
984981
@@ -996,8 +993,15 @@ function build_tree(::Type{NodeType}, stream::ParseStream;
996993 i += 1
997994 continue # Ignore removed tokens
998995 end
999- node = NodeType (head (t), token_span (stream, i))
1000- push! (stack, (first_token= i, node= node))
996+ srcrange = (stream. tokens[i- 1 ]. next_byte:
997+ stream. tokens[i]. next_byte - 1 )
998+ h = head (t)
999+ children = (is_syntax_kind (h) || is_keyword (h)) ?
1000+ (stack[n]. node for n= 1 : 0 ) : nothing
1001+ node = make_node (h, srcrange, children)
1002+ if ! isnothing (node)
1003+ push! (stack, (first_token= i, node= node))
1004+ end
10011005 i += 1
10021006 end
10031007 if j > lastindex (ranges)
@@ -1018,25 +1022,31 @@ function build_tree(::Type{NodeType}, stream::ParseStream;
10181022 while k > 1 && r. first_token <= stack[k- 1 ]. first_token
10191023 k -= 1
10201024 end
1025+ srcrange = (stream. tokens[r. first_token- 1 ]. next_byte:
1026+ stream. tokens[r. last_token]. next_byte - 1 )
10211027 children = (stack[n]. node for n = k: length (stack))
1022- node = NodeType (head (r), children)
1028+ node = make_node (head (r), srcrange , children)
10231029 resize! (stack, k- 1 )
1024- push! (stack, (first_token= r. first_token, node= node))
1030+ if ! isnothing (node)
1031+ push! (stack, (first_token= r. first_token, node= node))
1032+ end
10251033 j += 1
10261034 end
10271035 end
10281036 if length (stack) == 1
10291037 return only (stack). node
10301038 elseif ! isnothing (wrap_toplevel_as_kind)
10311039 # Mostly for debugging
1040+ srcrange = (stream. tokens[1 ]. next_byte:
1041+ stream. tokens[end ]. next_byte - 1 )
10321042 children = (x. node for x in stack)
1033- return NodeType (SyntaxHead (wrap_toplevel_as_kind, EMPTY_FLAGS), children)
1043+ return make_node (SyntaxHead (wrap_toplevel_as_kind, EMPTY_FLAGS),
1044+ srcrange, children)
10341045 else
10351046 error (" Found multiple nodes at top level" )
10361047 end
10371048end
10381049
1039-
10401050"""
10411051 sourcetext(stream::ParseStream; steal_textbuf=true)
10421052
0 commit comments