From 1c0ed6549010a537eabcc3501aa7cfe9db168fc4 Mon Sep 17 00:00:00 2001 From: Xavier Mignot Date: Sun, 26 Mar 2023 14:24:10 -0400 Subject: [PATCH] refactor: Use sets for lists of keywords and remove redundant rules in block state --- lib/rouge/lexers/bicep.rb | 48 +++++++++++++++++++-------------------- spec/visual/samples/bicep | 4 ++++ 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/lib/rouge/lexers/bicep.rb b/lib/rouge/lexers/bicep.rb index 2ee1f1df91..5b37fceac8 100644 --- a/lib/rouge/lexers/bicep.rb +++ b/lib/rouge/lexers/bicep.rb @@ -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 @@ -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(+ - * / % < <= > >= == != && || !) @@ -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 @@ -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 diff --git a/spec/visual/samples/bicep b/spec/visual/samples/bicep index ae1f455b44..e406acf15c 100644 --- a/spec/visual/samples/bicep +++ b/spec/visual/samples/bicep @@ -17,6 +17,10 @@ var someTags = [ key: 'location' value: location } + { + key: 'isTest' + value: true + } ] // Create a resource group