1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Lexers
class Nix < RegexLexer
title 'Nix'
desc 'The Nix expression language (https://nixos.org/nix/manual/#ch-expression-language)'
tag 'nix'
aliases 'nixos'
filenames '*.nix'
state :whitespaces do
rule %r/^\s*\n\s*$/m, Text
rule %r/\s+/, Text
end
state :comment do
rule %r/#.*$/, Comment
rule %r(/\*), Comment, :multiline_comment
end
state :multiline_comment do
rule %r(\*/), Comment, :pop!
rule %r/./, Comment
end
state :number do
rule %r/[0-9]/, Num::Integer
end
state :null do
rule %r/(null)/, Keyword::Constant
end
state :boolean do
rule %r/(true|false)/, Keyword::Constant
end
state :binding do
rule %r/[a-zA-Z_][a-zA-Z0-9-]*/, Name::Variable
end
state :path do
word = "[a-zA-Z0-9\._-]+"
section = "(\/#{word})"
prefix = "[a-z\+]+:\/\/"
root = /#{section}+/.source
tilde = /~#{section}+/.source
basic = /#{word}(\/#{word})+/.source
url = /#{prefix}(\/?#{basic})/.source
rule %r/(#{root}|#{tilde}|#{basic}|#{url})/, Str::Other
end
state :string do
rule %r/"/, Str::Double, :double_quoted_string
rule %r/''/, Str::Double, :indented_string
end
state :string_content do
rule %r/\\./, Str::Escape
rule %r/\$\$/, Str::Escape
rule %r/\${/, Str::Interpol, :string_interpolated_arg
end
state :indented_string_content do
rule %r/'''/, Str::Escape
rule %r/''\$/, Str::Escape
rule %r/\$\$/, Str::Escape
rule %r/''\\./, Str::Escape
rule %r/\${/, Str::Interpol, :string_interpolated_arg
end
state :string_interpolated_arg do
mixin :expression
rule %r/}/, Str::Interpol, :pop!
end
state :indented_string do
mixin :indented_string_content
rule %r/''/, Str::Double, :pop!
rule %r/./, Str::Double
end
state :double_quoted_string do
mixin :string_content
rule %r/"/, Str::Double, :pop!
rule %r/./, Str::Double
end
state :operator do
rule %r/(\.|\?|\+\+|\+|!=|!|\/\/|\=\=|&&|\|\||->|\/|\*|-|<|>|<=|=>)/, Operator
end
state :assignment do
rule %r/(=)/, Operator
rule %r/(@)/, Operator
end
state :accessor do
rule %r/(\$)/, Punctuation
end
state :delimiter do
rule %r/(;|,|:)/, Punctuation
end
state :atom_content do
mixin :expression
rule %r/\)/, Punctuation, :pop!
end
state :atom do
rule %r/\(/, Punctuation, :atom_content
end
state :list do
rule %r/\[/, Punctuation, :list_content
end
state :list_content do
rule %r/\]/, Punctuation, :pop!
mixin :expression
end
state :set do
rule %r/{/, Punctuation, :set_content
end
state :set_content do
rule %r/}/, Punctuation, :pop!
mixin :expression
end
state :expression do
mixin :ignore
mixin :comment
mixin :boolean
mixin :null
mixin :number
mixin :path
mixin :string
mixin :keywords
mixin :operator
mixin :accessor
mixin :assignment
mixin :delimiter
mixin :binding
mixin :atom
mixin :set
mixin :list
end
state :keywords do
mixin :keywords_namespace
mixin :keywords_declaration
mixin :keywords_conditional
mixin :keywords_reserved
mixin :keywords_builtin
end
state :keywords_namespace do
keywords = %w(with in inherit)
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Namespace
end
state :keywords_declaration do
keywords = %w(let)
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Declaration
end
state :keywords_conditional do
keywords = %w(if then else)
rule %r/(?:#{keywords.join('|')})\b/, Keyword
end
state :keywords_reserved do
keywords = %w(rec assert map)
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Reserved
end
state :keywords_builtin do
keywords = %w(
abort
baseNameOf
builtins
derivation
fetchTarball
import
isNull
removeAttrs
throw
toString
)
rule %r/(?:#{keywords.join('|')})\b/, Keyword::Reserved
end
state :ignore do
mixin :whitespaces
end
state :root do
mixin :ignore
mixin :expression
end
start do
end
end
end
end
|