Skip to content

Commit e730033

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

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-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: 37 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 {
@@ -86,8 +87,9 @@ static roleDefinition ZshFunctionRoles [] = {
8687
.referenceOnly = false, FUNCTION_ROLES_SPEC }, \
8788
{ true, 's', "script", "script files", \
8889
.referenceOnly = true, ATTACH_ROLES (SCRIPT_ROLES) }, \
89-
.referenceOnly = false, ATTACH_ROLES (HEREDOC_ROLES) }
9090
{ true, 'h', "heredoc", "labels for here document", \
91+
.referenceOnly = false, ATTACH_ROLES (HEREDOC_ROLES) }, \
92+
{ true, 'v', "variable", "variables assigment (experimental)" }
9193

9294
static kindDefinition ShKinds [] = {
9395
SH_KINDS_COMMON(ShScriptRoles, ShHeredocRoles,),
@@ -456,6 +458,34 @@ static size_t handleZshKeyword (int keyword,
456458
return vStringLength(token);
457459
}
458460

461+
static bool handleVariableAssignment (vString *input)
462+
{
463+
const char *base = vStringValue (input);
464+
const char *cp = base;
465+
466+
while (*cp != '\0')
467+
{
468+
if (*cp == '=')
469+
{
470+
size_t len = cp - base;
471+
/* Ignore VAR in if [[ \
472+
VAR == ... ]] */
473+
if (*(cp + 1) != '=' && len > 0)
474+
{
475+
vStringTruncate (input, len);
476+
return true;
477+
}
478+
break;
479+
}
480+
else if ( ((cp == base)?
481+
isIdentChar0: isIdentChar) ((unsigned char)*cp) )
482+
cp++;
483+
else
484+
break;
485+
}
486+
return false;
487+
}
488+
459489
typedef bool (* checkCharFunc) (int);
460490
static void findShTagsCommon (size_t (* keyword_handler) (int,
461491
vString *,
@@ -653,8 +683,9 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
653683
while (isspace ((int) *cp))
654684
++cp;
655685

656-
if ((found_kind != K_SCRIPT)
657-
&& *cp == '(')
686+
if (found_kind == K_SCRIPT)
687+
; /* Do NOTHING */
688+
else if (*cp == '(')
658689
{
659690
++cp;
660691
while (isspace ((int) *cp))
@@ -680,6 +711,9 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
680711
++cp;
681712
}
682713
}
714+
else if (found_kind == K_NOTHING
715+
&& handleVariableAssignment (name))
716+
found_kind = K_VARIABLE;
683717

684718
if (found_kind != K_NOTHING)
685719
{

0 commit comments

Comments
 (0)