Skip to content

Commit 917e87f

Browse files
authored
Parse docstring attachment as K"doc" kind (#217)
With this change `"str" f` now parses as (doc (string "str") f) This better represents the surface syntax which isn't an explicit macro call but a juxtaposition at top level.
1 parent c4f1f6a commit 917e87f

File tree

6 files changed

+23
-21
lines changed

6 files changed

+23
-21
lines changed

src/expr.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
4444
val isa UInt128 ? Symbol("@uint128_str") :
4545
Symbol("@big_str")
4646
return Expr(:macrocall, GlobalRef(Core, macname), nothing, str)
47-
elseif kind(node) == K"core_@doc"
48-
return GlobalRef(Core, Symbol("@doc"))
4947
elseif kind(node) == K"core_@cmd"
5048
return GlobalRef(Core, Symbol("@cmd"))
5149
elseif kind(node) == K"MacroName" && val === Symbol("@.")
@@ -156,6 +154,9 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
156154
if headsym === :macrocall
157155
reorder_parameters!(args, 2)
158156
insert!(args, 2, loc)
157+
elseif headsym === :doc
158+
return Expr(:macrocall, GlobalRef(Core, Symbol("@doc")),
159+
loc, args...)
159160
elseif headsym in (:dotcall, :call)
160161
# Julia's standard `Expr` ASTs have children stored in a canonical
161162
# order which is often not always source order. We permute the children

src/kinds.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,6 @@ const _kind_names =
864864
"MacroName"
865865
"StringMacroName"
866866
"CmdMacroName"
867-
"core_@doc"
868867
"core_@cmd"
869868
"core_@int128_str"
870869
"core_@uint128_str"

src/parser.jl

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ end
477477
#
478478
# a;b;c ==> (toplevel a b c)
479479
# a;;;b;; ==> (toplevel a b)
480-
# "x" a ; "y" b ==> (toplevel (macrocall core_@doc "x" a) (macrocall core_@doc "y" b))
480+
# "x" a ; "y" b ==> (toplevel (doc (string "x") a) (doc (string "y") b))
481481
#
482482
# flisp: parse-stmts
483483
function parse_stmts(ps::ParseState)
@@ -500,12 +500,10 @@ function parse_stmts(ps::ParseState)
500500
end
501501

502502
# Parse docstrings attached by a space or single newline
503-
# "doc" foo ==> (macrocall core_@doc "doc" foo)
504503
#
505504
# flisp: parse-docstring
506505
function parse_docstring(ps::ParseState, down=parse_eq)
507506
mark = position(ps)
508-
atdoc_mark = bump_invisible(ps, K"TOMBSTONE")
509507
down(ps)
510508
if peek_behind(ps).kind == K"string"
511509
is_doc = true
@@ -521,19 +519,18 @@ function parse_docstring(ps::ParseState, down=parse_eq)
521519
is_doc = false
522520
else
523521
# Allow a single newline
524-
# "doc" \n foo ==> (macrocall core_@doc (string "doc") foo)
522+
# "doc" \n foo ==> (doc (string "doc") foo)
525523
bump(ps, TRIVIA_FLAG) # NewlineWs
526524
end
527525
else
528-
# "doc" foo ==> (macrocall core_@doc (string "doc") foo)
529-
# "doc $x" foo ==> (macrocall core_@doc (string "doc " x) foo)
526+
# "doc" foo ==> (doc (string "doc") foo)
527+
# "doc $x" foo ==> (doc (string "doc " x) foo)
530528
# Allow docstrings with embedded trailing whitespace trivia
531-
# """\n doc\n """ foo ==> (macrocall core_@doc (string-s "doc\n") foo)
529+
# """\n doc\n """ foo ==> (doc (string-s "doc\n") foo)
532530
end
533531
if is_doc
534-
reset_node!(ps, atdoc_mark, kind=K"core_@doc")
535532
down(ps)
536-
emit(ps, mark, K"macrocall")
533+
emit(ps, mark, K"doc")
537534
end
538535
end
539536
end
@@ -2003,7 +2000,7 @@ function parse_resword(ps::ParseState)
20032000
parse_unary_prefix(ps)
20042001
end
20052002
# module A \n a \n b \n end ==> (module true A (block a b))
2006-
# module A \n "x"\na \n end ==> (module true A (block (core_@doc (string "x") a)))
2003+
# module A \n "x"\na \n end ==> (module true A (block (doc (string "x") a)))
20072004
parse_block(ps, parse_docstring)
20082005
bump_closing_token(ps, K"end")
20092006
emit(ps, mark, K"module")

src/syntax_tree.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ function SyntaxNode(source::SourceFile, raw::GreenNode{SyntaxHead}, position::In
117117
Symbol("@$(normalize_identifier(val_str))_str")
118118
elseif k == K"CmdMacroName"
119119
Symbol("@$(normalize_identifier(val_str))_cmd")
120-
elseif k == K"core_@doc"
121-
Symbol("core_@doc")
122120
elseif k == K"core_@cmd"
123121
Symbol("core_@cmd")
124122
elseif is_syntax_kind(raw)

test/expr.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,11 @@
295295
Expr(:block, LineNumberNode(1), :w),
296296
Expr(:block, LineNumberNode(1), :z))
297297
end
298+
299+
@testset "Core.@doc" begin
300+
@test parse(Expr, "\"x\" f") ==
301+
Expr(:macrocall, GlobalRef(Core, Symbol("@doc")), LineNumberNode(1), "x", :f)
302+
@test parse(Expr, "\n\"x\" f") ==
303+
Expr(:macrocall, GlobalRef(Core, Symbol("@doc")), LineNumberNode(2), "x", :f)
304+
end
298305
end

test/parser.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ tests = [
5959
"a;b;c" => "(toplevel a b c)"
6060
"a;;;b;;" => "(toplevel a b)"
6161
""" "x" a ; "y" b """ =>
62-
"""(toplevel (macrocall core_@doc (string "x") a) (macrocall core_@doc (string "y") b))"""
62+
"""(toplevel (doc (string "x") a) (doc (string "y") b))"""
6363
"x y" => "x (error-t y)"
6464
],
6565
JuliaSyntax.parse_eq => [
@@ -487,7 +487,7 @@ tests = [
487487
"module do \n end" => "(module true (error (do)) (block))"
488488
"module \$A end" => "(module true (\$ A) (block))"
489489
"module A \n a \n b \n end" => "(module true A (block a b))"
490-
"""module A \n "x"\na\n end""" => """(module true A (block (macrocall core_@doc (string "x") a)))"""
490+
"""module A \n "x"\na\n end""" => """(module true A (block (doc (string "x") a)))"""
491491
# export
492492
"export a" => "(export a)" => Expr(:export, :a)
493493
"export @a" => "(export @a)" => Expr(:export, Symbol("@a"))
@@ -912,11 +912,11 @@ tests = [
912912
""" "notdoc" ] """ => "(string \"notdoc\")"
913913
""" "notdoc" \n] """ => "(string \"notdoc\")"
914914
""" "notdoc" \n\n foo """ => "(string \"notdoc\")"
915-
""" "doc" \n foo """ => """(macrocall core_@doc (string "doc") foo)"""
916-
""" "doc" foo """ => """(macrocall core_@doc (string "doc") foo)"""
917-
""" "doc \$x" foo """ => """(macrocall core_@doc (string "doc " x) foo)"""
915+
""" "doc" \n foo """ => """(doc (string "doc") foo)"""
916+
""" "doc" foo """ => """(doc (string "doc") foo)"""
917+
""" "doc \$x" foo """ => """(doc (string "doc " x) foo)"""
918918
# Allow docstrings with embedded trailing whitespace trivia
919-
"\"\"\"\n doc\n \"\"\" foo" => """(macrocall core_@doc (string-s "doc\\n") foo)"""
919+
"\"\"\"\n doc\n \"\"\" foo" => """(doc (string-s "doc\\n") foo)"""
920920
],
921921
]
922922

0 commit comments

Comments
 (0)