Skip to content

Commit ff7e88e

Browse files
committed
[EXPERIMENTAL] Sh: extract variables in variable assignments
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 3bd62a1 commit ff7e88e

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

Units/parser-sh.r/sh-comments.d/expected.tags

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ afterpound1 input.sh /^foo="#"; afterpound1() {}$/;" f
22
afterpound2 input.sh /^foo="#\\""; afterpound2() {}$/;" f
33
afterpound3 input.sh /^foo='#'; afterpound3() {}$/;" f
44
afterpound4 input.sh /^foo='#'''; afterpound4() {}$/;" f
5+
foo input.sh /^foo="#"; afterpound1() {}$/;" v
6+
foo input.sh /^foo="#\\""; afterpound2() {}$/;" v
7+
foo input.sh /^foo='#'''; afterpound4() {}$/;" v
8+
foo input.sh /^foo='#'; afterpound3() {}$/;" v

Units/parser-sh.r/sh-quotes.d/expected.tags

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ afterpound1 input.sh /^foo="#"; afterpound1() {}$/;" f
22
afterpound2 input.sh /^foo="#\\""; afterpound2() {}$/;" f
33
afterpound3 input.sh /^foo='#'; afterpound3() {}$/;" f
44
afterpound4 input.sh /^foo='#'''; afterpound4() {}$/;" f
5+
foo input.sh /^foo="#"; afterpound1() {}$/;" v
6+
foo input.sh /^foo="#\\""; afterpound2() {}$/;" v
7+
foo input.sh /^foo="nofunction()"$/;" v
8+
foo input.sh /^foo='#'''; afterpound4() {}$/;" v
9+
foo input.sh /^foo='#'; afterpound3() {}$/;" v

parsers/sh.c

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef enum {
3838
K_FUNCTION,
3939
K_SCRIPT,
4040
K_HEREDOCLABEL,
41+
K_VARIABLE,
4142
} shKind;
4243

4344
typedef enum {
@@ -87,7 +88,9 @@ static roleDefinition ZshFunctionRoles [] = {
8788
{ true, 's', "script", "script files", \
8889
.referenceOnly = true, ATTACH_ROLES (SCRIPT_ROLES) }, \
8990
{ true, 'h', "heredoc", "labels for here document", \
90-
.referenceOnly = false, ATTACH_ROLES (HEREDOC_ROLES) }
91+
.referenceOnly = false, ATTACH_ROLES (HEREDOC_ROLES) }, \
92+
{ true, 'v', "variable", "variables assigment (experimental)" }
93+
9194

9295
static kindDefinition ShKinds [] = {
9396
SH_KINDS_COMMON(ShScriptRoles, ShHeredocRoles,),
@@ -456,6 +459,32 @@ static size_t handleZshKeyword (int keyword,
456459
return vStringLength(token);
457460
}
458461

462+
static bool handleVariableAssignment (vString *input)
463+
{
464+
const char *base = vStringValue (input);
465+
const char *cp = base;
466+
467+
while (*cp != '\0')
468+
{
469+
if (*cp == '=')
470+
{
471+
size_t len = cp - base;
472+
if (len > 0)
473+
{
474+
vStringTruncate (input, len);
475+
return true;
476+
}
477+
break;
478+
}
479+
else if ( ((cp == base)?
480+
isIdentChar0: isIdentChar) ((unsigned char)*cp) )
481+
cp++;
482+
else
483+
break;
484+
}
485+
return false;
486+
}
487+
459488
typedef bool (* checkCharFunc) (int);
460489
static void findShTagsCommon (size_t (* keyword_handler) (int,
461490
vString *,
@@ -653,8 +682,9 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
653682
while (isspace ((int) *cp))
654683
++cp;
655684

656-
if ((found_kind != K_SCRIPT)
657-
&& *cp == '(')
685+
if (found_kind == K_SCRIPT)
686+
; /* Do NOTHING */
687+
else if (*cp == '(')
658688
{
659689
++cp;
660690
while (isspace ((int) *cp))
@@ -680,6 +710,9 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
680710
++cp;
681711
}
682712
}
713+
else if (found_kind == K_NOTHING
714+
&& handleVariableAssignment (name))
715+
found_kind = K_VARIABLE;
683716

684717
if (found_kind != K_NOTHING)
685718
{

0 commit comments

Comments
 (0)