Skip to content

Commit

Permalink
Update HCL & Terraform Lexers (#1975)
Browse files Browse the repository at this point in the history
* HCL/Terraform Lexer Update

* Update lib/rouge/lexers/hcl.rb

Co-authored-by: Tan Le <numbat@fastmail.com>

---------

Co-authored-by: Tan Le <numbat@fastmail.com>
  • Loading branch information
X-Guardian and tancnle committed Jul 25, 2023
1 parent e87eb9e commit 1bf355c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 11 deletions.
5 changes: 3 additions & 2 deletions lib/rouge/lexers/hcl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def self.builtins
@builtins ||= %w()
end

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

state :root do
mixin :comments_and_whitespace
Expand Down Expand Up @@ -114,6 +114,7 @@ def self.builtins
state :hash do
mixin :comments_and_whitespace

rule %r/[.,()\\\/*]/, Punctuation
rule %r/\=/, Punctuation
rule %r/\}/, Punctuation, :pop!

Expand All @@ -123,7 +124,7 @@ def self.builtins
state :array do
mixin :comments_and_whitespace

rule %r/,/, Punctuation
rule %r/[.,()\\\/*]/, Punctuation
rule %r/\]/, Punctuation, :pop!

mixin :root
Expand Down
8 changes: 0 additions & 8 deletions lib/rouge/lexers/terraform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ def self.builtins
@builtins ||= %w()
end

prepend :hash do
rule %r/[.,()*]/, Punctuation
end

prepend :array do
rule %r/[.,()*]/, Punctuation
end

state :strings do
rule %r/\\./, Str::Escape
rule %r/\$\{/ do
Expand Down
87 changes: 86 additions & 1 deletion spec/visual/samples/hcl
Original file line number Diff line number Diff line change
@@ -1 +1,86 @@
# See Terraform lexer
# Comment

# Object with no key/value pairs
data "aws_availability_zones" "available" {}

# Object with single key/value
provider "aws" {
most_recent = false
}

# Object with various comma-separated values
provider "aws" {
most_recent = false, other_value = null
}

# Object with various newline-separated values
resource "aws_vpc" "main" {
most_recent = true
cidr_block = "10.10.0.0/16"
region = "${var.aws_region}"
count = 0
another_num = 3.14
}

# Object with interpolated values
resource "aws_subnet" "main" {
count = "${var.az_count}"
cidr_block = "${cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)}"
availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
vpc_id = "${aws_vpc.main.id}"
}

# Object with nested object
resource "aws_route_table" "r" {
vpc_id = "${aws_vpc.main.id}"

route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}
}

# Object with list value
resource "aws_autoscaling_group" "app" {
vpc_zone_identifier = ["${aws_subnet.main.*.id}"]
}

# Object with nested interpolation
data "template_file" "cloud_config" {
template = "${file("${path.module}/cloud-config.yml")}"
}

# Object with HEREDOC string
resource "aws_iam_role" "ecs_service" {
name = "tf_example_ecs_role"

assume_role_policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ecs.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

## Object with first-class expressions
resource "aws_subnet" "main" {
count = var.az_count
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)
availability_zone = data.aws_availability_zones.available.names[count.index]
vpc_id = aws_vpc.main.id
fqns = aws_route53_record.cert_validation[*].fqdn
}

## Object with regular expression
resource "aws_cloudfront_distribution" "s3_distribution" {
aliases = ["www.${replace(var.domain_name, "/\\.$/", "")}"]
}

0 comments on commit 1bf355c

Please sign in to comment.