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
|
# encoding: utf-8
module CodeRay
module Scanners
# Scanner for the Lua[http://lua.org] programming lanuage.
#
# The languageās complete syntax is defined in
# {the Lua manual}[http://www.lua.org/manual/5.2/manual.html],
# which is what this scanner tries to conform to.
class Lua4 < RuleBasedScanner
register_for :lua4
file_extension 'lua'
title 'Lua'
protected
state :initial do
on %r'#!(.*?)$', :doctype
on %r//, push_state(:base)
end
state :base do
on %r'--\[(=*)\[.*?\]\1\]'m, :comment
on %r'--.*$', :comment
on %r'(\d*\.\d+|\d+\.\d*)(e[+-]?\d+)?'i, :float
on %r'\d+e[+-]?\d+'i, :float
on %r'0x[0-9a-f]*'i, :hex
on %r'\d+', :integer
on %r'\n', :space
on %r'[^\S\n]', :space
# multiline strings
on %r'\[(=*)\[.*?\]\1\]'m, :string
on %r'(==|~=|<=|>=|\.\.\.|\.\.|[=+\-*/%^<>#!.\\:])', :operator
on %r'[\[\]{}().,:;]', :operator
on %r'(and|or|not)\b', :operator
on %r'(break|do|else|elseif|end|for|if|in|repeat|return|then|until|while)\b', :keyword
on %r'(local)\b', :keyword
on %r'(true|false|nil)\b', :predefined_constant
on %r'(function)\b', :keyword, push_state(:funcname)
on %r'[A-Za-z_]\w*(\.[A-Za-z_]\w*)?', :ident
# on %r"'", :string, combined(:stringescape, :sqs)
on %r"'", :string, push_state(:sqs)
# on %r'"', :string, combined(:stringescape, :dqs)
on %r'"', :string, push_state(:dqs)
end
state :funcname do
on %r'\s+', :space
on %r'(?:([A-Za-z_]\w*)(\.))?([A-Za-z_]\w*)', groups(:class, :operator, :function), pop_state
# inline function
on %r'\(', :operator, pop_state
end
# if I understand correctly, every character is valid in a lua string,
# so this state is only for later corrections
# state :string do
# on %r'.', :string
# end
# state :stringescape do
# on %r/\\([abfnrtv\\"']|\d{1,3})/, :escape
# end
state :sqs do
on %r"'", :string, pop_state
# include(:string)
on %r/\\([abfnrtv\\"']|\d{1,3})/, :escape
on %r'.', :string
end
state :dqs do
on %r'"', :string, pop_state
# include(:string)
on %r/\\([abfnrtv\\"']|\d{1,3})/, :escape
on %r'.', :string
end
end
end
end
|