Skip to content

Commit 1c0ed65

Browse files
committed
refactor: Use sets for lists of keywords and remove redundant rules in block state
1 parent 012c29d commit 1c0ed65

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

lib/rouge/lexers/bicep.rb

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ class Bicep < Rouge::RegexLexer
77
title "Bicep"
88
desc 'Bicep is a domain-specific language (DSL) that uses declarative syntax to deploy Azure resources.'
99

10-
keywords = %w(
10+
def self.keywords
11+
@keywords ||= Set.new %w(
1112
resource module param var output targetScope dependsOn
1213
existing for in if else true false null
13-
)
14+
)
15+
end
1416

15-
datatypes = %w(array bool int object string)
17+
def self.datatypes
18+
@datatypes ||= Set.new %w(array bool int object string)
19+
end
1620

17-
functions = %w(
21+
def self.functions
22+
@functions ||= Set.new %w(
1823
any array concat contains empty first intersection items last length min max range skip
1924
take union dateTimeAdd utcNow deployment environment loadFileAsBase64 loadTextContent int
2025
json extensionResourceId getSecret list listKeys listKeyValue listAccountSas listSecrets
@@ -23,7 +28,8 @@ class Bicep < Rouge::RegexLexer
2328
endsWith format guid indexOf lastIndexOf length newGuid padLeft replace split startsWith
2429
string substring toLower toUpper trim uniqueString uri uriComponent uriComponentToString
2530
toObject
26-
)
31+
)
32+
end
2733

2834
operators = %w(+ - * / % < <= > >= == != && || !)
2935

@@ -38,14 +44,18 @@ class Bicep < Rouge::RegexLexer
3844
# Match numbers
3945
rule %r/\b\d+\b/, Num
4046

41-
# Match keywords
42-
rule %r/\b(#{keywords.join('|')})\b/, Keyword
43-
44-
# Match data types
45-
rule %r/\b(#{datatypes.join('|')})\b/, Keyword::Type
46-
47-
# Match functions
48-
rule %r/\b(#{functions.join('|')})\b/, Name::Function
47+
# Rules for sets of reserved keywords
48+
rule %r/\b\w+\b/ do |m|
49+
if self.class.keywords.include? m[0]
50+
token Keyword
51+
elsif self.class.datatypes.include? m[0]
52+
token Keyword::Type
53+
elsif self.class.functions.include? m[0]
54+
token Name::Function
55+
else
56+
token Name
57+
end
58+
end
4959

5060
# Match operators
5161
rule %r/#{operators.map { |o| Regexp.escape(o) }.join('|')}/, Operator
@@ -89,21 +99,9 @@ class Bicep < Rouge::RegexLexer
8999
# Match property names
90100
rule %r/\b([a-zA-Z_]\w*)\b(?=\s*:)/, Name::Property
91101

92-
# Match property values that are strings
93-
rule %r/(?<=[:]\s)('[^']*')/, Str, :string
94-
95-
# Match property values that are numbers
96-
rule %r/(?<=[:]\s)\b\d+\b/, Num
97-
98-
# Match property values that are keywords
99-
rule %r/\b(#{keywords.join('|')})\b(?=[,}])/, Keyword::Constant
100-
101102
# Match closing curly brackets
102103
rule %r/}/, Punctuation::Indicator, :pop!
103104

104-
# Match nested curly brackets
105-
rule %r/{/, Punctuation::Indicator, :block
106-
107105
# Include the root state for nested tokens
108106
mixin :root
109107
end

spec/visual/samples/bicep

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ var someTags = [
1717
key: 'location'
1818
value: location
1919
}
20+
{
21+
key: 'isTest'
22+
value: true
23+
}
2024
]
2125

2226
// Create a resource group

0 commit comments

Comments
 (0)