Difference between revisions of "fcl-passrc"

From Lazarus wiki
(+ example source code using fcl-passrc)
m (Text replace - "pascal>" to "syntaxhighlight>")
Line 23: Line 23:
 
= an example =
 
= an example =
  
<pascal>
+
<syntaxhighlight>
 
{$mode objfpc}{$H+}
 
{$mode objfpc}{$H+}
  
Line 77: Line 77:
 
   finally FreeAndNil(E) end;
 
   finally FreeAndNil(E) end;
 
end.
 
end.
</pascal>
+
</syntaxhighlight>
  
 
Go to back [[Package_List|Packages List]]
 
Go to back [[Package_List|Packages List]]

Revision as of 00:51, 25 March 2012

This package contains some useful units for dealing with Pascal source files: Parsing, creating structures in memory, and output to files.

Used by: fpdoc

units

Unit - comment
pastree - Pascal parse tree classes, for storing a complete Pascal module in memory.
paswrite - A class and helper functions for creating Pascal source files from parse trees
pparser - Parser for Pascal source files. Reads files via the pscanner unit and stores all parsed data in a parse tree, as implemented in the pastree unit.
pscanner - Lexical scanner class for Pascal sources

an example

{$mode objfpc}{$H+}

uses SysUtils, Classes, PParser, PasTree;

type
  { We have to override abstract TPasTreeContainer methods.
    See utils/fpdoc/dglobals.pp for an implementation of TFPDocEngine,
    a "real" engine. }
  TSimpleEngine = class(TPasTreeContainer)
  public
    function CreateElement(AClass: TPTreeElement; const AName: String;
      AParent: TPasElement; AVisibility: TPasMemberVisibility;
      const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
      override;
    function FindElement(const AName: String): TPasElement; override;
  end;

function TSimpleEngine.CreateElement(AClass: TPTreeElement; const AName: String;
  AParent: TPasElement; AVisibility: TPasMemberVisibility;
  const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
begin
  Result := AClass.Create(AName, AParent);
  Result.Visibility := AVisibility;
  Result.SourceFilename := ASourceFilename;
  Result.SourceLinenumber := ASourceLinenumber;
end;

function TSimpleEngine.FindElement(const AName: String): TPasElement;
begin
  { dummy implementation, see TFPDocEngine.FindElement for a real example }
  Result := nil;
end;

var
  M: TPasModule;
  E: TPasTreeContainer;
  I: Integer;
  Decls: TList;
begin
  E := TSimpleEngine.Create;
  try
    M := ParseSource(E, ParamStr(1), 'linux', 'i386');

    { Cool, we successfully parsed the unit.
      Now output some info about it. }
    Decls := M.InterfaceSection.Declarations;
    for I := 0 to Decls.Count - 1 do
      Writeln('Interface item ', I, ': ' +
        (TObject(Decls[I]) as TPasElement).Name);

    FreeAndNil(M);
  finally FreeAndNil(E) end;
end.

Go to back Packages List