Skip to content

Commit

Permalink
Terraform: Fix class name lexing (#2097)
Browse files Browse the repository at this point in the history
* Merge unneeded state with it's parent rule

The `blockname` state did not have a `#pop` after it's only rule,
which caused outputting errors in #2094.

Instead of adding a `#pop`, I merged the `blockname` state with the
rule that uses it, to achieve the exact same lexing logic, but without
another state in the lexer.

* Add test for  #2094

* Regenerate tokens for previous tests
  • Loading branch information
amitkummer committed Mar 27, 2022
1 parent 3cc98c9 commit f06f6d6
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 25 deletions.
9 changes: 2 additions & 7 deletions pygments/lexers/configs.py
Expand Up @@ -720,20 +720,15 @@ def heredoc_callback(self, match, ctx):

# e.g. resource "aws_security_group" "allow_tls" {
# e.g. backend "consul" {
(classes_re + r'(\s+)', bygroups(Keyword.Reserved, Whitespace), 'blockname'),
(classes_re + r'(\s+)("[0-9a-zA-Z-_]+")?(\s*)("[0-9a-zA-Z-_]+")(\s+)(\{)',
bygroups(Keyword.Reserved, Whitespace, Name.Class, Whitespace, Name.Variable, Whitespace, Punctuation)),

# here-doc style delimited strings
(
r'(<<-?)\s*([a-zA-Z_]\w*)(.*?\n)',
heredoc_callback,
)
],
'blockname': [
# e.g. resource "aws_security_group" "allow_tls" {
# e.g. backend "consul" {
(r'(\s*)("[0-9a-zA-Z-_]+")?(\s*)("[0-9a-zA-Z-_]+")(\s+)(\{)',
bygroups(Whitespace, Name.Class, Whitespace, Name.Variable, Whitespace, Punctuation)),
],
'identifier': [
(r'\b(var\.[0-9a-zA-Z-_\.\[\]]+)\b', bygroups(Name.Variable)),
(r'\b([0-9a-zA-Z-_\[\]]+\.[0-9a-zA-Z-_\.\[\]]+)\b', bygroups(Name.Variable)),
Expand Down
2 changes: 1 addition & 1 deletion tests/snippets/terraform/test_backend.txt
Expand Up @@ -18,7 +18,7 @@ terraform {
'"consul"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

' ' Text.Whitespace
'address' Name.Attribute
Expand Down
20 changes: 20 additions & 0 deletions tests/snippets/terraform/test_comment.txt
Expand Up @@ -6,6 +6,9 @@
comment

*/
provider "azurerm" { # (1)
features {}
}

---tokens---
'# Single line comment\n' Comment.Single
Expand Down Expand Up @@ -42,3 +45,20 @@

'*/' Comment.Multiline
'\n' Text.Whitespace

'provider' Keyword.Reserved
' ' Text.Whitespace
'"azurerm"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
' # (1)\n' Comment.Single

' ' Text.Whitespace
'features' Name.Builtin
' ' Text.Whitespace
'{' Punctuation
'}' Punctuation
'\n' Text.Whitespace

'}' Punctuation
' \n' Text.Whitespace
4 changes: 2 additions & 2 deletions tests/snippets/terraform/test_functions.txt
Expand Up @@ -13,7 +13,7 @@ provider "aws" {
'"aws"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

' ' Text.Whitespace
'value' Name.Attribute
Expand All @@ -36,7 +36,7 @@ provider "aws" {
'"aws"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

' ' Text.Whitespace
'value' Name.Attribute
Expand Down
2 changes: 1 addition & 1 deletion tests/snippets/terraform/test_heredoc.txt
Expand Up @@ -13,7 +13,7 @@ resource "local_file" "heredoc" {
'"heredoc"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

' ' Text.Whitespace
'content' Name.Attribute
Expand Down
2 changes: 1 addition & 1 deletion tests/snippets/terraform/test_module.txt
Expand Up @@ -10,7 +10,7 @@ module "consul" {
'"consul"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

' ' Text.Whitespace
'source' Name.Attribute
Expand Down
4 changes: 2 additions & 2 deletions tests/snippets/terraform/test_resource.txt
Expand Up @@ -39,7 +39,7 @@ resource "aws_security_group" "allow_tls" {
'"base_igw"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

' ' Text.Whitespace
'vpc_id' Name.Attribute
Expand Down Expand Up @@ -81,7 +81,7 @@ resource "aws_security_group" "allow_tls" {
'"allow_tls"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

' ' Text.Whitespace
'name' Name.Attribute
Expand Down
18 changes: 9 additions & 9 deletions tests/snippets/terraform/test_types.txt
Expand Up @@ -17,7 +17,7 @@ variable "set-str" {
'"consul"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

'data' Keyword.Reserved
' ' Text.Whitespace
Expand All @@ -26,35 +26,35 @@ variable "set-str" {
'"example"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

'module' Keyword.Reserved
' ' Text.Whitespace
'"consul"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

'output' Keyword.Reserved
' ' Text.Whitespace
'"instance_ip_addr"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

'provider' Keyword.Reserved
' ' Text.Whitespace
'"aws"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

'provisioner' Keyword.Reserved
' ' Text.Whitespace
'"local-exec"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

'resource' Keyword.Reserved
' ' Text.Whitespace
Expand All @@ -63,21 +63,21 @@ variable "set-str" {
'"base_igw"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

'variable' Keyword.Reserved
' ' Text.Whitespace
'"aws_region"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

'variable' Keyword.Reserved
' ' Text.Whitespace
'"set-str"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

' ' Text.Whitespace
'type' Name.Attribute
Expand Down
2 changes: 1 addition & 1 deletion tests/snippets/terraform/test_variable_declaration.txt
Expand Up @@ -11,7 +11,7 @@ variable "aws_region" {
'"aws_region"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

' ' Text.Whitespace
'description' Name.Attribute
Expand Down
2 changes: 1 addition & 1 deletion tests/snippets/terraform/test_variable_read.txt
Expand Up @@ -9,7 +9,7 @@ provider "aws" {
'"aws"' Name.Variable
' ' Text.Whitespace
'{' Punctuation
'\n' Text
'\n' Text.Whitespace

' ' Text.Whitespace
'region' Name.Attribute
Expand Down

0 comments on commit f06f6d6

Please sign in to comment.