Skip to content

Commit

Permalink
ttcn: add "template" ["(omit)" | "(value)" | "(present)"]
Browse files Browse the repository at this point in the history
Modern TTCN3 has TemplateRestriction keywords, which improve detection
of invalid template use at compile time. They simply add one of
"(omit)", "(value)", "(present)" after the "template" keyword.

Example:

 template (value) ts_FOO_Request(integer bar := 0) :=
 	{ member := bar };

 template (present) tr_FOO_Request(template integer bar := *) :=
 	{ member := bar };

Before this patch, u-ctags would not tag these two definitons above,
making my work on the Osmocom test suite hard. (See
https://gitea.osmocom.org/ttcn3/osmo-ttcn3-hacks)

At first I tried to exactly match the three keywords within the braces,
but the token parsing does not support unrolling more than one token. So
if an unmatching token follows after the opening brace, I cannot un-get
the opening brace. The easiest solution, and one that also is tolerant
to keyword typos, is to just accept any braces after "template".

Related: ETSI ES 201 873-1 V4.14.1 § A.1.6.1.3
  • Loading branch information
neeels committed Aug 21, 2022
1 parent 40551e2 commit d92a92f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions Units/parser-ttcn.r/ttcn-template-restriction.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--sort=no
5 changes: 5 additions & 0 deletions Units/parser-ttcn.r/ttcn-template-restriction.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CoffeeTemplates input.ttcn /^module CoffeeTemplates$/;" M
AnyCoffee input.ttcn /^template (present) Coffee AnyCoffee$/;" d
SomeCoffee input.ttcn /^template (value) Coffee SomeCoffee(integer id, charstring description := "")$/;" d
Espresso input.ttcn /^template (omit) Coffee Espresso()$/;" d
Latte input.ttcn /^template (TYPO IN RESTRICTION) Coffee Latte()$/;" d
32 changes: 32 additions & 0 deletions Units/parser-ttcn.r/ttcn-template-restriction.d/input.ttcn
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import from CoffeeTypes all
import from CoffeeConstants all

module CoffeeTemplates
{

template (present) Coffee AnyCoffee
{
id := ?,
description := ?
}

template (value) Coffee SomeCoffee(integer id, charstring description := "")
{
id := id,
description := description
}

template (omit) Coffee Espresso()
{
id := c_espressoId,
description := c_espresso
}

template (TYPO IN RESTRICTION) Coffee Latte()
{
id := c_latteId,
description := c_latte
}

}

18 changes: 17 additions & 1 deletion parsers/ttcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,21 @@ static int matchExprOperator (void)
return 0;
}

/* Check if next token is one of "(omit)", "(value)", "(present)".
* ETSI ES 201 873-1 V4.14.1
* A.1.6.7 471. TemplateRestriction ::= "(" (OmitKeyword | ValueKeyword | PresentKeyword) ")"
* A.1.6.6 461. OmitKeyword ::= "omit"
* A.1.6.4.2 346. ValueKeyword ::= "value"
* A.1.6.1.3 145.PresentKeyword ::= "present"
*/
static int matchTemplateRestriction (void)
{
/* Simplistic: do not verify the actual "omit"/"value"/"present" keyword, just skip the entire brackets when
* present. This also is tolerant to typos in the keyword. Drawback: would also accept any amount of unrelated
* tokens and punctuation in the braces. */
return matchBrackets(BR_PAR);
}

static int parseExprOperand (void)
{
ttcnToken_t * pTok;
Expand Down Expand Up @@ -950,7 +965,8 @@ static void parseTTCN (void)
parseTypeDefBody();
break;
case T_TEMPLATE:
/* A.1.6.1.3 TemplateDef ::= "template" (Type | Signature) ID ... */
/* A.1.6.1.3 TemplateDef ::= "template" [TemplateRestriction] (Type | Signature) ID ... */
matchTemplateRestriction();
if (parseType() || parseSignature())
parseID(K_TEMPLATE);
break;
Expand Down

0 comments on commit d92a92f

Please sign in to comment.