fcl-passrc: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: fcl-passrc
| Old | New | Line | |
|---|---|---|---|
| 1 | + | This package contains some useful units for dealing with Pascal source files: Parsing, creating structures in memory, and output to files. | |
| 2 | + | ||
| 3 | + | Used by: [[fpdoc]] | |
| 4 | + | ||
| 5 | + | ## Units | |
| 6 | + | ||
| 7 | + | | Unit | Comment | | |
| 8 | + | |----|----| | |
| 9 | + | | [[pastree]] | Pascal parse tree classes, for storing a complete Pascal module in memory. | | |
| 10 | + | | [[paswrite]] | A class and helper functions for creating Pascal source files from parse trees | | |
| 11 | + | | [[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. | | |
| 12 | + | | [[pscanner]] | Lexical scanner class for Pascal sources | | |
| 13 | + | | | | | |
| 14 | + | ||
| 15 | + | ## An example | |
| 16 | + | ||
| 17 | + | ``` pascal | |
| 18 | + | {$mode objfpc}{$H+} | |
| 19 | + | ||
| 20 | + | uses SysUtils, Classes, PParser, PasTree; | |
| 21 | + | ||
| 22 | + | type | |
| 23 | + | { We have to override abstract TPasTreeContainer methods. | |
| 24 | + | See utils/fpdoc/dglobals.pp for an implementation of TFPDocEngine, | |
| 25 | + | a "real" engine. } | |
| 26 | + | TSimpleEngine = class(TPasTreeContainer) | |
| 27 | + | public | |
| 28 | + | function CreateElement(AClass: TPTreeElement; const AName: String; | |
| 29 | + | AParent: TPasElement; AVisibility: TPasMemberVisibility; | |
| 30 | + | const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement; | |
| 31 | + | override; | |
| 32 | + | function FindElement(const AName: String): TPasElement; override; | |
| 33 | + | end; | |
| 34 | + | ||
| 35 | + | function TSimpleEngine.CreateElement(AClass: TPTreeElement; const AName: String; | |
| 36 | + | AParent: TPasElement; AVisibility: TPasMemberVisibility; | |
| 37 | + | const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement; | |
| 38 | + | begin | |
| 39 | + | Result := AClass.Create(AName, AParent); | |
| 40 | + | Result.Visibility := AVisibility; | |
| 41 | + | Result.SourceFilename := ASourceFilename; | |
| 42 | + | Result.SourceLinenumber := ASourceLinenumber; | |
| 43 | + | end; | |
| 44 | + | ||
| 45 | + | function TSimpleEngine.FindElement(const AName: String): TPasElement; | |
| 46 | + | begin | |
| 47 | + | { dummy implementation, see TFPDocEngine.FindElement for a real example } | |
| 48 | + | Result := nil; | |
| 49 | + | end; | |
| 50 | + | ||
| 51 | + | var | |
| 52 | + | M: TPasModule; | |
| 53 | + | E: TPasTreeContainer; | |
| 54 | + | I: Integer; | |
| 55 | + | Decls: {$ifdef VER2_6} TList {$else} { for FPC > 2.6.0 } TFPList {$endif}; | |
| 56 | + | begin | |
| 57 | + | E := TSimpleEngine.Create; | |
| 58 | + | try | |
| 59 | + | M := ParseSource(E, ParamStr(1), 'linux', 'i386'); | |
| 60 | + | ||
| 61 | + | { Cool, we successfully parsed the module. | |
| 62 | + | Now output some info about it. } | |
| 63 | + | if M.InterfaceSection <> nil then | |
| 64 | + | begin | |
| 65 | + | Decls := M.InterfaceSection.Declarations; | |
| 66 | + | for I := 0 to Decls.Count - 1 do | |
| 67 | + | Writeln('Interface item ', I, ': ' + | |
| 68 | + | (TObject(Decls[I]) as TPasElement).Name); | |
| 69 | + | end else | |
| 70 | + | Writeln('No interface section --- this is not a unit, this is a ', M.ClassName); | |
| 71 | + | ||
| 72 | + | if M.ImplementationSection <> nil then // may be nil in case of a simple program | |
| 73 | + | begin | |
| 74 | + | Decls := M.ImplementationSection.Declarations; | |
| 75 | + | for I := 0 to Decls.Count - 1 do | |
| 76 | + | Writeln('Implementation item ', I, ': ' + | |
| 77 | + | (TObject(Decls[I]) as TPasElement).Name); | |
| 78 | + | end; | |
| 79 | + | ||
| 80 | + | FreeAndNil(M); | |
| 81 | + | finally FreeAndNil(E) end; | |
| 82 | + | end. | |
| 83 | + | ``` | |
| 84 | + | ||
| 85 | + | Go to back [[Package List|Packages List]] | |
| 86 | + | ||
| 87 | + | --- | |
| 88 | + | *From the Free Pascal wiki, page "fcl-passrc", as it stood on 2020-02-14 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 89 | + | Category: Packages |
