readworld.l
Go to the documentation of this file.
1 %{
2 /*
3 * Lexical analizer for world files
4 */
5 
8 #include <stdlib.h>
9 #include <string.h>
10 #include "defines.h"
11 #include "error_world.h"
12 #include "readworld.tab.h"
13 
14 typedef struct {
15  YY_BUFFER_STATE buffer;
16  char *prefix;
17  unsigned int pl;
18  unsigned int line;
19  unsigned int fl;
20  unsigned int fo;
21 } TincludeInfo;
22 
23 TincludeInfo includeInfo[MAX_INCLUDES];
24 unsigned int currentIncludeLevel=0;
25 
26 unsigned int firstLink; /* first link in the current object */
27 unsigned int firstObject; /* first object in the current file */
28 
29 int ReadWorldlex(void);
30 
31 extern unsigned int RWline; /*current line number (declared in the YACC module and updated here)*/
32 
33 void Switch2File(char *ln,unsigned int nl,unsigned int no,char *path,char *file);
34 
36 %}
37 %s IN_COMMENT
38 
39 EMPTY [ \t]+
40 RETURN \n
41 DIGIT [0-9]
42 LETTER [a-zA-Z]
43 R_NUMBER (({DIGIT}+(\.{DIGIT}+)?)|(\.{DIGIT}+))((E|e)[+-]?{DIGIT}+)?
44 I_NUMBER {DIGIT}+
45 STRING \"[^\"]+\"
46 IDENTIFIER {LETTER}({DIGIT}|{LETTER}|"_")*
47 EXT_IDENTIFIER {IDENTIFIER}("::"{IDENTIFIER})+
48 COMMENT \%[^\n]*
49 COMMENT2 \#[^\n]*
50 COMMENT3 \/\/[^\n]*
51 
52 %%
53 {COMMENT} { }
54 {COMMENT2} { }
55 {COMMENT3} { }
56 
57 <INITIAL>{
58  "/*" BEGIN(IN_COMMENT);
59 }
60 <IN_COMMENT>{
61  "*/" BEGIN(INITIAL);
62  [^*\n]+ // eat comment in chunks
63  "*" // eat the lone star
64  \n RWline++;
65 }
66 
67 {EMPTY} { }
68 
69 {RETURN} { RWline++; }
70 
71 "[CONSTANTS]" {
72  return(_CONSTANTS);
73  }
74 
75 ":=" {
76  return(_ASSIGN);
77  }
78 
79 PI {
80  return(_PI);
81  }
82 
83 SIN {
84  return(_SIN);
85  }
86 
87 COS {
88  return(_COS);
89  }
90 
91 SQRT {
92  return(_SQRT);
93  }
94 
95 "[LINKS]" {
96  return(_LINKS);
97  }
98 
99 "BODY" {
100  return(_BODY);
101  }
102 
103 "GRANULARITY" {
104  return(_GRANULARITY);
105  }
106 
107 "HIDDEN" {
108  return(_HIDDEN);
109  }
110 
111 "DECORATION" {
112  return(_DECORATION);
113  }
114 
115 "[JOINTS]" {
116  return(_JOINTS);
117  }
118 
119 "FIX" {
120  return(_FIX);
121  }
122 
123 "ID" {
124  return(_ID);
125  }
126 
127 "TX" {
128  return(_TX);
129  }
130 
131 "TY" {
132  return(_TY);
133  }
134 
135 "TZ" {
136  return(_TZ);
137  }
138 
139 "TXYZ" {
140  return(_TXYZ);
141  }
142 
143 "RX" {
144  return(_RX);
145  }
146 
147 "RY" {
148  return(_RY);
149  }
150 
151 "RZ" {
152  return(_RZ);
153  }
154 
155 "PRISMATIC" {
156  return(_PRISMATIC);
157  }
158 
159 "REVOLUTE" {
160  return(_REVOLUTE);
161  }
162 
163 "CREVOLUTE" {
164  return(_CREVOLUTE);
165  }
166 
167 "SPHERICAL" {
168  return(_SPHERICAL);
169  }
170 
171 "UNIVERSAL" {
172  return(_UNIVERSAL);
173  }
174 
175 "SPH_SPH" {
176  return(_SPH_SPH);
177  }
178 
179 "SPH_PRS_SPH" {
180  return(_SPH_PRS_SPH);
181  }
182 
183 "IN_PATCH" {
184  return(_IN_PATCH);
185  }
186 
187 "LENGTH" {
188  return(_LENGTH);
189  }
190 
191 "RADIUS" {
192  return(_RADIUS);
193  }
194 
195 "BOX" {
196  return(_BOX);
197  }
198 
199 "SPHERE" {
200  return(_SPHERE);
201  }
202 
203 "CYLINDER" {
204  return(_CYLINDER);
205  }
206 
207 "LINE" {
208  return(_LINE);
209  }
210 
211 "SEGMENTS" {
212  return(_SEGMENTS);
213  }
214 
215 
216 "[OBSTACLES]" {
217  return(_OBSTACLES);
218  }
219 
220 "[COLLISIONS]" {
221  return(_COLLISIONS);
222  }
223 
224 "COLOR" {
225  return(_COLOR);
226  }
227 
228 "RED" {
229  return(_RED);
230  }
231 
232 "GREEN" {
233  return(_GREEN);
234  }
235 
236 "BLUE" {
237  return(_BLUE);
238  }
239 
240 "WHITE" {
241  return(_WHITE);
242  }
243 
244 "BLACK" {
245  return(_BLACK);
246  }
247 
248 "GREY" {
249  return(_GREY);
250  }
251 
252 "YELLOW" {
253  return(_YELLOW);
254  }
255 
256 "CYAN" {
257  return(_CYAN);
258  }
259 
260 "PURPLE" {
261  return(_PURPLE);
262  }
263 
264 "RANGE" {
265  return(_RANGE);
266  }
267 
268 "CHECK" {
269  return(_CHECK);
270  }
271 
272 "DO NOT" {
273  return(_NO);
274  }
275 
276 "ALL" {
277  return(_ALL);
278  }
279 
280 "SELFCOLLISIONS" {
281  return(_SELFCOLLISIONS);
282  }
283 
284 "AVOID" {
285  return(_AVOID);
286  }
287 
288 "LIMITS" {
289  return(_LIMITS);
290  }
291 
292 "[INCLUDE]" {
293  return(_INCLUDE);
294  }
295 
296 {I_NUMBER} {
297  char string_tmp[100];
298 
299  memcpy(string_tmp,ReadWorldtext,(unsigned int)ReadWorldleng);
300  string_tmp[ReadWorldleng]=0;
301  ReadWorldlval.int_number=atoi(string_tmp);
302  return(_INTEGER);
303  }
304 
305 {R_NUMBER} {
306  char string_tmp[100];
307 
308  memcpy(string_tmp,ReadWorldtext,(unsigned int)ReadWorldleng);
309  string_tmp[ReadWorldleng]=0;
310  ReadWorldlval.real_number=atof(string_tmp);
311 
312  return(_REAL);
313  }
314 
315 {STRING} {
316  NEW(ReadWorldlval.string,ReadWorldleng,char);
317 
318  memcpy(ReadWorldlval.string,&(ReadWorldtext[1]),(unsigned int)ReadWorldleng-2);
319  ReadWorldlval.string[ReadWorldleng-2]=0;
320 
321  return(_STRING);
322  }
323 
324 {IDENTIFIER} {
325  if (currentIncludeLevel==0)
326  {
327  NEW(ReadWorldlval.id,ReadWorldleng+1,char);
328  memcpy(ReadWorldlval.id,ReadWorldtext,(unsigned int)ReadWorldleng);
329 
330  ReadWorldlval.id[ReadWorldleng]=0;
331  }
332  else
333  {
334  NEW(ReadWorldlval.id,includeInfo[currentIncludeLevel-1].pl+2+ReadWorldleng+1,char);
335  sprintf(ReadWorldlval.id,"%s__%s",includeInfo[currentIncludeLevel-1].prefix,ReadWorldtext);
336  ReadWorldlval.id[includeInfo[currentIncludeLevel-1].pl+ReadWorldleng+2]=0;
337  }
338 
339  return(_IDENTIFIER);
340  }
341 
342 {EXT_IDENTIFIER} {
343  unsigned int i;
344 
345  if (currentIncludeLevel==0)
346  {
347  NEW(ReadWorldlval.id,ReadWorldleng+1,char);
348  memcpy(ReadWorldlval.id,ReadWorldtext,(unsigned int)ReadWorldleng);
349 
350  ReadWorldlval.id[ReadWorldleng]=0;
351 
352  for(i=0;i<ReadWorldleng;i++)
353  {
354  if (ReadWorldlval.id[i]==':')
355  ReadWorldlval.id[i]='_';
356  }
357  }
358  else
359  {
360  unsigned int k;
361 
362  NEW(ReadWorldlval.id,includeInfo[currentIncludeLevel-1].pl+2+ReadWorldleng+1,char);
363  sprintf(ReadWorldlval.id,"%s__%s",includeInfo[currentIncludeLevel-1].prefix,ReadWorldtext);
364  ReadWorldlval.id[includeInfo[currentIncludeLevel-1].pl+ReadWorldleng+2]=0;
365 
366  for(i=0,k=includeInfo[currentIncludeLevel-1].pl+2;i<ReadWorldleng;i++,k++)
367  {
368  if (ReadWorldlval.id[k]==':')
369  ReadWorldlval.id[k]='_';
370  }
371  }
372 
373  return(_EXT_IDENTIFIER);
374  }
375 
376 <<EOF>> {
377  if (currentIncludeLevel>0)
378  {
379  currentIncludeLevel--;
380 
381  /* Recover the line number */
382  RWline=includeInfo[currentIncludeLevel].line;
383 
384  /* Deletes the prefix */
385  free(includeInfo[currentIncludeLevel].prefix);
386 
387  /* Recover the first link/object in the upper file */
388  firstLink=includeInfo[currentIncludeLevel].fl;
389  firstObject=includeInfo[currentIncludeLevel].fo;
390 
391  /* Deletes the lex buffer and switch */
392  yy_delete_buffer(YY_CURRENT_BUFFER);
393  yy_switch_to_buffer(includeInfo[currentIncludeLevel].buffer);
394  }
395  else
396  yyterminate();
397  }
398 
399 . { return(ReadWorldtext[0]); }
400 
401 %%
402 
403 /*
404  * This function is automatically called by the parser at the end of a file.
405  * If 1 is returned the lexical analysis is stopped.
406  */
407 int ReadWorldwrap()
408 {
409  return(1);
410 }
411 
412 
413 void Switch2File(char *ln,unsigned int nl,unsigned int no,char *path,char *file)
414 {
415  FILE *f;
416  unsigned int l1,l2;
417  char *name;
418 
419  if (currentIncludeLevel>=MAX_INCLUDES)
420  ReadWorlderror("Includes nested too deeply. A loop of includes?" );
421 
422  /* in the new file, lines are numbered from 1 again :) */
423  includeInfo[currentIncludeLevel].line=RWline;
424  RWline=1;
425 
426  /* Store the first link/object in the current file and reset them */
427  includeInfo[currentIncludeLevel].fl=firstLink;
428  includeInfo[currentIncludeLevel].fo=firstObject;
429  firstLink=nl;
430  firstObject=no;
431 
432  /* identifiers in the new file will need the prefix defined next */
433  l2=strlen(ln);
434  if (currentIncludeLevel>0)
435  {
436  /* If we have a previous prefix, add it to the current prefix */
437  l1=includeInfo[currentIncludeLevel-1].pl;
438  NEW(includeInfo[currentIncludeLevel].prefix,l1+l2+2,char);
439  memcpy(includeInfo[currentIncludeLevel].prefix,includeInfo[currentIncludeLevel-1].prefix,l1*sizeof(char));
440  includeInfo[currentIncludeLevel].prefix[l1]='_';
441  l1++;
442  }
443  else
444  {
445  l1=0;
446  NEW(includeInfo[currentIncludeLevel].prefix,l2+1,char);
447  }
448  /* add the link name to the prefix */
449  memcpy(&(includeInfo[currentIncludeLevel].prefix[l1]),ln,l2*sizeof(char));
450  includeInfo[currentIncludeLevel].prefix[l1+l2]=0;
451 
452  includeInfo[currentIncludeLevel].pl=l1+l2; /* cache of the prefix lenght */
453 
454  /* stack the yybuffer */
455  includeInfo[currentIncludeLevel].buffer=YY_CURRENT_BUFFER;
456  currentIncludeLevel++;
457 
458  /* Now try to open the file to be included */
459  l1=strlen(path);
460  l2=strlen(file);
461  NEW(name,l1+l2+1,char);
462  memcpy(name,path,l1*sizeof(char));
463  memcpy(&(name[l1]),file,l2*sizeof(char));
464  name[l1+l2]=0;
465 
466  f=fopen(name,"r");
467  if (!f)
468  {
469  char *m;
470 
471  NEW(m,l1+l1+100,char);
472  sprintf(m,"Can not open included file: %s",name);
473  ReadWorlderror(m);
474  free(m);
475  }
476  else
477  {
478  yy_switch_to_buffer(yy_create_buffer(f,YY_BUF_SIZE));
479  BEGIN(INITIAL);
480  }
481 }
unsigned int RWline
Number of the line currently parsed when reading a .world file.
Definition: error_world.c:45
#define RZ
One of the types of homogeneous transforms in R^3.
Definition: htransform.h:75
#define RY
One of the types of homogeneous transforms in R^3.
Definition: htransform.h:67
#define CYLINDER
One of the possible type of polyhedrons.
Definition: polyhedron.h:80
#define TZ
One of the types of homogeneous transforms in R^3.
Definition: htransform.h:51
#define SEGMENTS
One of the possible type of polyhedrons.
Definition: polyhedron.h:100
#define LINE
One of the possible type of polyhedrons.
Definition: polyhedron.h:90
#define TX
One of the types of homogeneous transforms in R^3.
Definition: htransform.h:35
Definitions of constants and macros used in several parts of the cuik library.
#define MAX_INCLUDES
Maximum number of nested included files.
Definition: defines.h:148
#define SPHERE
One of the possible type of polyhedrons.
Definition: polyhedron.h:70
#define RX
One of the types of homogeneous transforms in R^3.
Definition: htransform.h:59
#define TY
One of the types of homogeneous transforms in R^3.
Definition: htransform.h:43
Error function specific of world.