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 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,Resource", NULL, NULL, false},
{"^data[[:space:]]\"([^\"]+)\"[[:space:]]\"([^\"]+)\"", "\\2",
"d,Data", NULL, NULL, false},
{"^variable[[:space:]]\"([^\"]+)\"", "\\1",
"v,Variable", NULL, NULL, false},
{"^provider[[:space:]]\"([^\"]+)\"", "\\1",
"p,Provider", NULL, NULL, false},
{"^module[[:space:]]\"([^\"]+)\"", "\\1",
"m,Module", NULL, NULL, false},
{"^output[[:space:]]\"([^\"]+)\"", "\\1",
"o,Output", 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
Copy link
Member

Choose a reason for hiding this comment

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

Terraform or TerraForm is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. It should be 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,Resource/
Copy link
Member

Choose a reason for hiding this comment

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

You use both kind letter and kind long name in "r,Resource".
However, r is already defined with --kinddef-terraform. So you should use only 'r' here.

--regex-terraform=/^resource[[:space:]]"([^"]+)"[[:space:]]"([^"]+)"/\2/r/

--regex-terraform=/^data[[:space:]]"([^"]+)"[[:space:]]"([^"]+)"/\2/d,Data/
--regex-terraform=/^variable[[:space:]]"([^"]+)"/\1/v,Variable/
--regex-terraform=/^provider[[:space:]]"([^"]+)"/\1/p,Provider/
--regex-terraform=/^module[[:space:]]"([^"]+)"/\1/m,Module/
--regex-terraform=/^output[[:space:]]"([^"]+)"/\1/o,Output/
--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 @@ -358,6 +358,7 @@
<ClCompile Include="..\parsers\systemdunit.c" />
<ClCompile Include="..\parsers\tcl.c" />
<ClCompile Include="..\parsers\tcloo.c" />
<ClCompile Include="..\parsers\terraform.c" />
<ClCompile Include="..\parsers\tex-beamer.c" />
<ClCompile Include="..\parsers\tex.c" />
<ClCompile Include="..\parsers\ttcn.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 @@ -597,6 +597,9 @@
<ClCompile Include="..\parsers\tcloo.c">
<Filter>Source Files\parsers</Filter>
</ClCompile>
<ClCompile Include="..\parsers\terraform.c">
<Filter>Source Files\Parsers</Filter>
</ClCompile>
<ClCompile Include="..\parsers\tex-beamer.c">
<Filter>Source Files\parsers</Filter>
</ClCompile>
Expand Down