Skip to content

Commit 910a460

Browse files
committed
Sh: make the way to extract variables more robust
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 4255def commit 910a460

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Tmain/ptag-in-optlib-parser.d/stdout-expected.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
!_TAG_KIND_DESCRIPTION!Sh f,function /functions/
1111
!_TAG_KIND_DESCRIPTION!Sh h,heredoc /labels for here document/
1212
!_TAG_KIND_DESCRIPTION!Sh s,script /script files/
13+
!_TAG_KIND_DESCRIPTION!Sh v,variable /variables assigment (experimental)/
1314
!_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/
1415
!_TAG_OUTPUT_FILESEP slash /slash or backslash/
1516
!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/

parsers/sh.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,27 @@ static size_t handleZshKeyword (int keyword,
512512
return vStringLength(token);
513513
}
514514

515+
static bool doesLineCotinue(const unsigned char *start, const unsigned char *cp)
516+
{
517+
cp--;
518+
if (start >= cp)
519+
return false;
520+
521+
if (*cp != '\\')
522+
return false;
523+
524+
while (start < cp)
525+
{
526+
if (*cp == ';' || *cp == '|' || *cp == '&')
527+
return false;
528+
else if (isspace(*cp))
529+
cp--;
530+
else
531+
return true;
532+
}
533+
return false;
534+
}
535+
515536
static bool handleVariableAssignment (vString *input)
516537
{
517538
const char *base = vStringValue (input);
@@ -556,6 +577,7 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
556577
struct hereDocParsingState hstate;
557578
hdocStateInit (&hstate);
558579

580+
bool cont_line = false;
559581
while ((line = readLineFromInputFile ()) != NULL)
560582
{
561583
const unsigned char* cp = line;
@@ -581,10 +603,12 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
581603
vStringDelete (hereDocDelimiter);
582604
hereDocDelimiter = NULL;
583605
}
606+
cont_line = false;
584607
continue;
585608
}
586609

587610
hdocStateClear (&hstate);
611+
bool beginning_of_line = !cont_line;
588612
while (*cp != '\0')
589613
{
590614
subparser *sub = NULL;
@@ -722,6 +746,7 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
722746
cp += d;
723747
else if (*cp != '\0')
724748
++cp;
749+
beginning_of_line = false;
725750
continue;
726751
}
727752

@@ -763,7 +788,8 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
763788
++cp;
764789
}
765790
}
766-
else if (found_kind == K_NOTHING
791+
else if (beginning_of_line
792+
&& found_kind == K_NOTHING
767793
&& handleVariableAssignment (name))
768794
found_kind = K_VARIABLE;
769795

@@ -782,7 +808,11 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
782808
else if (!hereDocDelimiter)
783809
hdocStateUpdateArgs (&hstate, name);
784810
vStringClear (name);
811+
beginning_of_line = false;
785812
}
813+
if (*cp == '#')
814+
cont_line = false;
815+
cont_line = doesLineCotinue (line, cp);
786816
}
787817
hdocStateFini (&hstate);
788818
vStringDelete (name);

0 commit comments

Comments
 (0)