Skip to content

Commit e89c841

Browse files
committed
Recognize operators; add reference link comments and more regex comments
1 parent 8b41364 commit e89c841

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

lib/coderay/scanners/lua.rb

+54-31
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
# -*- coding: utf-8 -*-
1+
# encoding: utf-8
22

3-
# Scanner for the Lua[http://lua.org] programming lanuage.
4-
#
5-
# The language’s complete syntax is defined in
6-
# {the Lua manual}[http://www.lua.org/manual/5.2/manual.html],
7-
# which is what this scanner tries to conform to.
3+
# Scanner for the Lua programming lanuage.
4+
# This scanner attempts to mimic the syntax defined at:
5+
# http://www.lua.org/manual/5.2/manual.html
86
class CodeRay::Scanners::Lua < CodeRay::Scanners::Scanner
97

108
register_for :lua
119
file_extension "lua"
1210
title "Lua"
1311

12+
# http://www.lua.org/manual/5.2/manual.html#3.1
1413
KEYWORDS = %w[
1514
and break do else elseif end
1615
for function goto if in
1716
local not or repeat return
1817
then until while
1918
]
2019

21-
PREDEFINED_CONSTANTS = %w[false true nil]
20+
# http://www.lua.org/manual/5.2/manual.html#3.1
21+
CONSTANTS = %w[false true nil]
2222

23-
PREDEFINED_EXPRESSIONS = %w[
23+
# http://www.lua.org/manual/5.2/manual.html#6.1
24+
LIBRARY = %w[
2425
assert collectgarbage dofile error getmetatable
2526
ipairs load loadfile next pairs pcall print
2627
rawequal rawget rawlen rawset select setmetatable
@@ -33,52 +34,72 @@ class CodeRay::Scanners::Lua < CodeRay::Scanners::Scanner
3334
(?:
3435
\b(?<keyword>#{KEYWORDS.join('|')})\b
3536
|
36-
(?<blockcomment>
37-
--\[(?<commentequals>=*)\[[\d\D]*?\]\k<commentequals>\]
38-
)
39-
|
40-
(?:
41-
(?<s1q1>")(?<s1>(?:[^\\"\n]|\\[abfnrtvz\\"']|\\\n|\\\d{1,3}|\\x[\da-fA-F]{2})*)(?<s1q2>")
37+
(?: # strings
38+
(?<s1q1>")
39+
(?<s1>(?:[^\\"\n]|\\[abfnrtvz\\"']|\\\n|\\\d{1,3}|\\x[\da-fA-F]{2})*)
40+
(?<s1q2>")
4241
|
43-
(?<s2q1>')(?<s2>(?:[^\\'\n]|\\[abfnrtvz\\"']|\\\n|\\\d{1,3}|\\x[\da-fA-F]{2})*)(?<s2q2>')
42+
(?<s2q1>')
43+
(?<s2>(?:[^\\'\n]|\\[abfnrtvz\\"']|\\\n|\\\d{1,3}|\\x[\da-fA-F]{2})*)
44+
(?<s2q2>')
4445
|
45-
(?<s3q1>\[(?<stringequals>=*)\[)(?<s3>[\d\D]*?)(?<s3q2>\]\k<stringequals>\])
46-
)
47-
|
48-
(?<comment>
49-
--(?!\[).+
46+
(?<s3q1>\[(?<stringequals>=*)\[)
47+
(?<s3>[\d\D]*?) # Not using multiline mode due to single-line comments
48+
(?<s3q2>\]\k<stringequals>\])
5049
)
5150
|
5251
\b(?<number>
5352
-? # Allows -2 to be properly highlighted, but makes 10-5 show -5 as a single number
5453
(?:
5554
0[xX]
5655
(?:
57-
[\da-fA-F]+\.?[\da-fA-F]*
56+
[\da-fA-F]+\.?[\da-fA-F]* # 0xA and 0xA. and 0xA.1
5857
|
59-
[\da-fA-F]*\.[\da-fA-F]+
58+
\.[\da-fA-F]+ # 0x.A
6059
)
61-
(?:[pP][-+]?\d+)?
60+
(?:[pP][-+]?\d+)? # 0xA.1p-3
6261
|
6362
(?:
64-
\d+\.?\d*
63+
\d+\.?\d* # 3 and 3. and 3.14
6564
|
66-
\d*\.\d+
65+
\.\d+ # .3
6766
)
68-
(?:[eE][-+]?\d+)?
67+
(?:[eE][-+]?\d+)? # 3.1e-7
6968
)
7069
)\b
7170
|
72-
\b(?<constant>#{PREDEFINED_CONSTANTS.join('|')})\b
71+
(?<comment>
72+
--(?!\[).+
73+
)
7374
|
74-
\b(?<library>#{PREDEFINED_EXPRESSIONS.join('|')})\b
75+
\b(?<constant>#{CONSTANTS.join('|')})\b
7576
|
76-
(?<gotolabel>
77-
::[a-zA-Z_]\w*::
77+
\b(?<library>#{LIBRARY.join('|')})\b
78+
|
79+
(?<operators>
80+
(?<!\.)\.{2,3}(?!\.)
81+
|
82+
(?<!=)={1,2}(?!=)
83+
|
84+
[+\-*\/%^#]
85+
|
86+
~=
87+
|
88+
[<>]=?
89+
)
90+
|
91+
(?<blockcomment>
92+
--\[(?<commentequals>=*)
93+
\[[\d\D]*? # Not using multiline mode due to single-line comments
94+
\]\k<commentequals>\]
7895
)
7996
|
8097
(?<reserved>
81-
\b_[A-Z]+\b
98+
\b_[A-Z]+\b # _VERSION
99+
)
100+
|
101+
(?<gotolabel>
102+
::[a-zA-Z_]\w*::
82103
)
83104
)
84105
/x
@@ -101,11 +122,13 @@ class CodeRay::Scanners::Lua < CodeRay::Scanners::Scanner
101122
s3: :string,
102123
s3q2: :delimiter,
103124
gotolabel: :label,
125+
operators: :operator,
104126
}
105127

106128
protected
107129

108130
def scan_tokens(tokens, options)
131+
# We use the block form of gsub instead of the StringScanner capabilities because StringScanner does not support named captures in 1.9
109132
string.gsub(SCANNER) do
110133
match = $~
111134
tokens.text_token( match[:space], :space ) unless match[:space].empty?

0 commit comments

Comments
 (0)