@@ -28,21 +28,25 @@ Base.display_error(io::IO, err::ParseError, bt) = Base.showerror(io, err, bt)
28
28
29
29
30
30
"""
31
- parse!(stream::ParseStream; rule=:toplevel )
31
+ parse!(stream::ParseStream; rule=:all )
32
32
33
33
Parse Julia source code from a [`ParseStream`](@ref) object. Output tree data
34
34
structures may be extracted from `stream` with the [`build_tree`](@ref) function.
35
35
36
36
`rule` may be any of
37
- * `:toplevel ` (default) — parse a whole "file" of top level statements. In this
37
+ * `:all ` (default) — parse a whole "file" of top level statements. In this
38
38
mode, the parser expects to fully consume the input.
39
39
* `:statement` — parse a single statement, or statements separated by semicolons.
40
40
* `:atom` — parse a single syntax "atom": a literal, identifier, or
41
41
parenthesized expression.
42
42
"""
43
- function parse! (stream:: ParseStream ; rule:: Symbol = :toplevel )
43
+ function parse! (stream:: ParseStream ; rule:: Symbol = :all )
44
+ if rule == :toplevel
45
+ Base. depwarn (" Use of rule == :toplevel in parse!() is deprecated. use `rule=:all` instead." , :parse! )
46
+ rule = :all
47
+ end
44
48
ps = ParseState (stream)
45
- if rule === :toplevel
49
+ if rule === :all
46
50
parse_toplevel (ps)
47
51
elseif rule === :statement
48
52
parse_stmts (ps)
@@ -56,14 +60,14 @@ function parse!(stream::ParseStream; rule::Symbol=:toplevel)
56
60
end
57
61
58
62
"""
59
- parse!(TreeType, io::IO; rule=:toplevel , version=VERSION)
63
+ parse!(TreeType, io::IO; rule=:all , version=VERSION)
60
64
61
65
Parse Julia source code from a seekable `IO` object. The output is a tuple
62
66
`(tree, diagnostics)`. When `parse!` returns, the stream `io` is positioned
63
67
directly after the last byte which was consumed during parsing.
64
68
"""
65
69
function parse! (:: Type{TreeType} , io:: IO ;
66
- rule:: Symbol = :toplevel , version= VERSION , kws... ) where {TreeType}
70
+ rule:: Symbol = :all , version= VERSION , kws... ) where {TreeType}
67
71
stream = ParseStream (io; version= version)
68
72
parse! (stream; rule= rule)
69
73
tree = build_tree (TreeType, stream; kws... )
@@ -75,7 +79,7 @@ function _parse(rule::Symbol, need_eof::Bool, ::Type{T}, text, index=1; version=
75
79
ignore_trivia= true , filename= nothing , first_line= 1 , ignore_errors= false ,
76
80
ignore_warnings= ignore_errors) where {T}
77
81
stream = ParseStream (text, index; version= version)
78
- if ignore_trivia && rule != :toplevel
82
+ if ignore_trivia && rule != :all
79
83
bump_trivia (stream, skip_newlines= true )
80
84
empty! (stream)
81
85
end
@@ -100,19 +104,22 @@ function _parse(rule::Symbol, need_eof::Bool, ::Type{T}, text, index=1; version=
100
104
end
101
105
102
106
_parse_docs = """
103
- parse(TreeType, text, [index];
104
- version=VERSION,
105
- ignore_trivia=true,
106
- filename=nothing,
107
- ignore_errors=false,
108
- ignore_warnings=ignore_errors)
109
-
110
- # Or, with the same arguments
107
+ # Parse a single expression/statement
108
+ parsestmt(TreeType, text, [index];
109
+ version=VERSION,
110
+ ignore_trivia=true,
111
+ filename=nothing,
112
+ ignore_errors=false,
113
+ ignore_warnings=ignore_errors)
114
+
115
+ # Parse all statements at top level (file scope)
111
116
parseall(...)
117
+
118
+ # Parse a single syntax atom
112
119
parseatom(...)
113
120
114
121
Parse Julia source code string `text` into a data structure of type `TreeType`.
115
- `parse ` parses a single Julia statement, `parseall` parses top level statements
122
+ `parsestmt ` parses a single Julia statement, `parseall` parses top level statements
116
123
at file scope and `parseatom` parses a single Julia identifier or other "syntax
117
124
atom".
118
125
@@ -136,16 +143,17 @@ parsing. To avoid exceptions due to warnings, use `ignore_warnings=true`. To
136
143
also avoid exceptions due to errors, use `ignore_errors=true`.
137
144
"""
138
145
139
- parse (:: Type{T} , text:: AbstractString ; kws... ) where {T} = _parse (:statement , true , T, text; kws... )[1 ]
140
- parseall (:: Type{T} , text:: AbstractString ; kws... ) where {T} = _parse (:toplevel , true , T, text; kws... )[1 ]
141
- parseatom (:: Type{T} , text:: AbstractString ; kws... ) where {T} = _parse (:atom , true , T, text; kws... )[1 ]
146
+ " $_parse_docs "
147
+ parsestmt (:: Type{T} , text:: AbstractString ; kws... ) where {T} = _parse (:statement , true , T, text; kws... )[1 ]
142
148
143
- @eval @doc $ _parse_docs parse
144
- @eval @doc $ _parse_docs parseall
145
- @eval @doc $ _parse_docs parseatom
149
+ " $_parse_docs "
150
+ parseall (:: Type{T} , text:: AbstractString ; kws... ) where {T} = _parse (:all , true , T, text; kws... )[1 ]
146
151
147
- parse (:: Type{T} , text:: AbstractString , index:: Integer ; kws... ) where {T} = _parse (:statement , false , T, text, index; kws... )
148
- parseall (:: Type{T} , text:: AbstractString , index:: Integer ; kws... ) where {T} = _parse (:toplevel , false , T, text, index; kws... )
152
+ " $_parse_docs "
153
+ parseatom (:: Type{T} , text:: AbstractString ; kws... ) where {T} = _parse (:atom , true , T, text; kws... )[1 ]
154
+
155
+ parsestmt (:: Type{T} , text:: AbstractString , index:: Integer ; kws... ) where {T} = _parse (:statement , false , T, text, index; kws... )
156
+ parseall (:: Type{T} , text:: AbstractString , index:: Integer ; kws... ) where {T} = _parse (:all , false , T, text, index; kws... )
149
157
parseatom (:: Type{T} , text:: AbstractString , index:: Integer ; kws... ) where {T} = _parse (:atom , false , T, text, index; kws... )
150
158
151
159
# -------------------------------------------------------------------------------
@@ -178,7 +186,7 @@ This interface works on UTF-8 encoded string or buffer data only.
178
186
"""
179
187
function tokenize (text)
180
188
ps = ParseStream (text)
181
- parse! (ps, rule= :toplevel )
189
+ parse! (ps, rule= :all )
182
190
ts = ps. tokens
183
191
output_tokens = Token[]
184
192
for i = 2 : length (ts)
198
206
function untokenize (token:: Token , text:: Vector{UInt8} )
199
207
text[token. range]
200
208
end
209
+
210
+ @deprecate parse parsestmt
0 commit comments