summaryrefslogtreecommitdiff
path: root/setedit/mainsrc/pclipper.cc
blob: a8026bf9efbd9f5eafdcdda0ed06364b5f748b0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* Copyright (C) 2000-2002 by Salvador E. Tropea (SET),
   see copyrigh file for details */
/**[txh]********************************************************************

  Description:
  Clipper parser used by bufun.cc when the syntax is Clipper 5.x.

***************************************************************************/

#define Uses_string
#define Uses_stdio
#define Uses_ctype
#define Uses_TVCodePage
#include <tv.h>

#include <bufun.h>

#if 0
FILE *f;

static
int GetChar()
{
 return fgetc(f);
}
#else
static char *buffer;
static unsigned lenBuffer, indexBuffer;

static
int GetChar()
{
 return indexBuffer>=lenBuffer ? EOF : buffer[indexBuffer++];
}
#endif

const int LargoMax=255;
static char FirstCol=1;
static int  Line=1;

static
int GetUpToEOL()
{
 int c;
 do
   {
    c=GetChar();
   }
 while (c!='\n' && c!=EOF);
 FirstCol=1;
 Line++;
 return c;
}

static
int GetWord()
{
 int c,IndiceW=0;
 do
   {
    c=GetChar();
    switch (c)
      {
       case '"':
            FirstCol=0;
            do
              {
               c=GetChar();
              }
            while (c!='"' && c!='\n' && c!=EOF);
            if (c=='\n')
              {
               FirstCol=1;
               Line++;
              }
            if (IndiceW) return IndiceW;
            break;

       case '\'':
            FirstCol=0;
            do
              {
               c=GetChar();
              }
            while (c!='\'' && c!='\n' && c!=EOF);
            if (c=='\n')
              {
               FirstCol=1;
               Line++;
              }
            if (IndiceW) return IndiceW;
            break;

       case '*':
            FirstCol=0;
            c=GetChar();
            if (c==' ')
              {
               c=GetChar();
               if (c=='-')
                 { // That's an EOL comment.
                  c=GetUpToEOL();
                 }
              }
            if (IndiceW) return IndiceW;
            break;

       case '/':
            FirstCol=0;
            c=GetChar();
            if (c=='/')
              { // That's an EOL comment.
               c=GetUpToEOL();
              }
            else
              {
               if (c=='*')
                 { // Large comment
                  c=GetChar();
                  if (c=='\n') Line++;
                  do
                    {
                     if (c=='*')
                       {
                        c=GetChar();
                        if (c=='/')
                           break;
                       }
                     else
                        c=GetChar();
                     if (c=='\n') Line++;
                    }
                  while (c!=EOF);
                  FirstCol=0;
                 }
              }
            if (IndiceW) return IndiceW;
            break;

       case '&':
            FirstCol=0;
            c=GetChar();
            if (c=='&')
               c=GetUpToEOL();
            if (IndiceW) return IndiceW;
            break;

       case '#':
            if (FirstCol)
               c=GetUpToEOL();
            if (IndiceW) return IndiceW;
            break;

       default:
            if (isalnum(c) || c=='_')
              {
               if (IndiceW<MaxLen)
                  bfBuffer[IndiceW++]=c;
              }
            else
              {
               if (c=='\n')
                 {
                  FirstCol=1;
                  Line++;
                 }
               else
                 {
                  if (!isspace(c))
                     FirstCol=0;
                 }
               if (IndiceW) return IndiceW;
              }
      }
   }
 while (c!=EOF);
 return IndiceW;
}

static
int CheckStr(char *word, char *toCheck)
{
 int l=0;
 while (*toCheck && *word)
   {
    if (toupper(*toCheck)!=*word) return 0;
    toCheck++; word++;
    l++;
   }      
 if (*toCheck) return 0;
 return l>=4;
}

static
int Parsear(tAddFunc AddFunc, int mode)
{
 int r,f,p,line=-1,funs=0,Static=0,lastReturn=-1,lenTemp=0;
 FirstCol=1;
 Line=1;
 do
   {
    r=GetWord();
    if (r)
      {
       bfBuffer[r]=0; p=0;
       // Memorize the last return/quit
       if (CheckStr("RETURN",bfBuffer) || CheckStr("QUIT",bfBuffer))
         {
          lastReturn=Line;
          Static=0;
          continue;
         }
       // Memorize if the static modifier is there
       if (!Static)
         {
          Static=CheckStr("STATIC",bfBuffer);
          if (Static) continue;
         }
       // Check if that's a function or procedure
       f=CheckStr("FUNCTION",bfBuffer);
       if (!f) p=CheckStr("PROCEDURE",bfBuffer);
       if (f || p)
         {
          if (line>=0)
            { // We have one cached, add it
             AddFunc(bfNomFun,lenTemp,line,lastReturn);
             lastReturn=-1;
            }
          line=Line;
          r=GetWord();
          bfBuffer[r]=0;
          if (r<MaxLen-5 && (mode & modeBFClassSep))
            {
             sprintf(bfNomFun,"%s (%c%c)",bfBuffer,
                     f ? 'F' : 'P',         // Function or Procedure
                     Static ? 'S' : ' ');   // Static or normal
             lenTemp=r+6;
            }
          else
            {
             strcpy(bfNomFun,bfBuffer);
             lenTemp=r+1;
            }
          //printf("%s %s %d\n",f ? "Function" : "Procedure",bfNomFun,line);
          funs++;
         }
       Static=0;
      }
   }
 while (r);
 if (line>=0) // We have one cached, add it
    AddFunc(bfNomFun,lenTemp,line,lastReturn);

 return funs;
}

int SearchClipperFuncs(char *b, unsigned l, int mode, tAddFunc AddFunc)
{
 buffer=b;
 lenBuffer=l;
 indexBuffer=0;
 return Parsear(AddFunc,mode);
}

#if 0
int main(int argc, char *argv[])
{
 if (argc!=2)
   {
    printf("Prueba de parser de Clipper, Copyright 2000 by Salvador E. Tropea\n");
    printf("Uso: pclipper archivo\n");
    return 1;
   }
 f=fopen(argv[1],"rt");
 if (!f)
   {
    printf("No se pudo abrir %s\n",argv[1]);
    return 2;
   }
 Parsear();
 fclose(f);
 return 0;
}
#endif