File: TokenUtils.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (882 lines) | stat: -rw-r--r-- 23,857 bytes parent folder | download | duplicates (2)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
unit TokenUtils;

{ AFS 2 Jan
  procedureal code that works on the parse tree
  not put on the class as it's most fairly specific stuff
    (but has been put here because 2 or more processes use it )
  and needs to know both classes - TParseTreeNode and TSoruceTOken
  }

{(*}
(*------------------------------------------------------------------------------
 Delphi Code formatter source code 

The Original Code is TokenUtils, released May 2003.
The Initial Developer of the Original Code is Anthony Steele. 
Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
All Rights Reserved. 
Contributor(s): Anthony Steele. 

The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"). you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/NPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied.
See the License for the specific language governing rights and limitations 
under the License.

Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL") 
See http://www.gnu.org/licenses/gpl.html
------------------------------------------------------------------------------*)
{*)}

{$I JcfGlobal.inc}

interface

uses ParseTreeNode, SourceToken, SettingsTypes;

{ make a new return token }
function NewReturn: TSourceToken;
function NewSpace(const piLength: integer): TSourceToken;

procedure InsertTokenAfter(const pt, ptNew: TSourceToken);
procedure InsertTokenBefore(const pt, ptNew: TSourceToken);

function InsertReturnAfter(const pt: TSourceToken): TSourceToken;
function InsertSpacesBefore(const pt: TSourceToken;
  const piSpaces: integer): TSourceToken;

{ effectively remove the token by making it empty }
procedure BlankToken(const pt: TSourceToken);

{ return the name of the procedure around any parse tree node or source token
  empty string if there is none }
function GetProcedureName(const pcNode: TParseTreeNode; const pbFullName: boolean;
  const pbTopmost: boolean): string;


{ depending on context, one of Procedure, function, constructor, destructor }
function GetBlockType(const pcNode: TParseTreeNode): string;


function ExtractNameFromFunctionHeading(const pcNode: TParseTreeNode;
  const pbFullName: boolean): string;

function IsClassFunctionOrProperty(const pt: TSourceToken): boolean;

function RHSExprEquals(const pt: TSourceToken): boolean;

function RHSTypeEquals(const pt: TSourceToken): boolean;


function RoundBracketLevel(const pt: TSourceToken): integer;
function SquareBracketLevel(const pt: TSourceToken): integer;
function AllBracketLevel(const pt: TSourceToken): integer;
function BlockLevel(const pt: TSourceToken): integer;
function CaseLevel(const pt: TSourceToken): integer;

function InRoundBrackets(const pt: TSourceToken): boolean;

function SemicolonNext(const pt: TSourceToken): boolean;


{ true if the token is in code, ie in procedure/fn body,
  init section, finalization section, etc

  False if it is vars, consts, types etc
  or in asm }
function IsClassHelperWords(const pt: TSourceToken): boolean;

function InStatements(const pt: TSourceToken): boolean;
function InProcedureDeclarations(const pt: TsourceToken): boolean;
function InDeclarations(const pt: TsourceToken): boolean;

function IsCaseColon(const pt: TSourceToken): boolean;
function IsLabelColon(const pt: TSourceToken): boolean;

function IsFirstSolidTokenOnLine(const pt: TSourceToken): boolean;

function IsUnaryOperator(const pt: TSourceToken): boolean;

function InFormalParams(const pt: TSourceToken): boolean;

function IsActualParamOpenBracket(const pt: TSourceToken): boolean;
function IsFormalParamOpenBracket(const pt: TSourceToken): boolean;

function IsMultiLineComment(const pcToken: TSourceToken): boolean;
function IsSingleLineComment(const pcToken: TSourceToken): boolean;

function IsBlankLineEnd(const pcToken: TSourceToken): boolean;

function VarIdentCount(const pcNode: TParseTreeNode): integer;
function IdentListNameCount(const pcNode: TParseTreeNode): integer;

function ProcedureHasBody(const pt: TParseTreeNode): boolean;

function FirstSolidChild(const pt: TParseTreeNode): TParseTreeNode;

function InFilesUses(const pt: TParseTreeNode): boolean;


function Root(const pt: TParseTreeNode): TParseTreeNode;

function JCFUnitName(const pt: TParseTreeNode): string;

{ identifying identifiers is tricky
since delphi is more lenient about
what is allowed as an identifier for a variable or proc name  -
 directive names can be reused
 and in expressions involving OLE automation servers,
 all sorts of reserved words like "begin" and "end" can be used.
 Values are

 idStrict: no reserved words at all allowed.
  Used for unit names, type names, class names, etc
 idAllowDirectives: some directives can be reused.
  Used for procedure names, constant or variable names etc
 idAny: any textual token is valid
  used in expressions, which might involve a late-binding COM server with property names like "begin" and "end"
 }

type
  TIdentifierStrictness = (idStrict, idAllowDirectives, idAny);

{ use to build a parse tree}
function IsIdentifierToken(const pt: TSourceToken; const peStrictness: TIdentifierStrictness): boolean;
{ use on a built parse tree }
function IsIdentifier(const pt: TSourceToken; const peStrictness: TIdentifierStrictness): boolean;

function IsSymbolOperator(const pt: TSourceToken): boolean;

function IsClassDirective(const pt: TSourceToken): boolean;
function IsDfmIncludeDirective(const pt: TSourceToken): boolean;
function IsGenericResIncludeDirective(const pt: TSourceToken): boolean;
function IsHintDirective(const pt: TSourceToken): boolean;
function IsPropertyDirective(const pt: TSourceToken): boolean;
function IsDirectiveInContext(const pt: TSourceToken): Boolean;

function StartsLiteralString(const pt: TSourceToken): boolean;

function NextToWhiteSpace(const pt: TSourceToken): boolean;


function CompilerDirectiveLineBreak(const pt: TSourceToken; const pbBefore: Boolean): TTriOptionStyle;

function IsInsideAsm(const pt: TSourceToken): boolean;
function HasAsmCaps(const pt: TSourceToken): boolean;


implementation

uses
  { delphi }
  {$IFNDEF FPC}Windows,{$ENDIF} SysUtils, 
  { local }
  JcfSettings,
  JcfStringUtils,
  ParseTreeNodeType, Tokens, Nesting,
  AsmKeywords;


function NewReturn: TSourceToken;
begin
  Result := TSourceToken.Create;
  Result.TokenType := ttReturn;
  Result.SourceCode := NativeLineBreak;
end;

function NewSpace(const piLength: integer): TSourceToken;
begin
  Assert(piLength > 0, 'Bad space length of' + IntToStr(piLength));

  Result := TSourceToken.Create;
  Result.TokenType := ttWhiteSpace;
  Result.SourceCode := StringOfChar(' ', piLength);
end;

procedure InsertTokenAfter(const pt, ptNew: TSourceToken);
begin
  Assert(pt <> nil);
  Assert(pt.Parent <> nil);
  Assert(ptNew <> nil);

  ptNew.FileName := pt.FileName;
  pt.Parent.InsertChild(pt.IndexOfSelf + 1, ptNew);
end;

procedure InsertTokenBefore(const pt, ptNew: TSourceToken);
begin
  Assert(pt <> nil);
  Assert(pt.Parent <> nil);
  Assert(ptNew <> nil);

  ptNew.FileName := pt.FileName;
  pt.Parent.InsertChild(pt.IndexOfSelf, ptNew);
end;

function InsertReturnAfter(const pt: TSourceToken): TSourceToken;
begin
  Assert(pt <> nil);
  Assert(pt.Parent <> nil);

  Result := NewReturn;

  InsertTokenAfter(pt, Result);
end;

function InsertSpacesBefore(const pt: TSourceToken;
  const piSpaces: integer): TSourceToken;
begin
  Assert(pt <> nil);
  Assert(pt.Parent <> nil);

  Result := NewSpace(piSpaces);
  Result.FileName := pt.FileName;
  pt.Parent.InsertChild(pt.IndexOfSelf, Result);
end;

procedure BlankToken(const pt: TSourceToken);
begin
  pt.TokenType  := ttWhiteSpace;
  pt.SourceCode := '';
end;

{ given a function header parse tree node, extract the fn name underneath it }
function ExtractNameFromFunctionHeading(const pcNode: TParseTreeNode;
  const pbFullName: boolean): string;
var
  liLoop:      integer;
  lcChildNode: TParseTreeNode;
  lcSourceToken: TSourceToken;
  lcNameToken: TSourceToken;
  lcPriorToken1, lcPriorToken2: TSourceToken;
begin
  lcNameToken := nil;

  { function heading is of one of these forms
      function foo(param: integer): integer;
      function foo: integer;
      function TBar.foo(param: integer): integer;
      function TBar.foo: integer;

    within the fn heading, the name will be last identifier before nFormalParams or ':'

  }
  for liLoop := 0 to pcNode.ChildNodeCount - 1 do
  begin
    lcChildNode := pcNode.ChildNodes[liLoop];

    if lcChildNode.NodeType = nFormalParams then
      break;

    if lcChildNode is TSourceToken then
    begin
      lcSourceToken := TSourceToken(lcChildNode);

      { keep the name of the last identifier }
      if lcSourceToken.TokenType in IdentiferTokens then
        lcNameToken := lcSourceToken
      else if lcSourceToken.TokenType = ttColon then
        break;
    end
    else if (lcChildNode.NodeType = nIdentifier) then
    begin
      lcNameToken := TSourceToken(lcChildNode.LastLeaf);
    end;
  end;

  if lcNameToken = nil then
    Result := ''
  else if pbFullName then
  begin
    Result := lcNameToken.SourceCode;

    // is it a qualified name
    lcPriorToken1 := lcNameToken.PriorSolidToken;
    if (lcPriorToken1 <> nil) and (lcPriorToken1.TokenType = ttDot) then
    begin
      lcPriorToken2 := lcPriorToken1.PriorSolidToken;
      if (lcPriorToken2 <> nil) and (lcPriorToken2.TokenType in IdentiferTokens) then
      begin
        Result := lcPriorToken2.SourceCode + lcPriorToken1.SourceCode +
          lcNameToken.SourceCode;
      end;
    end;
  end
  else
  begin
    // just the proc name, no prefix
    Result := lcNameToken.SourceCode;
  end;
end;

function GetProcedureName(const pcNode: TParseTreeNode; const pbFullName: boolean;
  const pbTopmost: boolean): string;
var
  lcFunction, lcTemp, lcHeading: TParseTreeNode;
begin
  Assert(pcNode <> nil);

  lcFunction := pcNode.GetParentNode(ProcedureNodes);

  if lcFunction = nil then
  begin
    // not in a function, procedure or method
    Result := '';
    exit;
  end;

  if pbTopmost then
  begin
    { find the top level function }
    lcTemp := lcFunction.GetParentNode(ProcedureNodes);
    while lcTemp <> nil do
    begin
      lcFunction := lcTemp;
      lcTemp     := lcFunction.GetParentNode(ProcedureNodes);
    end;
  end;

  lcHeading := lcFunction.GetImmediateChild(ProcedureHeadings);

  Result := ExtractNameFromFunctionHeading(lcHeading, pbFullName)
end;

function GetBlockType(const pcNode: TParseTreeNode): string;
var
  lcFunction: TParseTreeNode;
begin
  lcFunction := pcNode.GetParentNode(ProcedureNodes + [nInitSection]);

  if lcFunction = nil then
  begin
    Result := '';
    exit;
  end;

  case lcFunction.NodeType of
    nProcedureDecl:
      Result := 'procedure';
    nFunctionDecl:
      Result := 'function';
    nConstructorDecl:
      Result := 'constructor';
    nDestructorDecl:
      Result := 'destructor';
    nInitSection:
      Result := 'initialization section';
    else
      Result := '';
  end;
end;

function IsClassFunctionOrProperty(const pt: TSourceToken): boolean;
begin
  Result := pt.IsOnRightOf(ProcedureHeadings + [nProperty], [ttClass]);
end;


function RHSExprEquals(const pt: TSourceToken): boolean;
begin
  Result := pt.IsOnRightOf(nExpression, ttEquals);
end;

function RHSTypeEquals(const pt: TSourceToken): boolean;
begin
  Result := pt.IsOnRightOf(nType, ttEquals);
end;


function RoundBracketLevel(const pt: TSourceToken): integer;
begin
  if pt = nil then
    Result := 0
  else
    Result := pt.Nestings.GetLevel(nlRoundBracket);
end;

function InRoundBrackets(const pt: TSourceToken): boolean;
begin
  if pt = nil then
    Result := False
  else
    Result := (pt.Nestings.GetLevel(nlRoundBracket) > 0);
end;


function SquareBracketLevel(const pt: TSourceToken): integer;
begin
  if pt = nil then
    Result := 0
  else
    Result := pt.Nestings.GetLevel(nlSquareBracket);
end;

function AllBracketLevel(const pt: TSourceToken): integer;
begin
  Result := RoundBracketLevel(pt) + SquareBracketLevel(pt);
end;

function BlockLevel(const pt: TSourceToken): integer;
begin
  if pt = nil then
    Result := 0
  else
    Result := pt.Nestings.GetLevel(nlBlock);
end;

function CaseLevel(const pt: TSourceToken): integer;
begin
  if pt = nil then
    Result := 0
  else
    Result := pt.Nestings.GetLevel(nlCaseSelector);
end;


function SemicolonNext(const pt: TSourceToken): boolean;
var
  lcNext: TSourceToken;
begin
  Result := False;

  if pt <> nil then
  begin
    lcNext := pt.NextSolidToken;
    if lcNext <> nil then
      Result := (lcNext.TokenType = ttSemiColon);
  end;
end;

function InStatements(const pt: TSourceToken): boolean;
begin
  Result := pt.HasParentNode(nStatementList) or pt.HasParentNode(nBlock);
  Result := Result and ( not pt.HasParentNode(nAsm));
end;

function IsCLassHelperWords(const pt: TSourceToken): boolean;
var
  lcNext: TSourceToken;
begin
  Result := (pt.TokenType in [ttClass, ttHelper, ttFor]);

  if Result then
  begin
    { must be just under a class def }
    Result := pt.HasParentNode(nClassType, 1);

    { class not followed by "helper" is not a class helper word }
    if Result and (pt.TokenType = ttClass) then
    begin
      lcNext := pt.NextSolidToken;
      if (lcNext <> nil) and (lcNext.TokenType <> ttHelper) then
        Result := False;
    end;
  end;
end;

function InProcedureDeclarations(const pt: TsourceToken): boolean;
begin
  if pt.HasParentNode(ProcedureNodes) then
  begin
    Result := (pt.HasParentNode(InProcedureDeclSections));
       //or pt.HasParentNode(ProcedureNodes, 1);
  end
  else
    Result := False;
end;

function InDeclarations(const pt: TsourceToken): boolean;
begin
  Result := ( not InStatements(pt)) and ( not pt.HasParentNode(nAsm)) and
    pt.HasParentNode(nDeclSection);
end;

function IsLabelColon(const pt: TSourceToken): boolean;
begin
  Result := (pt.TokenType = ttColon) and pt.HasParentNode(nStatementLabel, 1);
end;


function IsCaseColon(const pt: TSourceToken): boolean;
begin
  Result := (pt.TokenType = ttColon) and pt.HasParentNode(nCaseLabels, 1);
end;

function IsFirstSolidTokenOnLine(const pt: TSourceToken): boolean;
begin
  Result := pt.IsSolid and (pt.SolidTokenOnLineIndex = 0);
end;

function IsUnaryOperator(const pt: TSourceToken): boolean;
begin
  Result := (pt <> nil) and (pt.TokenType in PossiblyUnarySymbolOperators);
  if not Result then
    exit;

  { now must find if there is another token before it,
    ie true for the minus sign in '-2' but false for '2-2' }

  Result := pt.HasParentNode(nUnaryOp, 1);
end;

function InFormalParams(const pt: TSourceToken): boolean;
begin
  Result := (RoundBracketLevel(pt) = 1) and pt.HasParentNode(nFormalParams);
end;

function IsActualParamOpenBracket(const pt: TSourceToken): boolean;
begin
  Result := (pt.TokenType = ttOpenBracket) and (pt.HasParentNode(nActualParams, 1));
end;

function IsFormalParamOpenBracket(const pt: TSourceToken): boolean;
begin
  Result := (pt.TokenType = ttOpenBracket) and (pt.HasParentNode(nFormalParams, 1));
end;


function IsMultiLineComment(const pcToken: TSourceToken): boolean;
begin
  Result := False;

  if pcToken.TokenType <> ttComment then
    exit;

  // double-slash coments are never multiline
  if (pcToken.CommentStyle = eDoubleSlash) then
    exit;

  // otherwise, if it contains a return it's not single line 
  if (Pos(NativeLineBreak, pcToken.SourceCode) <= 0) then
    exit;

  Result := True;
end;

function IsSingleLineComment(const pcToken: TSourceToken): boolean;
begin
  if pcToken.TokenType <> ttComment then
    Result := False
  else
    Result := not IsMultiLineComment(pcToken);
end;

function IsBlankLineEnd(const pcToken: TSourceToken): boolean;
var
  lcPrev: TSourceToken;
begin
  Result := False;
  if (pcToken <> nil) and (pcToken.TokenType = ttReturn) then
  begin
    lcPrev := pcToken.PriorToken;
    Result := (lcPrev <> nil) and (lcPrev.TokenType = ttReturn);
  end;
end;

{ count the number of identifiers in the var decl
  e.g. "var i,j,k,l: integer" has 4 vars
}
function VarIdentCount(const pcNode: TParseTreeNode): integer;
var
  lcIdents: TParseTreeNode;
begin
  Result := 0;
  if pcNode.NodeType <> nVarDecl then
    exit;

  { the ident list is an immediate child of the var node }
  lcIdents := pcNode.GetImmediateChild(nIdentList);
  Assert(lcIdents <> nil);

  Result := IdentListNameCount(lcIdents);
end;

function IdentListNameCount(const pcNode: TParseTreeNode): integer;
var
  liLoop:     integer;
  lcLeafItem: TParseTreeNode;
begin
  Result := 0;
  if pcNode.NodeType <> nIdentList then
    exit;

  {and under it we find words (names), commas and assorted white space
   count the names }
  for liLoop := 0 to pcNode.ChildNodeCount - 1 do
  begin
    lcLeafItem := pcNode.ChildNodes[liLoop];
    if (lcLeafItem is TSourceToken) and
      (TSourceToken(lcLeafItem).TokenType = ttWord) then
      Inc(Result);
  end;
end;

function ProcedureHasBody(const pt: TParseTreeNode): boolean;
var
  lcProcedureNode: TParseTreeNode;
begin
  Result := False;
  if pt = nil then
    exit;

  lcProcedureNode := pt.GetParentNode(ProcedureNodes);
  if lcProcedureNode = nil then
    exit;

  Result := lcProcedureNode.HasChildNode(nBlock, 1);
end;

function IsDfmIncludeDirective(const pt: TSourceToken): boolean;
begin
  // form dfm comment
  Result := (pt.TokenType = ttComment) and AnsiSameText(pt.SourceCode, '{$R *.dfm}') and
    pt.HasParentNode(nImplementationSection, 4);
end;

function IsGenericResIncludeDirective(const pt: TSourceToken): boolean;
begin
  // form dfm comment
  Result := (pt.TokenType = ttComment) and AnsiSameText(pt.SourceCode, '{$R *.res}');
end;


{ get the first child node that is not a space leaf}
function FirstSolidChild(const pt: TParseTreeNode): TParseTreeNode;
var
  liLoop:  integer;
  lcChild: TParseTreeNode;
begin
  Result := nil;
  for liLoop := 0 to pt.ChildNodeCount - 1 do
  begin
    lcChild := pt.ChildNodes[liLoop];

    if (lcChild is TSourceToken) then
    begin
      if TSourceToken(lcChild).IsSolid then
      begin
        Result := lcChild;
        break;
      end;
    end
    else
    begin
      Result := lcChild;
      break;
    end;
  end;
end;

{ these uses clauses can specify file names for the units }
function InFilesUses(const pt: TParseTreeNode): boolean;
begin
  Assert(pt <> nil);
  Result := pt.HasParentNode(nUses) and
    pt.HasParentNode([nProgram, nPackage, nLibrary]);
end;

function Root(const pt: TParseTreeNode): TParseTreeNode;
begin
  Result := pt;
  while (Result <> nil) and (Result.Parent <> nil) do
    Result := Result.Parent;
end;

function JCFUnitName(const pt: TParseTreeNode): string;
var
  lcRoot: TParseTreeNode;
  lcUnitHeader: TParseTreeNode;
  lcName: TSourceToken;
begin
  Result := '';

  { go to the top }
  lcRoot := Root(pt);
  if lcRoot = nil then
    exit;

  { find the unit header }
  lcUnitHeader := lcRoot.GetImmediateChild(nUnitHeader);
  if lcUnitHeader = nil then
    exit;

  { tokens therein are of the form 'program foo' or 'unit bar' }
  lcName := TSourceToken(lcUnitHeader.FirstLeaf);
  lcName := lcName.NextSolidToken;
  if lcName = nil then
    exit;

  if (lcName.TokenType = ttWord) then
    Result := lcName.SourceCode;
end;

function IsIdentifierToken(const pt: TSourceToken; const peStrictness: TIdentifierStrictness): boolean;
const
  DIRECTIVE_IDENTIFIER_NAMES = [ttOut, ttOperator, ttHelper, ttSealed, ttStatic, ttUnsafe, ttAbstract];
begin
  if pt = nil then
  begin
    Result := False;
    exit;
  end;

  if pt.WordType in IdentifierTypes then
  begin
    Result := True;
    exit;
  end;

  case peStrictness of
    idStrict:
      // accept only identifier names
      Result := False;
    idAllowDirectives:
      // also accept directives
      Result := (pt.TokenType in DIRECTIVE_IDENTIFIER_NAMES);
    idAny:
      // accept any textual token
      Result := StrIsAlpha(pt.SourceCode);
    else
    begin
      Result := False;
      Assert(false);
    end;
  end;

end;

function IsIdentifier(const pt: TSourceToken; const peStrictness: TIdentifierStrictness): boolean;
begin
  Result := IsIdentifierToken(pt, peStrictness);

  if Result then
    Result := pt.HasParentNode(nIdentifier, 1);
end;

function IsSymbolOperator(const pt: TSourceToken): boolean;
begin
  Result := (pt.TokenType in Operators) and (not StrIsAlpha(pt.SourceCode));
end;

function IsClassDirective(const pt: TSourceToken): boolean;
begin
  { property Public: Boolean;
    function Protected: Boolean
    are both legal so have to check that we're not in a property or function def. }

  Result := (pt.TokenType in ClassDirectives) and
    pt.HasParentNode(nClassVisibility, 1) and
    ( not (pt.HasParentNode(ProcedureNodes + [nProperty])));

end;


function IsHintDirective(const pt: TSourceToken): boolean;
begin
  Result := (pt <> nil) and (pt.TokenType in HintDirectives);

  if Result then
    Result := pt.HasParentNode(nHintDirectives, 1);
end;

function IsPropertyDirective(const pt: TSourceToken): boolean;
begin
  Result := (pt <> nil) and (pt.TokenType in PropertyDirectives);

  if Result then
    Result := (pt.HasParentNode(nPropertySpecifier, 1));
end;

{ directives can occur in other contexts - they are valid proc & variable names
  so we need to know if this one was parsed as a directive }
function IsDirectiveInContext(const pt: TSourceToken): Boolean;
begin
  Result := pt.HasParentNode(DirectiveNodes) or
    IsPropertyDirective(pt) or IsClassDirective(pt) or IsHintDirective(pt);
end;

function StartsLiteralString(const pt: TSourceToken): boolean;

  function InLiteralString(const pt: TSourceToken): boolean;
  begin
    Result := (pt <> nil) and pt.HasParentNode(nLiteralString, 2);
  end;

begin
  Result := (pt.TokenType in LiteralStringStarters) and InLiteralString(pt);
  if Result then
  begin
    // is this actually the first solid token in the literal string?
    Result := not InLiteralString(pt.PriorToken);
  end;
end;

{ true if this token is preceeded or followed by a non-empty whitespace }
function NextToWhiteSpace(const pt: TSourceToken): Boolean;
var
  lcPrior, lcNext: TSourceToken;
begin
  if pt = nil then
  begin
    Result := False;
  end
  else
  begin
    lcPrior := pt.PriorToken;
    Result := (lcPrior <> nil) and (lcPrior.TokenType = ttWhiteSpace) and (lcPrior.SourceCode <> '');

    if not Result then
    begin
      lcNext := pt.NextToken;
      Result := (lcNext <> nil) and (lcNext.TokenType = ttWhiteSpace) and (lcNext.SourceCode <> '');
    end;
  end;
end;

function CompilerDirectiveLineBreak(const pt: TSourceToken; const pbBefore: Boolean): TTriOptionStyle;
begin
  if InStatements(pt) then
  begin
    if pbBefore then
      Result := FormattingSettings.Returns.BeforeCompilerDirectStatements
    else
      Result := FormattingSettings.Returns.AfterCompilerDirectStatements;
  end
  else if pt.HasParentNode(nUses)  then
  begin
    if pbBefore then
      Result := FormattingSettings.Returns.BeforeCompilerDirectUses
    else
      Result := FormattingSettings.Returns.AfterCompilerDirectUses;
  end
  else
  begin
    if pbBefore then
      Result := FormattingSettings.Returns.BeforeCompilerDirectGeneral
    else
      Result := FormattingSettings.Returns.AfterCompilerDirectGeneral;
  end;
end;

function IsInsideAsm(const pt: TSourceToken): boolean;
begin
   Result := pt.HasParentNode(nAsm) and not (pt.TokenType in [ttAsm, ttEnd]);
end;

function HasAsmCaps(const pt: TSourceToken): boolean;
begin
  if pt.TokenType = ttComment then
  begin
    Result := False;
  end
  else
  begin
    Result := pt.HasParentNode(nAsmOpcode, 2) or IsAsmParamKeyword(pt.SourceCode);
  end;
end;

end.