Skip to content

Commit

Permalink
Merge pull request #157 from kaushalmodi/nim_underscore
Browse files Browse the repository at this point in the history
Allow standalone _ to be recognized as identifier in Nim
  • Loading branch information
alecthomas committed Aug 2, 2018
2 parents cccad29 + a85919f commit 5d7fef2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions lexers/n/nim.go
Expand Up @@ -31,6 +31,7 @@ var Nim = internal.Register(MustNewLexer(
{`(v_?a_?r)\b`, KeywordDeclaration, nil},
{`(i_?n_?t_?|i_?n_?t_?8_?|i_?n_?t_?1_?6_?|i_?n_?t_?3_?2_?|i_?n_?t_?6_?4_?|f_?l_?o_?a_?t_?|f_?l_?o_?a_?t_?3_?2_?|f_?l_?o_?a_?t_?6_?4_?|b_?o_?o_?l_?|c_?h_?a_?r_?|r_?a_?n_?g_?e_?|a_?r_?r_?a_?y_?|s_?e_?q_?|s_?e_?t_?|s_?t_?r_?i_?n_?g_?)\b`, KeywordType, nil},
{`(n_?i_?l_?|t_?r_?u_?e_?|f_?a_?l_?s_?e_?)\b`, KeywordPseudo, nil},
{`\b_\b`, Name, nil}, // Standalone _ used as discardable variable identifier
{`\b((?![_\d])\w)(((?!_)\w)|(_(?!_)\w))*`, Name, nil},
{`[0-9][0-9_]*(?=([e.]|\'f(32|64)))`, LiteralNumberFloat, Push("float-suffix", "float-number")},
{`0x[a-f0-9][a-f0-9_]*`, LiteralNumberHex, Push("int-suffix")},
Expand Down
7 changes: 7 additions & 0 deletions lexers/testdata/nim.actual
@@ -0,0 +1,7 @@
# -*- nim -*-
import random
proc randBool(): bool =
randomize()
result = rand(1).bool
for _ in 0..5:
echo randBool()
45 changes: 45 additions & 0 deletions lexers/testdata/nim.expected
@@ -0,0 +1,45 @@
[
{"type":"Comment","value":"# -*- nim -*-"},
{"type":"Text","value":"\n"},
{"type":"KeywordNamespace","value":"import"},
{"type":"Text","value":" "},
{"type":"Name","value":"random"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"proc "},
{"type":"NameFunction","value":"randBool"},
{"type":"Punctuation","value":"():"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"bool"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":"\n "},
{"type":"Name","value":"randomize"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"result"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"rand"},
{"type":"Punctuation","value":"("},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Punctuation","value":")."},
{"type":"KeywordType","value":"bool"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"for"},
{"type":"Text","value":" "},
{"type":"Name","value":"_"},
{"type":"Text","value":" "},
{"type":"OperatorWord","value":"in"},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"0"},
{"type":"Punctuation","value":".."},
{"type":"LiteralNumberInteger","value":"5"},
{"type":"Punctuation","value":":"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"echo"},
{"type":"Text","value":" "},
{"type":"Name","value":"randBool"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":"\n"}
]

0 comments on commit 5d7fef2

Please sign in to comment.