Skip to content

Commit

Permalink
TerraformVariable: new parser
Browse files Browse the repository at this point in the history
This parser doesn't have its own kinds.
Instead, this parser emits Terraform parser's "variable"
kind tags with "assigned" role.

ref. https://developer.hashicorp.com/terraform/language/values/variables#variable-definitions-tfvars-files

TODO:
* This should be rewritten in optlib like:

    --langdef=TerraformVariables{_foreign=Terraform}
    --map-TerraformVariables=.tfvars
    --regex-TerraformVariables=/^([a-z0-9_]+)[[:space:]]*=/\1/v/{_role=assigned}{_lang=Terraform}

* Introduce "indexer:" or "parser:" fields.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Mar 30, 2023
1 parent 8cbaef5 commit c27b6ec
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 11 deletions.
10 changes: 5 additions & 5 deletions Tmain/list-map-extensions.d/stdout-expected.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## all|grep LdScript
#LANGUAGE EXTENSION
LdScript lds
LdScript scr
LdScript ld
LdScript ldi
#LANGUAGE EXTENSION
LdScript lds
LdScript scr
LdScript ld
LdScript ldi
## LdScript
#EXTENSION
lds
Expand Down
2 changes: 2 additions & 0 deletions Tmain/list-roles.d/stdout-expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ SystemdUnit u/unit RequiredBy on referred in Required
SystemdUnit u/unit Requires on referred in Requires key
SystemdUnit u/unit WantedBy on referred in WantedBy key
SystemdUnit u/unit Wants on referred in Wants key
Terraform v/variable assigned on assigned in Variable Definitions (.tfvars) files
Tex e/environment used off environment usage introduced by \begin{MyEnv}
Tex i/xinput bibliography on bibliography (.bib) file
Tex i/xinput included on external input file specified with \include
Expand Down Expand Up @@ -221,6 +222,7 @@ SystemdUnit u/unit RequiredBy on referred in Required
SystemdUnit u/unit Requires on referred in Requires key
SystemdUnit u/unit WantedBy on referred in WantedBy key
SystemdUnit u/unit Wants on referred in Wants key
Terraform v/variable assigned on assigned in Variable Definitions (.tfvars) files
Tex e/environment used off environment usage introduced by \begin{MyEnv}
Tex i/xinput bibliography on bibliography (.bib) file
Tex i/xinput included on external input file specified with \include
Expand Down
2 changes: 2 additions & 0 deletions Units/parser-terraform.r/simple-terraform.d/args.ctags
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
--sort=no
--extras=+r
--fields=+r
13 changes: 7 additions & 6 deletions Units/parser-terraform.r/simple-terraform.d/expected.tags
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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
aws input.tf /^provider "aws" {$/;" p roles:def
events_bus_name input.tf /^variable "events_bus_name" {$/;" v roles:def
example_events_bus input.tf /^resource "aws_cloudwatch_event_bus" "example_events_bus" {$/;" r roles:def
database input.tf /^module "database" {$/;" m roles:def
example_database_password input.tf /^data "aws_ssm_parameter" "example_database_password" {$/;" d roles:def
password input.tf /^output "password" {$/;" o roles:def
events_bus_name input-0.tfvars /^events_bus_name = "hyper-connector"$/;" v roles:assigned
1 change: 1 addition & 0 deletions Units/parser-terraform.r/simple-terraform.d/input-0.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
events_bus_name = "hyper-connector"
1 change: 1 addition & 0 deletions main/parsers_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
TclParser, \
TclOOParser, \
TerraformParser, \
TerraformVariablesParser, \
TexParser, \
TexBeamerParser, \
TTCNParser, \
Expand Down
4 changes: 4 additions & 0 deletions optlib/terraform.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ extern parserDefinition* TerraformParser (void)
NULL
};

static roleDefinition TerraformVariableRoleTable [] = {
{ true, "assigned", "assigned in Variable Definitions (.tfvars) files" },
};
static kindDefinition TerraformKindTable [] = {
{
true, 'r', "resource", "resources",
Expand All @@ -36,6 +39,7 @@ extern parserDefinition* TerraformParser (void)
},
{
true, 'v', "variable", "variables",
ATTACH_ROLES(TerraformVariableRoleTable),
},
{
true, 'p', "provider", "providers",
Expand Down
6 changes: 6 additions & 0 deletions optlib/terraform.ctags
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@
#
# Another notable thing is that this parser doesn't skip /* block comments */.
#
# .tfvars files relates code are moved to TerraformVariables parser.
#
--langdef=Terraform

--map-Terraform=+.tf

--kinddef-Terraform=r,resource,resources
--kinddef-Terraform=d,data,data

--kinddef-Terraform=v,variable,variables
# See https://developer.hashicorp.com/terraform/language/values/variables#variable-definitions-tfvars-files
--_roledef-Terraform.v=assigned,assigned in Variable Definitions (.tfvars) files

--kinddef-Terraform=p,provider,providers
--kinddef-Terraform=m,module,modules
--kinddef-Terraform=o,output,output
Expand Down
95 changes: 95 additions & 0 deletions parsers/terraformvariables.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2021, Julio Tain Sueiras
* Copyright (c) 2023 Masatake YAMATO
* Copyright (c) 2023 Red Hat, Inc.
*
* 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.
*
* The regex pattern is derrived from
* - https://github.com/juliosueiras/vim-terraform-completion/blob/master/ctags/terraform.ctags
*
* This module contains functions to extract language objects for Terraform from
* Terraform Variable Definitions (.tfvars) files.
* See: https://developer.hashicorp.com/terraform/language/values/variables#variable-definitions-tfvars-files
*/

#include "general.h" /* must always come first */

#include <regex.h>

#include "debug.h"
#include "entry.h"
#include "parse.h"
#include "read.h"

#include "dependency.h"

struct terraformVariablesCtx {
langType terraformLang;
int variableKindIndex;
int assignedRoleIndex;
};

static void findTerraformVariablesTags (void)
{
findRegexTags ();
}

static bool found_tag_cb (const char *line,
const regexMatch *matches,
unsigned int count,
void *userData)
{
if (count >0)
{
tagEntryInfo e;
struct terraformVariablesCtx *ctx = userData;

vString *name = vStringNew ();
vStringNCopyS (name, line + matches[1].start, matches[1].length);
initForeignRefTagEntry(&e, vStringValue (name), ctx->terraformLang,
ctx->variableKindIndex, ctx->assignedRoleIndex);
makeTagEntry (&e);
vStringDelete (name);
}
return true;
}

static void initializeTerraformVariablesParser (langType language)
{
static struct terraformVariablesCtx ctx;
kindDefinition *kdef;
roleDefinition *rdef;

ctx.terraformLang = getNamedLanguage("Terraform", 0);
Assert(ctx.terraformLang != LANG_IGNORE);
kdef = getLanguageKindForName (ctx.terraformLang, "variable");
Assert(kdef);
ctx.variableKindIndex = kdef->id;
rdef = getLanguageRoleForName (ctx.terraformLang, ctx.variableKindIndex,
"assigned");
Assert(rdef);
ctx.assignedRoleIndex = rdef->id;

addLanguageCallbackRegex (language, "^([a-z0-9_]+)[[:space:]]*=",
"", found_tag_cb, NULL, &ctx);
}

extern parserDefinition* TerraformVariablesParser (void)
{
static const char *const extensions [] = { "tfvars", NULL };
parserDefinition* const def = parserNew ("TerraformVariables");

static parserDependency dependencies [] = {
[0] = { DEPTYPE_FOREIGNER, "Terraform", NULL },
};

def->extensions = extensions;
def->dependencies = dependencies;
def->dependencyCount = ARRAY_SIZE (dependencies);
def->parser = findTerraformVariablesTags;
def->method = METHOD_REGEX;
def->initialize = initializeTerraformVariablesParser;
return def;
}
1 change: 1 addition & 0 deletions source.mak
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ PARSER_SRCS = \
parsers/systemdunit.c \
parsers/tcl.c \
parsers/tcloo.c \
parsers/terraformvariables.c \
parsers/tex.c \
parsers/tex-beamer.c \
parsers/ttcn.c \
Expand Down
1 change: 1 addition & 0 deletions win32/ctags_vs2013.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@
<ClCompile Include="..\parsers\systemdunit.c" />
<ClCompile Include="..\parsers\tcl.c" />
<ClCompile Include="..\parsers\tcloo.c" />
<ClCompile Include="..\parsers\terraformvariables.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 @@ -600,6 +600,9 @@
<ClCompile Include="..\parsers\tcloo.c">
<Filter>Source Files\parsers</Filter>
</ClCompile>
<ClCompile Include="..\parsers\terraformvariables.c">
<Filter>Source Files\parsers</Filter>
</ClCompile>
<ClCompile Include="..\parsers\tex-beamer.c">
<Filter>Source Files\parsers</Filter>
</ClCompile>
Expand Down

0 comments on commit c27b6ec

Please sign in to comment.