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
|
#!/usr/bin/tclsh
# Part of MCU 8051 IDE ( http://mcu8051ide.sf.net )
############################################################################
# Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 by Martin Ošmera #
# martin.osmera@gmail.com #
# #
# This program is free software; you can redistribute it and#or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program 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 General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
############################################################################
# >>> File inclusion guard
if { ! [ info exists _CSYNTAXHIGHLIGHT_TCL ] } {
set _CSYNTAXHIGHLIGHT_TCL _
# <<< File inclusion guard
# --------------------------------------------------------------------------
# DESCRIPTION
# Implements syntax highlighting interface for ISO C language
# --------------------------------------------------------------------------
namespace eval CsyntaxHighlight {
variable editor ;# Widget: Editor text widget
variable line_number ;# Int: Line number
variable line_content ;# String: Line content
variable line_start ;# Index of line start
variable line_end ;# Index of line end
variable validation_L0 1 ;# Bool: Basic validation enabled
variable validation_L1 1 ;# Bool: Advanced validation enabled
# List of compiler directives
variable directives {
#define #error #include
#if #ifdef #ifndef
#else #elif #endif
#line #pragma #undef
#warning
}
# List of data type specifiers
variable data_types {
void int float double char signed unsigned
long short uchar ushort uint ulong const
export extern static mutable volatile
__data __near __xdata __far __idata __pdata __code
__bit __sfr __sfr16 __sfr32 __sbit __at
}
# List of C keywords
variable keywords {
auto break case _endasm while union continue
default do else enum sizeof for namespace
if goto return struct switch typedef using
_asm __asm __endasm
}
# List of doxygen tags -- No argument
variable doxy_tags_type0 {
@return @see @sa @arg
@li @nosubgrouping @subpage @f$
@f[ @f] @interface
}
# List of doxygen tags -- Word after tag
variable doxy_tags_type1 {
@param @defgroup @addtogroup @weakgroup
@ref @page @struct @union
@enum @def @file @namespace
@package
}
# List of doxygen tags -- Name after tag
variable doxy_tags_type2 {
@brief @ingroup @name @mainpage
@fn @var @typedef @author
@authors
}
# List of HTML tags (HTML 4.0 Strict)
variable html_tags {
a abbr acronym address
area b base bdo
big blockquote body br
button caption cite code
col colgroup dd del
dfn div dl dt
em fieldset form frame
frameset h1 h2 h3
h4 h5 h6 head
hr html i img
input ins kbd label
legend li link map
meta noscript object ol
optgroup option p param
pre q samp script
select small span strong
style sub sup table
tbody td textarea tfoot
th thead title tr
tt ul var
}
## Highlight pattern - highlight tags definition
# {
# {tag_name ?foreground? ?overstrike? ?italic? ?bold?}
# }
variable highlight_tags {
{tag_c_keyword #0000DD 0 0 1}
{tag_c_data_type #00CC00 0 0 1}
{tag_c_dec #0000FF 0 0 0}
{tag_c_hex #8800FF 0 0 0}
{tag_c_bin #5555AA 0 0 0}
{tag_c_oct #883300 0 0 0}
{tag_c_char #DD00DD 0 0 0}
{tag_c_float #AA00AA 0 0 0}
{tag_c_string #BB0000 0 0 0}
{tag_c_string_char #DD00DD 0 0 0}
{tag_c_comment #888888 0 1 0}
{tag_c_symbol #FF0000 0 0 1}
{tag_c_bracket #EE6600 0 0 1}
{tag_c_preprocessor #008800 0 0 0}
{tag_c_directive #558800 0 0 0}
{tag_c_prep_lib #885500 0 0 0}
{tag_normal #000000 0 0 0}
{tag_c_dox_comment #4444FF 0 1 0}
{tag_c_dox_tag #AA00DD 0 0 1}
{tag_c_dox_word #0088FF 0 0 1}
{tag_c_dox_name #FF0000 0 0 0}
{tag_c_dox_html #000000 0 0 1}
{tag_c_dox_harg #008800 0 0 0}
{tag_c_dox_hargval #DD0000 0 0 0}
}
## Define highlighting text tags in the given text widget
# @parm Widget text_widget - ID of the target text widget
# @parm Int fontSize - font size
# @parm String fontFamily - font family
# @parm List highlight=default - Highlighting tags definition
# @parm Bool nobold=0 - Ignore bold flag
# @return void
proc create_tags {text_widget fontSize fontFamily {highlight {}} {nobold 0}} {
variable highlight_tags ;# Highlight tags definition
# Handle arguments
if {$highlight == {}} { ;# highlighting definition
set highlight $highlight_tags
}
# Iterate over highlighting tags definition
foreach item $highlight {
# Create array of tag attributes
for {set i 0} {$i < 5} {incr i} {
set tag($i) [lindex $item $i]
}
# Foreground color
if {$tag(1) == {}} {
set tag(1) black
}
# Fonr slant
if {$tag(3) == 1} {
set tag(3) italic
} else {
set tag(3) roman
}
# Font weight
if {$tag(4) == 1 && !$nobold} {
set tag(4) bold
} else {
set tag(4) normal
}
# Create the tag in the target text widget
$text_widget tag configure $tag(0) \
-foreground $tag(1) \
-font [font create \
-overstrike $tag(2) \
-slant $tag(3) \
-weight $tag(4) \
-size -$fontSize \
-family $fontFamily \
]
}
# Set tag priorities
$text_widget tag raise tag_c_dox_tag tag_c_dox_comment
$text_widget tag raise tag_c_dox_word tag_c_dox_comment
$text_widget tag raise tag_c_dox_html tag_c_dox_comment
foreach t {
tag_c_bracket tag_c_symbol tag_c_keyword
tag_c_data_type tag_c_char tag_c_dec
tag_c_oct tag_c_hex tag_c_float
tag_c_bin
} {
$text_widget tag raise $t tag_normal
}
}
## Perform syntax highlight on the given line in the given widget
# @parm Widget Editor - Text widget
# @parm Int LineNumber - Number of line to highlight
# @parm Int status - Exit status of previous line
# @return Int - Exit status
# 1 - Normal
# 2 - Doxygen
# 3 - Comment
# 4 - String
# 5 - Assembly
# 6 - Assembly within
# 7 - Preprocessor
proc highlight {Editor LineNumber status} {
variable validation_L0 ;# Bool: Basic validation enabled
variable editor ;# Widget: Editor text widget
variable line_number ;# Int: Line number
variable line_content ;# String: Line content
variable line_start ;# Index of line start
variable line_end ;# Index of line end
variable directives ;# List of compiler directives
# Set NS variables
set editor $Editor
set line_number $LineNumber
set line_start $line_number.0
set line_end [$editor index [list $line_number.0 lineend]]
set line_content [$editor get $line_start $line_end]
# Validate input arguments
if {!$status} {
set status 1
}
# Local variables
set last_idx_s 0 ;# Int: Last search index
set incr_last_i 0 ;# Int: Increment last_idx by
set last_idx 0 ;# Int: Last index
set idx 0 ;# Int: Current index
set this_line_only 0 ;# Bool: Status is valid for this line only
set last_status $status ;# Int: Last highlight status
# Remove existing highlighting tags
delete_tags
# Handle status "preprocessor"
if {$status == 7} {
if {[string index $line_content end] == "\\"} {
$editor tag add tag_c_preprocessor $line_start $line_end-1c
$editor tag add tag_c_symbol $line_end-1c $line_end
return 7
} else {
$editor tag add tag_c_preprocessor $line_start $line_end
return 1
}
# Search for preprocessor directive
} elseif {$status == 1} {
# Common directive
if {[regexp {^\s*#\w+} $line_content directive]} {
# Local variables
set dir_start [string first {#} $line_content] ;# Int: Directive start index
set dir_end [string length $directive] ;# Int: Directive end index
set directive [string trim $directive] ;# String: Directive itself
# Highlight directive
$editor tag add tag_c_directive \
$line_number.$dir_start $line_number.$dir_end
# Validate directive
if {$validation_L0} {
if {[lsearch -ascii -exact $directives $directive] == -1} {
$editor tag add tag_error \
$line_number.$dir_start $line_number.$dir_end
}
}
# Highlight directive argument
if {$directive == {#include}} {
set prep_tag {tag_c_prep_lib}
} else {
set prep_tag {tag_c_preprocessor}
}
# Determinate start of comment
set com_start [string first {//} $line_content]
if {$com_start != -1} {
set cur_line_end $com_start
incr cur_line_end -1
set cur_line_end $line_number.$cur_line_end
} else {
set cur_line_end $line_end
}
# Determinate whether the directive continue on the next line or not
if {[string index [regsub {\s+$} $line_content {}] end] == "\\"} {
$editor tag add $prep_tag $line_number.$dir_end $cur_line_end-1c
$editor tag add tag_c_symbol $cur_line_end-1c $cur_line_end
set cur_status 7
} else {
$editor tag add $prep_tag $line_number.$dir_end $cur_line_end
set cur_status 1
}
# There is a comment on the line
if {$com_start != -1} {
$editor tag add tag_c_comment \
$line_number.$com_start \
[list $line_number.0 lineend]
}
return $cur_status
# Inline assembler
} elseif {[regexp {^\s*_?_asm\s*$} $line_content]} {
$editor tag add tag_c_keyword $line_start $line_end
return 5
}
}
# Split line into fields with different highlight status
while {1} {
set incr_last_i 0
switch -- $status {
1 { ;# Normal
set i 0
set idx [list {} {} {} {} {} {}]
foreach str {{/**} {///} {/*} {//} \"} {
lset idx $i [string first $str $line_content $last_idx_s]
incr i
}
set min_idx 0
set val 0
set min 0xFFFF
for {set i 0} {$i < 5} {incr i} {
set val [lindex $idx $i]
if {$val != -1 && $val < $min} {
set min_idx $i
set min $val
}
}
set idx [lindex $idx $min_idx]
if {$idx == -1} {break}
set last_status $status
switch -- $min_idx {
0 {
set status 2
set incr_last_i 3
set this_line_only 0
}
1 {
set status 2
set incr_last_i 3
set this_line_only 1
}
2 {
set status 3
set incr_last_i 2
set this_line_only 0
}
3 {
set status 3
set incr_last_i 2
set this_line_only 1
}
4 {
set status 4
set incr_last_i 1
set this_line_only 0
}
}
}
2 { ;# Doxygen
set idx [string first {*/} $line_content $last_idx_s]
if {$idx == -1} {break}
incr idx 2
set last_status $status
set status 1
}
3 { ;# Comment
set idx [string first {*/} $line_content $last_idx_s]
if {$idx == -1} {break}
incr idx 2
set last_status $status
set status 1
}
4 { ;# String
set l_idx $last_idx_s
while {1} {
set idx [string first "\"" $line_content $l_idx]
if {$idx < 1} {break}
if {[string index $line_content [expr {$idx - 1}]] == "\\"} {
incr l_idx
} else {
break
}
}
if {$idx == -1} {break}
incr idx
set last_status $status
set status 1
}
5 { ;# Inline assembler
if {[regexp {^\s*_?_endasm[^\w]*} $line_content]} {
mode_normal 0 [string length $line_content]
return 1
}
set idx 0
set last_status $status
set status 6
break
}
6 { ;# Inline assembler -- within asm block
if {[regexp {^\s*_?_endasm[^\w]*} $line_content]} {
mode_normal 0 [string length $line_content]
return 1
} else {
break
}
}
}
# Highliht this chunk
if {$last_idx != $idx} {
highlight_aux $last_status $last_idx $idx
}
set last_idx $idx
if {$this_line_only} {break}
set last_idx_s $last_idx
incr last_idx_s $incr_last_i
}
# Highlight last remaining chunk
if {$last_idx != [string length $line_content]} {
highlight_aux $status $last_idx [string length $line_content]
}
# Return final status
if {$this_line_only} {
return 1
} else {
return $status
}
}
## Auxiliary procedure for procedure highlight
# This procedure calls other procedures to perform syntax
#+ highlight according to the given highlight status
# @parm Int status - Highlight status
# @parm Int idx0 - Start index
# @parm Int idx1 - End index
# @return void
proc highlight_aux {status idx0 idx1} {
variable editor ;# Widget: Editor text widget
variable line_number ;# Int: Line number
# Validate input arguments
if {$idx0 < 0} {
set idx0 0
}
if {$idx1 < 0} {
set idx1 0
}
# Determinate what to do
switch -- $status {
1 { ;# Normal
mode_normal $idx0 $idx1
}
2 { ;# Doxygen
mode_doxygen $idx0 $idx1
}
3 { ;# Comment
$editor tag add tag_c_comment $line_number.$idx0 $line_number.$idx1
}
4 { ;# String
mode_string $idx0 $idx1
}
5 { ;# Inline assembly
mode_normal $idx0 $idx1
}
6 { ;# Inline assembly -- within
::ASMsyntaxHighlight::highlight $editor $line_number 1
}
}
}
## Highlight text within specified indexes as string
# @parm Int idx0 - Start index
# @parm Int idx1 - End index
# @return void
proc mode_string {idx0 idx1} {
variable editor ;# Widget: Editor text widget
variable line_number ;# Int: Line number
variable line_content ;# String: Line content
# Local variables
set idx 0 ;# Int: Index of backslash in the string
set idx_idx0 0 ;# Int: ($idx0 + $idx)
set last_idx 0 ;# Int: Last value of $idx
# String to highlight
set string [string range $line_content $idx0 [expr {$idx1 - 1}]]
# Highlight escaped characters and character between them
while {1} {
# Search for backslash
set idx [string first "\\" $string $idx]
if {$idx == -1} {break}
# Highlight
set idx_idx0 [expr {$idx + $idx0}]
$editor tag add tag_c_string $line_number.[expr {$last_idx + $idx0}] $line_number.$idx_idx0
$editor tag add tag_c_string_char $line_number.$idx_idx0 $line_number.$idx_idx0+2c
incr idx 2
set last_idx $idx
}
# Highlight remaining chunk of the string
$editor tag add tag_c_string $line_number.[expr {$last_idx + $idx0}] $line_number.$idx1
}
## Highlight text within specified indexes as doxygen document
# @parm Int idx0 - Start index
# @parm Int idx1 - End index
# @return void
proc mode_doxygen {idx0 idx1} {
variable validation_L1 ;# Bool: Advanced validation enabled
variable editor ;# Widget: Editor text widget
variable line_number ;# Int: Line number
variable line_content ;# String: Line content
variable doxy_tags_type0;# List of doxygen tags -- No argument
variable doxy_tags_type1;# List of doxygen tags -- Word after tag
variable doxy_tags_type2;# List of doxygen tags -- Name after tag
variable html_tags ;# List of HTML tags (HTML 4.0 Strict)
# Local variables
set tag_present 0 ;# Bool: Doxygen tag present on line
set asterix_p 0 ;# Bool: Leading asterix present on line
set i -1 ;# Int: Number of iteration
set idx -1 ;# Int: Word start index
set len 0 ;# Int: Word length
set tags {} ;# List: Highlight tags to put on current word
set is_word 0 ;# Bool: This word is doxygen tag word
# Determinate string to highlight
set string [string range $line_content $idx0 [expr {$idx1 - 1}]]
# Split line into words
set words [split [regsub -all {>} [regsub -all {<} $string { &}] {& }]]
# Adjust HTML tags with argument(s) (they must be represented as a single word)
set tag_opened 0
set result_words {}
foreach word $words {
if {!$tag_opened} {
append result_words { } ;# Insert a common space
} else {
append result_words "\xA0" ;# Insert NBSP
}
if {!$tag_opened && [regexp {^<\w+$} $word]} {
set tag_opened 1
} elseif {$tag_opened && [string index $word end] == {>}} {
set tag_opened 0
}
append result_words [regsub -all {[\{\}]} $word {\\&}]
}
set words $result_words
set result_words {}
# Iterate over string words
foreach word $words {
# Skip empty words
if {$word == {}} {continue}
incr i
set idx [string first [regsub -all "\xA0" $word { }] $string [expr {$idx + $len}]]
set len [string length $word]
# Detect doxygen tag word
if {$is_word} {
set is_word 0
set tags {tag_c_dox_word}
# Detect dogygen tag
} elseif {[string index $word 0] == {@}} {
# Tags without argument
if {[lsearch $doxy_tags_type0 $word] != -1 || $word == {@f[}} {
set tags {tag_c_dox_tag}
# Tags with one argument
} elseif {[lsearch $doxy_tags_type1 $word] != -1} {
set tags {tag_c_dox_tag}
set is_word 1
# Tags witch has name after
} elseif {[lsearch $doxy_tags_type2 $word] != -1} {
$editor tag add tag_c_dox_tag \
$line_number.[expr {$idx0 + $idx}] \
$line_number.[expr {$idx0 + $idx + $len}]
$editor tag add tag_c_dox_name \
$line_number.[expr {$idx0 + $idx + $len}] \
$line_number.$idx1
break
# Invalid tag
} else {
set tags {tag_c_dox_tag}
if {$validation_L1} {
lappend tags {tag_error}
}
}
# Detect HTML tags
} elseif {[string index $word 0] == {<} && [string index $word end] == {>}} {
set tags {}
# Adjust word
set word [string replace $word 0 0 { }]
if {[string index $word 1] == {/}} {
set word [string replace $word 1 1 { }]
}
set word [string replace $word end end { }]
set word [regsub -all "\xA0" $word { }]
# Mark empty tags as errors
if {$validation_L1 && ![string length [string trim $word]]} {
$editor tag add tag_error \
$line_number.[expr {$idx0 + $idx}] \
$line_number.[expr {$idx0 + $idx + $len}]
$editor tag add tag_c_dox_html \
$line_number.[expr {$idx0 + $idx}] \
$line_number.[expr {$idx0 + $idx + $len}]
}
# Highlight each part of word separately (tag argument="value" ...)
set sub_len 0
set sub_idx -1
set w_idx -1
foreach w [split $word] {
if {$w == {}} {continue}
incr w_idx
set sub_len [string length $w]
incr sub_idx
set sub_idx [string first $w $word $sub_idx]
# Highlight and validate HTML tag
if {!$w_idx} {
# Highlight tagname
$editor tag add tag_c_dox_html \
$line_number.[expr {$idx0 + $idx}] \
$line_number.[expr {$idx0 + $idx + $sub_idx + $sub_len}]
# Check if HTML tag is valid HTML-4.0 Strict tag
if {$validation_L1 && [lsearch $html_tags [string tolower $w]] == -1} {
$editor tag add tag_error \
$line_number.[expr {$idx0 + $idx + $sub_idx}] \
$line_number.[expr {$idx0 + $idx + $sub_idx + $sub_len}]
}
# Highlight arguments
} else {
set first_equ_mark [string first {=} $w]
incr first_equ_mark
# Check if argument notation is valid
if {$validation_L1 && $first_equ_mark == $sub_len} {
$editor tag add tag_error \
$line_number.[expr {$idx0 + $idx + $sub_idx}] \
$line_number.[expr {$idx0 + $idx + $sub_idx + $sub_len}]
}
# Highlight argument value
if {$first_equ_mark} {
$editor tag add tag_c_dox_hargval \
$line_number.[expr {$idx0 + $idx + $sub_idx + $first_equ_mark}] \
$line_number.[expr {$idx0 + $idx + $sub_idx + $sub_len}]
} else {
set first_equ_mark $sub_len
}
# Highlight argument name
$editor tag add tag_c_dox_harg \
$line_number.[expr {$idx0 + $idx + $sub_idx}] \
$line_number.[expr {$idx0 + $idx + $sub_idx + $first_equ_mark}]
}
}
# Highlight last ">"
$editor tag add tag_c_dox_html \
$line_number.[expr {$idx0 + $idx + $len - 1}] \
$line_number.[expr {$idx0 + $idx + $len}]
# Doxygen comment
} else {
set tags {tag_c_dox_comment}
}
# Create chosen highlighting tags
foreach tag $tags {
$editor tag add $tag \
$line_number.[expr {$idx0 + $idx}] \
$line_number.[expr {$idx0 + $idx + $len}]
}
}
}
## Highlight text within specified indexes as normal text
# @parm Int idx0 - Start index
# @parm Int idx1 - End index
# @return void
proc mode_normal {idx0 idx1} {
variable validation_L0 ;# Bool: Basic validation enabled
variable editor ;# Widget: Editor text widget
variable line_number ;# Int: Line number
variable line_content ;# String: Line content
variable data_types ;# List of data type specifiers
variable keywords ;# List of C keywords
# Determinate string to highligh and its length
set string [string range $line_content $idx0 [expr {$idx1 - 1}]]
set len [string length $string]
# At first highlight all as a normal text
$editor tag add tag_normal $line_number.$idx0 $line_number.$idx1
# Highlight symbols
set char {}
for {set i 0; set j $idx0} {$i < $len} {incr i; incr j} {
set char [string index $string $i]
# Brackets
if {
$char == {(} || $char == {)} || $char == "\{" ||
$char == "\}" || $char == {[} || $char == {]}
} then {
$editor tag add tag_c_bracket $line_number.$j $line_number.$j+1c
# Other symbols
} elseif {[lsearch {; = , + - < > ! | & * / ? : % ^} $char] != -1} {
$editor tag add tag_c_symbol $line_number.$j $line_number.$j+1c
}
}
# Highlight keywords and data types
set idx 0
foreach words [list $keywords $data_types] \
tag {tag_c_keyword tag_c_data_type} \
{
set idx -1
foreach word $words {
while {1} {
incr idx
set idx [string first $word $string $idx]
if {$idx == -1} {break}
set len [string length $word]
if {[string is wordchar -strict [string index $string [expr {$idx - 1}]]]} {
continue
}
if {[string is wordchar -strict [string index $string [expr {$idx + $len}]]]} {
continue
}
$editor tag add $tag \
$line_number.[expr {$idx0 + $idx}] \
$line_number.[expr {$idx0 + $idx + $len}]
}
}
}
# Highlight numbers
set idx -1
set len 0
set tags {}
foreach word [split $string { ;=,+-<>!|&*/?:%^{}[]()}] {
if {$word == {}} {continue}
incr idx
set len [string length $word]
set idx [string first $word $string $idx]
# Char
if {![string is digit -strict [string index $word 0]]} {
if {$word == {''}} {
set tags {tag_error}
} elseif {[regexp {^'[^']+'$} $word]} {
set tags {tag_c_char}
} else {
continue
}
# Oct | Dec
} elseif {[string is digit -strict $word]} {
if {[string index $word 0] == {0}} {
if {$len == 1} {
set tags {tag_c_dec}
} elseif {!$validation_L0 || [regexp {^0[0-7]+$} $word]} {
set tags {tag_c_oct}
} else {
set tags {tag_c_oct tag_error}
}
} else {
set tags {tag_c_dec}
}
# Hex
} elseif {
[string index $word 0] == {0} && (
[string index $word 1] == {x}
||
[string index $word 1] == {X}
)
} then {
if {!$validation_L0 || [string is xdigit -strict [string range $word 2 end]]} {
set tags {tag_c_hex}
} else {
set tags {tag_c_hex tag_error}
}
# Bin
} elseif {
[string index $word 0] == {0} && (
[string index $word 1] == {b}
||
[string index $word 1] == {B}
)
} then {
if {!$validation_L0 || [regexp {^[01]+$} [string range $word 2 end]]} {
set tags {tag_c_bin}
} else {
set tags {tag_c_bin tag_error}
}
# Float
} elseif {[regexp {^\d+\.\d+$} $word]} {
set tags {tag_c_float}
# Invalid number
} else {
if {$validation_L0} {
set tags {tag_error}
}
}
# Put tags on text widget
foreach tag $tags {
$editor tag add $tag \
$line_number.[expr {$idx0 + $idx}] \
$line_number.[expr {$idx0 + $idx + $len}]
}
}
}
## Remove previously defined syntax highlighting tags
# @return void
proc delete_tags {} {
variable editor ;# Widget: Editor text widget
variable highlight_tags ;# Highlight tags definition
variable line_start ;# Index of line start
variable line_end ;# Index of line end
# Remove tag error
$editor tag remove tag_error $line_start $line_end
$editor tag remove tag_error_line $line_start $line_start+1l
$editor tag remove c_lang_func $line_start $line_start+1l
$editor tag remove c_lang_var $line_start $line_start+1l
# Remove tags according to pattern
foreach tag $highlight_tags {
$editor tag remove [lindex $tag 0] $line_start $line_end
}
foreach tag $::ASMsyntaxHighlight::highlight_tags {
$editor tag remove [lindex $tag 0] $line_start $line_end
}
}
}
# >>> File inclusion guard
}
# <<< File inclusion guard
|