src/highlighter.c

Wed, 10 Jul 2024 21:35:37 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Jul 2024 21:35:37 +0200
changeset 75
c72b250866ab
parent 67
5da2cb5aea6b
child 76
44c7423d6ce2
permissions
-rw-r--r--

add basic support for line continuations in C highlighter

things that still do not work:
- user includes
- splitting keywords

fixes #392

21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
35
abaf2489c549 adjusted copyright info
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
4 * Copyright 2016 Mike Becker. All rights reserved.
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
30 #include "highlighter.h"
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 54
diff changeset
32 #include <string.h>
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 54
diff changeset
33 #include <ctype.h>
51
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
34
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
35 #include <cx/string.h>
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
36 #include <cx/printf.h>
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
37
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
38 static void put_htmlescaped(CxBuffer *dest, char c) {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 if (c == '>') {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
40 cxBufferPutString(dest, "&gt;");
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 } else if (c == '<') {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
42 cxBufferPutString(dest, "&lt;");
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
43 } else if (c) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
44 cxBufferPut(dest, c);
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 }
48
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
46 }
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
48 static void put_htmlescapedstr(CxBuffer *dest, cxstring s) {
48
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
49 for (int i = 0 ; i < s.length ; i++) {
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
50 put_htmlescaped(dest, s.ptr[i]);
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
51 }
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
54 static int check_keyword(cxstring word, const char** keywords) {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 for (int i = 0 ; keywords[i] ; i++) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
56 if (cx_strcmp(word, cx_str(keywords[i])) == 0) {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 return 1;
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 return 0;
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
63 static int check_capsonly(cxstring word) {
47
c39ecbbca7c0 words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
64 for (size_t i = 0 ; i < word.length ; i++) {
c39ecbbca7c0 words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
65 if (!isupper(word.ptr[i]) && !isdigit(word.ptr[i])
c39ecbbca7c0 words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
66 && word.ptr[i] != '_') {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 return 0;
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 return 1;
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 }
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
72
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
73 /* Plaintext Highlighter */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
74
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
75 void c2html_plain_highlighter(char const *src, CxBuffer *dest,
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
76 c2html_highlighter_data *hd) {
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
77 while (*src && *src != '\n') {
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
78 if (*src != '\r') {
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
79 put_htmlescaped(dest, *src);
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
80 }
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
81 src++;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
82 }
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
83 cxBufferPut(dest, '\n');
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
84 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
85
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
86 /* C Highlighter */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
87
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
88 static const char* ckeywords[] = {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
89 "auto", "break", "case", "char", "const", "continue", "default", "do",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
90 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
91 "long", "register", "return", "short", "signed", "sizeof", "static",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
92 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
93 "while", NULL
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
94 };
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
95
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
96 void c2html_c_highlighter(char const *src, CxBuffer *dest,
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
97 c2html_highlighter_data *hd) {
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
98 /* reset buffers without clearing them */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
99 hd->primary_buffer.size = hd->primary_buffer.pos = 0;
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
100 hd->secondary_buffer.size = hd->secondary_buffer.pos = 0;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
101
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
102 /* alias the buffers for better handling */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
103 CxBuffer *wbuf = &hd->primary_buffer;
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
104 CxBuffer *ifilebuf = &hd->secondary_buffer;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
105
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
106 /* local information */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
107 size_t sp = (size_t)-1;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
108
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
109 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
110 char quote = '\0';
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
111 int isescaping = 0;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
112
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
113 int continuation_enabled = 0;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
114 const char* current_highlight = NULL;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
115
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
116 /* define convenience macros */
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
117 #define start_span(cl) \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
118 current_highlight = cl; \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
119 cx_bprintf(dest, "<span class=\"c2html-%s\">", current_highlight)
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
120 #define stop_span \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
121 current_highlight = NULL;\
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
122 cxBufferPutString(dest, "</span>")
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
123
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
124 /* continue a multi line comment highlighting */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
125 if (hd->multiline_comment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
126 iscomment = 1;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
127 start_span("comment");
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
128 }
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
129
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
130 /* continue highlighting in case of line continuation */
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
131 if (hd->continue_highlight) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
132 start_span(hd->continue_highlight);
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
133 hd->continue_highlight = NULL;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
134 isinclude = hd->continuation_info & 0x1;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
135 isstring = hd->continuation_info & 0x2;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
136 iscomment = hd->continuation_info & 0x4;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
137 if (hd->continuation_info & 0x10) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
138 quote = '\'';
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
139 } else if (hd->continuation_info & 0x20) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
140 quote = '\"';
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
141 }
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
142 hd->continuation_info = 0;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
143 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
144
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
145 char c;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
146 do {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
147 c = src[++sp];
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
148 if (c == '\r') continue;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
149
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
150 /* line continuation */
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
151 if (c == '\\') {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
152 /* currently do not support continuations in user includes */
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
153 // TODO: also support user includes
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
154 if (!parseinclude) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
155 continuation_enabled = 1;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
156 }
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
157 } else if (continuation_enabled) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
158 if (!isspace(c)) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
159 continuation_enabled = 0;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
160 } else if (c == '\n') {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
161 cxBufferPut(dest, '\n');
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
162 hd->continue_highlight = current_highlight;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
163 hd->continuation_info = \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
164 isinclude | \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
165 (isstring << 1) | \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
166 (iscomment << 2);
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
167 if (quote == '\'') {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
168 hd->continuation_info |= 0x10;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
169 } else if (quote == '\"') {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
170 hd->continuation_info |= 0x20;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
171 }
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
172 stop_span;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
173 continue;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
174 }
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
175 }
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
176
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
177 /* comments */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
178 if (!isstring && c == '/') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
179 if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
180 iscomment = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
181 hd->multiline_comment = 0;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
182 cxBufferPut(dest, '/');
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
183 stop_span;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
184 continue;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
185 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
186 iscomment = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
187 hd->multiline_comment = (src[sp+1] == '*');
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
188 start_span("comment");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
189 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
190 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
191
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
192 if (iscomment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
193 if (c == '\n') {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
194 stop_span;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
195 cxBufferPut(dest, '\n');
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
196 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
197 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
198 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
199 } else if (isinclude) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
200 if (c == '<') {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
201 start_span("stdinclude");
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
202 cxBufferPutString(dest, "&lt;");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
203 } else if (c == '\"') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
204 if (parseinclude) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
205 cxBufferPutString(dest, "\">");
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
206 cxBufferWrite(ifilebuf->space, 1, ifilebuf->size, dest);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
207 cxBufferPutString(dest, "\"</a>");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
208 parseinclude = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
209 } else {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
210 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
211 "<a class=\"c2html-userinclude\" href=\"");
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
212 cxBufferPut(ifilebuf, '\"');
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
213 parseinclude = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
214 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
215 } else if (c == '>') {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
216 cxBufferPutString(dest, "&gt;");
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
217 stop_span;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
218 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
219 if (parseinclude) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
220 cxBufferPut(ifilebuf, c);
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
221 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
222 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
223 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
224 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
225 /* strings */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
226 if (!isescaping && (c == '\'' || c == '\"')) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
227 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
228 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
229 if (c == quote) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
230 isstring = 0;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
231 stop_span;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
232 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
233 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
234 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
235 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
236 isstring = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
237 quote = c;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
238 start_span("string");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
239 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
240 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
241 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
242 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
243 put_htmlescaped(dest, c);
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
244 } else if (isalnum(c) || c == '_' || c == '#') {
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
245 /* buffer the current word */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
246 cxBufferPut(wbuf, c);
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
247 } else {
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
248 /* write buffered word, if any */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
249 if (wbuf->size > 0) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
250 cxstring word = cx_strn(wbuf->space, wbuf->size);
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
251 int closespan = 1;
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
252 cxstring typesuffix = CX_STR("_t");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
253 if (check_keyword(word, ckeywords)) {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
254 start_span("keyword");
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
255 } else if (cx_strsuffix(word, typesuffix)) {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
256 start_span("type");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
257 } else if (word.ptr[0] == '#') {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
258 isinclude = !cx_strcmp(word, CX_STR("#include"));
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
259 start_span("directive");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
260 } else if (check_capsonly(word)) {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
261 start_span("macroconst");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
262 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
263 closespan = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
264 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
265 put_htmlescapedstr(dest, word);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
266 if (closespan) {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
267 stop_span;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
268 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
269 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
270 wbuf->pos = wbuf->size = 0; /* reset word buffer */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
271
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
272 /* write current character */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
273 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
274 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
275 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
276
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
277 isescaping = !isescaping & (c == '\\');
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
278 }
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
279 } while (c && c != '\n');
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
280
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
281 #undef start_span
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
282 #undef stop_span
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
283 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
284
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
285 /* Java Highlighter */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
286
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
287 static const char* jkeywords[] = {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
288 "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
289 "package", "synchronized", "boolean", "do", "if", "private", "this",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
290 "break", "double", "implements", "protected", "throw", "byte", "else",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
291 "import", "public", "throws", "case", "enum", "instanceof", "return",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
292 "transient", "catch", "extends", "int", "short", "try", "char", "final",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
293 "interface", "static", "void", "class", "finally", "long", "strictfp",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
294 "volatile", "const", "float", "native", "super", "while", NULL
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
295 };
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
296
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
297 void c2html_java_highlighter(char const *src, CxBuffer *dest,
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
298 c2html_highlighter_data *hd) {
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
299 /* reset buffers without clearing them */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
300 hd->primary_buffer.size = hd->primary_buffer.pos = 0;
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
301 hd->secondary_buffer.size = hd->secondary_buffer.pos = 0;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
302
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
303 /* alias the buffers for better handling */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
304 CxBuffer *wbuf = &hd->primary_buffer;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
305
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
306 /* local information */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
307 size_t sp = (size_t)-1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
308 int isstring = 0, iscomment = 0, isimport = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
309 char quote = '\0';
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
310 int isescaping = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
311
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
312 if (hd->multiline_comment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
313 iscomment = 1;
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
314 cxBufferPutString(dest, "<span class=\"c2html-comment\">");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
315 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
316
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
317 char c;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
318 do {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
319 c = src[++sp];
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
320 if (c == '\r') continue;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
321
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
322 /* comments */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
323 if (!isstring && c == '/') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
324 if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
325 iscomment = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
326 hd->multiline_comment = 0;
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
327 cxBufferPutString(dest, "/</span>");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
328 continue;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
329 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
330 iscomment = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
331 hd->multiline_comment = (src[sp+1] == '*');
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
332 cxBufferPutString(dest, "<span class=\"c2html-comment\">");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
333 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
334 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
335
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
336 if (iscomment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
337 if (c == '\n') {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
338 cxBufferPutString(dest, "</span>\n");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
339 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
340 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
341 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
342 } else if (isimport) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
343 /* TODO: local imports */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
344 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
345 /* strings */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
346 if (!isescaping && (c == '\'' || c == '\"')) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
347 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
348 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
349 if (c == quote) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
350 isstring = 0;
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
351 cxBufferPutString(dest, "</span>");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
352 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
353 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
354 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
355 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
356 isstring = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
357 quote = c;
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
358 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
359 "<span class=\"c2html-string\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
360 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
361 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
362 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
363 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
364 put_htmlescaped(dest, c);
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
365 } else if (isalnum(c) || c == '_' || c == '@') {
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
366 /* buffer the current word */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
367 cxBufferPut(wbuf, c);
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
368 } else {
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
369 /* write buffered word, if any */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
370 if (wbuf->size > 0) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
371 cxstring word = cx_strn(wbuf->space, wbuf->size);
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
372 int closespan = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
373 if (check_keyword(word, jkeywords)) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
374 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
375 "<span class=\"c2html-keyword\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
376 } else if (isupper(word.ptr[0])) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
377 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
378 "<span class=\"c2html-type\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
379 } else if (word.ptr[0] == '@') {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
380 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
381 "<span class=\"c2html-directive\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
382 } else if (check_capsonly(word)) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
383 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
384 "<span class=\"c2html-macroconst\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
385 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
386 closespan = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
387 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
388 put_htmlescapedstr(dest, word);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
389
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
390 if (closespan) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
391 cxBufferPutString(dest, "</span>");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
392 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
393 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
394 wbuf->pos = wbuf->size = 0; /* reset buffer */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
395
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
396 /* write current character */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
397 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
398 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
399 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
400
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
401 isescaping = !isescaping & (c == '\\');
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
402 }
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
403 } while (c && c != '\n');
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
404 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
405

mercurial