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 #2952

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main/parsers_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
SystemTapParser, \
TclParser, \
TclOOParser, \
TfParser, \
TerraformParser, \
TexParser, \
TexBeamerParser, \
TTCNParser, \
Expand Down
39 changes: 31 additions & 8 deletions optlib/terraform.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
#include "xtag.h"


static void initializeTfParser (const langType language CTAGS_ATTR_UNUSED)
static void initializeTerraformParser (const langType language CTAGS_ATTR_UNUSED)
{
}

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

Expand All @@ -27,7 +28,27 @@ extern parserDefinition* TfParser (void)
NULL
};

static tagRegexTable TfTagRegexTable [] = {
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 [] = {
{"^[[:space:]]*resource[[:space:]]*\"([^\"]*)\"[[:space:]]*\"([^\"]*)\"", "\\2",
"r,Resource", NULL, NULL, false},
{"^[[:space:]]*data[[:space:]]*\"([^\"]*)\"[[:space:]]*\"([^\"]*)\"", "\\2",
Expand All @@ -45,16 +66,18 @@ extern parserDefinition* TfParser (void)
};


parserDefinition* const def = parserNew ("tf");
parserDefinition* const def = parserNew ("terraform");

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

return def;
}
25 changes: 16 additions & 9 deletions optlib/terraform.ctags
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@
# it's way harder to write a good regex for that.
# - https://www.terraform.io/docs/language/values/locals.html

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put a URL for the language reference?

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot writing one thing. A parser that is part of ctags should have Capitalized name.
Please, do s/terraform/Terraform/g.

--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=/^[[:space:]]*resource[[:space:]]*"([^"]*)"[[:space:]]*"([^"]*)"/\2/r,Resource/
--regex-terraform=/^[[:space:]]*data[[:space:]]*"([^"]*)"[[:space:]]*"([^"]*)"/\2/d,Data/
--regex-terraform=/^[[:space:]]*variable[[:space:]]*"([^"]*)"/\1/v,Variable/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v is already defined with --kinddef-.... So you don't have to specify v,Variable. Just v is enough:

--regex-Terraform=/^[[:space:]]*variable[[:space:]]*"([^"]*)"/\1/v/

I don't know well about the syntax of Terraforms. However, I guess a whitespace may be needed after variable keyword. We don't record a variable having an empty string as name. So + is better than *.

--regex-Terraform=/^[[:space:]]*variable[[:space:]]+"([^"]+)"/\1/v/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is applicable to other lines of --regex-....

--regex-terraform=/^[[:space:]]*provider[[:space:]]*"([^"]*)"/\1/p,Provider/
--regex-terraform=/^[[:space:]]*module[[:space:]]*"([^"]*)"/\1/m,Module/
--regex-terraform=/^[[:space:]]*output[[:space:]]*"([^"]*)"/\1/o,Output/
--regex-terraform=/^([a-z0-9_]+)[[:space:]]*=/\1/f,TFVar/

2 changes: 1 addition & 1 deletion win32/ctags_vs2013.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
2 changes: 1 addition & 1 deletion win32/ctags_vs2013.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -851,4 +851,4 @@
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup>
</Project>
</Project>