-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclang.c
3381 lines (3061 loc) · 105 KB
/
clang.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
/*
* C mode for QEmacs.
*
* Copyright (c) 2001-2002 Fabrice Bellard.
* Copyright (c) 2002-2020 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "qe.h"
/* C mode flavors */
enum {
CLANG_C,
CLANG_CPP,
CLANG_C2,
CLANG_OBJC,
CLANG_CSHARP,
CLANG_AWK,
CLANG_CSS,
CLANG_JSON,
CLANG_JS,
CLANG_TS,
CLANG_JSPP,
CLANG_KOKA,
CLANG_AS,
CLANG_JAVA,
CLANG_SCALA,
CLANG_PHP,
CLANG_GO,
CLANG_D,
CLANG_LIMBO,
CLANG_CYCLONE,
CLANG_CH,
CLANG_SQUIRREL,
CLANG_ICI,
CLANG_JSX,
CLANG_HAXE,
CLANG_DART,
CLANG_PIKE,
CLANG_IDL,
CLANG_CALC,
CLANG_ENSCRIPT,
CLANG_QSCRIPT,
CLANG_ELASTIC,
CLANG_JED,
CLANG_CSL,
CLANG_NEKO,
CLANG_NML,
CLANG_ALLOY,
CLANG_SCILAB,
CLANG_KOTLIN,
CLANG_CBANG,
CLANG_VALA,
CLANG_PAWN,
CLANG_CMINUS,
CLANG_GMSCRIPT,
CLANG_WREN,
CLANG_JACK,
CLANG_SMAC,
CLANG_RUST,
CLANG_SWIFT,
CLANG_ICON,
CLANG_GROOVY,
CLANG_VIRGIL,
CLANG_V,
CLANG_FLAVOR = 0x3F,
};
/* C mode options */
#define CLANG_LEX 0x00200
#define CLANG_YACC 0x00400
#define CLANG_REGEX 0x00800
#define CLANG_WLITERALS 0x01000
#define CLANG_PREPROC 0x02000
#define CLANG_CAP_TYPE 0x04000 /* Mixed case initial cap is type */
#define CLANG_STR3 0x08000 /* Support """strings""" */
#define CLANG_LINECONT 0x10000 /* support \<newline> as line continuation */
#define CLANG_CC 0x13100 /* all C language features */
static const char c_keywords[] = {
"auto|break|case|const|continue|default|do|else|enum|extern|for|goto|"
"if|inline|register|restrict|return|sizeof|static|struct|switch|"
"typedef|union|volatile|while|"
/* C99 and C11 keywords */
"_Alignas|_Alignof|_Atomic|_Generic|_Noreturn|_Static_assert|_Thread_local|"
};
static const char c_types[] = {
"char|double|float|int|long|unsigned|short|signed|void|va_list|"
"_Bool|_Complex|_Imaginary|FILE|"
};
static const char c_extensions[] = {
"c|h|i|C|H|I|" /* C language */
/* Other C flavors */
"e|" /* EEL */
"ecp|" /* Informix embedded C */
"pgc|" /* Postgres embedded C */
"pcc|" /* Oracle C++ */
"h.in|c.in|" /* preprocessed C input (should use more generic approach) */
};
/* grab a C identifier from a uint buf, return char count. */
static int get_c_identifier(char *buf, int buf_size, const unsigned int *p,
int flavor)
{
unsigned int c;
int i, j;
i = j = 0;
c = p[i];
if (qe_isalpha_(c)
|| c == '$'
|| (c == '@' && flavor != CLANG_PIKE)
|| (flavor == CLANG_RUST && c >= 128)) {
for (;;) {
if (j < buf_size - 1) {
/* XXX: utf-8 bug */
buf[j++] = (c < 0xFF) ? c : 0xFF;
}
i++;
c = p[i];
if (c == '-' && flavor == CLANG_CSS)
continue;
if (qe_isalnum_(c))
continue;
if (flavor == CLANG_RUST && c >= 128)
continue;
if (c == ':' && p[i + 1] == ':'
&& flavor == CLANG_CPP
&& qe_isalpha_(p[i + 2])) {
if (j < buf_size - 2) {
buf[j++] = c;
buf[j++] = c;
}
i += 2;
c = p[i];
continue;
}
break;
}
}
buf[j] = '\0';
return i;
}
static int qe_haslower(const char *str) {
while (*str) {
if (qe_islower(*str++)) return 1;
}
return 0;
}
enum {
C_STYLE_DEFAULT = 0,
C_STYLE_PREPROCESS = QE_STYLE_PREPROCESS,
C_STYLE_COMMENT = QE_STYLE_COMMENT,
C_STYLE_REGEX = QE_STYLE_STRING_Q,
C_STYLE_STRING = QE_STYLE_STRING,
C_STYLE_STRING_Q = QE_STYLE_STRING_Q,
C_STYLE_STRING_BQ = QE_STYLE_STRING,
C_STYLE_NUMBER = QE_STYLE_NUMBER,
C_STYLE_KEYWORD = QE_STYLE_KEYWORD,
C_STYLE_TYPE = QE_STYLE_TYPE,
C_STYLE_FUNCTION = QE_STYLE_FUNCTION,
C_STYLE_VARIABLE = QE_STYLE_VARIABLE,
};
/* c-mode colorization states */
enum {
IN_C_COMMENT = 0x01, /* multiline comment */
IN_C_COMMENT1 = 0x02, /* single line comment with \ at EOL */
IN_C_STRING = 0x04, /* double-quoted string */
IN_C_STRING_Q = 0x08, /* single-quoted string */
IN_C_STRING_BQ = 0x10, /* back-quoted string (go's multi-line string) */
/* """ multiline quoted string (dart, scala) */
IN_C_PREPROCESS = 0x20, /* preprocessor directive with \ at EOL */
IN_C_REGEX = 0x40, /* regex */
IN_C_CHARCLASS = 0x80, /* regex char class */
IN_C_COMMENT_D = 0x700, /* nesting D comment level (max 7 deep) */
IN_C_COMMENT_D_SHIFT = 8,
};
static void c_colorize_line(QEColorizeContext *cp,
unsigned int *str, int n, ModeDef *syn)
{
int i = 0, start, i1, i2, indent, level;
int c, style, style0, style1, type_decl, klen, delim, prev, tag;
char kbuf[64];
int mode_flags = syn->colorize_flags;
int flavor = (mode_flags & CLANG_FLAVOR);
int state = cp->colorize_state;
for (indent = 0; qe_isblank(str[indent]); indent++)
continue;
tag = !indent && cp->s->mode == syn;
start = i;
type_decl = 0;
c = 0;
style0 = style = C_STYLE_DEFAULT;
if (i >= n)
goto the_end;
if (state) {
/* if already in a state, go directly in the code parsing it */
if (state & IN_C_PREPROCESS)
style0 = style = C_STYLE_PREPROCESS;
if (state & IN_C_COMMENT)
goto parse_comment;
if (state & IN_C_COMMENT1)
goto parse_comment1;
if (state & IN_C_COMMENT_D)
goto parse_comment_d;
if (state & IN_C_STRING)
goto parse_string;
if (state & IN_C_STRING_Q)
goto parse_string_q;
if ((state & IN_C_STRING_BQ)
&& (flavor == CLANG_SCALA || flavor == CLANG_DART))
goto parse_string3;
if (state & IN_C_STRING_BQ)
goto parse_string_bq;
if (state & IN_C_REGEX) {
delim = '/';
goto parse_regex;
}
}
while (i < n) {
start = i;
c = str[i++];
switch (c) {
case '/':
if (str[i] == '*') {
/* normal comment */
/* XXX: support nested comments for Scala */
i++;
parse_comment:
style = C_STYLE_COMMENT;
state |= IN_C_COMMENT;
for (; i < n; i++) {
if (str[i] == '*' && str[i + 1] == '/') {
i += 2;
state &= ~IN_C_COMMENT;
style = style0;
break;
}
}
SET_COLOR(str, start, i, C_STYLE_COMMENT);
continue;
} else
if (str[i] == '/') {
/* line comment */
parse_comment1:
style = C_STYLE_COMMENT;
state |= IN_C_COMMENT1;
i = n;
SET_COLOR(str, start, i, C_STYLE_COMMENT);
continue;
}
if (flavor == CLANG_D && (str[i] == '+')) {
/* D language nesting long comment */
i++;
state |= (1 << IN_C_COMMENT_D_SHIFT);
parse_comment_d:
style = C_STYLE_COMMENT;
level = (state & IN_C_COMMENT_D) >> IN_C_COMMENT_D_SHIFT;
while (i < n) {
if (str[i] == '/' && str[i + 1] == '+') {
i += 2;
level++;
} else
if (str[i] == '+' && str[i + 1] == '/') {
i += 2;
level--;
if (level == 0) {
style = style0;
break;
}
} else {
i++;
}
}
state = (state & ~IN_C_COMMENT_D) |
(min(level, 7) << IN_C_COMMENT_D_SHIFT);
SET_COLOR(str, start, i, C_STYLE_COMMENT);
continue;
}
/* XXX: should use more context to tell regex from divide */
prev = ' ';
for (i1 = start; i1 > indent; ) {
prev = str[--i1] & CHAR_MASK;
if (!qe_isblank(prev))
break;
}
if ((mode_flags & CLANG_REGEX)
&& !qe_findchar("])", prev)
&& (qe_findchar(" [({},;=<>!~^&|*/%?:", prev)
|| (str[i1] >> STYLE_SHIFT) == C_STYLE_KEYWORD
|| (str[i] != ' ' && (str[i] != '=' || str[i + 1] != ' ')
&& !(qe_isalnum(prev) || prev == ')')))) {
/* parse regex */
state |= IN_C_REGEX;
delim = '/';
parse_regex:
style = C_STYLE_REGEX;
while (i < n) {
c = str[i++];
if (c == '\\') {
if (i < n) {
i += 1;
}
} else
if (state & IN_C_CHARCLASS) {
if (c == ']') {
state &= ~IN_C_CHARCLASS;
}
/* ECMA 5: ignore '/' inside char classes */
} else {
if (c == '[') {
state |= IN_C_CHARCLASS;
} else
if (c == delim) {
while (qe_isalnum_(str[i])) {
i++;
}
state &= ~IN_C_REGEX;
style = style0;
break;
}
}
}
SET_COLOR(str, start, i, C_STYLE_REGEX);
continue;
}
break;
case '%':
if (flavor == CLANG_JED) {
goto parse_comment1;
}
break;
case '#': /* preprocessor */
if (start == 0 && str[i] == '!') {
/* recognize a shebang comment line */
style = style0 = C_STYLE_PREPROCESS;
i = n;
SET_COLOR(str, start, i, C_STYLE_PREPROCESS);
break;
}
if (mode_flags & CLANG_PREPROC) {
state |= IN_C_PREPROCESS;
style = style0 = C_STYLE_PREPROCESS;
}
if (flavor == CLANG_D) {
/* only #line is supported, but can occur anywhere */
state |= IN_C_PREPROCESS;
style = style0 = C_STYLE_PREPROCESS;
}
if (flavor == CLANG_PHP || flavor == CLANG_LIMBO || flavor == CLANG_SQUIRREL) {
goto parse_comment1;
}
if (flavor == CLANG_ICI) {
delim = '#';
goto parse_regex;
}
if (flavor == CLANG_HAXE || flavor == CLANG_CBANG) {
i += get_c_identifier(kbuf, countof(kbuf), str + i, flavor);
// XXX: check for proper preprocessor directive?
SET_COLOR(str, start, i, C_STYLE_PREPROCESS);
continue;
}
if (flavor == CLANG_PIKE) {
if (str[i] == '\"') {
i++;
goto parse_string;
}
state |= IN_C_PREPROCESS;
style = style0 = C_STYLE_PREPROCESS;
}
break;
case 'L': /* wide character and string literals */
if (mode_flags & CLANG_WLITERALS) {
if (str[i] == '\'') {
i++;
goto parse_string_q;
}
if (str[i] == '\"') {
i++;
goto parse_string;
}
}
goto normal;
// case 'r':
/* XXX: D language r" wysiwyg chars " */
// case 'X':
/* XXX: D language X" hex string chars " */
// case 'q':
/* XXX: D language q" delim wysiwyg chars delim " */
/* XXX: D language q{ tokens } */
case '\'': /* character constant */
if (flavor == CLANG_SCILAB)
goto normal;
parse_string_q:
state |= IN_C_STRING_Q;
style1 = C_STYLE_STRING_Q;
delim = '\'';
goto string;
case '`':
if (flavor == CLANG_SCALA || flavor == CLANG_GMSCRIPT) {
/* scala quoted identifier */
while (i < n) {
c = str[i++];
if (c == '`')
break;
}
SET_COLOR(str, start, i, C_STYLE_VARIABLE);
continue;
}
if (flavor == CLANG_GO || flavor == CLANG_D) {
/* go language multi-line string, no escape sequences */
parse_string_bq:
state |= IN_C_STRING_BQ;
style1 = C_STYLE_STRING_BQ;
delim = '`';
while (i < n) {
c = str[i++];
if (c == delim) {
state &= ~IN_C_STRING_BQ;
break;
}
}
if (state & IN_C_PREPROCESS)
style1 = C_STYLE_PREPROCESS;
SET_COLOR(str, start, i, style1);
continue;
}
break;
case '@':
if (flavor == CLANG_C2) {
// XXX: should colorize attributes as C_STYLE_PREPROC
// @(...)
}
if (flavor == CLANG_CSHARP || flavor == CLANG_SQUIRREL) {
if (str[i] == '\"') {
/* Csharp and Squirrel Verbatim strings */
/* ignore escape sequences and newlines */
state |= IN_C_STRING; // XXX: IN_RAW_STRING
style1 = C_STYLE_STRING;
delim = str[i];
style = style1;
for (i++; i < n;) {
c = str[i++];
if (c == delim) {
if (str[i] == (unsigned int)c) {
i++;
continue;
}
state &= ~(IN_C_STRING | IN_C_STRING_Q | IN_C_STRING_BQ);
style = style0;
break;
}
}
SET_COLOR(str, start, i, style1);
continue;
}
}
if ((flavor == CLANG_JAVA || flavor == CLANG_SCALA) && qe_isalpha(str[i])) {
/* Java annotations */
while (qe_isalnum_(str[i]) || str[i] == '.')
i++;
if (start == 0 || str[start - 1] != '.')
SET_COLOR(str, start, i, C_STYLE_PREPROCESS);
continue;
}
goto normal;
case '\"': /* string literal */
if ((mode_flags & CLANG_STR3)
&& (str[i] == '\"' && str[i + 1] == '\"')) {
/* multiline """ quoted string */
i += 2;
parse_string3:
state |= IN_C_STRING_BQ;
style1 = C_STYLE_STRING;
while (i < n) {
c = str[i++];
if (c == '\\' && flavor != CLANG_KOTLIN) {
if (i < n)
i++;
} else
if (c == '\"' && str[i] == '\"' && str[i + 1] == '\"') {
state &= ~IN_C_STRING_BQ;
style = style0;
break;
}
}
SET_COLOR(str, start, i, style1);
continue;
}
parse_string:
state |= IN_C_STRING;
style1 = C_STYLE_STRING;
delim = '\"';
string:
style = style1;
while (i < n) {
c = str[i++];
if (c == '\\' && flavor != CLANG_SCILAB) {
if (i >= n)
break;
i++;
} else
if (c == delim) {
if (flavor == CLANG_SCILAB && (int)str[i] == delim) {
i++;
continue;
}
state &= ~(IN_C_STRING | IN_C_STRING_Q | IN_C_STRING_BQ);
style = style0;
break;
}
}
if (flavor == CLANG_D) {
/* ignore optional string postfix */
if (qe_findchar("cwd", str[i]))
i++;
}
if (state & IN_C_PREPROCESS)
style1 = C_STYLE_PREPROCESS;
SET_COLOR(str, start, i, style1);
continue;
case '=':
/* exit type declaration */
/* does not handle this: int i = 1, j = 2; */
type_decl = 0;
break;
case '<': /* JavaScript extension */
if (flavor == CLANG_JS) {
if (str[i] == '!' && str[i + 1] == '-' && str[i + 2] == '-')
goto parse_comment1;
}
break;
case '(':
case '{':
tag = 0;
break;
default:
normal:
if (state & IN_C_PREPROCESS)
break;
if (qe_isdigit(c)) {
/* XXX: should parse actual number syntax */
/* XXX: D ignores embedded '_' and accepts l,u,U,f,F,i suffixes */
/* XXX: Java accepts 0b prefix for binary literals,
* ignores '_' between digits and accepts 'l' or 'L' suffixes */
/* scala ignores '_' in integers */
/* XXX: should parse decimal and hex floating point syntaxes */
/* 2 dots in a row are a range operator, not a decimal point */
while (qe_isalnum_(str[i]) || (str[i] == '.' && str[i + 1] != '.')) {
i++;
}
SET_COLOR(str, start, i, C_STYLE_NUMBER);
continue;
}
if (qe_isalpha_(c) || c == '$' || (c == '@' && flavor != CLANG_PIKE)) {
/* XXX: should support :: */
klen = get_c_identifier(kbuf, countof(kbuf), str + start, flavor);
i = start + klen;
if (strfind(syn->keywords, kbuf)
|| ((mode_flags & CLANG_CC) && strfind(c_keywords, kbuf))
|| ((flavor == CLANG_CSS) && str[i] == ':')) {
SET_COLOR(str, start, i, C_STYLE_KEYWORD);
continue;
}
i1 = i;
while (qe_isblank(str[i1]))
i1++;
i2 = i1;
while (str[i2] == '*' || qe_isblank(str[i2]))
i2++;
if (tag && qe_findchar("({[,;=", str[i1])) {
eb_add_property(cp->b, cp->offset + start,
QE_PROP_TAG, qe_strdup(kbuf));
}
if ((start == 0 || str[start - 1] != '.')
&& (!qe_findchar(".(:", str[i]) || flavor == CLANG_PIKE)
&& (strfind(syn->types, kbuf)
|| ((mode_flags & CLANG_CC) && strfind(c_types, kbuf))
|| (((mode_flags & CLANG_CC) || (flavor == CLANG_D)) &&
strend(kbuf, "_t", NULL))
|| ((mode_flags & CLANG_CAP_TYPE) &&
qe_isupper(c) && qe_haslower(kbuf))
|| (flavor == CLANG_HAXE && qe_isupper(c) && qe_haslower(kbuf) &&
(start == 0 || !qe_findchar("(", str[start - 1]))))) {
/* if not cast, assume type declaration */
if (str[i2] != ')') {
type_decl = 1;
}
style1 = C_STYLE_TYPE;
if (str[i1] == '(' && flavor != CLANG_PIKE) {
/* function style cast */
style1 = C_STYLE_FUNCTION; //C_STYLE_KEYWORD;
}
SET_COLOR(str, start, i, style1);
continue;
}
if (str[i1] == '(') {
/* function call */
/* XXX: different styles for call and definition */
SET_COLOR(str, start, i, C_STYLE_FUNCTION);
continue;
}
if ((mode_flags & CLANG_CC) || flavor == CLANG_JAVA) {
/* assume typedef if starting at first column */
if (start == 0 && qe_isalpha_(str[i]))
type_decl = 1;
if (type_decl) {
if (start == 0) {
/* assume type if first column */
SET_COLOR(str, start, i, C_STYLE_TYPE);
} else {
SET_COLOR(str, start, i, C_STYLE_VARIABLE);
}
}
}
continue;
}
break;
}
SET_COLOR1(str, start, style);
}
the_end:
if (state & (IN_C_COMMENT | IN_C_COMMENT1 | IN_C_COMMENT_D |
IN_C_PREPROCESS |
IN_C_STRING | IN_C_STRING_Q | IN_C_STRING_BQ)) {
/* set style on eol char */
SET_COLOR1(str, n, style);
}
/* strip state if not overflowing from a comment */
if (!(state & IN_C_COMMENT) &&
(!(mode_flags & CLANG_LINECONT) || n <= 0 || (str[n - 1] & CHAR_MASK) != '\\')) {
state &= ~(IN_C_COMMENT1 | IN_C_PREPROCESS);
}
cp->colorize_state = state;
}
#define MAX_STACK_SIZE 64
/* gives the position of the first non white space character in
buf. TABs are counted correctly */
static int find_indent1(EditState *s, unsigned int *buf)
{
unsigned int *p;
int pos, c, tw;
tw = s->b->tab_width > 0 ? s->b->tab_width : 8;
p = buf;
pos = 0;
for (;;) {
c = *p++;
if (c == '\t')
pos += tw - (pos % tw);
else if (c == ' ')
pos++;
else if (c == '\f')
pos = 0;
else
break;
}
return pos;
}
static int find_pos(EditState *s, unsigned int *buf, int size)
{
int pos, c, tw, i;
tw = s->b->tab_width > 0 ? s->b->tab_width : 8;
pos = 0;
for (i = 0; i < size; i++) {
c = buf[i];
if (c == '\t') {
pos += tw - (pos % tw);
} else {
/* simplistic case: assume single width characters */
pos++;
}
}
return pos;
}
enum {
INDENT_NORM,
INDENT_FIND_EQ,
};
/* Normalize indentation at <offset>, return offset past indentation */
static int normalize_indent(EditState *s, int offset, int indent)
{
int ntabs, nspaces, update, offset0, offset1;
if (indent < 0)
indent = 0;
ntabs = 0;
nspaces = indent;
if (s->indent_tabs_mode) {
int tw = s->b->tab_width > 0 ? s->b->tab_width : 8;
ntabs = nspaces / tw;
nspaces = nspaces % tw;
}
offset0 = offset;
for (update = 0;;) {
int c = eb_nextc(s->b, offset1 = offset, &offset);
if (c == '\t') {
if (!update && ntabs > 0) {
ntabs--;
offset0 = offset;
} else {
update = 1;
}
continue;
}
if (c == ' ') {
if (!update && ntabs == 0 && nspaces > 0) {
nspaces--;
offset0 = offset;
} else {
update = 1;
}
continue;
}
break;
}
if (offset1 > offset0)
eb_delete_range(s->b, offset0, offset1);
if (ntabs)
offset0 += eb_insert_uchars(s->b, offset0, '\t', ntabs);
if (nspaces)
offset0 += eb_insert_spaces(s->b, offset0, nspaces);
return offset0;
}
/* Check if line starts with a label or a switch case */
static int c_line_has_label(EditState *s, const unsigned int *buf, int len,
const QETermStyle *sbuf)
{
char kbuf[64];
int i, j, style;
for (i = 0; i < len && qe_isblank(buf[i]); i++)
continue;
style = sbuf[i];
if (style == C_STYLE_COMMENT
|| style == C_STYLE_STRING
|| style == C_STYLE_STRING_Q
|| style == C_STYLE_PREPROCESS)
return 0;
j = get_c_identifier(kbuf, countof(kbuf), buf + i, CLANG_C);
if (style == C_STYLE_KEYWORD && strfind("case|default", kbuf))
return 1;
for (i += j; i < len && qe_isblank(buf[i]); i++)
continue;
return (buf[i] == ':');
}
/* indent a line of C code starting at <offset> */
/* Rationale:
- determine the current line indentation from the current expression or
the indentation of the previous complete statement.
- scan backwards previous lines, ignoring blank and comment lines.
- scan backwards for a block start, skipping fully bracketed code fragments
or a previous statement end and the ne before that.
- if the block start char is a `(`, try and align the line at the same offset
as the code fragment following the `(`.
- if the block start char is a `{` or a `[`, indent the line one level down from
the line where the block starts
- if the previous statement is an `if|else|do|for|while|switch`, indent the line one level
- if the line starts with a label, unindent by one level + c_label_offset
- if the previous line starts with a label, increment the previous indent by one level - c_label_offset
- by default, indent the line like the previous code line,
*/
static void c_indent_line(EditState *s, int offset0)
{
int offset, offset1, offsetl, c, pos, line_num, col_num;
int i, eoi_found, len, pos1, lpos, style, line_num1, state;
int off, qe__unused__ found_semi, found_comma, has_else;
unsigned int buf[COLORED_MAX_LINE_SIZE];
QETermStyle sbuf[COLORED_MAX_LINE_SIZE];
unsigned char stack[MAX_STACK_SIZE];
char kbuf[64], *q;
int stack_ptr;
/* find start of line */
eb_get_pos(s->b, &line_num, &col_num, offset0);
line_num1 = line_num;
offset = eb_goto_bol(s->b, offset0);
/* now find previous lines and compute indent */
pos = 0;
lpos = -1; /* position of the last instruction start */
offsetl = offset;
eoi_found = 0;
found_semi = found_comma = has_else = 0;
stack_ptr = 0;
state = INDENT_NORM;
for (;;) {
if (offsetl == 0)
break;
line_num--;
offsetl = eb_prev_line(s->b, offsetl);
/* XXX: deal with truncation */
len = get_colorized_line(s, buf, countof(buf), sbuf,
offsetl, &offset1, line_num);
/* get current indentation of this line, adjust if it has a label */
pos1 = find_indent1(s, buf);
/* ignore empty and preprocessor lines */
if (pos1 == len || sbuf[0] == C_STYLE_PREPROCESS)
continue;
if (c_line_has_label(s, buf, len, sbuf)) {
pos1 = pos1 - s->qe_state->c_label_indent + s->indent_size;
}
/* scan the line from end to start */
for (off = len; off-- > 0;) {
c = buf[off];
style = sbuf[off];
/* skip strings or comments */
if (style == C_STYLE_COMMENT
|| style == C_STYLE_STRING
|| style == C_STYLE_STRING_Q
|| style == C_STYLE_PREPROCESS) {
continue;
}
if (state == INDENT_FIND_EQ) {
/* special case to search '=' or ; before { to know if
we are in data definition */
/* XXX: why do we need this special case? */
if (c == '=') {
/* data definition case */
pos = lpos;
goto end_parse;
}
if (c == ';') {
/* normal instruction case */
goto check_instr;
}
continue;
}
if (style == C_STYLE_KEYWORD) {
int off0, off1;
/* special case for if/for/while */
off1 = off;
while (off > 0 && sbuf[off - 1] == C_STYLE_KEYWORD)
off--;
off0 = off;
if (stack_ptr == 0) {
q = kbuf;
while (q < kbuf + countof(kbuf) - 1 && off0 <= off1) {
*q++ = buf[off0++];
}
*q = '\0';
if (!eoi_found && strfind("if|for|while|do|switch|foreach", kbuf)) {
pos = pos1 + s->indent_size;
goto end_parse;
}
if (has_else == 0)
has_else = strequal(kbuf, "else") ? 1 : -1;
lpos = pos1;
}
} else {
if (has_else == 0)
has_else = -1;
switch (c) {
case '}':
if (stack_ptr < MAX_STACK_SIZE)
stack[stack_ptr] = c;
stack_ptr++;
goto check_instr;
case '{':
if (stack_ptr == 0) {
/* start of a block: check if object definition or code block */
if (found_comma) {
pos = pos1;
eoi_found = 1;
goto end_parse;
}
if (lpos == -1) {
pos = pos1 + s->indent_size;
eoi_found = 1;
goto end_parse;
} else {
state = INDENT_FIND_EQ;
}
} else {
--stack_ptr;
if (stack_ptr < MAX_STACK_SIZE && stack[stack_ptr] != '}') {
/* XXX: syntax check ? */
/* XXX: should set mark and complain */
goto check_instr;
}
goto check_instr;
}
break;
case ')':
case ']':
if (stack_ptr < MAX_STACK_SIZE)
stack[stack_ptr] = c;
stack_ptr++;
break;
case '(':
case '[':
if (stack_ptr == 0) {
pos = find_pos(s, buf, off) + 1;
goto end_parse;
} else {
int matchc = (c == '(') ? ')' : ']';
--stack_ptr;
if (stack_ptr < MAX_STACK_SIZE && stack[stack_ptr] != matchc) {
/* XXX: syntax check ? */
/* XXX: should set mark and complain */
pos = pos1;
goto end_parse;
}
}
break;
case ' ':
case '\f':
case '\t':
case '\n':
break;
case ',':
if (stack_ptr == 0) {
found_comma = 1;
}
break;
//case '=':
// if (p[1] == ' ' && !eoi_found && stack_ptr == 0) {
// pos = find_pos(s, buf, p - buf) + 2;
// goto end_parse;
//}
//break;
case ';':
/* level test needed for 'for(;;)' */
if (stack_ptr == 0) {
found_semi = 1;
/* ; { or } are found before an instruction */
check_instr:
if (lpos >= 0) {
/* start of instruction already found */
pos = lpos;
if (!eoi_found)
pos += s->indent_size;
goto end_parse;
}
eoi_found = 1;
}
break;
case ':':
/* a label line is ignored: regular, case and default labels
* are assumed to have no preceding space
*/
/* XXX: should handle labels differently:
measure the indent and adjust by label_indent */
if (style == C_STYLE_DEFAULT
&& (off == 0 || !qe_isspace(buf[off - 1]))) {
off = 0;
}
break;
default:
if (stack_ptr == 0)
lpos = pos1;
break;
}
}
}
if (pos1 == 0 && len > 0) {
style = sbuf[0];