PageRenderTime 19ms CodeModel.GetById 16ms app.highlight 1ms RepoModel.GetById 0ms app.codeStats 1ms

/thirdparty/PascalPreProcessor/ppp.dpr

https://github.com/the-Arioch/jcl
Pascal | 230 lines | 172 code | 22 blank | 36 comment | 6 complexity | 167c3e8529834331cd3502986bd7c7c5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1{ **************************************************************************** }
  2{                                                                              }
  3{    Pascal PreProcessor                                                       }
  4{    Copyright (c) 2001 Barry Kelly.                                           }
  5{    barry_j_kelly@hotmail.com                                                 }
  6{                                                                              }
  7{    The contents of this file are subject to the Mozilla Public License       }
  8{    Version 1.1 (the "License"); you may not use this file except in          }
  9{    compliance with the License. You may obtain a copy of the License at      }
 10{    http://www.mozilla.org/MPL/                                               }
 11{                                                                              }
 12{    Software distributed under the License is distributed on an "AS IS"       }
 13{    basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the   }
 14{    License for the specific language governing rights and limitations        }
 15{    under the License.                                                        }
 16{                                                                              }
 17{    The Original Code is ppp.dpr                                              }
 18{                                                                              }
 19{    The Initial Developer of the Original Code is Barry Kelly.                }
 20{    Portions created by Barry Kelly are Copyright (C) 2001                    }
 21{    Barry Kelly. All Rights Reserved.                                         }
 22{                                                                              }
 23{    Alternatively, the contents of this file may be used under the terms      }
 24{    of the Lesser GNU Public License (the  "LGPL License"), in which case     }
 25{    the provisions of LGPL License are applicable instead of those            }
 26{    above.  If you wish to allow use of your version of this file only        }
 27{    under the terms of the LPGL License and not to allow others to use        }
 28{    your version of this file under the MPL, indicate your decision by        }
 29{    deleting  the provisions above and replace  them with the notice and      }
 30{    other provisions required by the LGPL License.  If you do not delete      }
 31{    the provisions above, a recipient may use your version of this file       }
 32{    under either the MPL or the LPGL License.                                 }
 33{                                                                              }
 34{ **************************************************************************** }
 35{ $Id$ }
 36
 37{$APPTYPE CONSOLE}
 38program ppp;
 39
 40uses
 41  Windows,
 42  SysUtils,
 43  Classes,
 44  TypInfo,
 45  PppState in 'PppState.pas',
 46  PppParser in 'PppParser.pas',
 47  FindFileIter in 'FindFileIter.pas',
 48  PppLexer in 'PppLexer.pas',
 49  PCharUtils in 'PCharUtils.pas';
 50
 51const
 52  ProcessedExtension = '.pi';
 53
 54procedure Syntax;
 55begin
 56  Writeln(
 57    'Pascal PreProcessor'#10,
 58    'Copyright (C) 2001 Barry Kelly'#10,
 59    #10,
 60    'Syntax:'#10,
 61    '  ' + ParamStr(0) + ' [options] <input files>...'#10,
 62    #10,
 63    'Options:'#10,
 64    '  -h, -?   This help'#10,
 65    '  -i       Process includes'#10,
 66    '  -c       Process conditional directives'#10,
 67    '  -C       Strip comments'#10,
 68    '  -pxxx    Add xxx to include path'#10,
 69    '  -dxxx    Define xxx as a preprocessor conditional symbol'#10,
 70    #10,
 71    'Preprocessed files will be written to new files with extension ',
 72        ProcessedExtension, #10,
 73    'If you have any suggestions or bug-reports, contact me at'#10,
 74    'barry_j_kelly@hotmail.com'
 75  );
 76  Halt(2);
 77end;
 78
 79procedure Process(AState: TPppState; const AOld, ANew: string);
 80var
 81  parse: TPppParser;
 82  fsIn, fsOut: TStream;
 83  answer: string;
 84begin
 85  fsOut := nil;
 86  parse := nil;
 87  fsIn := nil;
 88  AState.PushState;
 89  try
 90    fsIn := TFileStream.Create(AOld, fmOpenRead);
 91    parse := TPppParser.Create(fsIn, AState);
 92    answer := parse.Parse;
 93    fsOut := TFileStream.Create(ANew, fmCreate);
 94    fsOut.WriteBuffer(Pointer(answer)^, Length(answer));
 95  finally
 96    AState.PopState;
 97    fsOut.Free;
 98    parse.Free;
 99    fsIn.Free;
100  end;
101end;
102
103procedure Params(ACommandLine: PChar);
104var
105  pppState: TSimplePppState;
106
107  function HandleOptions(cp: PChar): PChar;
108
109    function CheckOpt(cp: PChar; AOpt: TPppOption): PChar;
110    begin
111      case cp^ of
112        '+':
113          pppState.Options := pppState.Options + [AOpt];
114        '-':
115          pppState.Options := pppState.Options - [AOpt];
116      else
117        pppState.Options := pppState.Options + [AOpt];
118      end;
119      if cp^ in ['+', '-'] then
120        Result := cp + 1
121      else
122        Result := cp;
123    end;
124
125  var
126    tmp: string;
127  begin
128    cp := SkipWhite(cp);
129
130    while cp^ = '-' do
131    begin
132      Inc(cp);
133
134      case cp^ of
135        'h', 'H', '?':
136          Syntax;
137
138        'i', 'I':
139          cp := CheckOpt(cp + 1, poProcessIncludes);
140
141        'c':
142          cp := CheckOpt(cp + 1, poProcessDefines);
143
144        'C':
145          cp := CheckOpt(cp + 1, poStripComments);
146
147        'p', 'P':
148        begin
149          Inc(cp);
150          cp := ReadStringDoubleQuotedMaybe(cp, tmp);
151          pppState.SearchPath.Add(tmp);
152        end;
153
154        'd':
155        begin
156          Inc(cp);
157          cp := ReadIdent(cp, tmp);
158          pppState.Define(tmp);
159        end;
160
161      else
162        Syntax;
163      end;
164
165      cp := SkipWhite(cp);
166    end;
167    Result := cp;
168  end;
169
170  function HandleFiles(cp: PChar): PChar;
171  var
172    tmp: string;
173    iter: IFindFileIterator;
174  begin
175    while not (cp^ in ['-', #0]) do
176    begin
177      cp := SkipWhite(ReadStringDoubleQuotedMaybe(cp, tmp));
178      if CreateFindFile(tmp, faAnyFile and not faDirectory, iter) then
179        repeat
180          try
181            Process(pppState, iter.Name, ChangeFileExt(iter.Name, ProcessedExtension));
182          except
183            on e: Exception do
184              Writeln('Error: ', iter.Name, e.Message);
185          end;
186        until not iter.Next
187      else
188        Writeln('Could not find ', tmp);
189    end;
190    Result := cp;
191  end;
192
193var
194  cp: PChar;
195begin
196  cp := ACommandLine;
197
198  pppState := TSimplePppState.Create;
199  try
200    repeat
201      cp := HandleOptions(cp);
202      cp := HandleFiles(cp);
203    until cp^ = #0;
204  finally
205    pppState.Free;
206  end;
207end;
208
209var
210  CommandLine: string;
211  i: Integer;
212begin
213  try
214    i := 1;
215    if ParamCount = 0 then
216      Syntax
217    else
218    begin
219      while i <= ParamCount do
220      begin
221        CommandLine := CommandLine + ' ' + ParamStr(i);
222        Inc(i);
223      end;
224      Params(PChar(CommandLine));
225    end;
226  except
227    on e: Exception do
228      Writeln(e.Message);
229  end;
230end.