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
|
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Lexers
class Nim < RegexLexer
# This is pretty much a 1-1 port of the pygments NimrodLexer class
title "Nim"
desc "The Nim programming language (http://nim-lang.org/)"
tag 'nim'
aliases 'nimrod'
filenames '*.nim'
KEYWORDS = %w(
addr as asm atomic bind block break case cast const continue
converter defer discard distinct do elif else end enum except export
func finally for from generic if import include interface iterator let
macro method mixin nil object of out proc ptr raise ref return static
template try tuple type using var when while with without yield
)
OPWORDS = %w(
and or not xor shl shr div mod in notin is isnot
)
PSEUDOKEYWORDS = %w(
nil true false
)
TYPES = %w(
int int8 int16 int32 int64 float float32 float64 bool char range array
seq set string
)
NAMESPACE = %w(
from import include
)
def self.underscorize(words)
words.map do |w|
w.gsub(/./) { |x| "#{Regexp.escape(x)}_?" }
end.join('|')
end
state :chars do
rule(/\\([\\abcefnrtvl"\']|x[a-fA-F0-9]{2}|[0-9]{1,3})/, Str::Escape)
rule(/'/, Str::Char, :pop!)
rule(/./, Str::Char)
end
state :strings do
rule(/(?<!\$)\$(\d+|#|\w+)+/, Str::Interpol)
rule(/[^\\\'"\$\n]+/, Str)
rule(/[\'"\\]/, Str)
rule(/\$/, Str)
end
state :dqs do
rule(/\\([\\abcefnrtvl"\']|\n|x[a-fA-F0-9]{2}|[0-9]{1,3})/,
Str::Escape)
rule(/"/, Str, :pop!)
mixin :strings
end
state :rdqs do
rule(/"(?!")/, Str, :pop!)
rule(/"/, Str::Escape, :pop!)
mixin :strings
end
state :tdqs do
rule(/"""(?!")/, Str, :pop!)
mixin :strings
mixin :nl
end
state :funcname do
rule(/((?![\d_])\w)(((?!_)\w)|(_(?!_)\w))*/, Name::Function, :pop!)
rule(/`.+`/, Name::Function, :pop!)
end
state :nl do
rule(/\n/, Str)
end
state :floatnumber do
rule(/\.(?!\.)[0-9_]*/, Num::Float)
rule(/[eE][+-]?[0-9][0-9_]*/, Num::Float)
rule(//, Text, :pop!)
end
# Making apostrophes optional, as only hexadecimal with type suffix
# possibly ambiguous.
state :floatsuffix do
rule(/'?[fF](32|64)/, Num::Float)
rule(//, Text, :pop!)
end
state :intsuffix do
rule(/'?[iI](32|64)/, Num::Integer::Long)
rule(/'?[iI](8|16)/, Num::Integer)
rule(/'?[uU]/, Num::Integer)
rule(//, Text, :pop!)
end
state :root do
rule(/##.*$/, Str::Doc)
rule(/#.*$/, Comment)
rule(/\*|=|>|<|\+|-|\/|@|\$|~|&|%|\!|\?|\||\\|\[|\]/, Operator)
rule(/\.\.|\.|,|\[\.|\.\]|{\.|\.}|\(\.|\.\)|{|}|\(|\)|:|\^|`|;/,
Punctuation)
# Strings
rule(/(?:\w+)"/,Str, :rdqs)
rule(/"""/, Str, :tdqs)
rule(/"/, Str, :dqs)
# Char
rule(/'/, Str::Char, :chars)
# Keywords
rule(%r[(#{Nim.underscorize(OPWORDS)})\b], Operator::Word)
rule(/(p_?r_?o_?c_?\s)(?![\(\[\]])/, Keyword, :funcname)
rule(%r[(#{Nim.underscorize(KEYWORDS)})\b], Keyword)
rule(%r[(#{Nim.underscorize(NAMESPACE)})\b], Keyword::Namespace)
rule(/(v_?a_?r)\b/, Keyword::Declaration)
rule(%r[(#{Nim.underscorize(TYPES)})\b], Keyword::Type)
rule(%r[(#{Nim.underscorize(PSEUDOKEYWORDS)})\b], Keyword::Pseudo)
# Identifiers
rule(/\b((?![_\d])\w)(((?!_)\w)|(_(?!_)\w))*/, Name)
# Numbers
# Note: Have to do this with a block to push multiple states first,
# since we can't pass array of states like w/ Pygments.
rule(/[0-9][0-9_]*(?=([eE.]|'?[fF](32|64)))/) do
push :floatsuffix
push :floatnumber
token Num::Float
end
rule(/0[xX][a-fA-F0-9][a-fA-F0-9_]*/, Num::Hex, :intsuffix)
rule(/0[bB][01][01_]*/, Num, :intsuffix)
rule(/0o[0-7][0-7_]*/, Num::Oct, :intsuffix)
rule(/[0-9][0-9_]*/, Num::Integer, :intsuffix)
# Whitespace
rule(/\s+/, Text)
rule(/.+$/, Error)
end
end
end
end
|