src/javacodegen.c

changeset 29
ec6e97454e64
parent 28
1be8ea902ef4
child 32
10389d866a4d
equal deleted inserted replaced
28:1be8ea902ef4 29:ec6e97454e64
47 47
48 int isjdirective(char *word) { 48 int isjdirective(char *word) {
49 return word[0] == '@'; 49 return word[0] == '@';
50 } 50 }
51 51
52 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
53 dp += sizeof(str)-1
54
52 void jparseline(char *src, char *dest, highlighter_t *hltr) { 55 void jparseline(char *src, char *dest, highlighter_t *hltr) {
53 size_t sp = 0, dp = 0; 56 size_t sp = 0, dp = 0;
54 /* indent */ 57 /* indent */
55 while (isspace(src[sp])) { 58 while (isspace(src[sp])) {
56 dest[dp++] = src[sp++]; 59 dest[dp++] = src[sp++];
62 char quote = '\0'; 65 char quote = '\0';
63 int isescaping = 0; 66 int isescaping = 0;
64 67
65 if (hltr->iscommentml) { 68 if (hltr->iscommentml) {
66 iscomment = 1; 69 iscomment = 1;
67 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 70 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
68 dp += 29;
69 } 71 }
70 72
71 for (char c = src[sp] ; c ; c=src[++sp]) { 73 for (char c = src[sp] ; c ; c=src[++sp]) {
72 /* comments */ 74 /* comments */
73 if (!isstring && c == '/') { 75 if (!isstring && c == '/') {
74 if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { 76 if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
75 iscomment = 0; 77 iscomment = 0;
76 hltr->iscommentml = 0; 78 hltr->iscommentml = 0;
77 memcpy(&(dest[dp]), "/</span>", 8); 79 memcpy_const(dest, dp, "/</span>");
78 dp += 8;
79 continue; 80 continue;
80 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 81 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
81 iscomment = 1; 82 iscomment = 1;
82 hltr->iscommentml = (src[sp+1] == '*'); 83 hltr->iscommentml = (src[sp+1] == '*');
83 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 84 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
84 dp += 29;
85 } 85 }
86 } 86 }
87 87
88 if (iscomment) { 88 if (iscomment) {
89 if (c == '\n') { 89 if (c == '\n') {
95 // TODO: local imports 95 // TODO: local imports
96 } else { 96 } else {
97 /* strings */ 97 /* strings */
98 if (!isescaping && (c == '\'' || c == '\"')) { 98 if (!isescaping && (c == '\'' || c == '\"')) {
99 if (isstring) { 99 if (isstring) {
100 dp = writeescapedchar(dest, dp, c);
100 if (c == quote) { 101 if (c == quote) {
101 isstring = 0; 102 isstring = 0;
102 memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); 103 memcpy_const(dest, dp, "</span>");
103 dp += 28;
104 dp = writeescapedchar(dest, dp, c);
105 } else { 104 } else {
106 dp = writeescapedchar(dest, dp, c); 105 dp = writeescapedchar(dest, dp, c);
107 } 106 }
108 } else { 107 } else {
109 isstring = 1; 108 isstring = 1;
110 quote = c; 109 quote = c;
110 memcpy_const(dest, dp,
111 "<span class=\"c2html-string\">");
111 dp = writeescapedchar(dest, dp, c); 112 dp = writeescapedchar(dest, dp, c);
112 memcpy(&(dest[dp]), "</span>", 7);
113 dp += 7;
114 } 113 }
115 } else { 114 } else {
116 if (isstring) { 115 if (isstring) {
117 dp = writeescapedchar(dest, dp, c); 116 dp = writeescapedchar(dest, dp, c);
118 } else if (!iswordcharacter(c)) { 117 } else if (!iswordcharacter(c)) {
119 /* interpret word int_t */ 118 /* interpret word int_t */
120 if (wp > 0 && wp < WORDBUF_SIZE) { 119 if (wp > 0 && wp < WORDBUF_SIZE) {
121 int closespan = 1; 120 int closespan = 1;
122 if (iskeyword(hltr->word, hltr->keywords)) { 121 if (iskeyword(hltr->word, hltr->keywords)) {
123 memcpy(&(dest[dp]), 122 memcpy_const(dest, dp,
124 "<span class=\"c2html-keyword\">", 29); 123 "<span class=\"c2html-keyword\">");
125 dp += 29;
126 } else if (hltr->istype(hltr->word, wp)) { 124 } else if (hltr->istype(hltr->word, wp)) {
127 memcpy(&(dest[dp]), 125 memcpy_const(dest, dp,
128 "<span class=\"c2html-type\">", 26); 126 "<span class=\"c2html-type\">");
129 dp += 26;
130 } else if (hltr->isdirective(hltr->word)) { 127 } else if (hltr->isdirective(hltr->word)) {
131 memcpy(&(dest[dp]), 128 memcpy_const(dest, dp,
132 "<span class=\"c2html-directive\">", 31); 129 "<span class=\"c2html-directive\">");
133 dp += 31;
134 } else if (iscapsonly(hltr->word, wp)) { 130 } else if (iscapsonly(hltr->word, wp)) {
135 memcpy(&(dest[dp]), 131 memcpy_const(dest, dp,
136 "<span class=\"c2html-macroconst\">", 32); 132 "<span class=\"c2html-macroconst\">");
137 dp += 32;
138 } else { 133 } else {
139 closespan = 0; 134 closespan = 0;
140 } 135 }
141 for (int i = 0 ; i < wp ; i++) { 136 for (int i = 0 ; i < wp ; i++) {
142 dp = writeescapedchar(dest, dp, hltr->word[i]); 137 dp = writeescapedchar(dest, dp, hltr->word[i]);
143 } 138 }
144 if (closespan) { 139 if (closespan) {
145 memcpy(&(dest[dp]), "</span>", 7); 140 memcpy_const(dest, dp, "</span>");
146 dp += 7;
147 } 141 }
148 } 142 }
149 memset(hltr->word, 0, WORDBUF_SIZE); 143 memset(hltr->word, 0, WORDBUF_SIZE);
150 wp = 0; 144 wp = 0;
151 dp = writeescapedchar(dest, dp, c); 145 dp = writeescapedchar(dest, dp, c);

mercurial