Skip to content

Commit fb15342

Browse files
author
jneen
committed
use the keywords api in cfscript
1 parent 2692ea8 commit fb15342

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ Performance/StringInclude:
5959
Rouge/NoBuildingAlternationPatternInRegexp:
6060
Exclude:
6161
- 'lib/rouge/lexers/armasm.rb'
62-
- 'lib/rouge/lexers/cfscript.rb'
6362
- 'lib/rouge/lexers/cmhg.rb'
6463
- 'lib/rouge/lexers/console.rb'
6564
- 'lib/rouge/lexers/crystal.rb'

lib/rouge/lexers/cfscript.rb

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,44 @@ class Cfscript < RegexLexer
1212
filenames '*.cfc'
1313

1414
def self.keywords
15-
@keywords ||= %w(
15+
@keywords ||= Set.new %w(
1616
if else var xml default break switch do try catch throw in continue for return while required
1717
)
1818
end
1919

2020
def self.declarations
21-
@declarations ||= %w(
21+
@declarations ||= Set.new %w(
2222
component property function remote public package private
2323
)
2424
end
2525

2626
def self.types
27-
@types ||= %w(
27+
@types ||= Set.new %w(
2828
any array binary boolean component date guid numeric query string struct uuid void xml
2929
)
3030
end
3131

32-
constants = %w(application session client cookie super this variables arguments cgi)
32+
CONSTANTS = Set.new %w(
33+
application session client cookie super this variables arguments cgi
34+
)
3335

3436

35-
operators = %w(\+\+ -- && \|\| <= >= < > == != mod eq lt gt lte gte not is and or xor eqv imp equal contains \? )
3637
dotted_id = /[$a-zA-Z_][a-zA-Z0-9_.]*/
3738

3839
state :root do
3940
mixin :comments_and_whitespace
40-
rule %r/(?:#{operators.join('|')}|does not contain|greater than(?: or equal to)?|less than(?: or equal to)?)\b/i, Operator, :expr_start
41+
42+
rule %r(
43+
[+][+] | -- | [|][|] | <= | >= | == | !=
44+
| [<>?]
45+
| (?:
46+
mod | eq | gte? | lte? | not | is | and | x?or | eqv | imp | equal
47+
| contains
48+
| does\s+not\s+contain
49+
| (?:greater|less)\s+than(?:\s+or\s+equal\s+to)?
50+
)\b
51+
)x, Operator, :expr_start
52+
4153
rule %r([-<>+*%&|\^/!=]=?), Operator, :expr_start
4254

4355
rule %r/[(\[,]/, Punctuation, :expr_start
@@ -52,47 +64,41 @@ def self.types
5264

5365
rule %r/[{}]/, Punctuation, :statement
5466

55-
rule %r/(?:#{constants.join('|')})\b/, Name::Constant
67+
keywords %r/\w+/ do
68+
rule CONSTANTS, Name::Constant
69+
end
70+
5671
rule %r/(?:true|false|null)\b/, Keyword::Constant
5772
rule %r/import\b/, Keyword::Namespace, :import
5873
rule %r/(#{dotted_id})(\s*)(:)(\s*)/ do
5974
groups Name, Text, Punctuation, Text
6075
push :expr_start
6176
end
6277

63-
rule %r/([A-Za-z_$][\w.]*)(\s*)(\()/ do |m|
64-
if self.class.keywords.include? m[1]
65-
token Keyword, m[1]
66-
token Text, m[2]
67-
token Punctuation, m[3]
68-
else
69-
token Name::Function, m[1]
70-
token Text, m[2]
71-
token Punctuation, m[3]
78+
keywords %r/([a-z_$][\w.]*)(\s*)(\()/i do |m|
79+
group 1
80+
81+
rule :keywords do |m|
82+
groups Keyword, Text, Punctuation
7283
end
73-
end
7484

75-
rule dotted_id do |m|
76-
if self.class.declarations.include? m[0]
77-
token Keyword::Declaration
78-
push :expr_start
79-
elsif self.class.keywords.include? m[0]
80-
token Keyword
81-
push :expr_start
82-
elsif self.class.types.include? m[0]
83-
token Keyword::Type
84-
push :expr_start
85-
else
86-
token Name::Other
85+
default do |m|
86+
groups Name::Function, Text, Punctuation
8787
end
8888
end
8989

90+
keywords dotted_id do
91+
rule :declarations, Keyword::Declaration, :expr_start
92+
rule :keywords, Keyword, :expr_start
93+
rule :types, Keyword::Type, :expr_start
94+
default Name::Other
95+
end
96+
9097
rule %r/[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?/, Num::Float
9198
rule %r/0x[0-9a-fA-F]+/, Num::Hex
9299
rule %r/[0-9]+/, Num::Integer
93100
rule %r/"(\\\\|\\"|[^"])*"/, Str::Double
94101
rule %r/'(\\\\|\\'|[^'])*'/, Str::Single
95-
96102
end
97103

98104
# same as java, broken out

0 commit comments

Comments
 (0)