Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform (HCL) (*.tf): new parser #3683

Closed
wants to merge 14 commits into from
Closed
1 change: 1 addition & 0 deletions Units/parser-terraform.r/data.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example_database_password input.tf /^data "aws_ssm_parameter" "example_database_password" {$/;" d
3 changes: 3 additions & 0 deletions Units/parser-terraform.r/data.d/input.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "aws_ssm_parameter" "example_database_password" {
name = "example-database-password"
}
1 change: 1 addition & 0 deletions Units/parser-terraform.r/module.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
database input.tf /^module "database" {$/;" m
3 changes: 3 additions & 0 deletions Units/parser-terraform.r/module.d/input.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module "database" {
source = "../../modules/database"
}
1 change: 1 addition & 0 deletions Units/parser-terraform.r/output.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
password input.tf /^output "password" {$/;" o
3 changes: 3 additions & 0 deletions Units/parser-terraform.r/output.d/input.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "password" {
value = data.aws_ssm_parameter.example_database_password.value
}
1 change: 1 addition & 0 deletions Units/parser-terraform.r/provider.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aws input.tf /^provider "aws" {$/;" p
3 changes: 3 additions & 0 deletions Units/parser-terraform.r/provider.d/input.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "aws" {
region = "us-east-1"
}
1 change: 1 addition & 0 deletions Units/parser-terraform.r/resource.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example_events_bus input.tf /^resource "aws_cloudwatch_event_bus" "example_events_bus" {$/;" r
3 changes: 3 additions & 0 deletions Units/parser-terraform.r/resource.d/input.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_cloudwatch_event_bus" "example_events_bus" {
name = var.events_bus_name
}
1 change: 1 addition & 0 deletions Units/parser-terraform.r/simple-terraform.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--sort=no
6 changes: 6 additions & 0 deletions Units/parser-terraform.r/simple-terraform.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
aws input.tf /^provider "aws" {$/;" p
events_bus_name input.tf /^variable "events_bus_name" {$/;" v
example_events_bus input.tf /^resource "aws_cloudwatch_event_bus" "example_events_bus" {$/;" r
database input.tf /^module "database" {$/;" m
example_database_password input.tf /^data "aws_ssm_parameter" "example_database_password" {$/;" d
password input.tf /^output "password" {$/;" o
24 changes: 24 additions & 0 deletions Units/parser-terraform.r/simple-terraform.d/input.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
provider "aws" {
region = "us-east-1"
}

variable "events_bus_name" {
type = string
default = "hello-world"
}

resource "aws_cloudwatch_event_bus" "example_events_bus" {
name = var.events_bus_name
}

module "database" {
source = "../../modules/database"
}

data "aws_ssm_parameter" "example_database_password" {
name = "example-database-password"
}

output "password" {
value = data.aws_ssm_parameter.example_database_password.value
}
1 change: 1 addition & 0 deletions Units/parser-terraform.r/variable.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
events_bus_name input.tf /^variable "events_bus_name" {$/;" v
4 changes: 4 additions & 0 deletions Units/parser-terraform.r/variable.d/input.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "events_bus_name" {
type = string
default = "hello-world"
}
1 change: 1 addition & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ The following parsers have been added:
* SVG *libxml*
* TclOO (see :ref:`The new Tcl parser <tcl>`)
* Thrift *peg/packcc*
* Terraform (HCL) *optlib*
* TTCN
* Txt2tags
* TypeScript
Expand Down
1 change: 1 addition & 0 deletions main/parsers_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
SystemTapParser, \
TclParser, \
TclOOParser, \
TerraformParser, \
TexParser, \
TexBeamerParser, \
TTCNParser, \
Expand Down
85 changes: 85 additions & 0 deletions optlib/terraform.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Generated by ./misc/optlib2c from optlib/terraform.ctags, Don't edit this manually.
*/
#include "general.h"
#include "parse.h"
#include "routines.h"
#include "field.h"
#include "xtag.h"


static void initializeTerraformParser (const langType language CTAGS_ATTR_UNUSED)
{
}

extern parserDefinition* TerraformParser (void)
{
static const char *const extensions [] = {
"tf",
"tfvars",
NULL
};

static const char *const aliases [] = {
NULL
};

static const char *const patterns [] = {
NULL
};

static kindDefinition TerraformKindTable [] = {
{
true, 'r', "Resource", "Terraform Resource",
},
{
true, 'd', "Data", "Terraform Data",
},
{
true, 'v', "Variable", "Terraform Variable",
},
{
true, 'p', "Provider", "Terraform Provider",
},
{
true, 'm', "Module", "Terraform Module",
},
{
true, 'o', "Output", "Terraform Output",
},
};
static tagRegexTable TerraformTagRegexTable [] = {
{"^resource[[:space:]]\"([^\"]+)\"[[:space:]]\"([^\"]+)\"", "\\2",
"r", NULL, NULL, false},
{"^data[[:space:]]\"([^\"]+)\"[[:space:]]\"([^\"]+)\"", "\\2",
"d", NULL, NULL, false},
{"^variable[[:space:]]\"([^\"]+)\"", "\\1",
"v", NULL, NULL, false},
{"^provider[[:space:]]\"([^\"]+)\"", "\\1",
"p", NULL, NULL, false},
{"^module[[:space:]]\"([^\"]+)\"", "\\1",
"m", NULL, NULL, false},
{"^output[[:space:]]\"([^\"]+)\"", "\\1",
"o", NULL, NULL, false},
{"^([a-z0-9_]+)[[:space:]]*=", "\\1",
"f,TFVar", NULL, NULL, false},
};


parserDefinition* const def = parserNew ("Terraform");

def->versionCurrent= 0;
def->versionAge = 0;
def->enabled = true;
def->extensions = extensions;
def->patterns = patterns;
def->aliases = aliases;
def->method = METHOD_NOT_CRAFTED|METHOD_REGEX;
def->kindTable = TerraformKindTable;
def->kindCount = ARRAY_SIZE(TerraformKindTable);
def->tagRegexTable = TerraformTagRegexTable;
def->tagRegexCount = ARRAY_SIZE(TerraformTagRegexTable);
def->initialize = initializeTerraformParser;

return def;
}
35 changes: 35 additions & 0 deletions optlib/terraform.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# terraform.ctags: regex parse for Terraform (HCL).
#
# Author: Antony Southworth <https://github.com/antonysouthworth-halter>
#
# This source code is released for free distribution under the terms of the
# GNU General Public License version 2 or (at your option) any later version.
#
# Derived from `vim-terraform-completion`:
# - https://github.com/juliosueiras/vim-terraform-completion/blob/master/ctags/terraform.ctags
#
# Changed the name from `terraform` to `tf` so vim will recognise it properly based
# on file extension (*.tf).
#
# A notable abscence is `local`, because `locals` are defined inside a block and
# it's way harder to write a good regex for that.
# - https://www.terraform.io/docs/language/values/locals.html

--langdef=Terraform
--map-Terraform=+.tf
--map-Terraform=+.tfvars
--kinddef-Terraform=r,Resource,Terraform Resource
--kinddef-Terraform=d,Data,Terraform Data
--kinddef-Terraform=v,Variable,Terraform Variable
--kinddef-Terraform=p,Provider,Terraform Provider
--kinddef-Terraform=m,Module,Terraform Module
--kinddef-Terraform=o,Output,Terraform Output
--regex-Terraform=/^resource[[:space:]]"([^"]+)"[[:space:]]"([^"]+)"/\2/r/
--regex-Terraform=/^data[[:space:]]"([^"]+)"[[:space:]]"([^"]+)"/\2/d/
--regex-Terraform=/^variable[[:space:]]"([^"]+)"/\1/v/
--regex-Terraform=/^provider[[:space:]]"([^"]+)"/\1/p/
--regex-Terraform=/^module[[:space:]]"([^"]+)"/\1/m/
--regex-Terraform=/^output[[:space:]]"([^"]+)"/\1/o/
--regex-Terraform=/^([a-z0-9_]+)[[:space:]]*=/\1/f,TFVar/

1 change: 1 addition & 0 deletions source.mak
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ OPTLIB2C_INPUT = \
optlib/rpmMacros.ctags \
optlib/scss.ctags \
optlib/systemtap.ctags \
optlib/terraform.ctags \
optlib/yacc.ctags \
\
$(NULL)
Expand Down
1 change: 1 addition & 0 deletions win32/ctags_vs2013.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
<ClCompile Include="..\optlib\rpmMacros.c" />
<ClCompile Include="..\optlib\scss.c" />
<ClCompile Include="..\optlib\systemtap.c" />
<ClCompile Include="..\optlib\terraform.c" />
<ClCompile Include="..\optlib\yacc.c" />
<ClCompile Include="..\parsers\abaqus.c" />
<ClCompile Include="..\parsers\abc.c" />
Expand Down
3 changes: 3 additions & 0 deletions win32/ctags_vs2013.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@
<ClCompile Include="..\optlib\systemtap.c">
<Filter>Source Files\optlib</Filter>
</ClCompile>
<ClCompile Include="..\optlib\terraform.c">
<Filter>Source Files\optlib</Filter>
</ClCompile>
<ClCompile Include="..\optlib\yacc.c">
<Filter>Source Files\optlib</Filter>
</ClCompile>
Expand Down