Skip to content

Commit 1bf355c

Browse files
X-Guardiantancnle
andauthored
Update HCL & Terraform Lexers (#1975)
* HCL/Terraform Lexer Update * Update lib/rouge/lexers/hcl.rb Co-authored-by: Tan Le <[email protected]> --------- Co-authored-by: Tan Le <[email protected]>
1 parent e87eb9e commit 1bf355c

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed

lib/rouge/lexers/hcl.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def self.builtins
5656
@builtins ||= %w()
5757
end
5858

59-
id = /[$a-z_][a-z0-9_]*/io
59+
id = /[$a-z_\-][a-z0-9_\-]*/io
6060

6161
state :root do
6262
mixin :comments_and_whitespace
@@ -114,6 +114,7 @@ def self.builtins
114114
state :hash do
115115
mixin :comments_and_whitespace
116116

117+
rule %r/[.,()\\\/*]/, Punctuation
117118
rule %r/\=/, Punctuation
118119
rule %r/\}/, Punctuation, :pop!
119120

@@ -123,7 +124,7 @@ def self.builtins
123124
state :array do
124125
mixin :comments_and_whitespace
125126

126-
rule %r/,/, Punctuation
127+
rule %r/[.,()\\\/*]/, Punctuation
127128
rule %r/\]/, Punctuation, :pop!
128129

129130
mixin :root

lib/rouge/lexers/terraform.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ def self.builtins
3737
@builtins ||= %w()
3838
end
3939

40-
prepend :hash do
41-
rule %r/[.,()*]/, Punctuation
42-
end
43-
44-
prepend :array do
45-
rule %r/[.,()*]/, Punctuation
46-
end
47-
4840
state :strings do
4941
rule %r/\\./, Str::Escape
5042
rule %r/\$\{/ do

spec/visual/samples/hcl

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,86 @@
1-
# See Terraform lexer
1+
# Comment
2+
3+
# Object with no key/value pairs
4+
data "aws_availability_zones" "available" {}
5+
6+
# Object with single key/value
7+
provider "aws" {
8+
most_recent = false
9+
}
10+
11+
# Object with various comma-separated values
12+
provider "aws" {
13+
most_recent = false, other_value = null
14+
}
15+
16+
# Object with various newline-separated values
17+
resource "aws_vpc" "main" {
18+
most_recent = true
19+
cidr_block = "10.10.0.0/16"
20+
region = "${var.aws_region}"
21+
count = 0
22+
another_num = 3.14
23+
}
24+
25+
# Object with interpolated values
26+
resource "aws_subnet" "main" {
27+
count = "${var.az_count}"
28+
cidr_block = "${cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)}"
29+
availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
30+
vpc_id = "${aws_vpc.main.id}"
31+
}
32+
33+
# Object with nested object
34+
resource "aws_route_table" "r" {
35+
vpc_id = "${aws_vpc.main.id}"
36+
37+
route {
38+
cidr_block = "0.0.0.0/0"
39+
gateway_id = "${aws_internet_gateway.gw.id}"
40+
}
41+
}
42+
43+
# Object with list value
44+
resource "aws_autoscaling_group" "app" {
45+
vpc_zone_identifier = ["${aws_subnet.main.*.id}"]
46+
}
47+
48+
# Object with nested interpolation
49+
data "template_file" "cloud_config" {
50+
template = "${file("${path.module}/cloud-config.yml")}"
51+
}
52+
53+
# Object with HEREDOC string
54+
resource "aws_iam_role" "ecs_service" {
55+
name = "tf_example_ecs_role"
56+
57+
assume_role_policy = <<EOF
58+
{
59+
"Version": "2008-10-17",
60+
"Statement": [
61+
{
62+
"Sid": "",
63+
"Effect": "Allow",
64+
"Principal": {
65+
"Service": "ecs.amazonaws.com"
66+
},
67+
"Action": "sts:AssumeRole"
68+
}
69+
]
70+
}
71+
EOF
72+
}
73+
74+
## Object with first-class expressions
75+
resource "aws_subnet" "main" {
76+
count = var.az_count
77+
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)
78+
availability_zone = data.aws_availability_zones.available.names[count.index]
79+
vpc_id = aws_vpc.main.id
80+
fqns = aws_route53_record.cert_validation[*].fqdn
81+
}
82+
83+
## Object with regular expression
84+
resource "aws_cloudfront_distribution" "s3_distribution" {
85+
aliases = ["www.${replace(var.domain_name, "/\\.$/", "")}"]
86+
}

0 commit comments

Comments
 (0)