Remove Unixware and openserver support
[oweals/cde.git] / cde / programs / dtinfo / dtinfogen / infolib / etc / lex.l
1 %{ /* -*- c++ -*- */
2 /* $XConsortium: lex.l /main/5 1996/11/19 16:55:12 drk $ */
3
4 #if !defined(__osf__) && !defined(linux) && !defined(CSRG_BASED)
5 #include <osfcn.h>
6 #else
7 #include <unistd.h>
8 #endif
9
10 #include <iostream.h>
11 #include <stdio.h>
12 #include <stream.h>  
13 #include <string.h>
14
15 #include "Exceptions.hh"
16
17 #include "SGMLDefn.h"  
18 #include "Dispatch.h"
19 #include "AttributeRec.h"
20 #include "AttributeList.h"  
21 #include "FlexBuffer.h"
22 #include "Token.h"
23 #include "api/utility.h"
24
25 static SGMLDefn *defn = new SGMLDefn();
26 static FlexBuffer *DataBuffer = new FlexBuffer();
27
28 static
29 unsigned char oct2dec( const char *str )
30 {
31   unsigned char  value = 0;
32   const char *ptr = str;
33   const char *endptr = str + strlen(str) -1 ;
34   
35   for ( ; *ptr != '\0' ; ptr++ ) {
36     
37     int power = endptr - ptr;
38     int result     = 1;
39     
40     for ( int i = 1; i <= power; i++ ) {
41       result = result * 8;
42     }
43
44     value = value + ( *ptr - '0' ) * result ;
45   }
46
47   
48   return ( value );
49 }
50
51
52
53 %}
54 %x ProcessData
55 %%
56
57 ^A[^\n]+                   {
58                              char *name;
59                              char *tstr;
60                              char *value;
61                              int type;
62                              // Parse the attStr
63
64                              name = strtok ( (char*)yytext+1, "\n\t ");
65                              tstr = strtok ( NULL, "\n\t ");
66                              value = strtok ( NULL, "\n\t");
67                              type = SGMLName::intern(tstr);
68
69                              if ( type != SGMLName::IMPLIED ){
70                                if(!value) value = "";
71                                Dispatch::tok->StoreAttribute( name, value, type);
72                              }
73                            }
74
75 ^"("[^\n\t ]+              {
76                              // An generic identifier is found
77                              // Need to determine how to display/return the
78                              // information associated with it
79
80                              Dispatch::token(START, (unsigned char *)yytext+1 );
81                             
82                            }
83
84 ^"L"[0-9]+" "[^ \n]+       {    /* file & line num info. */
85                                 char *p = (char*)yytext+1;
86                                 Dispatch::line(atoi(p));
87                                 while(*p != ' ') p++;
88                                 p++;
89                                 Dispatch::file(p);
90                            }
91                                 
92 ^"L"[0-9]+                 {    /* line num info. */
93                                 char *p = (char*)yytext+1;
94                                 Dispatch::line(atoi(p));
95                            }
96                                 
97 ^"s"[^\n]+                 {
98                               /* system id found */
99                               defn->store_sys_id( (char *)yytext + 1 );
100                             }
101 ^"f"[^\n]+                  {
102                               /* file name found */
103                               defn->store_file_name( (char *)yytext + 1 );
104                             }
105 ^"E"[^\n]+                  {
106                               /*
107                                * For now, only entity definition is recorded
108                                * Eventually Notation, Subdoc....
109                                */
110                               defn->store_defn( ENTITY_TYPE,
111                                                 (char *)yytext + 1 );
112                               
113                               Dispatch::entity_decl( defn );
114                               
115                             }
116 ^"&"[^\n]+                  {
117                               /*
118                                * external entity reference
119                                */
120                               Dispatch::token(EXTERNAL_ENTITY, 
121                                               (unsigned char *)yytext+1 );
122                             }
123
124 ^"{"                        {
125                               /*
126                                * subdoc start event
127                                */
128                              Dispatch::subdoc_start();
129
130                            }
131
132 ^"}"                       {
133                              /*
134                               * subdoc end event
135                               */
136                              Dispatch::subdoc_end();
137                            }
138
139 ^-                         {
140                              BEGIN ( ProcessData );
141                            }
142
143 <ProcessData>"\\""\\"      {
144                               // A slash
145                               DataBuffer->put ('\\');
146                            }
147 <ProcessData>"\\n"         {
148                              // Replace new line with space
149                              DataBuffer->put('\015');
150                              Dispatch::newline();
151                              
152                            }
153 <ProcessData>"\\|"         {
154                              // Bypass this entity
155                            }
156
157 <ProcessData>"\\|lnfeed\\|"  {
158                                DataBuffer->put('\n');
159                              }
160
161 <ProcessData>"\\"[0-7][0-7][0-7] {
162   
163                                    unsigned char ch = oct2dec(
164                                       (const char *)yytext + 1 );
165                                    DataBuffer->put ( ch );
166
167                                  }
168 <ProcessData>\n            {
169                              Dispatch::data( DataBuffer );
170                              delete DataBuffer;
171                              DataBuffer = new FlexBuffer;
172
173                              BEGIN(0);
174                            }
175
176 <ProcessData>[^\n\\]+      {
177                              DataBuffer->write( (char*)yytext,
178                                                 strlen((char*)yytext) );
179                            }
180
181 <ProcessData>.             {
182                              throw(Unexpected(form("Bad character '%s' found",
183                                                    (char *)yytext)));
184                            }
185
186 ^")"[^\n\t ]+              {
187                              Dispatch::token (END, (unsigned char *)yytext+1);
188                            }
189
190 .                          |
191 \n                         ;
192                              
193 %%
194
195 #ifdef DEBUG
196 //---------------------------------------------------------------------
197 #include "OLAF.h"  
198 #include "SGMLName.h"
199
200 static void TestToken( Token *tok )
201 {
202   cout << "(" << tok->giName() << endl;
203   for ( const AttributeRec *a = tok->GetFirstAttr(); a; a=tok->GetNextAttr( a ) ) {
204     cout << "AttributeName = " << SGMLName::lookup(a->getAttrName()) << endl;
205     cout << "AttributeValueStr = " << a->getAttrValueString() << endl;
206     cout << "AttributeValue = " << a->getAttValue() << endl;
207     cout << "AttributeType = " << a->getAttrType() << endl;
208
209     if ( !(strcmp( a->getAttrValueString(), "NODE" ) ) ){
210       if ( tok->AttributeMatch ( OLAF::OLIAS, OLAF::OL_Section ) ) {
211         cout << "AttrMatch Test 1 passed\n";
212       }
213       else {
214         cout << "AttrMatch Test 1 failed\n";
215       }
216
217       if ( !tok->AttributeMatch ( OLAF::OLIAS, OLAF::OL_Graphic ) ) {
218         cout << "AttrMatch Test 2 passed\n";
219       }
220       else {
221         cout << "AttrMatch Test 2 failed\n";
222       }
223       
224     }
225   }
226 }
227 #endif /* DBUG_OFF */