Skip to content

Commit

Permalink
Bicep: Resolved TODO + improvements (#3028)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Aug 5, 2021
1 parent b0365e7 commit 748bb9a
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 46 deletions.
64 changes: 56 additions & 8 deletions components/prism-bicep.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,64 @@ Prism.languages.bicep = {
greedy: true
}
],
'string': {
// this doesn't handle string interpolalation or multiline strings as yet
pattern: /'(?:\\.|[^'\\\r\n])*'/,
greedy: true

'property': [
{
pattern: /([\r\n][ \t]*)[a-z_]\w*(?=[ \t]*:)/i,
lookbehind: true
},
{
pattern: /([\r\n][ \t]*)'(?:\\.|\$(?!\{)|[^'\\\r\n$])*'(?=[ \t]*:)/,
lookbehind: true,
greedy: true
}
],
'string': [
{
pattern: /'''[^'][\s\S]*?'''/,
greedy: true
},
{
pattern: /(^|[^\\'])'(?:\\.|\$(?!\{)|[^'\\\r\n$])*'/,
lookbehind: true,
greedy: true,
}
],
'interpolated-string': {
pattern: /(^|[^\\'])'(?:\\.|\$(?:(?!\{)|\{[^{}\r\n]*\})|[^'\\\r\n$])*'/,
lookbehind: true,
greedy: true,
inside: {
'interpolation': {
pattern: /\$\{[^{}\r\n]*\}/,
inside: {
'expression': {
pattern: /(^\$\{)[\s\S]+(?=\}$)/,
lookbehind: true
},
'punctuation': /^\$\{|\}$/,
}
},
'string': /[\s\S]+/
}
},
'number': /(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:E[+-]?\d+)?/i,

'datatype': {
pattern: /(\b(?:output|param)\b[ \t]+\w+[ \t]+)\w+\b/,
lookbehind: true,
alias: 'class-name'
},

'boolean': /\b(?:true|false)\b/,
'keyword': /\b(?:targetScope|resource|module|param|var|output|for|in|if|existing|null)\b/, // https://github.com/Azure/bicep/blob/114a3251b4e6e30082a58729f19a8cc4e374ffa6/src/textmate/bicep.tmlanguage#L184
'function': /\b(?:array|concat|contains|createArray|empty|first|intersection|last|length|max|min|range|skip|take|union)(?:\$|\b)/, // https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-array
// https://github.com/Azure/bicep/blob/114a3251b4e6e30082a58729f19a8cc4e374ffa6/src/textmate/bicep.tmlanguage#L184
'keyword': /\b(?:targetScope|resource|module|param|var|output|for|in|if|existing|null)\b/,

'decorator': /@\w+\b/,
'function': /\b[a-z_]\w*(?=[ \t]*\()/i,

'number': /(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:E[+-]?\d+)?/i,
'operator': /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/,
'punctuation': /[{}[\];(),.:]/,
'decorator': /@\w+\b/
};

Prism.languages.bicep['interpolated-string'].inside['interpolation'].inside['expression'].inside = Prism.languages.bicep;
2 changes: 1 addition & 1 deletion components/prism-bicep.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/languages/bicep/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// this is a comment

/* this
is a
multi line comment */

/* so is
this */

----------------------------------------------------

[
["comment", "// this is a comment"],

["comment", "/* this\r\nis a\r\nmulti line comment */"],

["comment", "/* so is\r\nthis */"]
]
47 changes: 47 additions & 0 deletions tests/languages/bicep/datatype_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
param myString string
param myInt int
param myBool bool
param myObject object
param myArray array

output myHardcodedOutput int = 42
output myLoopyOutput array = [for myItem in myArray: {
myProperty: myItem.myOtherProperty
}]

----------------------------------------------------

[
["keyword", "param"], " myString ", ["datatype", "string"],
["keyword", "param"], " myInt ", ["datatype", "int"],
["keyword", "param"], " myBool ", ["datatype", "bool"],
["keyword", "param"], " myObject ", ["datatype", "object"],
["keyword", "param"], " myArray ", ["datatype", "array"],

["keyword", "output"],
" myHardcodedOutput ",
["datatype", "int"],
["operator", "="],
["number", "42"],

["keyword", "output"],
" myLoopyOutput ",
["datatype", "array"],
["operator", "="],
["punctuation", "["],
["keyword", "for"],
" myItem ",
["keyword", "in"],
" myArray",
["operator", ":"],
["punctuation", "{"],

["property", "myProperty"],
["operator", ":"],
" myItem",
["punctuation", "."],
"myOtherProperty\r\n",

["punctuation", "}"],
["punctuation", "]"]
]
42 changes: 40 additions & 2 deletions tests/languages/bicep/decorator_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,54 @@ param demoPassword string
])
param demoEnum string

@minLength(3)
@maxLength(24)
@description('Name of the storage account')
param storageAccountName string = concat(uniqueString(resourceGroup().id), 'sa')

----------------------------------------------------

[
["decorator", "@secure"], ["punctuation", "("], ["punctuation", ")"],
["keyword", "param"], " demoPassword string\r\n",
["keyword", "param"], " demoPassword ", ["datatype", "string"],
["decorator", "@allowed"], ["punctuation", "("], ["punctuation", "["],
["string", "'one'"],
["string", "'two'"],
["punctuation", "]"], ["punctuation", ")"],
["keyword", "param"], " demoEnum string"
["keyword", "param"], " demoEnum ", ["datatype", "string"],

["decorator", "@minLength"],
["punctuation", "("],
["number", "3"],
["punctuation", ")"],

["decorator", "@maxLength"],
["punctuation", "("],
["number", "24"],
["punctuation", ")"],

["decorator", "@description"],
["punctuation", "("],
["string", "'Name of the storage account'"],
["punctuation", ")"],

["keyword", "param"],
" storageAccountName ",
["datatype", "string"],
["operator", "="],
["function", "concat"],
["punctuation", "("],
["function", "uniqueString"],
["punctuation", "("],
["function", "resourceGroup"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "."],
"id",
["punctuation", ")"],
["punctuation", ","],
["string", "'sa'"],
["punctuation", ")"]
]

----------------------------------------------------
Expand Down
79 changes: 51 additions & 28 deletions tests/languages/bicep/function_feature.test
Original file line number Diff line number Diff line change
@@ -1,37 +1,60 @@
array
concat
contains
createArray
empty
first
intersection
last
length
max
min
range
skip
take
union
param location string = resourceGroup().location
var hostingPlanName = 'hostingplan${uniqueString(resourceGroup().id)}'

array(parameters('stringToConvert'))
createArray(1, 2, 3)

----------------------------------------------------

[
["keyword", "param"],
" location ",
["datatype", "string"],
["operator", "="],
["function", "resourceGroup"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "."],
"location\r\n",

["keyword", "var"],
" hostingPlanName ",
["operator", "="],
["interpolated-string", [
["string", "'hostingplan"],
["interpolation", [
["punctuation", "${"],
["expression", [
["function", "uniqueString"],
["punctuation", "("],
["function", "resourceGroup"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "."],
"id",
["punctuation", ")"]
]],
["punctuation", "}"]
]],
["string", "'"]
]],

["function", "array"],
["function", "concat"],
["function", "contains"],
["punctuation", "("],
["function", "parameters"],
["punctuation", "("],
["string", "'stringToConvert'"],
["punctuation", ")"],
["punctuation", ")"],

["function", "createArray"],
["function", "empty"],
["function", "first"],
["function", "intersection"],
["function", "last"],
["function", "length"],
["function", "max"],
["function", "min"],
["function", "range"],
["function", "skip"],
["function", "take"],
["function", "union"]
["punctuation", "("],
["number", "1"],
["punctuation", ","],
["number", "2"],
["punctuation", ","],
["number", "3"],
["punctuation", ")"]
]

----------------------------------------------------
Expand Down
12 changes: 7 additions & 5 deletions tests/languages/bicep/keyword_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ null
["punctuation", ")"],
["punctuation", "{"],

"\r\n name",
["property", "name"],
["operator", ":"],
" logAnalyticsWsName\r\n",

Expand All @@ -43,22 +43,24 @@ null
["operator", "="],
["punctuation", "{"],

"\r\n name",
["property", "name"],
["operator", ":"],
["string", "'cosmosDbDeploy'"],

["punctuation", "}"],

["keyword", "param"],
" env string\r\n",
" env ",
["datatype", "string"],

["keyword", "var"],
" oneNumber ",
["operator", "="],
["number", "123"],

["keyword", "output"],
" databaseName string ",
" databaseName ",
["datatype", "string"],
["operator", "="],
" cosmosdbDatabaseName\r\n",

Expand All @@ -69,7 +71,7 @@ null
["operator", ":"],
["punctuation", "{"],

"\r\n\tipAddressOrRange",
["property", "ipAddressOrRange"],
["operator", ":"],
" item\r\n",

Expand Down
Loading

0 comments on commit 748bb9a

Please sign in to comment.