1
- # -*- coding : utf-8 -*-
1
+ # encoding : utf-8
2
2
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
8
6
class CodeRay ::Scanners ::Lua < CodeRay ::Scanners ::Scanner
9
7
10
8
register_for :lua
11
9
file_extension "lua"
12
10
title "Lua"
13
11
12
+ # http://www.lua.org/manual/5.2/manual.html#3.1
14
13
KEYWORDS = %w[
15
14
and break do else elseif end
16
15
for function goto if in
17
16
local not or repeat return
18
17
then until while
19
18
]
20
19
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 ]
22
22
23
- PREDEFINED_EXPRESSIONS = %w[
23
+ # http://www.lua.org/manual/5.2/manual.html#6.1
24
+ LIBRARY = %w[
24
25
assert collectgarbage dofile error getmetatable
25
26
ipairs load loadfile next pairs pcall print
26
27
rawequal rawget rawlen rawset select setmetatable
@@ -33,52 +34,72 @@ class CodeRay::Scanners::Lua < CodeRay::Scanners::Scanner
33
34
(?:
34
35
\b (?<keyword>#{ KEYWORDS . join ( '|' ) } )\b
35
36
|
36
- (?<blockcomment>
37
- --\[ (?<commentequals>=*)\[ [\d \D ]*?\] \k <commentequals>\]
38
- )
39
- |
40
- (?:
41
- (?<s1q1>")(?<s1>(?:[^\\ "\n ]|\\ [abfnrtvz\\ "']|\\ \n |\\ \d {1,3}|\\ x[\d a-fA-F]{2})*)(?<s1q2>")
37
+ (?: # strings
38
+ (?<s1q1>")
39
+ (?<s1>(?:[^\\ "\n ]|\\ [abfnrtvz\\ "']|\\ \n |\\ \d {1,3}|\\ x[\d a-fA-F]{2})*)
40
+ (?<s1q2>")
42
41
|
43
- (?<s2q1>')(?<s2>(?:[^\\ '\n ]|\\ [abfnrtvz\\ "']|\\ \n |\\ \d {1,3}|\\ x[\d a-fA-F]{2})*)(?<s2q2>')
42
+ (?<s2q1>')
43
+ (?<s2>(?:[^\\ '\n ]|\\ [abfnrtvz\\ "']|\\ \n |\\ \d {1,3}|\\ x[\d a-fA-F]{2})*)
44
+ (?<s2q2>')
44
45
|
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>\] )
50
49
)
51
50
|
52
51
\b (?<number>
53
52
-? # Allows -2 to be properly highlighted, but makes 10-5 show -5 as a single number
54
53
(?:
55
54
0[xX]
56
55
(?:
57
- [\d a-fA-F]+\. ?[\d a-fA-F]*
56
+ [\d a-fA-F]+\. ?[\d a-fA-F]* # 0xA and 0xA. and 0xA.1
58
57
|
59
- [ \d a-fA-F]* \ . [\d a-fA-F]+
58
+ \ . [\d a-fA-F]+ # 0x.A
60
59
)
61
- (?:[pP][-+]?\d +)?
60
+ (?:[pP][-+]?\d +)? # 0xA.1p-3
62
61
|
63
62
(?:
64
- \d +\. ?\d *
63
+ \d +\. ?\d * # 3 and 3. and 3.14
65
64
|
66
- \d * \ .\d +
65
+ \. \d + # .3
67
66
)
68
- (?:[eE][-+]?\d +)?
67
+ (?:[eE][-+]?\d +)? # 3.1e-7
69
68
)
70
69
)\b
71
70
|
72
- \b (?<constant>#{ PREDEFINED_CONSTANTS . join ( '|' ) } )\b
71
+ (?<comment>
72
+ --(?!\[ ).+
73
+ )
73
74
|
74
- \b (?<library >#{ PREDEFINED_EXPRESSIONS . join ( '|' ) } )\b
75
+ \b (?<constant >#{ CONSTANTS . join ( '|' ) } )\b
75
76
|
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>\]
78
95
)
79
96
|
80
97
(?<reserved>
81
- \b _[A-Z]+\b
98
+ \b _[A-Z]+\b # _VERSION
99
+ )
100
+ |
101
+ (?<gotolabel>
102
+ ::[a-zA-Z_]\w *::
82
103
)
83
104
)
84
105
/x
@@ -101,11 +122,13 @@ class CodeRay::Scanners::Lua < CodeRay::Scanners::Scanner
101
122
s3 : :string ,
102
123
s3q2 : :delimiter ,
103
124
gotolabel : :label ,
125
+ operators : :operator ,
104
126
}
105
127
106
128
protected
107
129
108
130
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
109
132
string . gsub ( SCANNER ) do
110
133
match = $~
111
134
tokens . text_token ( match [ :space ] , :space ) unless match [ :space ] . empty?
0 commit comments