Skip to content

Commit 9572202

Browse files
vtjnashivarneYu Gong
authored
export Meta.isidentifier (and others) (#38197)
May be useful for embedding into other languages, for example. Co-authored-by: Ivar Nesje <[email protected]> Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Yu Gong <[email protected]>
1 parent e68dda9 commit 9572202

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

base/meta.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ using ..CoreLogging
99

1010
export quot,
1111
isexpr,
12+
isidentifier,
13+
isoperator,
14+
isunaryoperator,
15+
isbinaryoperator,
16+
ispostfixoperator,
1217
replace_sourceloc!,
1318
show_sexpr,
1419
@dump
1520

21+
using Base: isidentifier, isoperator, isunaryoperator, isbinaryoperator, ispostfixoperator
22+
1623
"""
1724
Meta.quot(ex)::Expr
1825

base/show.jl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,26 @@ const expr_parens = Dict(:tuple=>('(',')'), :vcat=>('[',']'),
11311131

11321132
is_id_start_char(c::AbstractChar) = ccall(:jl_id_start_char, Cint, (UInt32,), c) != 0
11331133
is_id_char(c::AbstractChar) = ccall(:jl_id_char, Cint, (UInt32,), c) != 0
1134+
1135+
"""
1136+
isidentifier(s) -> Bool
1137+
1138+
Return whether the symbol or string `s` contains characters that are parsed as
1139+
a valid identifier in Julia code.
1140+
1141+
Internally Julia allows any sequence of characters in a `Symbol` (except `\0`s),
1142+
and macros automatically use variable names containing `#` in order to avoid
1143+
naming collision with the surrounding code. In order for the parser to
1144+
recognize a variable, it uses a limited set of characters (greatly extended by
1145+
Unicode). `isidentifier()` makes it possible to query the parser directly
1146+
whether a symbol contains valid characters.
1147+
1148+
# Examples
1149+
```jldoctest
1150+
julia> Meta.isidentifier(:x), Meta.isidentifier("1x")
1151+
(true, false)
1152+
```
1153+
"""
11341154
function isidentifier(s::AbstractString)
11351155
isempty(s) && return false
11361156
(s == "true" || s == "false") && return false
@@ -1151,7 +1171,7 @@ Return `true` if the symbol can be used as an operator, `false` otherwise.
11511171
11521172
# Examples
11531173
```jldoctest
1154-
julia> Base.isoperator(:+), Base.isoperator(:f)
1174+
julia> Meta.isoperator(:+), Meta.isoperator(:f)
11551175
(true, false)
11561176
```
11571177
"""
@@ -1164,7 +1184,7 @@ Return `true` if the symbol can be used as a unary (prefix) operator, `false` ot
11641184
11651185
# Examples
11661186
```jldoctest
1167-
julia> Base.isunaryoperator(:-), Base.isunaryoperator(:√), Base.isunaryoperator(:f)
1187+
julia> Meta.isunaryoperator(:-), Meta.isunaryoperator(:√), Meta.isunaryoperator(:f)
11681188
(true, true, false)
11691189
```
11701190
"""
@@ -1178,7 +1198,7 @@ Return `true` if the symbol can be used as a binary (infix) operator, `false` ot
11781198
11791199
# Examples
11801200
```jldoctest
1181-
julia> Base.isbinaryoperator(:-), Base.isbinaryoperator(:√), Base.isbinaryoperator(:f)
1201+
julia> Meta.isbinaryoperator(:-), Meta.isbinaryoperator(:√), Meta.isbinaryoperator(:f)
11821202
(true, false, false)
11831203
```
11841204
"""
@@ -1194,7 +1214,7 @@ Return `true` if the symbol can be used as a postfix operator, `false` otherwise
11941214
11951215
# Examples
11961216
```jldoctest
1197-
julia> Base.ispostfixoperator(Symbol("'")), Base.ispostfixoperator(Symbol("'ᵀ")), Base.ispostfixoperator(:-)
1217+
julia> Meta.ispostfixoperator(Symbol("'")), Meta.ispostfixoperator(Symbol("'ᵀ")), Meta.ispostfixoperator(:-)
11981218
(true, true, false)
11991219
```
12001220
"""

doc/src/base/base.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,5 +425,9 @@ Base.precompile
425425
```@docs
426426
Meta.quot
427427
Meta.isexpr
428+
Meta.isidentifier
429+
Meta.isoperator
430+
Meta.isunaryoperator
431+
Meta.isbinaryoperator
428432
Meta.show_sexpr
429433
```

test/show.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,16 @@ end
480480
@test sprint(show, Symbol("'")) == "Symbol(\"'\")"
481481
@test_repr "var\"'\" = 5"
482482

483+
# isidentifier
484+
@test Meta.isidentifier("x")
485+
@test Meta.isidentifier("x1")
486+
@test !Meta.isidentifier("x.1")
487+
@test !Meta.isidentifier("1x")
488+
@test Meta.isidentifier(Symbol("x"))
489+
@test Meta.isidentifier(Symbol("x1"))
490+
@test !Meta.isidentifier(Symbol("x.1"))
491+
@test !Meta.isidentifier(Symbol("1x"))
492+
483493
# issue #32408: Printing of names which are invalid identifiers
484494
# Invalid identifiers which need `var` quoting:
485495
@test sprint(show, Expr(:call, :foo, Symbol("##"))) == ":(foo(var\"##\"))"

0 commit comments

Comments
 (0)