Skip to content

Commit efe0355

Browse files
committed
Code refectoring to share documentation extraction code.
1 parent 3ea2fe5 commit efe0355

File tree

1 file changed

+96
-128
lines changed

1 file changed

+96
-128
lines changed

source/gnatdoc-comments-extractor.adb

Lines changed: 96 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ package body GNATdoc.Comments.Extractor is
211211
-- Call both Extract_General_Leading_Documentation and
212212
-- Extract_General_Trailing_Documentation subprograms.
213213

214+
procedure Extract_Upper_Intermediate_Section
215+
(Token_Start : Token_Reference;
216+
Token_End : Token_Reference;
217+
Options : GNATdoc.Comments.Options.Extractor_Options;
218+
Documentation : in out Structured_Comment'Class;
219+
Section : out Section_Access)
220+
with Pre => Kind (Data (Token_Start)) in Ada_Is | Ada_With;
221+
-- Extract documentation from the upper intermediate section: after
222+
-- Token_Start ('is' or 'with') and before any other declarations.
223+
214224
procedure Fill_Code_Snippet
215225
(Node : Ada_Node'Class;
216226
Documentation : in out Structured_Comment'Class);
@@ -646,22 +656,8 @@ package body GNATdoc.Comments.Extractor is
646656

647657
-- Upper intermediate section: after 'is' and before any declarations.
648658

649-
Intermediate_Upper_Section :=
650-
new Section'
651-
(Kind => Raw,
652-
Symbol => "<<INTERMEDIATE UPPER>>",
653-
Name => <>,
654-
Text => <>,
655-
others => <>);
656-
Documentation.Sections.Append (Intermediate_Upper_Section);
657-
658659
declare
659-
Token : Token_Reference := Base_Package_Decl_Node.Token_Start;
660-
Found : Boolean := False;
661-
Separated : Boolean := False;
662-
-- Whether comment block is separated from the list with 'is'
663-
-- keyword by empty line. In this case comment block can belong
664-
-- to the entity declaration below.
660+
Token : Token_Reference := Base_Package_Decl_Node.Token_Start;
665661

666662
begin
667663
-- Lookup 'is' in the package declaration
@@ -673,46 +669,12 @@ package body GNATdoc.Comments.Extractor is
673669
Token = No_Token or else Kind (Data (Token)) = Ada_Is;
674670
end loop;
675671

676-
loop
677-
Token := Next (Token);
678-
679-
exit when Token = No_Token;
680-
681-
case Kind (Data (Token)) is
682-
when Ada_Comment =>
683-
Found := True;
684-
Append_Documentation_Line
685-
(Intermediate_Upper_Section.Text,
686-
Text (Token),
687-
Options.Pattern);
688-
689-
when Ada_Whitespace =>
690-
declare
691-
Location : constant Source_Location_Range :=
692-
Sloc_Range (Data (Token));
693-
694-
begin
695-
if Location.End_Line - Location.Start_Line > 1 then
696-
exit when Found;
697-
698-
Found := True;
699-
Separated := True;
700-
end if;
701-
end;
702-
703-
when others =>
704-
if Separated then
705-
-- Comment block is separated from the line with 'is'
706-
-- keyword by an empty line, but not separated from the
707-
-- entity declaration below, thus don't include it into
708-
-- package documentation.
709-
710-
Intermediate_Upper_Section.Text.Clear;
711-
end if;
712-
713-
exit;
714-
end case;
715-
end loop;
672+
Extract_Upper_Intermediate_Section
673+
(Token,
674+
Base_Package_Decl_Node.Token_End,
675+
Options,
676+
Documentation,
677+
Intermediate_Upper_Section);
716678
end;
717679

718680
-- Lower intermediate section: after 'is' and before any declarations.
@@ -721,7 +683,7 @@ package body GNATdoc.Comments.Extractor is
721683
Intermediate_Lower_Section :=
722684
new Section'
723685
(Kind => Raw,
724-
Symbol => "<<INTERMEDIATE LOWER>>",
686+
Symbol => "<<INTERMEDIATE UPPER>>",
725687
Name => <>,
726688
Text => <>,
727689
others => <>);
@@ -1244,7 +1206,7 @@ package body GNATdoc.Comments.Extractor is
12441206

12451207
Leading_Section : Section_Access;
12461208
Trailing_Section : Section_Access;
1247-
Intermediate_Lower_Section : Section_Access;
1209+
Intermediate_Upper_Section : Section_Access;
12481210

12491211
begin
12501212
Extract_General_Leading_Documentation
@@ -1263,18 +1225,6 @@ package body GNATdoc.Comments.Extractor is
12631225
-- Overwise, documentation may be provided inside task definition
12641226
-- before the first entry.
12651227

1266-
-- Lower intermediate section: after 'is'/'with' and before any
1267-
-- declarations.
1268-
1269-
Intermediate_Lower_Section :=
1270-
new Section'
1271-
(Kind => Raw,
1272-
Symbol => "<<INTERMEDIATE LOWER>>",
1273-
Name => <>,
1274-
Text => <>,
1275-
others => <>);
1276-
Documentation.Sections.Append (Intermediate_Lower_Section);
1277-
12781228
-- Lookup for 'is' token that begins task definition, or 'with'
12791229
-- token that ends interface part.
12801230

@@ -1301,62 +1251,12 @@ package body GNATdoc.Comments.Extractor is
13011251
end loop;
13021252
end if;
13031253

1304-
declare
1305-
Token : Token_Reference := Is_Or_With_Token;
1306-
Found : Boolean := False;
1307-
Separated : Boolean := False;
1308-
-- Comments block starts after an empty line. it means that
1309-
-- documentation can be leading documentation for the first
1310-
-- entry.
1311-
1312-
begin
1313-
loop
1314-
Token := Next (Token);
1315-
1316-
exit when Token = No_Token or else Token = Definition.Token_End;
1317-
1318-
case Kind (Data (Token)) is
1319-
when Ada_Comment =>
1320-
Found := True;
1321-
Append_Documentation_Line
1322-
(Intermediate_Lower_Section.Text,
1323-
Text (Token),
1324-
Options.Pattern);
1325-
1326-
when Ada_Whitespace =>
1327-
declare
1328-
Location : constant Source_Location_Range :=
1329-
Sloc_Range (Data (Token));
1330-
1331-
begin
1332-
if Location.End_Line - Location.Start_Line > 1 then
1333-
exit when Found;
1334-
1335-
Found := True;
1336-
Separated := True;
1337-
end if;
1338-
end;
1339-
1340-
when Ada_End | Ada_Pragma =>
1341-
exit;
1342-
1343-
when Ada_Entry =>
1344-
if Separated then
1345-
-- Comments block is separated from the start of the
1346-
-- task declaration by the empty line, but not from
1347-
-- the entry declaration, thus, it belongs to the
1348-
-- entry declaration. Clear accumilated text.
1349-
1350-
Intermediate_Lower_Section.Text.Clear;
1351-
end if;
1352-
1353-
exit;
1354-
1355-
when others =>
1356-
raise Program_Error;
1357-
end case;
1358-
end loop;
1359-
end;
1254+
Extract_Upper_Intermediate_Section
1255+
(Is_Or_With_Token,
1256+
Definition.Token_End,
1257+
Options,
1258+
Documentation,
1259+
Intermediate_Upper_Section);
13601260
end if;
13611261

13621262
Remove_Comment_Start_And_Indentation (Documentation, Options.Pattern);
@@ -1375,10 +1275,10 @@ package body GNATdoc.Comments.Extractor is
13751275

13761276
Raw_Section := Trailing_Section;
13771277

1378-
elsif Intermediate_Lower_Section /= null
1379-
and then not Intermediate_Lower_Section.Text.Is_Empty
1278+
elsif Intermediate_Upper_Section /= null
1279+
and then not Intermediate_Upper_Section.Text.Is_Empty
13801280
then
1381-
Raw_Section := Intermediate_Lower_Section;
1281+
Raw_Section := Intermediate_Upper_Section;
13821282

13831283
elsif not Leading_Section.Text.Is_Empty then
13841284
Raw_Section := Leading_Section;
@@ -1652,6 +1552,74 @@ package body GNATdoc.Comments.Extractor is
16521552
end;
16531553
end Extract_Subprogram_Documentation;
16541554

1555+
----------------------------------------
1556+
-- Extract_Upper_Intermediate_Section --
1557+
----------------------------------------
1558+
1559+
procedure Extract_Upper_Intermediate_Section
1560+
(Token_Start : Token_Reference;
1561+
Token_End : Token_Reference;
1562+
Options : GNATdoc.Comments.Options.Extractor_Options;
1563+
Documentation : in out Structured_Comment'Class;
1564+
Section : out Section_Access)
1565+
is
1566+
Token : Token_Reference := Token_Start;
1567+
Found : Boolean := False;
1568+
Separated : Boolean := False;
1569+
-- Whether comment block is separated from the list with 'is' keyword
1570+
-- by empty line. In this case comment block can belong to the entity
1571+
-- declaration below.
1572+
1573+
begin
1574+
Section :=
1575+
new GNATdoc.Comments.Section'
1576+
(Kind => Raw,
1577+
Symbol => "<<INTERMEDIATE UPPER>>",
1578+
Name => <>,
1579+
Text => <>,
1580+
others => <>);
1581+
Documentation.Sections.Append (Section);
1582+
1583+
loop
1584+
Token := Next (Token);
1585+
1586+
exit when Token = No_Token or else Token = Token_End;
1587+
1588+
case Kind (Data (Token)) is
1589+
when Ada_Comment =>
1590+
Found := True;
1591+
Append_Documentation_Line
1592+
(Section.Text, Text (Token), Options.Pattern);
1593+
1594+
when Ada_Whitespace =>
1595+
declare
1596+
Location : constant Source_Location_Range :=
1597+
Sloc_Range (Data (Token));
1598+
1599+
begin
1600+
if Location.End_Line - Location.Start_Line > 1 then
1601+
exit when Found;
1602+
1603+
Found := True;
1604+
Separated := True;
1605+
end if;
1606+
end;
1607+
1608+
when others =>
1609+
if Separated then
1610+
-- Comment block is separated from the line with 'is'
1611+
-- keyword by an empty line, but not separated from the
1612+
-- entity declaration below, thus don't include it into
1613+
-- package documentation.
1614+
1615+
Section.Text.Clear;
1616+
end if;
1617+
1618+
exit;
1619+
end case;
1620+
end loop;
1621+
end Extract_Upper_Intermediate_Section;
1622+
16551623
-----------------------
16561624
-- Fill_Code_Snippet --
16571625
-----------------------

0 commit comments

Comments
 (0)