Skip to content

Commit

Permalink
support public in fallback/legacy parser (JuliaLang#51575)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Oct 16, 2023
1 parent ab94a24 commit bbcbac9
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 66 deletions.
117 changes: 58 additions & 59 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1072,83 +1072,82 @@ export

@main

# TODO: use normal syntax once JuliaSyntax.jl becomes available at this point in bootstrapping
eval(Expr(:public,
public
# Modules
:Checked,
:Filesystem,
:Order,
:Sort,
Checked,
Filesystem,
Order,
Sort,

# Types
:AbstractLock,
:AsyncCondition,
:CodeUnits,
:Event,
:Fix1,
:Fix2,
:Generator,
:ImmutableDict,
:OneTo,
:UUID,
AbstractLock,
AsyncCondition,
CodeUnits,
Event,
Fix1,
Fix2,
Generator,
ImmutableDict,
OneTo,
UUID,

# Semaphores
:Semaphore,
:acquire,
:release,
Semaphore,
acquire,
release,

# collections
:IteratorEltype,
:IteratorSize,
:to_index,
:vect,
:isdone,
:front,
:rest,
:split_rest,
:tail,
:checked_length,
IteratorEltype,
IteratorSize,
to_index,
vect,
isdone,
front,
rest,
split_rest,
tail,
checked_length,

# Loading
:DL_LOAD_PATH,
:load_path,
:active_project,
DL_LOAD_PATH,
load_path,
active_project,

# Reflection and introspection
:isambiguous,
:isexpr,
:isidentifier,
:issingletontype,
:identify_package,
:locate_package,
:moduleroot,
:jit_total_bytes,
:summarysize,
:isexported,
:ispublic,
isambiguous,
isexpr,
isidentifier,
issingletontype,
identify_package,
locate_package,
moduleroot,
jit_total_bytes,
summarysize,
isexported,
ispublic,

# Opperators
:operator_associativity,
:operator_precedence,
:isbinaryoperator,
:isoperator,
:isunaryoperator,
operator_associativity,
operator_precedence,
isbinaryoperator,
isoperator,
isunaryoperator,

# C interface
:cconvert,
:unsafe_convert,
cconvert,
unsafe_convert,

# Error handling
:exit_on_sigint,
:windowserror,
exit_on_sigint,
windowserror,

# Macros
Symbol("@assume_effects"),
Symbol("@constprop"),
Symbol("@locals"),
Symbol("@propagate_inbounds"),
@assume_effects,
@constprop,
@locals,
@propagate_inbounds,

# misc
:notnothing,
:runtests,
:text_colors))
notnothing,
runtests,
text_colors
2 changes: 1 addition & 1 deletion src/ast.scm
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
;; misc syntax forms
((import using)
(string (car e) " " (string.join (map deparse-import-path (cdr e)) ", ")))
((global local export) (string (car e) " " (string.join (map deparse (cdr e)) ", ")))
((global local export public) (string (car e) " " (string.join (map deparse (cdr e)) ", ")))
((const) (string "const " (deparse (cadr e))))
((top) (deparse (cadr e)))
((core) (string "Core." (deparse (cadr e))))
Expand Down
2 changes: 1 addition & 1 deletion src/jlfrontend.scm
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@

(define (toplevel-only-expr? e)
(and (pair? e)
(or (memq (car e) '(toplevel line module import using export
(or (memq (car e) '(toplevel line module import using export public
error incomplete))
(and (memq (car e) '(global const)) (every symbol? (cdr e))))))

Expand Down
21 changes: 16 additions & 5 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@

;; ";" at the top level produces a sequence of top level expressions
(define (parse-stmts s)
(let ((ex (parse-Nary s (lambda (s) (parse-docstring s parse-eq))
(let ((ex (parse-Nary s (lambda (s) (parse-public s parse-eq))
'(#\;) 'toplevel (lambda (x) (eqv? x #\newline)) #f)))
;; check for unparsed junk after an expression
(let ((t (peek-token s)))
Expand Down Expand Up @@ -1608,18 +1608,18 @@
((module baremodule)
(let* ((name (parse-unary-prefix s))
(loc (line-number-node s))
(body (parse-block s (lambda (s) (parse-docstring s parse-eq)))))
(body (parse-block s (lambda (s) (parse-public s parse-eq)))))
(if (reserved-word? name)
(error (string "invalid module name \"" name "\"")))
(expect-end s word)
(list 'module (if (eq? word 'module) '(true) '(false)) name
`(block ,loc ,@(cdr body)))))
((export)
((export public)
(let ((es (map macrocall-to-atsym
(parse-comma-separated s parse-unary-prefix))))
(if (not (every symbol-or-interpolate? es))
(error "invalid \"export\" statement"))
`(export ,@es)))
(error (string "invalid \"" word "\" statement")))
`(,word ,@es)))
((import using)
(parse-imports s word))
((do)
Expand Down Expand Up @@ -2664,6 +2664,17 @@
;; string interpolation
(eq? (car e) 'string))))

(define (parse-public s production)
(if (eq? (peek-token s) 'public)
(let ((spc (ts:space? s)))
(take-token s)
(if (memv (peek-token s) '(#\( = #\[))
(begin ;; TODO: deprecation warning here
(ts:put-back! s 'public spc)
(parse-docstring s production))
(parse-resword s 'public)))
(parse-docstring s production)))

(define (parse-docstring s production)
(let ((startloc (line-number-node s)) ; be sure to use the line number from the head of the docstring
(ex (production s)))
Expand Down
23 changes: 23 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3536,3 +3536,26 @@ end
@test_throws ErrorException("syntax: Attempted to use slot marked unused") @eval function funused50518(::Float64)
$(Symbol("#unused#"))
end

@testset "public keyword" begin
p(str) = Base.remove_linenums!(Meta.parse(str))
# tests ported from JuliaSyntax.jl
@test p("function f(public)\n public + 3\nend") == Expr(:function, Expr(:call, :f, :public), Expr(:block, Expr(:call, :+, :public, 3)))
@test p("public A, B") == Expr(:public, :A, :B)
@test p("if true \n public *= 4 \n end") == Expr(:if, true, Expr(:block, Expr(:*=, :public, 4)))
@test p("module Mod\n public A, B \n end") == Expr(:module, true, :Mod, Expr(:block, Expr(:public, :A, :B)))
@test p("module Mod2\n a = 3; b = 6; public a, b\n end") == Expr(:module, true, :Mod2, Expr(:block, Expr(:(=), :a, 3), Expr(:(=), :b, 6), Expr(:public, :a, :b)))
@test p("a = 3; b = 6; public a, b") == Expr(:toplevel, Expr(:(=), :a, 3), Expr(:(=), :b, 6), Expr(:public, :a, :b))
@test_throws Meta.ParseError p("begin \n public A, B \n end")
@test_throws Meta.ParseError p("if true \n public A, B \n end")
@test_throws Meta.ParseError p("public export=true foo, bar")
@test_throws Meta.ParseError p("public experimental=true foo, bar")
@test p("public(x::String) = false") == Expr(:(=), Expr(:call, :public, Expr(:(::), :x, :String)), Expr(:block, false))
@test p("module M; export @a; end") == Expr(:module, true, :M, Expr(:block, Expr(:export, :var"@a")))
@test p("module M; public @a; end") == Expr(:module, true, :M, Expr(:block, Expr(:public, :var"@a")))
@test p("module M; export ⤈; end") == Expr(:module, true, :M, Expr(:block, Expr(:export, :)))
@test p("module M; public ⤈; end") == Expr(:module, true, :M, Expr(:block, Expr(:public, :)))
@test p("public = 4") == Expr(:(=), :public, 4)
@test p("public[7] = 5") == Expr(:(=), Expr(:ref, :public, 7), 5)
@test p("public() = 6") == Expr(:(=), Expr(:call, :public), Expr(:block, 6))
end

0 comments on commit bbcbac9

Please sign in to comment.