Skip to content

Commit

Permalink
Add regex support to Lua lexer (#1403)
Browse files Browse the repository at this point in the history
The Lua lexer does not currently provide any support for tokenising
regular expressions. This commit adds support for regular expressions
using code based on the regular expression rules in the JavaScript
lexer.
  • Loading branch information
pyrmont authored Feb 1, 2020
1 parent 988414a commit 835f83f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
40 changes: 39 additions & 1 deletion lib/rouge/lexers/lua.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def builtins

rule %r([A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)?) do |m|
name = m[0]
if self.builtins.include?(name)
if name == "gsub"
token Name::Builtin
push :gsub
elsif self.builtins.include?(name)
token Name::Builtin
elsif name =~ /\./
a, b = name.split('.', 2)
Expand All @@ -98,6 +101,41 @@ def builtins
rule %r(\(), Punctuation, :pop!
end

state :gsub do
rule %r/\)/, Punctuation, :pop!
rule %r/[(,]/, Punctuation
rule %r/\s+/, Text
rule %r/"/, Str::Regex, :regex
end

state :regex do
rule %r(") do
token Str::Regex
goto :regex_end
end

rule %r/\[\^?/, Str::Escape, :regex_group
rule %r/\\./, Str::Escape
rule %r{[(][?][:=<!]}, Str::Escape
rule %r/[{][\d,]+[}]/, Str::Escape
rule %r/[()?]/, Str::Escape
rule %r/./, Str::Regex
end

state :regex_end do
rule %r/[$]+/, Str::Regex, :pop!
rule(//) { pop! }
end

state :regex_group do
rule %r(/), Str::Escape
rule %r/\]/, Str::Escape, :pop!
rule %r/(\\)(.)/ do |m|
groups Str::Escape, Str::Regex
end
rule %r/./, Str::Regex
end

state :escape_sqs do
mixin :string_escape
mixin :sqs
Expand Down
3 changes: 3 additions & 0 deletions spec/visual/samples/lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,6 @@ end
acc = Account.create(1000)
acc:withdraw(100)

if
url = url:gsub("^['\"]", ""):gsub("['\"]$", "")
end

0 comments on commit 835f83f

Please sign in to comment.