1 /*
2  *  Copyright (C) 2014  Christian Heckendorf <heckendorfc@gmail.com>
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "lex.h"
19 #include "lex_dfa.h"
20 #include "edit_shell.h"
21 
22 State *trap=NULL;
23 
add_path(State * dfa,const char * str,int token)24 void add_path(State *dfa,const char *str,int token){
25 	int i,j;
26 	State *o=dfa;
27 	for(i=0;str[i];i++){
28 		if(!dfa->out){
29 			INIT_MEM(dfa->out,SIGMA_SIZE);
30 			for(j=0;j<SIGMA_SIZE;j++){
31 				dfa->out[j].state=trap;
32 			}
33 		}
34 		if(!dfa->out[(int)str[i]].state ||
35 				dfa->out[(int)str[i]].state==o ||
36 				dfa->out[(int)str[i]].state==trap){
37 			INIT_MEM(dfa->out[(int)str[i]].state,1);
38 			dfa->out[(int)str[i]].state->final=TOK_NULL;
39 			dfa->out[(int)str[i]].state->out=NULL;
40 		}
41 		dfa=dfa->out[(int)str[i]].state;
42 	}
43 	dfa->final=token;
44 	dfa->out=NULL;
45 }
46 
generate_operator_dfa()47 State* generate_operator_dfa(){
48 	int i;
49 
50 	State *dfa;
51 	INIT_MEM(dfa,1);
52 
53 	dfa->out=NULL;
54 	dfa->final=0;
55 	INIT_MEM(dfa->out,SIGMA_SIZE);
56 	for(i=0;i<SIGMA_SIZE;i++){
57 		dfa->out[i].state=dfa;
58 	}
59 
60 	if(!trap){
61 		INIT_MEM(trap,1);
62 		trap->final=TOK_NULL;
63 		trap->out=NULL;
64 	}
65 
66 	add_path(dfa,"(",TOK_OPERATOR|TOK_OPAR);
67 	add_path(dfa,")",TOK_OPERATOR|TOK_CPAR);
68 	add_path(dfa,",",TOK_OPERATOR|TOK_COMMA);
69 
70 	return dfa;
71 }
72 
generate_quote_dfa()73 State* generate_quote_dfa(){
74 	int i;
75 
76 	State *dfa;
77 	INIT_MEM(dfa,1);
78 
79 	dfa->out=NULL;
80 	dfa->final=0;
81 	INIT_MEM(dfa->out,SIGMA_SIZE);
82 	for(i=0;i<SIGMA_SIZE;i++){
83 		dfa->out[i].state=dfa;
84 	}
85 
86 	if(!trap){
87 		INIT_MEM(trap,1);
88 		trap->final=TOK_NULL;
89 		trap->out=NULL;
90 	}
91 
92 	add_path(dfa,"\"",TOK_QUOTE);
93 	add_path(dfa,"'",TOK_QUOTE);
94 
95 	return dfa;
96 }
97 /*
98 State* generate_reserved_dfa(){
99 	int i;
100 
101 	State *dfa;
102 	INIT_MEM(dfa,1);
103 
104 	dfa->out=NULL;
105 	dfa->final=0;
106 	INIT_MEM(dfa->out,SIGMA_SIZE);
107 	for(i=0;i<SIGMA_SIZE;i++){
108 		dfa->out[i].state=trap;
109 	}
110 
111 	if(!trap){
112 		INIT_MEM(trap,1);
113 		trap->final=TOK_NULL;
114 		trap->out=NULL;
115 	}
116 
117 	add_path(dfa,"set",TOK_RESERVED|TOK_SET);
118 	add_path(dfa,"for",TOK_RESERVED|TOK_FOR);
119 	add_path(dfa,"in",TOK_RESERVED|TOK_IN);
120 	add_path(dfa,"while",TOK_RESERVED|TOK_WHILE);
121 	add_path(dfa,"{",TOK_RESERVED|TOK_OCBRACE);
122 	add_path(dfa,"}",TOK_RESERVED|TOK_CCBRACE);
123 
124 	return dfa;
125 }
126 
127 State* generate_meta_dfa(){
128 	int i;
129 
130 	State *dfa;
131 	INIT_MEM(dfa,1);
132 
133 	dfa->out=NULL;
134 	dfa->final=0;
135 	INIT_MEM(dfa->out,SIGMA_SIZE);
136 	for(i=0;i<SIGMA_SIZE;i++){
137 		dfa->out[i].state=dfa;
138 	}
139 
140 	if(!trap){
141 		INIT_MEM(trap,1);
142 		trap->final=TOK_NULL;
143 		trap->out=NULL;
144 	}
145 
146 	//add_path(dfa,"!",TOK_META);
147 	add_path(dfa,"*",TOK_META);
148 	add_path(dfa,"?",TOK_META);
149 	add_path(dfa,"[",TOK_META);
150 	add_path(dfa,"]",TOK_META);
151 
152 	return dfa;
153 }
154 */
155