Initial revision
[egate.git] / fml / fmltoken.c
1 /*
2  * FML interpreter. Europagate, 1995
3  *
4  * $Log: fmltoken.c,v $
5  * Revision 1.1  1995/02/06 13:48:09  adam
6  * Initial revision
7  *
8  */
9 #include <string.h>
10 #include <assert.h>
11 #include <stdio.h>
12
13 #include "fmlp.h"
14
15 static int look_char;
16 static int look_type;
17 static char lex_buf[FML_MAX_TOKEN];
18
19 static void lexer (Fml fml);
20
21 struct fml_node *fml_group (Fml fml);
22
23 struct fml_node *fml_tokenize (Fml fml)
24 {
25     struct fml_node *p;
26
27     look_char = (*fml->read_func)();
28     p = fml_group (fml);
29     if (fml->debug)
30     {
31         fml_pr_list (p);
32         printf ("\n");
33     }
34     return p;
35 }
36
37 void fml_pr_list (struct fml_node *p)
38 {
39     printf ("{");
40
41     while (p)
42     {
43         if (p->is_atom)
44         {
45             char buf[100];
46             fml_atom_str (p->p[0], buf);
47             printf (" %s", buf);
48         }
49         else
50         {
51             printf (" ");
52             fml_pr_list (p->p[0]);
53         }
54         p = p->p[1];
55     }
56     printf (" }");         
57 }
58
59 struct fml_node *fml_group (Fml fml)
60 {
61     struct fml_node *ptr0 = NULL, *ptr1, *ptr2;
62
63     lexer (fml);
64     if (look_type == 0)
65         return NULL;
66     while (1)
67     {
68         if (look_type == 'a')
69         {
70             ptr2 = fml_node_alloc (fml);
71             if (!ptr0)
72                 ptr0 = ptr2;
73             else
74                 ptr1->p[1] = ptr2;
75             ptr2->p[0] = fml_atom_alloc (fml, lex_buf);
76             ptr2->is_atom = 1;
77         }
78         else if (look_type == '{')
79         {
80             struct fml_node *sptr = fml_group (fml);
81             if (sptr)
82                 if (sptr->p[1])
83                 {
84                     ptr2 = fml_node_alloc (fml);
85                     if (!ptr0)
86                         ptr0 = ptr2;
87                     else
88                         ptr1->p[1] = ptr2;
89                     ptr2->p[0] = sptr;
90                     ptr2->is_atom = 0;
91                 }
92                 else
93                 {
94                     ptr2 = sptr;
95                     if (!ptr0)
96                         ptr0 = ptr2;
97                     else
98                         ptr1->p[1] = ptr2;
99                 }
100             else
101             {
102                 ptr2 = fml_node_alloc (fml);
103                 if (!ptr0)
104                     ptr0 = ptr2;
105                 else
106                     ptr1->p[1] = ptr2;
107                 ptr2->is_atom = 0;
108             }
109         }
110         else
111             break;
112         lexer (fml);
113         ptr1 = ptr2;
114     }
115     return ptr0;
116 }
117
118 static void lexer (Fml fml)
119 {
120     int off;
121     while (1) 
122     {
123         if (look_char == fml->eof_mark)
124         {
125             look_type = 0;
126             return;
127         }
128         else if (look_char == fml->comment_char)
129         {
130             do
131                 look_char = (*fml->read_func)();
132             while (look_char != '\n' && look_char != fml->eof_mark);
133         }
134         else
135         {
136             if (!strchr (fml->white_chars, look_char))
137                 break;
138             look_char = (*fml->read_func)();
139         }
140     }
141     if (look_char == '{')
142     {
143         look_type = '{';
144         look_char = (*fml->read_func)();
145     }
146     else if (look_char == '}')
147     {
148         look_type = '}';
149         look_char = (*fml->read_func)();
150     }        
151     else
152     {
153         off = 0;
154         do
155         {
156             lex_buf[off++] = look_char;
157             look_char = (*fml->read_func)();
158         } while (look_char != fml->eof_mark
159                  && !strchr (fml->white_chars, look_char)
160                  && look_char != '{' && look_char != '}');
161         lex_buf[off] = '\0';
162         look_type = 'a';
163     }
164 #if 0
165     if (fml->debug)
166     {
167         if (look_type == 'a')
168             printf ("[%s]", lex_buf);
169         else
170             printf ("[%c]", look_type);
171     }
172 #endif
173 }