Skip to content

Commit 11796f7

Browse files
committed
Use doc contextual keyword to detect @doc macro in parser
1 parent 950b730 commit 11796f7

File tree

3 files changed

+17
-38
lines changed

3 files changed

+17
-38
lines changed

src/parse_stream.jl

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -318,28 +318,6 @@ function peek_token(stream::ParseStream, n::Integer=1; skip_newlines=false)
318318
stream.lookahead[_lookahead_index(stream, n, skip_newlines)]
319319
end
320320

321-
function _peek_equal_to(stream, first_byte, len, str)
322-
cbuf = codeunits(str)
323-
for i = 1:len
324-
if stream.textbuf[first_byte + i - 1] != cbuf[i]
325-
return false
326-
end
327-
end
328-
return true
329-
end
330-
331-
"""
332-
Return true if the node already emitted at `pos` covers the string `str`
333-
334-
This is a hack for edge cases where the parser needs access to interpret normal
335-
identifiers as contextural keywords. For example, the special parsing rules for
336-
`@doc` line contination :-(
337-
"""
338-
function peek_behind_str(stream::ParseStream, pos::ParseStreamPosition, str::String)
339-
s = stream.ranges[pos.output_index]
340-
return _peek_equal_to(stream, first_byte(s), span(s), str)
341-
end
342-
343321
function _peek_behind_fields(ranges, i)
344322
r = ranges[i]
345323
return (kind=kind(r),

src/parser.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ function peek_token(ps::ParseState, n=1; skip_newlines=nothing)
7171
peek_token(ps.stream, n, skip_newlines=skip_nl)
7272
end
7373

74-
function peek_behind_str(ps::ParseState, args...)
75-
peek_behind_str(ps.stream, args...)
76-
end
77-
7874
function peek_behind(ps::ParseState, args...)
7975
peek_behind(ps.stream, args...)
8076
end
@@ -1395,7 +1391,7 @@ function parse_call_chain(ps::ParseState, mark, is_macrocall=false)
13951391
# @A.foo a b ==> (macrocall (. A (quote @foo)) a b)
13961392
n_args = parse_space_separated_exprs(ps)
13971393
# TODO: Introduce K"doc" to make this hack less awful.
1398-
is_doc_macro = peek_behind_str(ps, macro_name_position, "doc")
1394+
is_doc_macro = peek_behind(ps, macro_name_position).orig_kind == K"doc"
13991395
if is_doc_macro && n_args == 1
14001396
# Parse extended @doc args on next line
14011397
# @doc x\ny ==> (macrocall @doc x y)

src/token_kinds.jl

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Mapping from token string identifiers to enumeration values as used in @K_str
2+
#
3+
# TODO: Unify Tokenize with this approach so we don't need to write these out
4+
# in two places.
25

36
const _str_to_kind = let Ts = TzTokens
47
Dict([
@@ -12,8 +15,6 @@ Dict([
1215
";" => Ts.SEMICOLON
1316

1417
"BEGIN_KEYWORDS" => Ts.begin_keywords
15-
"abstract" => Ts.ABSTRACT
16-
"as" => Ts.AS
1718
"baremodule" => Ts.BAREMODULE
1819
"begin" => Ts.BEGIN
1920
"break" => Ts.BREAK
@@ -35,18 +36,21 @@ Dict([
3536
"local" => Ts.LOCAL
3637
"macro" => Ts.MACRO
3738
"module" => Ts.MODULE
38-
"mutable" => Ts.MUTABLE
39-
"new" => Ts.NEW
40-
"outer" => Ts.OUTER
41-
"primitive" => Ts.PRIMITIVE
4239
"quote" => Ts.QUOTE
4340
"return" => Ts.RETURN
4441
"struct" => Ts.STRUCT
4542
"try" => Ts.TRY
46-
"type" => Ts.TYPE
4743
"using" => Ts.USING
48-
"var" => Ts.VAR
4944
"while" => Ts.WHILE
45+
# contextural keywords
46+
"abstract" => Ts.ABSTRACT
47+
"as" => Ts.AS
48+
"doc" => Ts.DOC
49+
"mutable" => Ts.MUTABLE
50+
"outer" => Ts.OUTER
51+
"primitive" => Ts.PRIMITIVE
52+
"type" => Ts.TYPE
53+
"var" => Ts.VAR
5054
"END_KEYWORDS" => Ts.end_keywords
5155

5256
# FIXME: Define precisely what Nothing means; integrate better with other tokens.
@@ -875,11 +879,12 @@ const _kind_to_str_unique =
875879
for kw in split("""
876880
( [ { } ] ) @ , ; " \"\"\" ` ```
877881
878-
as abstract baremodule begin break catch const
882+
baremodule begin break catch const
879883
continue do else elseif end export finally for
880884
function global if import let local
881-
macro module mutable new outer primitive quote
882-
return struct try type using var while
885+
macro module quote return struct try type using while
886+
887+
as abstract doc mutable outer primitive type var
883888
884889
block call comparison curly string inert macrocall kw parameters
885890
toplevel tuple ref vect braces bracescat hcat

0 commit comments

Comments
 (0)