summaryrefslogtreecommitdiff
path: root/AltParser/compiler/parseropl.pas
blob: c5a63324457072f99c14711ea99f83244a938bd9 (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
{
    Copyright (c) 1998-2010 by Florian Klaempfl and Dr. Diettrich

    This unit implements the parsing of modules (unit,program,library,package).
    Handling of ppufiles and other semantics were moved somewhere else
    (SModules, SSub, SStatmnt...)
}
unit parserOPL;

{$i fpcdefs.inc}

interface

uses
  pbase,node,symdef,symconst;

type
{ In order to support multiple parsers, some general procedures are virtualized.
  The original procedures (statement,expr...) have been renamed and replaced
  by forwarders to virtual TParser methods.
}
  TParserOPL = class(TParser)
  protected
    function  ParseBlock(_isLibrary: boolean): tnode; override;
    procedure read_declarations(is_library : boolean); override;
    procedure read_interface_declarations; override;
    procedure generate_specialization_procs; override;
    function  statement: tnode; override;
    { reads a whole expression }
    function  expr(dotypecheck : boolean) : tnode; override;
    { reads an expression without assignments and .. }
    function  comp_expr(accept_equal : boolean):tnode; override;
    procedure Parse_Parameter_Dec(_pd:tabstractprocdef); override;
    function  Parse_Proc_Dec(isclassmethod:boolean; aclass:tobjectdef):tprocdef; override;
    function  parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean; override;
  public
    procedure Compile; override;
  end;

implementation

uses
  globals,globtype,cfileutl,
  fmodule,scanner,tokens,
  SModules,PStatmntOPL,
  pmodules,pexpr,psub,psubOPL,
  PDecSubOPL,
  finput,verbose,
  symbase;

{ References to external parsing procedures:
  ProcUnit
  -> pmodules.try_consume_hintdirective
    [clean]
  -> psub.read_interface_declarations
    [virtualized]
  -> psub.init_procinfo.parse_body -> psub.block(=psubOPL.ParseBlock)
    [virtualized]
  ProcPackage
  --
  ProcProgram
    [-> parse_body]
  LoadUnits
  --
}

{***************************************************************
                    From pmodules
***************************************************************}

procedure LoadUnits;
var
   s,sorg  : TIDString;
   fn      : string;
begin //LoadUnits
{If you use units, you likely need unit initializations.}
   current_module.micro_exe_allowed:=false;

   consume(_USES);
   repeat
     s:=pattern;
     sorg:=orgpattern;
     consume(_ID);
   { support "<unit> in '<file>'" construct, but not for tp7 }
     if not(m_tp7 in current_settings.modeswitches)
     and try_to_consume(_OP_IN) then
       fn:=FixFileName(get_stringconst)
     else
       fn:='';
     SUsesAdd(s,sorg,fn);
   until not try_to_consume(_COMMA);
   consume(_SEMICOLON);

   SUsesDone(); //used units are either compiled, or ppu files have been found
end;

procedure ProcUnit;
var
  sm: TSemModule;
begin //ProcUnit
  sm.SModuleInitUnit;
  consume(_UNIT);
  if token=_ID then //else error on consume(_ID)
    sm.SUnitName;
  consume(_ID);
{ parse hint directives }
  try_consume_hintdirective(current_module.moduleoptions, current_module.deprecatedmsg);
  consume(_SEMICOLON);
//enter interface section
  consume(_INTERFACE);
  sm.SUnitInterface;
  if not(cs_compilesystem in current_settings.moduleswitches)
  and (token=_USES) then begin
  { insert qualifier for the system unit (allows system.writeln) (where?) }
    LoadUnits; //parse USES clause
  { has it been compiled at a higher level ?}
    if current_module.state=ms_compiled then
      exit; //all used units are available (interface uses only!?)
  end;
{ ... parse the declarations }
  sm.SUnitIntfInit;
  Message1(parser_u_parsing_interface,current_module.realmodulename^);
  begin //put all interface declarations into the module's global STB
    symtablestack.push(current_module.globalsymtable);
    read_interface_declarations; //<--------------------- parse!
    symtablestack.pop(current_module.globalsymtable);
  end;
  if sm.SUnitIntfDone((m_mac in current_settings.modeswitches)
  and try_to_consume(_END)) then
    exit; //no implementation section present! (could consume(_POINT))
//enter implementation section
  if not current_module.interface_only then begin
    consume(_IMPLEMENTATION);
    Message1(unit_u_loading_implementation_units,current_module.modulename^);
  { Read the implementation units }
    //inlined parse_implementation_uses;
    if token=_USES then
      LoadUnits; //parse USES clause
  end;
  if sm.SUnitImplInit then
    exit; //unit doesn't deserve recompilation
//parse the implementation body
  if not current_module.interface_only then begin
    sm.SUnitBodyInit;
    sm.init_procinfo.parse_body;
  { save file pos for debuginfo }
    current_module.mainfilepos:=sm.init_procinfo.entrypos;
  end;
  sm.SUnitBodyDone;
{ finalize? }
  if sm.SUnitFinalInit((token=_FINALIZATION) and not current_module.interface_only) then
    sm.finalize_procinfo.parse_body;
  sm.SUnitFinalDone;
{ the last char should always be a point }
  consume(_POINT);
  if sm.SUnitDone then
    exit; //without message?
  Message1(unit_u_finished_compiling,current_module.modulename^); //move into SUnitDone?
end;

procedure ProcPackage;
var
  sm: TSemModule;
begin //ProcPackage
  sm.SModuleInitPackage;
  consume(_ID);  //PACKAGE
  sm.SPackageName;
  consume(_ID);
  consume(_SEMICOLON);

  sm.SPackageInterface;
{Load the units/packages used by the package we compile.}
  if token=_REQUIRES then
     begin
      //todo: this clause will produce errors, if ever entered
     end;
{Load the units that make up the package }
  if try_to_consume(_CONTAINS) then begin
    consume(_ID);
    repeat
      if token=_ID then
        AddUnit(pattern);
      consume(_ID);
    until not try_to_consume(_COMMA);
    consume(_SEMICOLON);
  end;
  sm.SPackageImplInit;
{ consume the last point }
  consume(_END);
  consume(_POINT);
  sm.SPackageDone;
end;

procedure ProcProgram(islibrary : boolean);
var
  sm: TSemModule;
begin //ProcProgram
  sm.SModuleInitProgLib(isLibrary);
  if islibrary then begin
    consume(_LIBRARY); //may fail, intentionally
    if token=_ID then
      sm.SLibName;
    consume(_ID);
    consume(_SEMICOLON);
  end else if try_to_consume(_PROGRAM) then begin
  //PROGRAM header
    if token=_ID then
      sm.SProgName;
    consume(_ID);
    if token=_LKLAMMER then begin
      consume(_LKLAMMER);
      repeat
        consume(_ID);
      until not try_to_consume(_COMMA);
      consume(_RKLAMMER);
    end;
    consume(_SEMICOLON);
  end else //headerless program
    sm.SProgNameNone;
//enter implementation body
  sm.SProgLibImplInit;
  if token=_USES then
    LoadUnits; {Load the units used by the program we compile.}
  sm.SProgLibBodyInit;
  sm.main_procinfo.parse_body; //all declarations and main procedure (BEGIN..END.)
{ save file pos for debuginfo }
  current_module.mainfilepos:=sm.main_procinfo.entrypos;
  sm.SProgLibBodyDone;
{ finalize? }
  if token=_FINALIZATION then begin
    sm.SProgLibFinalInit(True);
    sm.finalize_procinfo.parse_body;
  end else if sm.force_init_final then
    sm.SProgLibFinalInit(False);
  //else no finalization procedure at all
  sm.SPrgLibFinalDone;
{ consume the last point }
  consume(_POINT);
  sm.SPrgLibDone;
end;


{ TParserOPL }

procedure TParserOPL.Compile;
begin
WriteLn('*** using alternate parser ***');
{ read the first token }
  current_scanner.readtoken(false);

  if (token=_UNIT) or (compile_level>main_program_level) then begin
  //non main-level modules can be nothing but units
     current_module.is_unit:=true;
     ProcUnit;
  end else if (token=_ID) and (idtoken=_PACKAGE) then begin
    current_module.IsPackage:=true;
    ProcPackage;
  end else //assume program or library
    ProcProgram(token=_LIBRARY);
end;

function TParserOPL.ParseBlock(_isLibrary: boolean): tnode;
begin
  Result := psubOPL.ParseBlock(_isLibrary);
end;

procedure TParserOPL.read_declarations(is_library: boolean);
begin
  psubOPL.ReadDeclarations(is_library);
end;

procedure TParserOPL.read_interface_declarations;
begin
  psubOPL.ReadInterfaceDeclarations;
end;

procedure TParserOPL.generate_specialization_procs;
begin
  psubOPL._generate_specialization_procs;
end;

function TParserOPL.comp_expr(accept_equal: boolean): tnode;
begin
  Result:=pexpr.comp_expr(accept_equal); //unless implemented differently
end;

function TParserOPL.expr(dotypecheck: boolean): tnode;
begin
  Result:=pexpr.expr(dotypecheck); //unless implemented differently
end;

function TParserOPL.statement: tnode;
begin
  Result:=PStatmntOPL.statementOPL;
end;

procedure TParserOPL.Parse_Parameter_Dec(_pd: tabstractprocdef);
begin
  PDecSubOPL.Parse_Parameter_Dec(_pd);
end;

function TParserOPL.Parse_Proc_Dec(isclassmethod: boolean; aclass: tobjectdef
  ): tprocdef;
begin
  Result:=PDecSubOPL.Parse_Proc_Dec(isclassmethod, aclass);
end;

function TParserOPL.parse_proc_head(aclass: tobjectdef;
  potype: tproctypeoption; var pd: tprocdef): boolean;
begin
  Result:=PDecSubOPL._parse_proc_head(aclass, potype, pd);
end;


const
  OPLpas2: TParserListItem = (
    ext: '.pas'; cls: TParserOPL;
    alt:nil; next:nil;
  );
  OPLpp2: TParserListItem = (
    ext: '.pp'; cls: TParserOPL;
    alt:nil; next:nil;
  );
  OPLlpr2: TParserListItem = (
    ext: '.lpr'; cls: TParserOPL;
    alt:nil; next:nil;
  );

initialization
  RegisterParser(@OPLpas2);
  RegisterParser(@OPLpp2);
  RegisterParser(@OPLlpr2);
end.