Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
Improve visual sample and macro support for Crystal lexer (rouge-ruby…
Browse files Browse the repository at this point in the history
…#1644)

The Crystal lexer does not include a very extensive visual sample. This
commit adds a sample based on the one included in the Pygments lexer. As
a result of testing with the sample, issues were identified with the
lexing of macros and the `{%` and `%}` conditionals. These were fixed.
  • Loading branch information
pyrmont authored and mattt committed May 19, 2021
1 parent 0ee3813 commit 8ace1f9
Show file tree
Hide file tree
Showing 2 changed files with 687 additions and 40 deletions.
21 changes: 13 additions & 8 deletions lib/rouge/lexers/crystal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def self.detect?(text)
)xi, Str::Symbol

# special symbols
rule %r(:(?:\*\*|[-+]@|[/\%&\|^`~]|\[\]=?|<<|>>|<=?>|<=?|===?)),
Str::Symbol
rule %r(:(?:===|=?~|\[\][=?]?|\*\*=?|\/\/=?|[=^*/+-]=?|&[&*+-]?=?|\|\|?=?|![=~]?|%=?|<=>|<<?=?|>>?=?|\.\.\.?)), Str::Symbol

rule %r/:'(\\\\|\\'|[^'])*'/, Str::Symbol
rule %r/:"/, Str::Symbol, :simple_sym
Expand All @@ -36,7 +35,7 @@ def self.detect?(text)
# %-sigiled strings
# %(abc), %[abc], %<abc>, %.abc., %r.abc., etc
delimiter_map = { '{' => '}', '[' => ']', '(' => ')', '<' => '>' }
rule %r/%([rqswQWxiI])?([^\w\s])/ do |m|
rule %r/%([rqswQWxiI])?([^\w\s}])/ do |m|
open = Regexp.escape(m[2])
close = Regexp.escape(delimiter_map[m[2]] || m[2])
interp = /[rQWxI]/ === m[1]
Expand Down Expand Up @@ -79,9 +78,11 @@ def self.detect?(text)
state :strings do
mixin :symbols
rule %r/\b[a-z_]\w*?[?!]?:\s+/, Str::Symbol, :expr_start
rule %r/'(\\\\|\\'|[^'])*'/, Str::Single
rule %r/"/, Str::Double, :simple_string
rule %r/(?<!\.)`/, Str::Backtick, :simple_backtick
rule %r/(')(\\u[a-fA-F0-9]{4}|\\u\{[a-fA-F0-9]{1,6}\}|\\[abefnrtv])?(\\\\|\\'|[^'])*(')/ do
groups Str::Single, Str::Escape, Str::Single, Str::Single
end
end

state :regex_flags do
Expand Down Expand Up @@ -154,11 +155,13 @@ def self.detect?(text)
mixin :whitespace
rule %r/__END__/, Comment::Preproc, :end_part

rule %r/0_?[0-7]+(?:_[0-7]+)*/, Num::Oct
rule %r/0o[0-7]+(?:_[0-7]+)*/, Num::Oct
rule %r/0x[0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*/, Num::Hex
rule %r/0b[01]+(?:_[01]+)*/, Num::Bin
rule %r/\d+\.\d+(e[\+\-]?\d+)?/, Num::Float
rule %r/[\d]+(?:_\d+)*/, Num::Integer
rule %r/\d+\.\d+(e[\+\-]?\d+)?(_f32)?/i, Num::Float
rule %r/\d+(e[\+\-]?\d+)/i, Num::Float
rule %r/\d+_f32/i, Num::Float
rule %r/[\d]+(?:_\d+)*(_[iu]\d+)?/, Num::Integer

rule %r/@\[([^\]]+)\]/, Name::Decorator

Expand All @@ -183,7 +186,7 @@ def self.detect?(text)
groups Keyword, Text, Name::Namespace
end

rule %r/(def\b)(\s*)/ do
rule %r/(def|macro\b)(\s*)/ do
groups Keyword, Text
push :funcname
end
Expand Down Expand Up @@ -214,6 +217,7 @@ def self.detect?(text)
rule %r/[a-zA-Z_]\w*/, Name, :method_call
rule %r/\*\*|\/\/|>=|<=|<=>|<<?|>>?|=~|={3}|!~|&&?|\|\||\./,
Operator, :expr_start
rule %r/{%|%}/, Punctuation
rule %r/[-+\/*%=<>&!^|~]=?/, Operator, :expr_start
rule(/[?]/) { token Punctuation; push :ternary; push :expr_start }
rule %r<[\[({,:\\;/]>, Punctuation, :expr_start
Expand Down Expand Up @@ -346,6 +350,7 @@ def self.detect?(text)
mixin :string_intp
rule %r/\\([\\abefnrstv#"']|x[a-fA-F0-9]{1,2}|[0-7]{1,3})/,
Str::Escape
rule %r/\\u([a-fA-F0-9]{4}|\{[^}]+\})/, Str::Escape
rule %r/\\./, Str::Escape
end

Expand Down
Loading

0 comments on commit 8ace1f9

Please sign in to comment.