Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/rouge/lexers/nix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Nix < RegexLexer
aliases 'nixos'
filenames '*.nix'

id_boundary = /(?![a-zA-Z0-9_'-])/

state :whitespaces do
rule %r/^\s*\n\s*$/m, Text
rule %r/\s+/, Text
Expand All @@ -30,15 +32,15 @@ class Nix < RegexLexer
end

state :null do
rule %r/(null)/, Keyword::Constant
rule %r/null#{id_boundary}/, Keyword::Constant
end

state :boolean do
rule %r/(true|false)/, Keyword::Constant
rule %r/(?:true|false)#{id_boundary}/, Keyword::Constant
end

state :binding do
rule %r/[a-zA-Z_][a-zA-Z0-9-]*/, Name::Variable
rule %r/[a-zA-Z_][a-zA-Z0-9_'-]*/, Name::Variable
end

state :path do
Expand Down Expand Up @@ -161,22 +163,22 @@ class Nix < RegexLexer

state :keywords_namespace do
keywords = %w(with in inherit)
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Namespace
rule %r/(?:#{keywords.join('|')})#{id_boundary}/, Keyword::Namespace
end

state :keywords_declaration do
keywords = %w(let)
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Declaration
rule %r/(?:#{keywords.join('|')})#{id_boundary}/, Keyword::Declaration
end

state :keywords_conditional do
keywords = %w(if then else)
rule %r/(?:#{keywords.join('|')})\b/, Keyword
rule %r/(?:#{keywords.join('|')})#{id_boundary}/, Keyword
end

state :keywords_reserved do
keywords = %w(rec assert map)
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Reserved
rule %r/(?:#{keywords.join('|')})#{id_boundary}/, Keyword::Reserved
end

state :keywords_builtin do
Expand All @@ -192,7 +194,7 @@ class Nix < RegexLexer
throw
toString
)
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Reserved
rule %r/(?:#{keywords.join('|')})#{id_boundary}/, Keyword::Reserved
end

state :ignore do
Expand Down
4 changes: 4 additions & 0 deletions spec/lexers/nix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@

describe 'lexing' do
include Support::Lexing

it 'recognizes Nix identifier boundaries (#2176)' do
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", "}"]
end
end
end