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
|
{ -*- compile-command: "make -C ../../ build-pascal_pre_proc" -*- }
{
Copyright 1998-2018 PasDoc developers.
This file is part of "PasDoc".
"PasDoc" is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"PasDoc" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with "PasDoc"; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
----------------------------------------------------------------------------
}
{ A Pascal preprocessor.
See [https://github.com/pasdoc/pasdoc/wiki/OtherTools] for
documentation.
TODO: should be made configurable to preserve some directives.
Conditional compilation, macros, includes are handled fully
by this preprocessor, so they should be stripped, but some
directives should be usually left in the source for the actual compiler.
Things like $mode, or $H+.
}
program pascal_pre_proc;
uses SysUtils, Classes, PasDoc_OptionParser, PasDoc_StringVector, PasDoc_Base,
PasDoc_Tokenizer, PasDoc_Scanner, PasDoc_Types, PasDoc_Versions;
{ TPascalPreProcessor -------------------------------------------------------- }
type
TPascalPreProcessor = class
private
procedure MessageEvent(const MessageType: TPasDocMessageType;
const AMessage: string; const AVerbosity: Cardinal);
public
Verbosity: Cardinal;
HandleMacros: boolean;
Directives: TStringVector;
IncludeDirectories: TStringVector;
constructor Create;
destructor Destroy; override;
procedure Process(const InputFileName: string);
end;
constructor TPascalPreProcessor.Create;
begin
inherited;
Directives := TStringVector.Create;
IncludeDirectories := TStringVector.Create;
end;
destructor TPascalPreProcessor.Destroy;
begin
FreeAndNil(Directives);
FreeAndNil(IncludeDirectories);
inherited;
end;
procedure TPascalPreProcessor.MessageEvent(
const MessageType: TPasDocMessageType;
const AMessage: string; const AVerbosity: Cardinal);
begin
if (AVerbosity <= Verbosity) then
case MessageType of
pmtInformation: WriteLn(ErrOutput, 'Info[', AVerbosity, ']: ', AMessage);
pmtWarning: WriteLn(ErrOutput, 'Warning[', AVerbosity, ']: ', AMessage);
pmtError: WriteLn(ErrOutput, 'Error[', AVerbosity, ']: ', AMessage);
else
WriteLn(ErrOutput, AMessage);
end;
end;
procedure TPascalPreProcessor.Process(const InputFileName: string);
var
Scanner: TScanner;
InputStream: TStream;
T: TToken;
begin
InputStream := TFileStream.Create(InputFileName, fmOpenRead);
{ We do not free InputStream anywhere in this procedure,
Scanner will take care of this. }
Scanner := TScanner.Create(InputStream,
{$ifdef FPC_OBJFPC}@{$endif} MessageEvent ,
Verbosity, InputFileName, ExtractFilePath(InputFileName),
HandleMacros);
try
Scanner.IncludeFilePaths := IncludeDirectories;
Scanner.AddSymbols(Directives);
try
repeat
T := Scanner.GetToken;
Write(T.Data);
until false;
except
on ETokenizerStreamEnd do
{ silence exception, as this is the normal end };
end;
finally FreeAndNil(Scanner) end;
end;
{ TPascalPreProcessorOptions ------------------------------------------------- }
type
TPascalPreProcessorOptions = class(TOptionParser)
OptionVerbosity: TIntegerOption;
OptionDefine: TStringOptionList;
OptionHelp: TBoolOption;
OptionIncludePaths: TPathListOption;
OptionConditionalFile: TStringOptionList;
OptionNoMacro: TBoolOption;
public
constructor Create; override;
procedure InterpretCommandline(PreProcessor: TPascalPreProcessor);
end;
constructor TPascalPreProcessorOptions.Create;
begin
inherited;
OptionHelp := TBoolOption.Create('?', 'help');
OptionHelp.Explanation := 'Show this help';
AddOption(OptionHelp);
OptionVerbosity := TIntegerOption.Create('v', 'verbosity');
OptionVerbosity.Value := DEFAULT_VERBOSITY_LEVEL;
OptionVerbosity.Explanation := 'Set log verbosity (0-6) ['+IntToStr(DEFAULT_VERBOSITY_LEVEL)+']';
AddOption(OptionVerbosity);
OptionDefine := TStringOptionList.Create('D', 'define');
OptionDefine.Explanation := 'Define conditional';
AddOption(OptionDefine);
OptionConditionalFile := TStringOptionList.Create('d', 'conditionals');
OptionConditionalFile.Explanation := 'Read conditionals from this file';
AddOption(OptionConditionalFile);
OptionIncludePaths := TPathListOption.Create('I', 'include');
OptionIncludePaths.Explanation := 'Includes search path';
AddOption(OptionIncludePaths);
OptionNoMacro := TBoolOption.Create(#0, 'no-macro');
OptionNoMacro.Explanation := 'Turn FPC macro support off';
AddOption(OptionNoMacro);
end;
procedure TPascalPreProcessorOptions.InterpretCommandline(
PreProcessor: TPascalPreProcessor);
var
I: Integer;
begin
PreProcessor.Directives.Assign(OptionDefine.Values);
for I := 0 to OptionConditionalFile.Values.Count - 1 do
PreProcessor.Directives.LoadFromTextFileAdd(OptionConditionalFile.Values[I]);
PreProcessor.IncludeDirectories.Assign(OptionIncludePaths.Values);
PreProcessor.Verbosity := OptionVerbosity.Value;
PreProcessor.HandleMacros := not OptionNoMacro.TurnedOn;
end;
{ Main program --------------------------------------------------------------- }
{ If CATCH_EXCEPTIONS then exceptions will be catched and displayed
nicely using MessageEvent, which means that exceptions will be
reported like other errors of pasdoc. This is what users most
definitely want.
For debug purposes, it's sometimes useful to undefine this,
to let the default FPC exception handler do it's work and
output nice backtrace of the exception. }
{$define CATCH_EXCEPTIONS}
var
PreProcessor: TPascalPreProcessor;
InputFileName: string;
Options: TPascalPreProcessorOptions;
begin
PreProcessor := TPascalPreProcessor.Create;
try
{$ifdef CATCH_EXCEPTIONS}
try
{$endif}
Options := TPascalPreProcessorOptions.Create;
try
Options.ParseOptions;
if Options.OptionHelp.TurnedOn then
begin
Writeln('pascal_pre_proc: Pascal preprocessor.');
Writeln('Using PasDoc parser:');
Writeln(PASDOC_FULL_INFO);
Writeln;
WriteLn('Usage:');
WriteLn(' pascal_pre_proc [options] file-name');
Writeln('This will parse file-name, interpreting all compiler directives');
Writeln('(like $define, $ifdef, $include, handling also FPC macros)');
Writeln('and output the result on standard output.');
Writeln;
WriteLn('Valid options are: ');
Options.WriteExplanations;
Exit;
end;
Options.InterpretCommandline(PreProcessor);
if Options.LeftList.Count = 0 then
raise Exception.Create('You must provide a filename to parse');
if Options.LeftList.Count > 1 then
raise Exception.Create('You cannot provide more than one filename to parse');
InputFileName := Options.LeftList[0];
finally FreeAndNil(Options) end;
PreProcessor.Process(InputFileName);
{$ifdef CATCH_EXCEPTIONS}
except
on E: Exception do
begin
PreProcessor.MessageEvent(pmtError, E.Message, 1);
ExitCode := 1;
Exit;
end;
end;
{$endif CATCH_EXCEPTIONS}
finally FreeAndNil(PreProcessor) end;
end.
|