Skip to content

Commit f82436b

Browse files
committed
nix: Fix identifier lexing
1 parent 6c22459 commit f82436b

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lib/rouge/lexers/nix.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Nix < RegexLexer
1010
aliases 'nixos'
1111
filenames '*.nix'
1212

13+
id_boundary = /(?![a-zA-Z_0-9'-])/
14+
1315
state :whitespaces do
1416
rule %r/^\s*\n\s*$/m, Text
1517
rule %r/\s+/, Text
@@ -30,15 +32,15 @@ class Nix < RegexLexer
3032
end
3133

3234
state :null do
33-
rule %r/(null)/, Keyword::Constant
35+
rule %r/null#{id_boundary}/, Keyword::Constant
3436
end
3537

3638
state :boolean do
37-
rule %r/(true|false)/, Keyword::Constant
39+
rule %r/(?:true|false)#{id_boundary}/, Keyword::Constant
3840
end
3941

4042
state :binding do
41-
rule %r/[a-zA-Z_][a-zA-Z0-9-]*/, Name::Variable
43+
rule %r/[a-zA-Z_][a-zA-Z_0-9'-]*/, Name::Variable
4244
end
4345

4446
state :path do
@@ -161,22 +163,22 @@ class Nix < RegexLexer
161163

162164
state :keywords_namespace do
163165
keywords = %w(with in inherit)
164-
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Namespace
166+
rule %r/(?:#{keywords.join('|')})#{id_boundary}/, Keyword::Namespace
165167
end
166168

167169
state :keywords_declaration do
168170
keywords = %w(let)
169-
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Declaration
171+
rule %r/(?:#{keywords.join('|')})#{id_boundary}/, Keyword::Declaration
170172
end
171173

172174
state :keywords_conditional do
173175
keywords = %w(if then else)
174-
rule %r/(?:#{keywords.join('|')})\b/, Keyword
176+
rule %r/(?:#{keywords.join('|')})#{id_boundary}/, Keyword
175177
end
176178

177179
state :keywords_reserved do
178180
keywords = %w(rec assert map)
179-
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Reserved
181+
rule %r/(?:#{keywords.join('|')})#{id_boundary}/, Keyword::Reserved
180182
end
181183

182184
state :keywords_builtin do
@@ -192,7 +194,7 @@ class Nix < RegexLexer
192194
throw
193195
toString
194196
)
195-
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Reserved
197+
rule %r/(?:#{keywords.join('|')})#{id_boundary}/, Keyword::Reserved
196198
end
197199

198200
state :ignore do

spec/lexers/nix_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66

77
describe 'lexing' do
88
include Support::Lexing
9+
10+
it 'recognizes Nix identifier boundaries (#2176)' do
11+
assert_tokens_equal "let\n if' = -1;\n trueTest = 1;\n falseTest = 0;\nin { }", ["Keyword.Declaration", "let"], ["Text", "\n "], ["Name.Variable", "if'"], ["Text", " "], ["Operator", "="], ["Text", " "], ["Operator", "-"], ["Literal.Number.Integer", "1"], ["Punctuation", ";"], ["Text", "\n "], ["Name.Variable", "trueTest"], ["Text", " "], ["Operator", "="], ["Text", " "], ["Literal.Number.Integer", "1"], ["Punctuation", ";"], ["Text", "\n "], ["Name.Variable", "falseTest"], ["Text", " "], ["Operator", "="], ["Text", " "], ["Literal.Number.Integer", "0"], ["Punctuation", ";"], ["Text", "\n"], ["Keyword.Namespace", "in"], ["Text", " "], ["Punctuation", "{"], ["Text", " "], ["Punctuation", "}"]
12+
end
913
end
1014
end

0 commit comments

Comments
 (0)