Skip to content

Commit

Permalink
refactor: Use sets for lists of keywords and remove redundant rules i…
Browse files Browse the repository at this point in the history
…n block state
  • Loading branch information
xaviermignot committed Oct 27, 2024
1 parent 012c29d commit 1c0ed65
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
48 changes: 23 additions & 25 deletions lib/rouge/lexers/bicep.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ class Bicep < Rouge::RegexLexer
title "Bicep"
desc 'Bicep is a domain-specific language (DSL) that uses declarative syntax to deploy Azure resources.'

keywords = %w(
def self.keywords
@keywords ||= Set.new %w(
resource module param var output targetScope dependsOn
existing for in if else true false null
)
)
end

datatypes = %w(array bool int object string)
def self.datatypes
@datatypes ||= Set.new %w(array bool int object string)
end

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

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

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

# Match keywords
rule %r/\b(#{keywords.join('|')})\b/, Keyword

# Match data types
rule %r/\b(#{datatypes.join('|')})\b/, Keyword::Type

# Match functions
rule %r/\b(#{functions.join('|')})\b/, Name::Function
# Rules for sets of reserved keywords
rule %r/\b\w+\b/ do |m|
if self.class.keywords.include? m[0]
token Keyword
elsif self.class.datatypes.include? m[0]
token Keyword::Type
elsif self.class.functions.include? m[0]
token Name::Function
else
token Name
end
end

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

# Match property values that are strings
rule %r/(?<=[:]\s)('[^']*')/, Str, :string

# Match property values that are numbers
rule %r/(?<=[:]\s)\b\d+\b/, Num

# Match property values that are keywords
rule %r/\b(#{keywords.join('|')})\b(?=[,}])/, Keyword::Constant

# Match closing curly brackets
rule %r/}/, Punctuation::Indicator, :pop!

# Match nested curly brackets
rule %r/{/, Punctuation::Indicator, :block

# Include the root state for nested tokens
mixin :root
end
Expand Down
4 changes: 4 additions & 0 deletions spec/visual/samples/bicep
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ var someTags = [
key: 'location'
value: location
}
{
key: 'isTest'
value: true
}
]

// Create a resource group
Expand Down

0 comments on commit 1c0ed65

Please sign in to comment.