Skip to content

Commit

Permalink
Improve regex and string lexing in CoffeeScript lexer (#1441)
Browse files Browse the repository at this point in the history
This commit improves the way that the CoffeeScript lexer lexes regular
expressions and strings (in particular the way that escaping is handled).
  • Loading branch information
pyrmont committed Feb 18, 2020
1 parent fcddd15 commit 3d7ee49
Showing 1 changed file with 47 additions and 15 deletions.
62 changes: 47 additions & 15 deletions lib/rouge/lexers/coffeescript.rb
Expand Up @@ -49,41 +49,64 @@ def self.builtins

id = /[$a-zA-Z_][a-zA-Z0-9_]*/

state :comments_and_whitespace do
rule %r/\s+/m, Text
state :comments do
rule %r/###[^#].*?###/m, Comment::Multiline
rule %r/#.*$/, Comment::Single
end

state :multiline_regex do
# this order is important, so that #{ isn't interpreted
# as a comment
mixin :has_interpolation
mixin :comments_and_whitespace
state :whitespace do
rule %r/\s+/m, Text
end

rule %r(///([gim]+\b|\B)), Str::Regex, :pop!
rule %r(/), Str::Regex
rule %r([^/#]+), Str::Regex
state :regex_comment do
rule %r/^#(?!\{).*$/, Comment::Single
rule %r/(\s+)(#(?!\{).*)$/ do
groups Text, Comment::Single
end
end

state :slash_starts_regex do
mixin :comments_and_whitespace
state :multiline_regex_begin do
rule %r(///) do
token Str::Regex
goto :multiline_regex
end
end

state :multiline_regex_end do
rule %r(///([gimy]+\b|\B)), Str::Regex, :pop!
end

state :multiline_regex do
mixin :multiline_regex_end
mixin :regex_comment
mixin :has_interpolation
mixin :comments
mixin :whitespace
mixin :code_escape

rule %r/\\\D/, Str::Escape
rule %r/\\\d+/, Name::Variable
rule %r/./m, Str::Regex
end

state :slash_starts_regex do
mixin :comments
mixin :whitespace
mixin :multiline_regex_begin

rule %r(
/(\\.|[^\[/\\\n]|\[(\\.|[^\]\\\n])*\])+/ # a regex
([gim]+\b|\B)
([gimy]+\b|\B)
)x, Str::Regex, :pop!

rule(//) { pop! }
end

state :root do
rule(%r(^(?=\s|/|<!--))) { push :slash_starts_regex }
mixin :comments_and_whitespace
mixin :comments
mixin :whitespace

rule %r(
[+][+]|--|~|&&|\band\b|\bor\b|\bis\b|\bisnt\b|\bnot\b|\bin\b|\bof\b|
[?]|:|=|[|][|]|\\(?=\n)|(<<|>>>?|==?|!=?|[-<>+*`%&|^/])=?
Expand Down Expand Up @@ -129,10 +152,19 @@ def self.builtins
rule %r/'/, Str, :sqs
end

state :code_escape do
rule %r(\\(
c[A-Z]|
x[0-9a-fA-F]{2}|
u[0-9a-fA-F]{4}|
u\{[0-9a-fA-F]{4}\}
))x, Str::Escape
end

state :strings do
# all coffeescript strings are multi-line
rule %r/[^#\\'"]+/m, Str

mixin :code_escape
rule %r/\\./, Str::Escape
rule %r/#/, Str
end
Expand Down

0 comments on commit 3d7ee49

Please sign in to comment.