A
download PHPCodeParser.pas
Language: Delphi
Copyright: (C) 2004,2005 Maguma, GmbH/Srl;
LOC: 237
Project Info
Maguma Open Studio(openstudio)
Server: SourceForge
Type: cvs
...tudio\OpenStudio\src\Units\
   .cvsignore
   dlgCancel.dfm
   dlgCancel.pas
   DosRedirect.pas
   MiscUtils.pas
   Passwords.pas
   PHPCodeElement.pas
   PHPCodeParser.pas
   PHPDocCommentParser.pas
   PHPUtils.pas
   RedirectConsole.pas
   RegExpr.pas
   ServiceControl.pas
   StringUtils.pas
   ToolButtons.pas

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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
{----------------------------------------------------------------------------

The contents of this file are subject to the Maguma Public License Version 
1.0 ("License"); You may not use this file except in compliance with the 
License. You may obtain a copy of the License at http://www.maguma.com/MaPL 
Software distributed under the License is distributed on an "AS IS" basis, 
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for 
the specific language governing rights and limitations under the License.

The Original Code is: Maguma Open Studio

The Initial Developer of the Original Code is Maguma, GmbH/Srl

Portions created by Maguma Open Studio are 
Copyright (C) 2004,2005 Maguma, GmbH/Srl;

All Rights Reserved.

Contributors listed in the contributors.txt file included with the 
distribution.

----------------------------------------------------------------------------

$Id: PHPCodeParser.pas,v 1.1 2005/03/09 09:29:28 spooker Exp $

You may retrieve the latest version of this file at the Open Studio home 
page, located at http://sourceforge.net/projects/openstudio/

----------------------------------------------------------------------------}

unit PHPCodeParser;

{$I PHPC.INC}

interface

uses Classes, PHPCodeElement, PHPDocCommentParser, Dialogs;

  function ParsePHPCode(const str: String; InPHP: Boolean = False): TPHPDocument;

implementation

uses SysUtils, MiscUtils;

const WHITE: set of Char = [#13, #10, #9, ' '];

(* ========================================================================== *)

procedure _ParsePHPCode(const str: String; var Root: TPHPCodeElement;
    CharPos: Integer; var InPHP: Boolean);
var
  len, i, p, bracelev: Integer;
  newel: TPHPCodeElement;
  quote: Char;
  InArgs: Boolean;
//  funcarg: String;

  procedure SkipWhite;
  begin
    while (i <= len) and (str[i] in WHITE) do inc(i);
  end;

  {$IFDEF PHPDOC}
  // Find the PHP Doc Comment before a code element
  function FindPHPDocComment(chrpos: Integer): TPHPDocComment;
  var
    iend, indnt: Integer;
  begin
    //Result := nil;
    Result := TPHPDocComment.Create;
    Result.FirstLineStartChar := CharPos + chrpos + 1;
    Result.LastLineEndChar := CharPos + chrpos + 1;

    // Find the indentation of the function, later, if there was a comment already,
    // we find the actual indent of the Comment, but we need this first if there
    // is no comment
    indnt := chrpos;
    while (indnt > 0) and not (str[indnt] in [#13, #10]) do
      dec(indnt);
    indnt := chrpos - indnt - 1;
    if indnt > 100 then // what happened?
      indnt := 24; // should be more than enough!
    Result.Indent := indnt + 1;


    while (chrpos > 0) and (str[chrpos] in WHITE) do dec(chrpos);
    dec(chrpos); // we should see a '*/' now!
    if (chrpos <= 0) or not (KeyComp('*/', @str[chrpos])) then Exit;

    // Set iend to end of line
    iend := chrpos;
    while (iend <= len) and not (str[iend] in [#13, #10]) do inc(iend);

    // Find the beginning of the comment
    while (chrpos > 0) and not ((str[chrpos] = '/') and (str[chrpos + 1] = '*')) do
      dec(chrpos);

    if chrpos = 0 then Exit;

    // Find the indentation of the comment
    indnt := chrpos;
    while (indnt > 0) and not (str[indnt] in [#13, #10]) do
      dec(indnt);
    indnt := chrpos - indnt - 1;
    if indnt > 100 then // what happened?
      indnt := 24; // should be more than enough!

    Result.Indent := indnt;

    // Find start of line
    while (chrpos > 0) and not (str[chrpos] in [#13, #10]) do dec(chrpos);
    inc(chrpos);

    // Include the last CRLFs into iend as well so that they're erased
    while (iend <= len) and (str[iend] in [#13, #10]) do inc(iend);

    //Result := TPHPDocComment.Create;
    Result.FirstLineStartChar := CharPos + chrpos;
    Result.LastLineEndChar := CharPos + iend;
    Result.CommentString := trim(copy(str, chrpos, iend - chrpos));
  end;
  {$ENDIF}

begin
  len := Length(str);
  i := 1;
  while i <= len do
  begin
    Case str[i] of
    'i', 'I', 'r', 'R': (* Include[Once] *)
          if InPHP then
          begin
            if (KeyComp('include', @str[i]) or KeyComp('include_once', @str[i]) or
                KeyComp('require', @str[i]) or KeyComp('require_once', @str[i])) // and we need whitespace in front of it!
              and (i > 1) and (str[i - 1] in WHITE) then
            begin
              p := i;
              while (i <= len) and (str[i] <> ';') do
                inc(i);
              newel := TPHPInclude.Create(Copy(str, p, i - p), CharPos + p - 1, i - p, Root);
              Root.AddChild(newel);
            end;
          end;
    'c', 'C': (* Class *)
          if InPHP then
          begin
            if KeyComp('class', @str[i]) // and we need whitespace in front of it!
              and (i > 1) and (str[i - 1] in WHITE) then
            begin
              p := i;

              if ((i > 0) and (not(str[i - 1] in WHITE))) or (not(str[i + 5] in WHITE)) then
                begin inc(i); continue; end;

              // Setup the class node
              while (i <= Length(str)) and (not (str[i] in ['{', ';'])) do inc(i);

              newel := TPHPClass.Create(copy(str, p, i - p), CharPos + p - 1, i - p, Root);
              Root.AddChild(newel);

              {$IFDEF PHPDOC}
              // Parse possible PHPDoc Comment
              TPHPClass(newel).PHPDocComment := FindPHPDocComment(p - 1);
              {$ENDIF}

              // now parse the class code, find end of class def first and then recurse into this function again
              bracelev := 0;
              p := i;
              while (i < len) and ((str[i] <> '}') or (bracelev > 1)) do
              begin
                if str[i] = '{' then
                  inc(bracelev)
                else if str[i] = '}' then
                  dec(bracelev);

                inc(i);
              end;

              _ParsePHPCode(copy(str, p, i - p + 1), newel, CharPos + p - 1, InPHP);
              TPHPClass(newel).ClassLength := newel.Length + i - p + 5;
            end;
          end;
    'f', 'F': (* Function *)
          if InPHP then
          begin
            if KeyComp('function', @str[i]) // and we need whitespace in front of it!
              and (i > 1) and (str[i - 1] in WHITE) and (Length(str) > (i+8)) and
              (str[i+8] <> '_') then
            begin
              p := i;
              // jump to the func args
              while (i < len) and (str[i] <> '(') do inc(i);

              // Function args
              while (i < len) and (str[i] <> ')') do
              begin

                if (str[i] = '$') then
                begin
                  // skip var name and any whitespace
                  inc(i);
                  while (i < len) and ((str[i] in ['A'..'Z', 'a'..'z', '0'..'9'])
                                         or (str[i] in WHITE)) do inc(i);

//                  if (str[i] = '(') then InArgs := True;
//                  if (str[i] = ')') and (not InArgs) then break;
//                  if (str[i] = ')') and (InArgs) then InArgs := False;
                  if (str[i] = ')') then break;
                  inc(i);
                  if (i < len) and (str[i-1] = '=') then
                  begin  // var has default value.  Overstep it.
                    while (i < len) and (str[i] in WHITE) do inc(i);
                    if (i < len) and ((str[i] = '"') or (str[i] = '''')) then
                    begin
                      if (str[i] = '"') then quote := '"'
                      else quote := '''';

                      inc(i);
                      while (i < len) and (str[i-1] <> '\') and (str[i] <> quote) do inc(i);
                      inc(i);
                    end
                    else // not a quoted value
                    begin
                      while (i < len) and (not (str[i] in [',',')'])) do
                      begin
                        if str[i] = '(' then
                        begin
                          inc(i);
                          while (i < len) and (not (str[i] <> ')')) do inc(i);
                        end;
                        inc(i);
                      end;
                    end;
                  end;
                end;

                if (i < len) and (str[i] = ')') then break;

                inc(i);
              end;

              if i > len then
                i := len - 1;

//              funcname := funcname;
              newel := TPHPFunction.Create(Copy(str, p, i - p + 1), CharPos + p - 1, i - p + 1, Root);
              Root.AddChild(newel);

              {$IFDEF PHPDOC}
              // Parse possible PHPDoc Comment
              TPHPFunction(newel).PHPDocComment := FindPHPDocComment(p - 1);
              {$ENDIF}
              // jump to beginning of func
              while (i < len) and (str[i] <> '{') do inc(i);  

              inc(i);
              p := i;
              bracelev := 1;
              while (i < len) and (bracelev > 0) do
              begin
                if str[i] = '{' then
                  inc(bracelev)
                else if str[i] = '}' then
                  dec(bracelev);
                  
                inc(i);
              end;

              TPHPFunction(newel).FunctionLength := newel.Length + i - p + 2;
              _ParsePHPCode(copy(str, p, i - p + 1), Root, CharPos + p - 1, InPHP);
            end;
          end;
    'v', 'V': (* var *)
          begin
            if (Root.ClassType = TPHPClass) and (i > 1) and (i+3 < len) and
               KeyComp('var', @str[i]) and (str[i - 1] in WHITE) and
               (str[i + 3] in WHITE) then
            begin
              p := i;
              while (i < len) and (str[i] <> ';') do
                inc(i);

              newel := TPHPVariable.Create(copy(str, p, i - p + 1),
                CharPos + p - 1, i - p + 1, nil);
              Root.AddChild(newel);

              {$IFDEF PHPDOC}
              // Parse possible PHPDoc Comment
              TPHPVariable(newel).PHPDocComment := FindPHPDocComment(p - 1);
              {$ENDIF}
            end;
          end;
    '<': (* PHP Open tag *)
          begin
            if KeyComp('<?', @str[i]) or KeyComp('<?php', @str[i]) then
              InPHP := True;
          end;
    '>': (* PHP Close tag *)
          begin
            if ((i > 2) and KeyComp('?>', @str[i - 1])) then
              InPHP := False;
          end;
     '/', '#':  (* Comments *)
          if InPHP then
          begin
            (* /* .. */ comments *)
            if (i < len) and (str[i + 1] = '*') then
            begin
              while (i < len) and (not KeyComp('*/', @str[i])) do
                inc(i)
            end
            (* // and # comments *)
            else
            if (str[i] = '#') or ((i < len) and (str[i] = '/') and (str[i + 1] = '/')) then
              while (i < len) and (not (str[i] in [#13, #10])) do
                inc(i);
          end;

     (* Strings *)
     '"':
          if InPHP then
          begin
            p := i - 1;
            while (p > 0) and (str[p] = '\') do dec(p);
            // The string delim char has only been escaped if there's an uneven number of \'s before
            if ((i - p + 1) mod 2) = 0 then
            begin
              inc(i);
              p := 0;
              while (i < len) and (p = 0) do
              begin
                if str[i] = '"' then
                begin
                  p := i - 1;
                  while (p > 0) and (str[p] = '\') do dec(p);
                  p := (i - p) mod 2;
                end;
                inc(i);
              end;
            end;
          end;
     '''':
          if InPHP then
          begin
            inc(i);
            while (i < len) and (str[i] <> '''') do
              inc(i)
          end;
    end; { case }
    inc(i);
  end;
end;

function ParsePHPCode(const str: String; InPHP: Boolean = False): TPHPDocument;
var
  el: TPHPCodeElement;
begin
  Result := TPHPDocument.Create('Document', 0, 0, nil);
  el := Result;
  _ParsePHPCode(str, el, 0, InPHP);
end;

end.

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us