Basic syntax system, right now only C and hardcode in eco!
[eco.git] / syntax.c
blobb6768837eca7bcd772cfa9314a530099c30cc484
1 /*
2 * Copyright (C) 2010 Diego Hernan Borghetti.
3 * Eco
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
10 #include "debug.h"
11 #include "list.h"
12 #include "term.h"
13 #include "screen.h"
14 #include "syntax.h"
17 /* our syntax data base. */
18 static E_List syntaxs= { NULL, NULL };
20 static E_Syntax_Word *e_syntax_word_search(E_Syntax *sy, char *word, int nch)
22 E_Syntax_Word *p;
24 p= (E_Syntax_Word *)sy->dirs.first;
25 while (p) {
26 if (p->nch == nch) {
27 if (!strncmp(p->word, word, nch))
28 return(p);
30 p= p->next;
33 return(NULL);
36 static E_Syntax_Char *e_syntax_char_search(E_Syntax *sy, char ch)
38 E_Syntax_Char *p;
40 p= (E_Syntax_Char *)sy->chars.first;
41 while (p) {
42 if (p->ch == ch)
43 return(p);
44 p= p->next;
46 return(NULL);
49 E_Syntax *e_syntax_search(char *ext)
51 E_Syntax *p;
53 p= (E_Syntax *)syntaxs.first;
54 while (p) {
55 if ((*p->check_type)(ext) == 0)
56 return(p);
57 p= p->next;
59 return(NULL);
62 void e_syntax_add(E_Syntax *sy)
64 if (sy) {
65 E_LIST_ADD(&syntaxs, sy);
69 void e_syntax_rem(E_Syntax *sy)
71 if (sy) {
72 E_LIST_REM(&syntaxs, sy);
76 E_Syntax *e_syntax_new(void)
78 E_Syntax *sy;
80 sy= (E_Syntax *)malloc(sizeof(E_Syntax));
81 sy->next= NULL;
82 sy->prev= NULL;
83 sy->name= NULL;
84 sy->check_type= NULL;
85 sy->dirs.first= NULL;
86 sy->dirs.last= NULL;
87 sy->chars.first= NULL;
88 sy->chars.last= NULL;
89 return(sy);
92 void e_syntax_free(E_Syntax *sy)
94 E_Syntax_Word *p;
96 while (sy->dirs.first) {
97 p= (E_Syntax_Word *)sy->dirs.first;
98 sy->dirs.first= sy->dirs.first->next;
99 free((void *)p->word);
100 free((void *)p);
103 if (sy->name)
104 free((void *)sy->name);
106 free((void *)sy);
109 void e_syntax_word_add(E_Syntax *sy, char *word, char fg, char bg)
111 E_Syntax_Word *wr;
113 wr= e_syntax_word_search(sy, word, strlen(word));
114 if (wr)
115 return;
117 wr= (E_Syntax_Word *)malloc(sizeof(E_Syntax_Word));
118 wr->next= NULL;
119 wr->prev= NULL;
120 wr->word= strdup(word);
121 wr->nch= strlen(wr->word);
122 wr->fgcol= fg;
123 wr->bgcol= bg;
124 E_LIST_ADD(&sy->dirs, wr);
127 void e_syntax_char_add(E_Syntax *sy, char ch, char fg, char bg)
129 E_Syntax_Char *cha;
131 cha= e_syntax_char_search(sy, ch);
132 if (cha)
133 return;
135 cha= (E_Syntax_Char *)malloc(sizeof(E_Syntax_Char));
136 cha->next= NULL;
137 cha->prev= NULL;
138 cha->ch= ch;
139 cha->fgcol= fg;
140 cha->bgcol= bg;
141 E_LIST_ADD(&sy->chars, cha);
144 static int e_syntax_word(char *line, int *idx, int nch)
146 int i, e, first;
148 first= 0;
149 e= 0;
150 for (i= *idx; i < nch; i++) {
151 if (line[i] != ' ' && line[i] != '\t' && !first) {
152 *idx= i;
153 first= 1;
156 if (first && (line[i] == ' ' || line[i] == '\t'))
157 return(e);
158 if (first)
159 e++;
161 return(-1);
164 static void e_syntax_color(E_Syntax_Word *wr, E_ScreenLine *ln, int i, int nch)
166 int e;
168 for (e= i; e < (i+nch); e++) {
169 ln->fcol[e]= wr->fgcol;
170 ln->bcol[e]= wr->bgcol;
174 void e_syntax_apply(E_Syntax *sy, E_ScreenLine *ln, int ncol)
176 E_Syntax_Word *wr;
177 E_Syntax_Char *ch;
178 int i, nch;
180 i= 0;
182 /* First round, check for words. */
183 do_word:
184 nch= e_syntax_word(ln->text, &i, ncol);
185 if (nch == -1) {
186 goto do_char;
187 return;
190 wr= e_syntax_word_search(sy, &(ln->text[i]), nch);
191 if (wr)
192 e_syntax_color(wr, ln, i, nch);
194 if (i+nch < ncol) {
195 i += nch;
196 goto do_word;
199 /* Second round, check for characters. */
200 do_char:
201 for (i= 0; i < ncol; i++) {
202 ch= e_syntax_char_search(sy, ln->text[i]);
203 if (ch) {
204 ln->fcol[i]= ch->fgcol;
205 ln->bcol[i]= ch->bgcol;