1 %start input
2 
3 %{
4 #include <stdio.h>
5 #include <string.h>
6 #include <strings.h>
7 
8 #include "xchar.h"
9 #include "mensajes.h"
10 #include "procesador.h"
11 #include "tree.h"
12 
13 /* define to 1 and set variable yydebug to 1 to get debug messages */
14 #define YYDEBUG 0
15 
16 /* list of attributes of the current element */
17 #define MAX_ELEMENT_ATTRIBUTES  255
18 static void setAttributeData(char *data);
19 /* static void freeAttributeData(void); */
20 
21 static char *element_attributes[MAX_ELEMENT_ATTRIBUTES];
22 static int num_element_attributes= 0;
23 
24 /* set the lexer in the script mode */
25 void lexer_begin_script(char *nombre);
26 
27 /* control <pre> elements */
28 extern int pre_state;
29 
30 extern int yylex (void);
31  extern int yyerror(char *e);
32 
33 #ifdef DEBUG_MEM
34 #define free(x)      fprintf(stderr,"==%p  [%s]\n",x,x); free(x)
35 #endif
36 
37 %}
38 
39 %union {
40   int  ent;
41   char *cad;
42 }
43 
44 
45 %token <cad> TOK_DOCTYPE TOK_COMMENT TOK_BAD_COMMENT TOK_STAG_INI TOK_ETAG TOK_CDATA
46 %token <cad> TOK_ATT_NAME TOK_ATT_NAMECHAR TOK_ATT_VALUE TOK_EREF TOK_CREF
47 %token <cad> TOK_CDATA_SEC TOK_XMLPI_INI
48 %token <ent> TOK_STAG_END TOK_EMPTYTAG_END TOK_ATT_EQ TOK_XMLPI_END
49 %token <ent> TOK_WHITESPACE
50 
51 %%
52 
53 input: html
54 ;
55 
56 html:
57 | html doctype
58 | html comment
59 | html bad_comment
60 | html stag
61 | html etag
62 | html cdata
63 | html cdata_sec
64 | html whitespace
65 | html ref
66 | html xmldecl
67 | html error
68 ;
69 
70 doctype: TOK_DOCTYPE {
71   //fprintf(stderr,"TOK_DOCTYPE: %s\n",$1);
72   saxDoctype($1);
73 }
74 ;
75 
76 comment: TOK_COMMENT {
77   //fprintf(stderr,"TOK_COMMENT: %s\n",$1);
78   saxComment($1);
79 }
80 ;
81 
82 bad_comment: TOK_BAD_COMMENT {
83   //fprintf(stderr,"TOK_BAD_COMMENT: %s\n",$1);
84   INFORM("bad comment");
85 }
86 ;
87 
88 stag: TOK_STAG_INI attributes TOK_STAG_END {
89   //fprintf(stderr,"STAG-: %s\n",$1);
90   setAttributeData(NULL);
91   saxStartElement($1,(xchar**)element_attributes);
92 /*   freeAttributeData(); */
93   num_element_attributes = 0;
94 
95   /* set the lexer in script mode (for SCRIPT and STYLE) */
96   if ((!strcasecmp($1,"script")) ||(!strcasecmp($1,"style")))
97     lexer_begin_script($1);
98 
99   if (!strcasecmp($1,"pre")) {DEBUG("enter PRE mode");pre_state++;}
100 
101 /*   free($1); */
102 }
103 ;
104 
105 etag: TOK_ETAG {
106   //fprintf(stderr,"ETAG-: %s\n",$1);
107   saxEndElement($1);
108   if (!strcasecmp($1,"pre")) {DEBUG("leaving PRE mode");pre_state--;}
109 
110 /*   free($1); */
111 }
112 ;
113 
114 stag: TOK_STAG_INI attributes TOK_EMPTYTAG_END {
115   //fprintf(stderr,"EMTYTAG-: %s\n",$1);
116   setAttributeData(NULL);
117   saxStartElement($1,(xchar**)element_attributes);
118 /*   freeAttributeData(); */
119   num_element_attributes = 0;
120 /*   free($1); */
121   saxEndElement($1);
122 }
123 ;
124 
125 cdata: TOK_CDATA {
126   //fprintf(stderr,"CDATA: <%s>\n",$1);
127   saxCharacters($1,strlen($1));
128 }
129 ;
130 
131 cdata_sec: TOK_CDATA_SEC {
132   //fprintf(stderr,"CDATA_SEC: <%s>\n",$1);
133   saxCDataSection($1,strlen($1));
134 }
135 ;
136 
137 whitespace: TOK_WHITESPACE {
138   saxWhiteSpace();
139 }
140 
141 ref: TOK_EREF {
142   //fprintf(stderr,"EREF: %s\n",$1);
143   saxReference($1);
144 /*   free($1); */
145 }
146 | TOK_CREF {
147   //fprintf(stderr,"CREF: %s\n",$1);
148   saxReference($1);
149 }
150 ;
151 
152 xmlpi_end: TOK_XMLPI_END
153 | TOK_STAG_END
154 | TOK_EMPTYTAG_END
155 ;
156 
157 xmldecl: TOK_XMLPI_INI attributes xmlpi_end {
158   /*fprintf(stderr,"XMLDECL-: %s\n",$1);*/
159   setAttributeData(NULL);
160   saxXmlProcessingInstruction($1,(xchar**)element_attributes);
161   num_element_attributes = 0;
162 }
163 ;
164 
165 
166 attributes:
167 | attributes attribute
168 | attributes error
169 ;
170 
171 attribute: TOK_ATT_NAME TOK_ATT_EQ TOK_ATT_VALUE {
172                      setAttributeData($1);
173                      setAttributeData($3);
174                      }
175 | TOK_ATT_NAME {
176                      setAttributeData($1);
177                      setAttributeData($1);
178 }
179 | TOK_ATT_NAME TOK_ATT_EQ {
180                      char *cad= (char*) tree_malloc(1);
181                      cad[0]= 0;
182                      setAttributeData($1);
183                      setAttributeData(cad);
184 }
185 ;
186 
187 %%
188 
189 
190 /*
191  * setAttributeData
192  *
193  * inserts 'data' into the attribute list of the current element.
194  * (only the pointer is inserted; therefore, the data must be kept
195  * in memory by the caller until it is no longer needed)
196  *
197  */
198 static void setAttributeData(char *data)
199 {
200   /* if data == NULL, close the attribute list */
201   if (!data) {
202     element_attributes[num_element_attributes]= NULL;
203     return;
204   }
205 
206   if (num_element_attributes == MAX_ELEMENT_ATTRIBUTES-1) {
207     EXIT("maximum number of attributes for one element reached\n");
208   }
209 
210   element_attributes[num_element_attributes++]= data;
211 }
212