Skip to content

Commit

Permalink
move interpolation into string
Browse files Browse the repository at this point in the history
Signed-off-by: Outsider <outsideris@gmail.com>
  • Loading branch information
outsideris committed Dec 25, 2018
1 parent 65d5c83 commit 8022bed
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 114 deletions.
39 changes: 20 additions & 19 deletions components/prism-hcl.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,31 @@ Prism.languages.hcl = {
pattern: /[\w-]+(?=\s+{)/
}
],
'interpolation': {
pattern: /\${(?:[\w[\](+)\-*%^"',?=:/\s\\]+\.?)+}/i,
'property': [
/[\w-\.]+(?=\s*=(?!=))/,
/"(?:\\[\s\S]|[^\\"])+"(?=\s*[:=])/,
],
'string': {
pattern: /"(?:[^\\$"]|\\[\s\S]|\$(?:(?=")|\$+|[^"${])|\$\{(?:[^{}"]|"(?:[^\\"]|\\[\s\S])*")*\})*"/,
inside: {
'type': {
pattern: /((?:terraform|var|self|count|module|path|data|local)\.)[\w\*]+/i,
'interpolation': {
pattern: /(^|[^$])\$\{(?:[^{}"]|"(?:[^\\"]|\\[\s\S])*")*\}/,
lookbehind: true,
alias: 'variable'
inside: {
'type': {
pattern: /((?:terraform|var|self|count|module|path|data|local)\.)[\w\*]+/i,
lookbehind: true,
alias: 'variable'
},
'keyword': /terraform|var|self|count|module|path|data|local/ig,
'function': /\w+(?=\()/,
'string': /"(?:\\[\s\S]|[^\\"])*"/,
'punctuation': /[!\$#%&'()*+,.\/;<=>@\[\\\]^`{|}~?:]/,
'number': /-?\d+\.?\d*/,
}
},
'keyword': /terraform|var|self|count|module|path|data|local/i,
'function': /\w+(?=\()/,
'string': /"(?:\\[\s\S]|[^\\"])*"/,
'punctuation': /[!"\$#%&'()*+,.\/;<=>@\[\\\]^`{|}~]/,
'number': /-?\d+\.?\d*/,
'others': {
pattern: /[\w\*]+/,
alias: 'string'
}
}
},
'property': [
/[\w-\.]+(?=\s*=)/,
/"(?:\\[\s\S]|[^\\"])+"(?=\s*[:=])/,
],
'string': /"(?:\\[\s\S]|[^\\"])*"/,
'number': /0x[\da-f]+|\d+\.?\d*(?:e[+-]?\d+)?/i,
'boolean': /\b(?:true|false)\b/i,
'punctuation': /[=\[\]{}]/,
Expand Down
219 changes: 130 additions & 89 deletions tests/languages/hcl/interpolation_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,115 +9,156 @@
----------------------------------------------------

[
"\"",
["interpolation",
["string",
[
["punctuation", "$"],
["punctuation", "{"],
["keyword", "data"],
["punctuation", "."],
["type", "aws_availability_zones"],
["punctuation", "."],
["others", "available"],
["punctuation", "."],
["others", "names"],
["punctuation", "["],
["number", "0"],
["punctuation", "]"],
["punctuation", "}"]
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["keyword", "data"],
["punctuation", "."],
["type", "aws_availability_zones"],
["punctuation", "."],
"available",
["punctuation", "."],
"names",
["punctuation", "["],
["number", "0"],
["punctuation", "]"],
["punctuation", "}"]
]
],
"\""
]
],
["string", "\"\r\n\""],
["interpolation",
["string",
[
["punctuation", "$"],
["punctuation", "{"],
["others", "aws_db_subnet_group"],
["punctuation", "."],
["others", "default"],
["punctuation", "."],
["others", "id"],
["punctuation", "}"]
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
"aws_db_subnet_group",
["punctuation", "."],
"default",
["punctuation", "."],
"id",
["punctuation", "}"]
]
],
"\""
]
],
["string", "\"\r\n\""],
["interpolation",
["string",
[
["punctuation", "$"],
["punctuation", "{"],
["keyword", "var"],
["punctuation", "."],
["type", "something"],
" ? ",
["number", "1"],
" : ",
["number", "0"],
["punctuation", "}"]
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["keyword", "var"],
["punctuation", "."],
["type", "something"],
["punctuation", "?"],
["number", "1"],
["punctuation", ":"],
["number", "0"],
["punctuation", "}"]
]
],
"\""
]
],
["string", "\"\r\n\""],
["interpolation",
["string",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "length"],
["punctuation", "("],
["keyword", "var"],
["punctuation", "."],
["type", "hostnames"],
["punctuation", ")"],
["punctuation", "}"]
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "length"],
["punctuation", "("],
["keyword", "var"],
["punctuation", "."],
["type", "hostnames"],
["punctuation", ")"],
["punctuation", "}"]
]
],
"\""
]
],
["string", "\"\r\n\""],
["interpolation",
["string",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "format"],
["punctuation", "("],
["string", "\"web-%03d\""],
["punctuation", ","],
["keyword", "count"],
["punctuation", "."],
["type", "index"],
["punctuation", "+"],
["number", "1"],
["punctuation", ")"],
["punctuation", "}"]
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "format"],
["punctuation", "("],
["string", "\"web-%03d\""],
["punctuation", ","],
["keyword", "count"],
["punctuation", "."],
["type", "index"],
["punctuation", "+"],
["number", "1"],
["punctuation", ")"],
["punctuation", "}"]
]
],
"\""
]
],
["string", "\"\r\n\""],
["interpolation",
["string",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "file"],
["punctuation", "("],
["string", "\"templates/web_init.tpl\""],
["punctuation", ")"],
["punctuation", "}"]
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "file"],
["punctuation", "("],
["string", "\"templates/web_init.tpl\""],
["punctuation", ")"],
["punctuation", "}"]
]
],
"\""
]
],
["string", "\"\r\n\""],
["interpolation",
["string",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "replace"],
["punctuation", "("],
["keyword", "var"],
["punctuation", "."],
["type", "sub_domain"],
["punctuation", ","],
["string", "\".\""],
["punctuation", ","],
["string", "\"\\\\\\\\\\\\\\\\.\""],
["punctuation", ")"],
["punctuation", "}"]
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "replace"],
["punctuation", "("],
["keyword", "var"],
["punctuation", "."],
["type", "sub_domain"],
["punctuation", ","],
["string", "\".\""],
["punctuation", ","],
["string", "\"\\\\\\\\\\\\\\\\.\""],
["punctuation", ")"],
["punctuation", "}"]
]
],
"\""
]
],
"\""
]
]

----------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/languages/hcl/keyword_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ terraform {
["keyword",
["resource ",
["type", "\"aws_db_instance\""]]],
["string", "\"main\""],
["string", ["\"main\""]],
["punctuation", "{"],
["keyword",
["data ",
["type", "\"terraform_remote_state\""]]],
["string", "\"main\""],
["string", ["\"main\""]],
["punctuation", "{"],
["keyword",
["output",
Expand Down
4 changes: 2 additions & 2 deletions tests/languages/hcl/property_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ FOO = "bar"
[
["property", "foo"],
["punctuation", "="],
["string", "\"bar\""],
["string", ["\"bar\""]],
["property", "c"],
["punctuation", "="],
["number", "1e10"],
["property", "FOO"],
["punctuation", "="],
["string", "\"bar\""],
["string", ["\"bar\""]],
["property", "\"bool\""],
["punctuation", "="],
["punctuation", "["],
Expand Down
4 changes: 2 additions & 2 deletions tests/languages/hcl/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ multiline_literal = "hello
----------------------------------------------------

[
["string", "\"double quoted\\\"' % string\""],
["string", ["\"double quoted\\\"' % string\""]],
["property", "multiline_literal"],
["punctuation", "="],
["string", "\"hello\r\n world\""]
["string", ["\"hello\r\n world\""]]
]

----------------------------------------------------
Expand Down

0 comments on commit 8022bed

Please sign in to comment.