forked from universal-ctags/ctags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
5830 lines (4952 loc) · 149 KB
/
parse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1996-2003, Darren Hiebert
*
* 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.
*
* This module contains functions for managing input languages and
* dispatching files to the appropriate language parser.
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
/* TODO: This definition should be removed. */
#define OPTION_WRITE
#include "options_p.h"
#include <string.h>
#include "ctags.h"
#include "debug.h"
#include "entry_p.h"
#include "field_p.h"
#include "flags_p.h"
#include "htable.h"
#include "keyword.h"
#include "lxpath_p.h"
#include "param.h"
#include "param_p.h"
#include "parse_p.h"
#include "parsers_p.h"
#include "promise.h"
#include "promise_p.h"
#include "ptag_p.h"
#include "ptrarray.h"
#include "read.h"
#include "read_p.h"
#include "routines.h"
#include "routines_p.h"
#include "stats_p.h"
#include "subparser.h"
#include "subparser_p.h"
#include "trace.h"
#include "trashbox.h"
#include "trashbox_p.h"
#include "vstring.h"
#ifdef HAVE_ICONV
# include "mbcs_p.h"
#endif
#include "writer_p.h"
#include "xtag_p.h"
/*
* DATA TYPES
*/
enum specType {
SPEC_NONE,
SPEC_NAME,
SPEC_ALIAS = SPEC_NAME,
SPEC_EXTENSION,
SPEC_PATTERN,
};
const char *specTypeName [] = {
"none", "name", "extension", "pattern"
};
typedef struct {
langType lang;
const char* spec;
enum specType specType;
} parserCandidate;
typedef struct sParserObject {
parserDefinition *def;
kindDefinition* fileKind;
stringList* currentPatterns; /* current list of file name patterns */
stringList* currentExtensions; /* current list of extensions */
stringList* currentAliases; /* current list of aliases */
unsigned int initialized:1; /* initialize() is called or not */
unsigned int dontEmit:1; /* run but don't emit tags.
This parser was disabled but a subparser on
this parser makes this parser run (to drive
the subparser). */
unsigned int pseudoTagPrinted:1; /* pseudo tags about this parser
is emitted or not. */
unsigned int justRunForSchedulingBase:1;
unsigned int used; /* Used for printing language specific statistics. */
unsigned int anonymousIdentiferId; /* managed by anon* functions */
struct slaveControlBlock *slaveControlBlock;
struct kindControlBlock *kindControlBlock;
struct lregexControlBlock *lregexControlBlock;
struct paramControlBlock *paramControlBlock;
langType pretendingAsLanguage; /* OLDLANG in --_pretend-<NEWLANG>=<OLDLANG>
is set here if this parser is NEWLANG.
LANG_IGNORE is set if no pretending. */
langType pretendedAsLanguage; /* NEWLANG in --_pretend-<NEWLANG>=<OLDLANG>
is set here if this parser is OLDLANG.
LANG_IGNORE is set if no being pretended. */
} parserObject;
/*
* FUNCTION PROTOTYPES
*/
static void lazyInitialize (langType language);
static void addParserPseudoTags (langType language);
static void installKeywordTable (const langType language);
static void installTagRegexTable (const langType language);
static void installTagXpathTable (const langType language);
static void anonResetMaybe (parserObject *parser);
static void setupAnon (void);
static void teardownAnon (void);
static void uninstallTagXpathTable (const langType language);
static bool hasLanguageAnyRegexPatterns (const langType language);
/*
* DATA DEFINITIONS
*/
static parserDefinition *FallbackParser (void);
static parserDefinition *CTagsParser (void);
static parserDefinition *CTagsSelfTestParser (void);
static parserDefinitionFunc* BuiltInParsers[] = {
#ifdef EXTERNAL_PARSER_LIST
EXTERNAL_PARSER_LIST
#else /* ! EXTERNAL_PARSER_LIST */
CTagsParser, /* This must be first entry. */
FallbackParser, /* LANG_FALLBACK */
CTagsSelfTestParser,
PARSER_LIST,
XML_PARSER_LIST
#ifdef HAVE_LIBXML
,
#endif
YAML_PARSER_LIST
#ifdef HAVE_LIBYAML
,
#endif
PEG_PARSER_LIST
#ifdef HAVE_PACKCC
,
#endif
OPTLIB2C_PCRE2_PARSER_LIST
#ifdef HAVE_PCRE2
,
#endif
#endif /* EXTERNAL_PARSER_LIST */
};
static parserObject* LanguageTable = NULL;
static unsigned int LanguageCount = 0;
static hashTable* LanguageHTable = NULL;
static kindDefinition defaultFileKind = {
.enabled = false,
.letter = KIND_FILE_DEFAULT_LETTER,
.name = KIND_FILE_DEFAULT_NAME,
.description = KIND_FILE_DEFAULT_NAME,
};
static langType ctagsSelfTestLang;
/*
* FUNCTION DEFINITIONS
*/
static bool isLanguageNameChar(int c)
{
if (isgraph(c))
{
if (c == '\'' || c == '"' || c == ';')
return false;
return true;
}
else
return false;
}
extern unsigned int countParsers (void)
{
return LanguageCount;
}
extern int makeSimpleTag (
const vString* const name, const int kindIndex)
{
return makeSimpleRefTag (name, kindIndex, ROLE_DEFINITION_INDEX);
}
extern int makeSimpleRefTag (const vString* const name, const int kindIndex,
int roleIndex)
{
int r = CORK_NIL;
Assert (roleIndex < (int)countInputLanguageRoles(kindIndex));
/* do not check for kind being disabled - that happens later in makeTagEntry() */
if (name != NULL && vStringLength (name) > 0)
{
tagEntryInfo e;
initRefTagEntry (&e, vStringValue (name), kindIndex, roleIndex);
r = makeTagEntry (&e);
}
return r;
}
extern int makeSimplePlaceholder(const vString* const name)
{
return makePlaceholder (vStringValue (name));
}
extern bool isLanguageEnabled (const langType language)
{
const parserDefinition* const def = LanguageTable [language].def;
return def->enabled;
}
extern bool isLanguageVisible (const langType language)
{
const parserDefinition* const lang = LanguageTable [language].def;
return !lang->invisible;
}
/*
* parserDescription mapping management
*/
extern parserDefinition* parserNew (const char* name)
{
parserDefinition* result = xCalloc (1, parserDefinition);
result->name = eStrdup (name);
result->enabled = true;
return result;
}
extern bool doesLanguageAllowNullTag (const langType language)
{
Assert (0 <= language && language < (int) LanguageCount);
return LanguageTable [language].def->allowNullTag;
}
extern bool doesLanguageRequestAutomaticFQTag (const langType language)
{
Assert (0 <= language && language < (int) LanguageCount);
return LanguageTable [language].def->requestAutomaticFQTag;
}
static const char *getLanguageNameFull (const langType language, bool noPretending)
{
const char* result;
if (language == LANG_IGNORE)
result = "unknown";
else
{
Assert (0 <= language && language < (int) LanguageCount);
if (noPretending)
result = LanguageTable [language].def->name;
else
{
langType real_language = LanguageTable [language].pretendingAsLanguage;
if (real_language == LANG_IGNORE)
result = LanguageTable [language].def->name;
else
{
Assert (0 <= real_language && real_language < (int) LanguageCount);
result = LanguageTable [real_language].def->name;
}
}
}
return result;
}
extern const char *getLanguageName (const langType language)
{
return getLanguageNameFull (language, false);
}
extern const char *getLanguageKindName (const langType language, const int kindIndex)
{
kindDefinition* kdef = getLanguageKind (language, kindIndex);
return kdef->name;
}
static kindDefinition kindGhost = {
.letter = KIND_GHOST_LETTER,
.name = KIND_GHOST_NAME,
.description = KIND_GHOST_NAME,
};
extern int defineLanguageKind (const langType language, kindDefinition *def,
freeKindDefFunc freeKindDef)
{
return defineKind (LanguageTable [language].kindControlBlock, def, freeKindDef);
}
extern unsigned int countLanguageKinds (const langType language)
{
return countKinds (LanguageTable [language].kindControlBlock);
}
extern unsigned int countLanguageRoles (const langType language, int kindIndex)
{
return countRoles (LanguageTable [language].kindControlBlock, kindIndex);
}
extern kindDefinition* getLanguageKind (const langType language, int kindIndex)
{
kindDefinition* kdef;
Assert (0 <= language && language < (int) LanguageCount);
switch (kindIndex)
{
case KIND_FILE_INDEX:
kdef = LanguageTable [language].fileKind;
break;
case KIND_GHOST_INDEX:
kdef = &kindGhost;
break;
default:
Assert (kindIndex >= 0);
kdef = getKind (LanguageTable [language].kindControlBlock, kindIndex);
}
return kdef;
}
extern kindDefinition* getLanguageKindForLetter (const langType language, char kindLetter)
{
Assert (0 <= language && language < (int) LanguageCount);
if (kindLetter == LanguageTable [language].fileKind->letter)
return LanguageTable [language].fileKind;
else if (kindLetter == KIND_GHOST_LETTER)
return &kindGhost;
else
return getKindForLetter (LanguageTable [language].kindControlBlock, kindLetter);
}
extern kindDefinition* getLanguageKindForName (const langType language, const char *kindName)
{
Assert (0 <= language && language < (int) LanguageCount);
Assert (kindName);
if (strcmp(kindName, LanguageTable [language].fileKind->name) == 0)
return LanguageTable [language].fileKind;
else if (strcmp(kindName, KIND_GHOST_NAME) == 0)
return &kindGhost;
else
return getKindForName (LanguageTable [language].kindControlBlock, kindName);
}
extern roleDefinition* getLanguageRole(const langType language, int kindIndex, int roleIndex)
{
return getRole (LanguageTable [language].kindControlBlock, kindIndex, roleIndex);
}
extern roleDefinition* getLanguageRoleForName (const langType language, int kindIndex,
const char *roleName)
{
return getRoleForName (LanguageTable [language].kindControlBlock, kindIndex, roleName);
}
extern langType getNamedLanguageFull (const char *const name, size_t len, bool noPretending,
bool include_aliases)
{
langType result = LANG_IGNORE;
unsigned int i;
Assert (name != NULL);
if (len == 0)
{
parserDefinition *def = (parserDefinition *)hashTableGetItem (LanguageHTable, name);
if (def)
result = def->id;
}
else
for (i = 0 ; i < LanguageCount && result == LANG_IGNORE ; ++i)
{
const parserDefinition* const lang = LanguageTable [i].def;
Assert (lang->name);
vString* vstr = vStringNewInit (name);
vStringTruncate (vstr, len);
if (strcasecmp (vStringValue (vstr), lang->name) == 0)
result = i;
else if (include_aliases)
{
stringList* const aliases = LanguageTable [i].currentAliases;
if (aliases && stringListCaseMatched (aliases, vStringValue (vstr)))
result = i;
}
vStringDelete (vstr);
}
if (result != LANG_IGNORE
&& (!noPretending)
&& LanguageTable [result].pretendedAsLanguage != LANG_IGNORE)
result = LanguageTable [result].pretendedAsLanguage;
return result;
}
extern langType getNamedLanguage (const char *const name, size_t len)
{
return getNamedLanguageFull (name, len, false, false);
}
extern langType getNamedLanguageOrAlias (const char *const name, size_t len)
{
return getNamedLanguageFull (name, len, false, true);
}
static langType getNameOrAliasesLanguageAndSpec (const char *const key, langType start_index,
const char **const spec, enum specType *specType)
{
langType result = LANG_IGNORE;
unsigned int i;
if (start_index == LANG_AUTO)
start_index = 0;
else if (start_index == LANG_IGNORE || start_index >= (int) LanguageCount)
return result;
for (i = start_index ; i < LanguageCount && result == LANG_IGNORE ; ++i)
{
if (! isLanguageEnabled (i))
continue;
const parserObject* const parser = LanguageTable + i;
stringList* const aliases = parser->currentAliases;
vString* tmp;
if (parser->def->name != NULL && strcasecmp (key, parser->def->name) == 0)
{
result = i;
*spec = parser->def->name;
*specType = SPEC_NAME;
}
else if (aliases != NULL && (tmp = stringListFileFinds (aliases, key)))
{
result = i;
*spec = vStringValue(tmp);
*specType = SPEC_ALIAS;
}
}
return result;
}
extern langType getLanguageForCommand (const char *const command, langType startFrom)
{
const char *const tmp_command = baseFilename (command);
char *tmp_spec;
enum specType tmp_specType;
return getNameOrAliasesLanguageAndSpec (tmp_command, startFrom,
(const char **const)&tmp_spec,
&tmp_specType);
}
static langType getPatternLanguageAndSpec (const char *const baseName, langType start_index,
const char **const spec, enum specType *specType)
{
langType result = LANG_IGNORE;
unsigned int i;
if (start_index == LANG_AUTO)
start_index = 0;
else if (start_index == LANG_IGNORE || start_index >= (int) LanguageCount)
return result;
*spec = NULL;
for (i = start_index ; i < LanguageCount && result == LANG_IGNORE ; ++i)
{
if (! isLanguageEnabled (i))
continue;
parserObject *parser = LanguageTable + i;
stringList* const ptrns = parser->currentPatterns;
vString* tmp;
if (ptrns != NULL && (tmp = stringListFileFinds (ptrns, baseName)))
{
result = i;
*spec = vStringValue(tmp);
*specType = SPEC_PATTERN;
goto found;
}
}
for (i = start_index ; i < LanguageCount && result == LANG_IGNORE ; ++i)
{
if (! isLanguageEnabled (i))
continue;
parserObject *parser = LanguageTable + i;
stringList* const exts = parser->currentExtensions;
vString* tmp;
if (exts != NULL && (tmp = stringListExtensionFinds (exts,
fileExtension (baseName))))
{
result = i;
*spec = vStringValue(tmp);
*specType = SPEC_EXTENSION;
goto found;
}
}
found:
return result;
}
extern langType getLanguageForFilename (const char *const filename, langType startFrom)
{
const char *const tmp_filename = baseFilename (filename);
char *tmp_spec;
enum specType tmp_specType;
return getPatternLanguageAndSpec (tmp_filename, startFrom,
(const char **const)&tmp_spec,
&tmp_specType);
}
const char *scopeSeparatorFor (langType language, int kindIndex, int parentKindIndex)
{
Assert (0 <= language && language < (int) LanguageCount);
parserObject *parser = LanguageTable + language;
struct kindControlBlock *kcb = parser->kindControlBlock;
const scopeSeparator *sep = getScopeSeparator (kcb, kindIndex, parentKindIndex);
return sep? sep->separator: NULL;
}
static bool processLangDefineScopesep(const langType language,
const char *const option,
const char *const parameter)
{
parserObject *parser;
const char * p = parameter;
char parentKletter;
int parentKindex = KIND_FILE_INDEX;
char kletter;
int kindex = KIND_FILE_INDEX;
const char *separator;
Assert (0 <= language && language < (int) LanguageCount);
parser = LanguageTable + language;
/*
* Parent
*/
parentKletter = p[0];
if (parentKletter == '\0')
error (FATAL, "no scope separator specified in \"--%s\" option", option);
else if (parentKletter == '/')
parentKindex = KIND_GHOST_INDEX;
else if (parentKletter == KIND_WILDCARD_LETTER)
parentKindex = KIND_WILDCARD_INDEX;
else if (parentKletter == KIND_FILE_DEFAULT_LETTER)
error (FATAL,
"the kind letter `%c' in \"--%s\" option is reserved for \"%s\" kind and no separator can be assigned to",
KIND_FILE_DEFAULT_LETTER, option, KIND_FILE_DEFAULT_NAME);
else if (isalpha ((unsigned char) parentKletter))
{
kindDefinition *kdef = getKindForLetter (parser->kindControlBlock, parentKletter);
if (kdef == NULL)
error (FATAL,
"the kind for letter `%c' specified in \"--%s\" option is not defined.",
parentKletter, option);
parentKindex = kdef->id;
}
else
error (FATAL,
"the kind letter `%c` given in \"--%s\" option is not an alphabet",
parentKletter, option);
/*
* Child
*/
if (parentKindex == KIND_GHOST_INDEX)
kletter = p[1];
else
{
if (p[1] != '/')
error (FATAL,
"wrong separator specification in \"--%s\" option: no slash after parent kind letter `%c'",
option, parentKletter);
kletter = p[2];
}
if (kletter == '\0')
error (FATAL, "no child kind letter in \"--%s\" option", option);
else if (kletter == '/')
error (FATAL,
"wrong separator specification in \"--%s\" option: don't specify slash char twice: %s",
option, parameter);
else if (kletter == ':')
error (FATAL,
"no child kind letter in \"--%s\" option", option);
else if (kletter == KIND_WILDCARD_LETTER)
{
if (parentKindex != KIND_WILDCARD_INDEX
&& parentKindex != KIND_GHOST_INDEX)
error (FATAL,
"cannot use wild card for child kind unless parent kind is also wild card or empty");
kindex = KIND_WILDCARD_INDEX;
}
else if (kletter == KIND_FILE_DEFAULT_LETTER)
error (FATAL,
"the kind letter `%c' in \"--%s\" option is reserved for \"%s\" kind and no separator can be assigned to",
KIND_FILE_DEFAULT_LETTER, option, KIND_FILE_DEFAULT_NAME);
else if (isalpha ((unsigned char) kletter))
{
kindDefinition *kdef = getKindForLetter (parser->kindControlBlock, kletter);
if (kdef == NULL)
error (FATAL,
"the kind for letter `%c' specified in \"--%s\" option is not defined.",
kletter, option);
kindex = kdef->id;
}
else
error (FATAL,
"the kind letter `%c` given in \"--%s\" option is not an alphabet",
kletter, option);
/*
* Separator
*/
if (parentKindex == KIND_GHOST_INDEX)
{
if (p[2] != ':')
error (FATAL,
"wrong separator specification in \"--%s\" option: cannot find a colon after child kind: %s",
option, parameter);
separator = p + 3;
}
else
{
if (p[3] != ':')
error (FATAL,
"wrong separator specification in \"--%s\" option: cannot find a colon after child kind: %s",
option, parameter);
separator = p + 4;
}
Assert (parentKindex != KIND_FILE_INDEX);
Assert (kindex != KIND_FILE_INDEX);
defineScopeSeparator (parser->kindControlBlock, kindex, parentKindex, separator);
return true;
}
extern bool processScopesepOption (const char *const option, const char * const parameter)
{
langType language;
language = getLanguageComponentInOption (option, "_scopesep-");
if (language == LANG_IGNORE)
return false;
return processLangDefineScopesep (language, option, parameter);
}
static parserCandidate* parserCandidateNew(unsigned int count CTAGS_ATTR_UNUSED)
{
parserCandidate* candidates;
unsigned int i;
candidates= xMalloc(LanguageCount, parserCandidate);
for (i = 0; i < LanguageCount; i++)
{
candidates[i].lang = LANG_IGNORE;
candidates[i].spec = NULL;
candidates[i].specType = SPEC_NONE;
}
return candidates;
}
/* If multiple parsers are found, return LANG_AUTO */
static unsigned int nominateLanguageCandidates (const char *const key, parserCandidate** candidates)
{
unsigned int count;
langType i;
const char* spec = NULL;
enum specType specType = SPEC_NONE;
*candidates = parserCandidateNew(LanguageCount);
for (count = 0, i = LANG_AUTO; i != LANG_IGNORE; )
{
i = getNameOrAliasesLanguageAndSpec (key, i, &spec, &specType);
if (i != LANG_IGNORE)
{
(*candidates)[count].lang = i++;
(*candidates)[count].spec = spec;
(*candidates)[count++].specType = specType;
}
}
return count;
}
static unsigned int
nominateLanguageCandidatesForPattern(const char *const baseName, parserCandidate** candidates)
{
unsigned int count;
langType i;
const char* spec;
enum specType specType = SPEC_NONE;
*candidates = parserCandidateNew(LanguageCount);
for (count = 0, i = LANG_AUTO; i != LANG_IGNORE; )
{
i = getPatternLanguageAndSpec (baseName, i, &spec, &specType);
if (i != LANG_IGNORE)
{
(*candidates)[count].lang = i++;
(*candidates)[count].spec = spec;
(*candidates)[count++].specType = specType;
}
}
return count;
}
static vString* extractEmacsModeAtFirstLine(MIO* input);
/* The name of the language interpreter, either directly or as the argument
* to "env".
*/
static vString* determineInterpreter (const char* const cmd)
{
vString* const interpreter = vStringNew ();
const char* p = cmd;
do
{
vStringClear (interpreter);
for ( ; isspace ((unsigned char) *p) ; ++p)
; /* no-op */
for ( ; *p != '\0' && ! isspace ((unsigned char) *p) ; ++p)
vStringPut (interpreter, *p);
} while (strcmp (vStringValue (interpreter), "env") == 0);
return interpreter;
}
static vString* extractInterpreter (MIO* input)
{
vString* const vLine = vStringNew ();
const char* const line = readLineRaw (vLine, input);
vString* interpreter = NULL;
if (line != NULL && line [0] == '#' && line [1] == '!')
{
/* "48.2.4.1 Specifying File Variables" of Emacs info:
---------------------------------------------------
In shell scripts, the first line is used to
identify the script interpreter, so you
cannot put any local variables there. To
accommodate this, Emacs looks for local
variable specifications in the _second_
line if the first line specifies an
interpreter. */
interpreter = extractEmacsModeAtFirstLine(input);
if (!interpreter)
{
const char* const lastSlash = strrchr (line, '/');
const char *const cmd = lastSlash != NULL ? lastSlash+1 : line+2;
interpreter = determineInterpreter (cmd);
}
}
vStringDelete (vLine);
return interpreter;
}
static bool isShellZsh (const char *p)
{
p = strstr (p, "sh-set-shell");
if (!p)
return false;
p += strlen("sh-set-shell");
if (*p == ':')
p++;
while (isspace ((unsigned char) *p))
p++;
if (strncmp (p, "\"zsh\"", 5) == 0
|| strncmp (p, "zsh", 3) == 0)
return true;
return false;
}
static vString* determineEmacsModeAtFirstLine (const char* const line)
{
vString* mode = vStringNew ();
const char* p = strstr(line, "-*-");
if (p == NULL)
goto out;
p += strlen("-*-");
for ( ; isspace ((unsigned char) *p) ; ++p)
; /* no-op */
if (strncasecmp(p, "mode:", strlen("mode:")) == 0)
{
/* -*- mode: MODE; -*- */
p += strlen("mode:");
for ( ; isspace ((unsigned char) *p) ; ++p)
; /* no-op */
for ( ; *p != '\0' && isLanguageNameChar ((unsigned char) *p) ; ++p)
vStringPut (mode, *p);
if ((strcmp(vStringValue (mode), "sh") == 0
|| strcmp(vStringValue (mode), "shell-script") == 0)
&& isShellZsh (p))
vStringCopyS (mode, "Zsh");
}
else
{
/* -*- MODE -*- */
const char* end = strstr (p, "-*-");
if (end == NULL)
goto out;
for ( ; p < end && isLanguageNameChar ((unsigned char) *p) ; ++p)
vStringPut (mode, *p);
for ( ; isspace ((unsigned char) *p) ; ++p)
; /* no-op */
if (strncmp(p, "-*-", strlen("-*-")) != 0)
vStringClear (mode);
}
vStringLower (mode);
out:
return mode;
}
static vString* extractEmacsModeAtFirstLine(MIO* input)
{
vString* const vLine = vStringNew ();
const char* const line = readLineRaw (vLine, input);
vString* mode = NULL;
if (line != NULL)
mode = determineEmacsModeAtFirstLine (line);
vStringDelete (vLine);
if (mode && (vStringLength(mode) == 0))
{
vStringDelete(mode);
mode = NULL;
}
return mode;
}
static vString* determineEmacsModeAtEOF (MIO* const fp)
{
vString* const vLine = vStringNew ();
const char* line;
bool headerFound = false;
const char* p;
vString* mode = vStringNew ();
bool is_shell_mode = false;
while ((line = readLineRaw (vLine, fp)) != NULL)
{
if (headerFound && ((p = strstr (line, "mode:")) != NULL))
{
vStringClear (mode);
headerFound = false;
p += strlen ("mode:");
for ( ; isspace ((unsigned char) *p) ; ++p)
; /* no-op */
for ( ; *p != '\0' && isLanguageNameChar ((unsigned char) *p) ; ++p)
vStringPut (mode, *p);
is_shell_mode = ((strcasecmp (vStringValue (mode), "sh") == 0
|| strcasecmp (vStringValue (mode), "shell-script") == 0));
}
else if (headerFound && (p = strstr(line, "End:")))
headerFound = false;
else if (strstr (line, "Local Variables:"))
headerFound = true;
else if (is_shell_mode && (p = strstr (line, "sh-set-shell")))
{
p += strlen("sh-set-shell");
while (isspace ((unsigned char) *p))
p++;
if (strncmp (p, "\"zsh\"", 5) == 0)
vStringCopyS (mode, "Zsh");
}
}
vStringDelete (vLine);
return mode;
}
static vString* extractEmacsModeLanguageAtEOF (MIO* input)
{
vString* mode;
/* "48.2.4.1 Specifying File Variables" of Emacs info:
---------------------------------------------------
you can define file local variables using a "local
variables list" near the end of the file. The start of the
local variables list should be no more than 3000 characters
from the end of the file, */
mio_seek(input, -3000, SEEK_END);
mode = determineEmacsModeAtEOF (input);
if (mode && (vStringLength (mode) == 0))
{
vStringDelete (mode);
mode = NULL;
}
return mode;
}
static vString* determineVimFileType (const char *const modeline)
{
/* considerable combinations:
--------------------------
... filetype=
... ft= */
unsigned int i;
const char* p;
const char* const filetype_prefix[] = {"filetype=", "ft="};
vString* const filetype = vStringNew ();
for (i = 0; i < ARRAY_SIZE(filetype_prefix); i++)
{
if ((p = strrstr(modeline, filetype_prefix[i])) == NULL)
continue;
p += strlen(filetype_prefix[i]);
for ( ; *p != '\0' && isalnum ((unsigned char) *p) ; ++p)
vStringPut (filetype, *p);
break;
}
return filetype;
}
static vString* extractVimFileTypeCommon(MIO* input, bool eof)
{
/* http://vimdoc.sourceforge.net/htmldoc/options.html#modeline
[text]{white}{vi:|vim:|ex:}[white]se[t] {options}:[text]
options=> filetype=TYPE or ft=TYPE
'modelines' 'mls' number (default 5)
global
{not in Vi}
If 'modeline' is on 'modelines' gives the number of lines that is
checked for set commands. */
vString* filetype = NULL;
#define RING_SIZE 5
vString* ring[RING_SIZE];
int i, j;
unsigned int k;
const char* const prefix[] = {
"vim:", "vi:", "ex:"
};
for (i = 0; i < RING_SIZE; i++)
ring[i] = vStringNew ();
i = 0;
while ((readLineRaw (ring[i++], input)) != NULL)
if (i == RING_SIZE)
{
i = 0;
if (!eof)
{
i++;
break;
}