Idiomatically determine all the characters that can be used for symbols: Difference between revisions
Content added Content deleted
(→{{header|Lua}}: added Lua solution) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 19: | Line 19: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk"># usage: gawk -f Idiomatically_determine_all_the_characters_that_can_be_used_for_symbols.awk |
||
function is_valid_identifier(id, rc) { |
function is_valid_identifier(id, rc) { |
||
Line 51: | Line 51: | ||
length(bad2), length(good2), good2) |
length(bad2), length(good2), good2) |
||
exit(0) |
exit(0) |
||
}</ |
}</syntaxhighlight> |
||
<p>output:</p> |
<p>output:</p> |
||
<pre> |
<pre> |
||
Line 60: | Line 60: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
Well, if the purpose of this task is to determine what can be used as an identifier then in F# anything so long as you enclose it in double backticks so: |
Well, if the purpose of this task is to determine what can be used as an identifier then in F# anything so long as you enclose it in double backticks so: |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let ``+`` = 5 |
let ``+`` = 5 |
||
printfn "%d" ``+`` |
printfn "%d" ``+`` |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 73: | Line 73: | ||
{{output?}} |
{{output?}} |
||
< |
<syntaxhighlight lang="factor">USING: parser see ; |
||
\ scan-word-name see</ |
\ scan-word-name see</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 90: | Line 90: | ||
or of length 2 starting with the underscore. |
or of length 2 starting with the underscore. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 173: | Line 173: | ||
_, _ = fmt.Println("Valid follow:", validFollow.String()) |
_, _ = fmt.Println("Valid follow:", validFollow.String()) |
||
_, _ = fmt.Println("Only follow:", validOnlyFollow.String()) |
_, _ = fmt.Println("Only follow:", validOnlyFollow.String()) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 209: | Line 209: | ||
According to the specification we may give predicates for valid symbols and identifiers in Haskell: |
According to the specification we may give predicates for valid symbols and identifiers in Haskell: |
||
< |
<syntaxhighlight lang="haskell">import Data.Char |
||
-- predicate for valid symbol |
-- predicate for valid symbol |
||
Line 229: | Line 229: | ||
, "else", "foreign", "if", "import", "in", "infix " |
, "else", "foreign", "if", "import", "in", "infix " |
||
, "infixl", "infixr", "instance", "let", "module " |
, "infixl", "infixr", "instance", "let", "module " |
||
, "newtype", "of", "then", "type", "where", "_"</ |
, "newtype", "of", "then", "type", "where", "_"</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 235: | Line 235: | ||
J is defined in terms of ascii, but that would not prevent it from being ported to other environments. But we can still use J's parser to determine if a specific character combination is a single, legal word: |
J is defined in terms of ascii, but that would not prevent it from being ported to other environments. But we can still use J's parser to determine if a specific character combination is a single, legal word: |
||
< |
<syntaxhighlight lang="j"> a.#~1=#@;: ::0:"1 'b',.a.,.'c' |
||
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz</ |
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz</syntaxhighlight> |
||
Here, [http://www.jsoftware.com/help/dictionary/dadot.htm a.] is the set of chararacters we are testing. We prefix each of these with an arbitrary letter, and suffix each with an arbitrary character and then try counting how many parsed tokens are formed by the result. If the token count is 1, then that character was a legal word-forming character. |
Here, [http://www.jsoftware.com/help/dictionary/dadot.htm a.] is the set of chararacters we are testing. We prefix each of these with an arbitrary letter, and suffix each with an arbitrary character and then try counting how many parsed tokens are formed by the result. If the token count is 1, then that character was a legal word-forming character. |
||
Line 244: | Line 244: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|8}} |
{{works with|Java|8}} |
||
< |
<syntaxhighlight lang="java">import java.util.function.IntPredicate; |
||
import java.util.stream.IntStream; |
import java.util.stream.IntStream; |
||
Line 275: | Line 275: | ||
System.out.println("..."); |
System.out.println("..."); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>Java Identifier start: $ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz¢£¤¥ªµºÀÁÂÃÄÅÆÇÈÉÊ... |
<pre>Java Identifier start: $ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz¢£¤¥ªµºÀÁÂÃÄÅÆÇÈÉÊ... |
||
Line 291: | Line 291: | ||
To generate a string of such characters idiomatically: |
To generate a string of such characters idiomatically: |
||
< |
<syntaxhighlight lang="jq">[range(0;128) | [.] | implode | select(test("[A-Za-z0-9$_]"))] | add</syntaxhighlight> |
||
jq 1.5 also allows ":" as a joining character in the form "module::name". |
jq 1.5 also allows ":" as a joining character in the form "module::name". |
||
Line 303: | Line 303: | ||
Therefore, assuming the availability in jq of the test/1 builtin, the test |
Therefore, assuming the availability in jq of the test/1 builtin, the test |
||
in jq for whether a character can appear literally in a jq identifier or key is: |
in jq for whether a character can appear literally in a jq identifier or key is: |
||
< |
<syntaxhighlight lang="jq">test("[^\u0000-\u0007F]")</syntaxhighlight> |
||
===Symbols=== |
===Symbols=== |
||
The following function screens for characters by "\p" class: |
The following function screens for characters by "\p" class: |
||
< |
<syntaxhighlight lang="jq">def is_character(class): |
||
test( "\\p{" + class + "}" );</ |
test( "\\p{" + class + "}" );</syntaxhighlight> |
||
For example, to test whether a character is a Unicode letter, symbol or numeric character: |
For example, to test whether a character is a Unicode letter, symbol or numeric character: |
||
< |
<syntaxhighlight lang="jq">is_character("L") or is_character("S") or is_character("N")</syntaxhighlight> |
||
An efficient way to count the number of Unicode characters within a character class is |
An efficient way to count the number of Unicode characters within a character class is |
||
to use the technique illustrated by the following function: |
to use the technique illustrated by the following function: |
||
< |
<syntaxhighlight lang="jq">def count(class; m; n): |
||
reduce (range(m;n) | [.] | implode | select( test( "\\p{" + class + "}" ))) as $i |
reduce (range(m;n) | [.] | implode | select( test( "\\p{" + class + "}" ))) as $i |
||
(0; . + 1);</ |
(0; . + 1);</syntaxhighlight> |
||
For example the number of Unicode "symbol" characters can be obtained by evaluating: |
For example the number of Unicode "symbol" characters can be obtained by evaluating: |
||
< |
<syntaxhighlight lang="jq">count("S"; 0; 1114112)</syntaxhighlight> |
||
The result is 3958. |
The result is 3958. |
||
Line 327: | Line 327: | ||
For example, x2 is a valid identifier, but 2x is not-- it is interpreted as 2 times the identifier x. In Julia, the Symbol() function turns a string into a symbolic token. So, for example: |
For example, x2 is a valid identifier, but 2x is not-- it is interpreted as 2 times the identifier x. In Julia, the Symbol() function turns a string into a symbolic token. So, for example: |
||
< |
<syntaxhighlight lang="julia"> |
||
for i in 1:0x200000 - 1 |
for i in 1:0x200000 - 1 |
||
Symbol("x" * Char(i)) |
Symbol("x" * Char(i)) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
When run, this loop runs without error up to 0x200000 but not at Unicode symbol numbered 0x200000. |
When run, this loop runs without error up to 0x200000 but not at Unicode symbol numbered 0x200000. |
||
Line 350: | Line 350: | ||
A Kotlin label name is a valid identifier followed by an @ symbol and an annotation name is an identifier preceded by an @ symbol. |
A Kotlin label name is a valid identifier followed by an @ symbol and an annotation name is an identifier preceded by an @ symbol. |
||
< |
<syntaxhighlight lang="scala">// version 1.1.4-3 |
||
typealias CharPredicate = (Char) -> Boolean |
typealias CharPredicate = (Char) -> Boolean |
||
Line 372: | Line 372: | ||
printChars("Kotlin Identifier ignorable: ", 0, 0x10FFFF, 25, |
printChars("Kotlin Identifier ignorable: ", 0, 0x10FFFF, 25, |
||
Character::isIdentifierIgnorable, true) |
Character::isIdentifierIgnorable, true) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 383: | Line 383: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
From the 5.4 reference manual: "Names (also called identifiers) in Lua can be any string of Latin letters, Arabic-Indic digits, and underscores, not beginning with a digit and not being a reserved word." |
From the 5.4 reference manual: "Names (also called identifiers) in Lua can be any string of Latin letters, Arabic-Indic digits, and underscores, not beginning with a digit and not being a reserved word." |
||
< |
<syntaxhighlight lang="lua">function isValidIdentifier(id) |
||
local reserved = { |
local reserved = { |
||
["and"]=true, ["break"]=true, ["do"]=true, ["end"]=true, ["else"]=true, ["elseif"]=true, ["end"]=true, |
["and"]=true, ["break"]=true, ["do"]=true, ["end"]=true, ["else"]=true, ["elseif"]=true, ["end"]=true, |
||
Line 398: | Line 398: | ||
end |
end |
||
print("Valid First Characters: " .. table.concat(vfc)) |
print("Valid First Characters: " .. table.concat(vfc)) |
||
print("Valid Subsequent Characters: " .. table.concat(vsc))</ |
print("Valid Subsequent Characters: " .. table.concat(vsc))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Valid First Characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz |
<pre>Valid First Characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz |
||
Line 404: | Line 404: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">chars = Characters[FromCharacterCode[Range[0, 1114111]]]; |
||
out = Reap[Do[ |
out = Reap[Do[ |
||
If[Quiet[Length[Symbol[c]] == 0], |
If[Quiet[Length[Symbol[c]] == 0], |
||
Line 420: | Line 420: | ||
{c, chars} |
{c, chars} |
||
]][[2, 1]]; |
]][[2, 1]]; |
||
Print["Possible 2nd-nth characters: ", out // Length]</ |
Print["Possible 2nd-nth characters: ", out // Length]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
In Wolfram Language almost all characters (there are 1114112 characters defined) can be used in variable/function names. I can't show all valid characters as there are over a million that are allowed. I do not show the list of characters 'out' but rather their length for practical purposes: |
In Wolfram Language almost all characters (there are 1114112 characters defined) can be used in variable/function names. I can't show all valid characters as there are over a million that are allowed. I do not show the list of characters 'out' but rather their length for practical purposes: |
||
Line 429: | Line 429: | ||
As regards identifiers, there exists a general rule which describes how they can be formed. For this rule, the following program prints the allowed starting characters and the allowed characters: |
As regards identifiers, there exists a general rule which describes how they can be formed. For this rule, the following program prints the allowed starting characters and the allowed characters: |
||
< |
<syntaxhighlight lang="nim">import sequtils, strutils |
||
echo "Allowed starting characters for identifiers:" |
echo "Allowed starting characters for identifiers:" |
||
Line 435: | Line 435: | ||
echo "" |
echo "" |
||
echo "Allowed characters in identifiers:" |
echo "Allowed characters in identifiers:" |
||
echo toSeq(IdentChars).join()</ |
echo toSeq(IdentChars).join()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 446: | Line 446: | ||
But Nim is a lot more flexible and allows using Unicode symbols in identifiers provided these are letters and digits. Thus, the following program is valid: |
But Nim is a lot more flexible and allows using Unicode symbols in identifiers provided these are letters and digits. Thus, the following program is valid: |
||
< |
<syntaxhighlight lang="nim">var à⁷ = 3 |
||
echo à⁷</ |
echo à⁷</syntaxhighlight> |
||
Using escape character <code>`</code>, it is possible to override the rules and to include any character in an identifier and even to use a keyword as identifier. Here is an example of the possibilities: |
Using escape character <code>`</code>, it is possible to override the rules and to include any character in an identifier and even to use a keyword as identifier. Here is an example of the possibilities: |
||
< |
<syntaxhighlight lang="nim">var `const`= 3 |
||
echo `const` |
echo `const` |
||
Line 468: | Line 468: | ||
var `1` = 2 |
var `1` = 2 |
||
echo `1` |
echo `1` |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
Line 482: | Line 482: | ||
Although this program does not use any feature that is not in Classic Rexx, |
Although this program does not use any feature that is not in Classic Rexx, |
||
it is included here to show what characters are valid for symbols in ooRexx. |
it is included here to show what characters are valid for symbols in ooRexx. |
||
< |
<syntaxhighlight lang="oorexx">/*REXX program determines what characters are valid for REXX symbols.*/ |
||
/* copied from REXX version 2 */ |
/* copied from REXX version 2 */ |
||
Parse Version v |
Parse Version v |
||
Line 492: | Line 492: | ||
symbol_characters=symbol_characters || c /* add to list. */ |
symbol_characters=symbol_characters || c /* add to list. */ |
||
end |
end |
||
say 'symbol characters:' symbol_characters /*display all */</ |
say 'symbol characters:' symbol_characters /*display all */</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>REXX-ooRexx_4.2.0(MT)_32-bit 6.04 22 Feb 2014 |
<pre>REXX-ooRexx_4.2.0(MT)_32-bit 6.04 22 Feb 2014 |
||
Line 499: | Line 499: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
The only symbols that can be used in variable names (including function names as a special case) are a-z, A-Z, 0-9, and the underscore. Additionally, the first character must be a letter. (That is, they must match this regex: <code>[a-zA-Z][a-zA-Z0-9_]*</code>.) |
The only symbols that can be used in variable names (including function names as a special case) are a-z, A-Z, 0-9, and the underscore. Additionally, the first character must be a letter. (That is, they must match this regex: <code>[a-zA-Z][a-zA-Z0-9_]*</code>.) |
||
< |
<syntaxhighlight lang="parigp">v=concat(concat([48..57],[65..90]),concat([97..122],95)); |
||
apply(Strchr,v)</ |
apply(Strchr,v)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>%1 = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_"]</pre> |
<pre>%1 = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_"]</pre> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl"># When not using the <code>use utf8</code> pragma, any word character in the ASCII range is allowed. |
||
# the loop below returns: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz |
# the loop below returns: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz |
||
for $i (0..0x7f) { |
for $i (0..0x7f) { |
||
Line 519: | Line 519: | ||
$c = chr($_); |
$c = chr($_); |
||
print $c if $c =~ /\p{Word}/; |
print $c if $c =~ /\p{Word}/; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Translation of AWK, extended with separation of ansi and utf8 handling |
Translation of AWK, extended with separation of ansi and utf8 handling |
||
<!--< |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- file i/o, system_exec, \t and \r chars</span> |
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- file i/o, system_exec, \t and \r chars</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">run</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">ident</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">run</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">ident</span><span style="color: #0000FF;">)</span> |
||
Line 580: | Line 580: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"utf8 characters: \n===============\n"</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"utf8 characters: \n===============\n"</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"bad:%,d, good:%,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ng8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ok8</span><span style="color: #0000FF;">})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"bad:%,d, good:%,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ng8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ok8</span><span style="color: #0000FF;">})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 604: | Line 604: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery">[ $ "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrS" |
||
$ QsTtUuVvWwXxYyZz()[]{}<>~=+-*/^\|_.,:;?!'"`%@&#$Q |
$ QsTtUuVvWwXxYyZz()[]{}<>~=+-*/^\|_.,:;?!'"`%@&#$Q |
||
join ] constant is tokenchars ( --> $ ) |
join ] constant is tokenchars ( --> $ ) |
||
Line 640: | Line 640: | ||
[ i^ validtoken if [ i^ emit ] ] ] is alltokens ( --> ) |
[ i^ validtoken if [ i^ emit ] ] ] is alltokens ( --> ) |
||
alltokens</ |
alltokens</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
Line 662: | Line 662: | ||
That's too much to be printing out here... call <code>(main)</code> yourself, at home. |
That's too much to be printing out here... call <code>(main)</code> yourself, at home. |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
;; Symbols that don't need to be specially quoted: |
;; Symbols that don't need to be specially quoted: |
||
(printf "~s~%" '(a a-z 3rd ...---... .hidden-files-look-like-this)) |
(printf "~s~%" '(a a-z 3rd ...---... .hidden-files-look-like-this)) |
||
Line 687: | Line 687: | ||
(when (zero? (modulo i 80)) (newline)) |
(when (zero? (modulo i 80)) (newline)) |
||
(display (list->string (list c))))) |
(display (list->string (list c))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 698: | Line 698: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
Any Unicode character or combination of characters can be used for symbols in Raku. Here's some counting rods and some cuneiform: |
Any Unicode character or combination of characters can be used for symbols in Raku. Here's some counting rods and some cuneiform: |
||
<lang |
<syntaxhighlight lang="raku" line>sub postfix:<𒋦>($n) { say "$n trilobites" } |
||
sub term:<𝍧> { unival('𝍧') } |
sub term:<𝍧> { unival('𝍧') } |
||
𝍧𒋦</ |
𝍧𒋦</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>8 trilobites</pre> |
<pre>8 trilobites</pre> |
||
Line 708: | Line 708: | ||
And here is a Zalgo-text symbol: |
And here is a Zalgo-text symbol: |
||
<lang |
<syntaxhighlight lang="raku" line>sub Z̧̔ͩ͌͑̉̎A̢̲̙̮̹̮͍̎L̔ͧ́͆G̰̬͎͔̱̅ͣͫO͙̔ͣ̈́̈̽̎ͣ ($n) { say "$n COMES" } |
||
Z̧̔ͩ͌͑̉̎A̢̲̙̮̹̮͍̎L̔ͧ́͆G̰̬͎͔̱̅ͣͫO͙̔ͣ̈́̈̽̎ͣ 'HE'</ |
Z̧̔ͩ͌͑̉̎A̢̲̙̮̹̮͍̎L̔ͧ́͆G̰̬͎͔̱̅ͣͫO͙̔ͣ̈́̈̽̎ͣ 'HE'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>HE COMES</pre> |
<pre>HE COMES</pre> |
||
Line 718: | Line 718: | ||
Actually, the above is a slight prevarication. The syntactic category notation does not allow you to use whitespace in the definition of a new symbol. But that leaves many more characters allowed than not allowed. Hence, it is much easier to enumerate the characters that <em>cannot</em> be used in symbols: |
Actually, the above is a slight prevarication. The syntactic category notation does not allow you to use whitespace in the definition of a new symbol. But that leaves many more characters allowed than not allowed. Hence, it is much easier to enumerate the characters that <em>cannot</em> be used in symbols: |
||
<lang |
<syntaxhighlight lang="raku" line>say .fmt("%4x"),"\t", uniname($_) |
||
if uniprop($_,'Z') |
if uniprop($_,'Z') |
||
for 0..0x1ffff;</ |
for 0..0x1ffff;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 20 SPACE |
<pre> 20 SPACE |
||
Line 746: | Line 746: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1=== |
===version 1=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program determines what characters are valid for REXX symbols. */ |
||
@= /*set symbol characters " " */ |
@= /*set symbol characters " " */ |
||
do j=0 for 2**8 /*traipse through all the chars. */ |
do j=0 for 2**8 /*traipse through all the chars. */ |
||
Line 754: | Line 754: | ||
say ' symbol characters: ' @ /*display all symbol characters.*/ |
say ' symbol characters: ' @ /*display all symbol characters.*/ |
||
/*stick a fork in it, we're done.*/</ |
/*stick a fork in it, we're done.*/</syntaxhighlight> |
||
Programming note: REXX allows any symbol to begin a (statement) label, but variables can't begin with a period ('''.''') or a numeric digit. |
Programming note: REXX allows any symbol to begin a (statement) label, but variables can't begin with a period ('''.''') or a numeric digit. |
||
<br><br>All examples below were executed on a (ASCII) PC using Windows/XP and Windows/7 with code page 437 in a DOS window. |
<br><br>All examples below were executed on a (ASCII) PC using Windows/XP and Windows/7 with code page 437 in a DOS window. |
||
Line 786: | Line 786: | ||
</pre> |
</pre> |
||
I've added version 2 which should work correctly for all Rexx interpreters and compilers |
I've added version 2 which should work correctly for all Rexx interpreters and compilers |
||
< |
<syntaxhighlight lang="rexx">/*REXX program determines what characters are valid for REXX symbols.*/ |
||
/* version 1 adapted for general acceptance */ |
/* version 1 adapted for general acceptance */ |
||
Parse Version v |
Parse Version v |
||
Line 797: | Line 797: | ||
end |
end |
||
say 'symbol characters:' symbol_characters /*display all */ |
say 'symbol characters:' symbol_characters /*display all */ |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} for some interpreters |
{{out}} for some interpreters |
||
Note that $#@ are not valid symbol characters for ooRexx. |
Note that $#@ are not valid symbol characters for ooRexx. |
||
Line 809: | Line 809: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/ZyPkGW8/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/4XdxscWGTtyw9MDQXCtRdg Scastie (remote JVM)]. |
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/ZyPkGW8/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/4XdxscWGTtyw9MDQXCtRdg Scastie (remote JVM)]. |
||
< |
<syntaxhighlight lang="scala">object IdiomaticallyDetermineSymbols extends App { |
||
private def print(msg: String, limit: Int, p: Int => Boolean, fmt: String) = |
private def print(msg: String, limit: Int, p: Int => Boolean, fmt: String) = |
||
Line 820: | Line 820: | ||
print("Unicode Identifier part : ", 25, cp => Character.isUnicodeIdentifierPart(cp), "[%d]") |
print("Unicode Identifier part : ", 25, cp => Character.isUnicodeIdentifierPart(cp), "[%d]") |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Tcl permits ''any'' character to be used in a variable or command name (subject to the restriction that <code>::</code> is a namespace separator and, for variables only, a <code>(…)</code> sequence is an array reference). The set of characters that can be used after <code>$</code> is more restricted, excluding many non-letter-like symbols, but still large. It is ''recommended practice'' to only use ASCII characters for variable names as this makes scripts more resistant to the majority of encoding problems when transporting them between systems, but the language does not itself impose such a restriction. |
Tcl permits ''any'' character to be used in a variable or command name (subject to the restriction that <code>::</code> is a namespace separator and, for variables only, a <code>(…)</code> sequence is an array reference). The set of characters that can be used after <code>$</code> is more restricted, excluding many non-letter-like symbols, but still large. It is ''recommended practice'' to only use ASCII characters for variable names as this makes scripts more resistant to the majority of encoding problems when transporting them between systems, but the language does not itself impose such a restriction. |
||
< |
<syntaxhighlight lang="tcl">for {set c 0;set printed 0;set special {}} {$c <= 0xffff} {incr c} { |
||
set ch [format "%c" $c] |
set ch [format "%c" $c] |
||
set v "_${ch}_" |
set v "_${ch}_" |
||
Line 838: | Line 838: | ||
puts "All Unicode characters legal in names" |
puts "All Unicode characters legal in names" |
||
} |
} |
||
puts "Characters legal after \$: $special"</ |
puts "Characters legal after \$: $special"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Only the first 256 characters are displayed: |
Only the first 256 characters are displayed: |
||
Line 848: | Line 848: | ||
Identifiers which begin with underscores can only be used as instance field names (one underscore) or static field names (two or more underscores). |
Identifiers which begin with underscores can only be used as instance field names (one underscore) or static field names (two or more underscores). |
||
< |
<syntaxhighlight lang="ecmascript">for (i in 97..122) System.write(String.fromByte(i)) |
||
for (i in 65..90) System.write(String.fromByte(i)) |
for (i in 65..90) System.write(String.fromByte(i)) |
||
System.print("_")</ |
System.print("_")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 859: | Line 859: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
Paraphrasing code from the compiler's parser: |
Paraphrasing code from the compiler's parser: |
||
< |
<syntaxhighlight lang="xpl0">char C, C1; |
||
[Text(0, "First character set: "); |
[Text(0, "First character set: "); |
||
for C:= 0 to 255 do |
for C:= 0 to 255 do |
||
Line 875: | Line 875: | ||
]; |
]; |
||
CrLf(0); |
CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 885: | Line 885: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
zkl only supports ASCII, although other character sets might be finessed. |
zkl only supports ASCII, although other character sets might be finessed. |
||
< |
<syntaxhighlight lang="zkl">[0..255].filter(fcn(n){ |
||
try{ Compiler.Compiler.compileText("var "+n.text) } |
try{ Compiler.Compiler.compileText("var "+n.text) } |
||
catch{ False } |
catch{ False } |
||
}).apply("text").concat()</ |
}).apply("text").concat()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |