Advertisement
WarPie90

SuperSimpleUglyParser

Jun 15th, 2014
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.60 KB | None | 0 0
  1. program new;
  2.  
  3. function String.StartsWith(Prefix:String): Boolean;
  4. var
  5.   i: Int32;
  6. begin
  7.   if Length(Prefix) > Length(Self) then
  8.     Exit(False);
  9.   Result := True;
  10.   for i:=1 to Length(Prefix) do
  11.     if (Prefix[i] <> Self[i]) then
  12.       Exit(False);
  13. end;
  14.  
  15. var
  16.   EndTokens:TStringArray = ['OVERLOAD','OVERRIDE','EXTERNAL','CDECL','STDCALL','SAFECALL','INLINE']; //ADD WHAT EVER
  17.  
  18. function ParseFunctionHeads(Data:String): TStringArray;
  19.   //Skip newlines, space, and other junk chars..
  20.   procedure SkipJunk(Data:String; var p:Int32);
  21.   begin
  22.     while (Data[p] in [#0,#9,#10,#11,#13,#32]) and (p <= length(Data)) do Inc(p);
  23.   end;
  24.  
  25.   //Increse position until we reach "C"
  26.   function SkipTo(Data:String; var p:Int32; C:Char): Boolean;
  27.   begin
  28.     Result := True;
  29.     while (Data[p] <> C) and (p <= length(Data)) do Inc(p);
  30.     if (p > length(Data)) then Exit(False);
  31.   end;
  32.  
  33.   //Parse from pos till the end of the method, returns false if it's not a method.
  34.   function ParseMathod(Data:String; var pos:Int32; var funcDef:String; Decl:String): Boolean;
  35.   var i,j:Int32; inString:Boolean=False; pcount:Int32;
  36.   begin
  37.     i := 1+Length(Decl);
  38.     SkipJunk(Data,i);
  39.  
  40.     //methods should alsawys start with given chars:
  41.     if not(Data[i] in ['a'..'z','A'..'Z', '_']) then  begin Inc(pos, i); Exit(False); end;
  42.     Inc(i);
  43.  
  44.     //Now they can be numeric as well, and have dots
  45.     while (Data[i] in ['a'..'z','A'..'Z', '_', '.', '0'..'9']) and (i < length(Data)) do Inc(i);
  46.     SkipJunk(Data,i);
  47.  
  48.     //Does it have parameters?
  49.     if Data[i] = '(' then begin Inc(pcount); Inc(i); end;
  50.  
  51.     //While inside param-expressions do:
  52.     while (pcount > 0) do begin
  53.       if Data[i] = #39 then InString := Not(InString)
  54.       else if (Data[i] = '(') and Not(InString) then inc(pcount)
  55.       else if (Data[i] = ')') and Not(InString) then dec(pcount);
  56.       inc(i);
  57.       if i > length(Data) then begin Inc(pos, i); Exit(False); end;
  58.     end;
  59.  
  60.     if not(SkipTo(Data, i, ';')) then begin Inc(pos, i); Exit(False); end;
  61.     Inc(i);
  62.  
  63.     //Check for all possible endtokens:
  64.     j := 0;
  65.     while j < Length(EndTokens) do begin
  66.       SkipJunk(data,i);
  67.       //if (UpperCase(Data[i]) = EndTokens[j][1]) then //uncomment for speedup.. lape fails.
  68.       if UpperCase(Copy(Data,i,Length(EndTokens[j]))) = EndTokens[j] then begin
  69.         Inc(i,length(EndTokens[j]));
  70.         if not(SkipTo(Data, i, ';')) then Break;
  71.         Inc(i);
  72.         j := 0;
  73.         continue;
  74.       end;
  75.  
  76.       Inc(j);
  77.     end;
  78.  
  79.     funcDef :=  Copy(Data,0,i-1);
  80.     Inc(pos, i-1);
  81.     Exit(True);
  82.   end;
  83.  
  84. var
  85.   i,j,tm:Int32;
  86.   tmp,funcDef:String;
  87.   DTypes:TStringArray = ['FUNCTION', 'PROCEDURE'];
  88. begin
  89.   i := 1;
  90.   while i < Length(Data) do
  91.   begin
  92.     tmp := Copy(Data,i,255);
  93.     for j:=0 to High(DTypes) do
  94.     //is procedure?
  95.     if uppercase(tmp).startswith(DTypes[0]) then begin
  96.       if ParseMathod(tmp, i, funcDef, DTypes[0]) then begin
  97.         j := Length(Result);
  98.         SetLength(Result, j+1);
  99.         Result[j] := FuncDef;
  100.       end else Inc(i);
  101.     end else
  102.     //is function?
  103.     if uppercase(tmp).startswith(DTypes[1]) then begin
  104.       if ParseMathod(tmp, i, funcDef,DTypes[1]) then begin
  105.         j := Length(Result);
  106.         SetLength(Result, j+1);
  107.         Result[j] := FuncDef;
  108.       end else Inc(i);
  109.     //junk...
  110.     end else begin
  111.       Inc(i);
  112.       Continue;
  113.     end;
  114.   end;
  115. end;
  116.  
  117.  
  118. var
  119.   heads:TStringArray;
  120.   s:String;
  121.   i:Int32;
  122. begin
  123.   s := getPage('http://paste.villavu.com/raw/6115/');
  124.   heads := ParseFunctionHeads(s);
  125.   for i:=0 to High(heads) do
  126.     WriteLn(heads[i] +#13#10);
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement