Difference between revisions of "fcl-passrc"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This package contains some useful units for dealing with Pascal source
+
This package contains some useful units for dealing with Pascal source files: Parsing, creating structures in memory, and output to files.
files: Parsing, creating structures in memory, and output to files.
 
  
 
Used by: [[fpdoc]]
 
Used by: [[fpdoc]]
  
= units =  
+
== Units ==
  
 
{| class="wikitable"
 
{| class="wikitable"
! Unit !! - !! Comment  
+
! Unit !! Comment  
|----
+
|-
|[[pastree]]   || || Pascal parse tree classes, for storing a complete Pascal module in memory.
+
|[[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
+
|[[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.
+
|[[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
+
|[[pscanner]] || Lexical scanner class for Pascal sources
|----
+
|-
 
|}
 
|}
  
= an example =
+
==An example ==
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
{$mode objfpc}{$H+}
 
{$mode objfpc}{$H+}
  
Line 76: Line 75:
 
       Writeln('No interface section --- this is not a unit, this is a ', M.ClassName);
 
       Writeln('No interface section --- this is not a unit, this is a ', M.ClassName);
  
     Decls := M.ImplementationSection.Declarations;
+
     if M.ImplementationSection <> nil then // may be nil in case of a simple program
    for I := 0 to Decls.Count - 1 do
+
    begin
      Writeln('Implementation item ', I, ': ' +
+
      Decls := M.ImplementationSection.Declarations;
        (TObject(Decls[I]) as TPasElement).Name);
+
      for I := 0 to Decls.Count - 1 do
 +
        Writeln('Implementation item ', I, ': ' +
 +
          (TObject(Decls[I]) as TPasElement).Name);
 +
    end;
  
 
     FreeAndNil(M);
 
     FreeAndNil(M);

Latest revision as of 23:44, 14 February 2020

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: {$ifdef VER2_6} TList {$else} { for FPC > 2.6.0 } TFPList {$endif};
begin
  E := TSimpleEngine.Create;
  try
    M := ParseSource(E, ParamStr(1), 'linux', 'i386');

    { Cool, we successfully parsed the module.
      Now output some info about it. }
    if M.InterfaceSection <> nil then
    begin
      Decls := M.InterfaceSection.Declarations;
      for I := 0 to Decls.Count - 1 do
        Writeln('Interface item ', I, ': ' +
          (TObject(Decls[I]) as TPasElement).Name);
    end else
      Writeln('No interface section --- this is not a unit, this is a ', M.ClassName);

    if M.ImplementationSection <> nil then // may be nil in case of a simple program
    begin
      Decls := M.ImplementationSection.Declarations;
      for I := 0 to Decls.Count - 1 do
        Writeln('Implementation item ', I, ': ' +
          (TObject(Decls[I]) as TPasElement).Name);
    end;

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

Go to back Packages List