Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
Support regular expressions in Terraform lexer (rouge-ruby#1490)
Browse files Browse the repository at this point in the history
In Terraform, an interpolated expression can include a regular
expression of the form `"/.../"`. This commit adds rules that identify
regular expressions and a state for tokenising these expressions
correctly.
  • Loading branch information
pyrmont authored and mattt committed May 21, 2020
1 parent fc036ac commit 14a8ae2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/rouge/lexers/terraform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,24 @@ def self.builtins
mixin :expression
end

state :regexps do
rule %r/"\// do
token Str::Delimiter
goto :regexp_inner
end
end

state :regexp_inner do
rule %r/[^"\/\\]+/, Str::Regex
rule %r/\\./, Str::Regex
rule %r/\/"/, Str::Delimiter, :pop!
rule %r/["\/]/, Str::Regex
end

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

state :expression do
mixin :regexps
mixin :primitives
rule %r/\s+/, Text

Expand Down
5 changes: 5 additions & 0 deletions spec/visual/samples/terraform
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ resource "aws_subnet" "main" {
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 14a8ae2

Please sign in to comment.