Skip to content

Commit 81afbbe

Browse files
committed
Unparsing: refactor field unparsing
Move the handling of pre/post tokens to the Unparse_Field subprogram, so neither Iterate_On_Fragments nor special handling for With_Recurse_Field node templates have to deal with them manually. This refactoring also serves as preparatory work for the handling of "text" nodes in field templates: it will be the responsibility of field templates to deal with pre/post tokens.
1 parent e83690f commit 81afbbe

File tree

1 file changed

+67
-39
lines changed

1 file changed

+67
-39
lines changed

langkit/support/langkit_support-generic_api-unparsing.adb

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ package body Langkit_Support.Generic_API.Unparsing is
7878
-- Syntax field for this fragment (the parent node is a
7979
-- regular node).
8080

81+
Field_Unparser_Ref : Field_Unparser;
82+
-- Unparser for this field
83+
8184
when List_Child_Fragment =>
8285
Child_Index : Positive;
8386
-- Index of this field for this fragment (the parent node is
@@ -350,12 +353,12 @@ package body Langkit_Support.Generic_API.Unparsing is
350353
-- Then append fragments for the field itself, if present
351354

352355
if Is_Field_Present (Child, Field_Unparser) then
353-
Append (Field_Unparser.Pre_Tokens);
354356
Process.all
355-
((Kind => Field_Fragment,
356-
Node => Child,
357-
Field => From_Index (Id, Field_Unparser.Member)));
358-
Append (Field_Unparser.Post_Tokens);
357+
((Kind => Field_Fragment,
358+
Node => Child,
359+
Field => From_Index
360+
(Id, Field_Unparser.Member),
361+
Field_Unparser_Ref => Field_Unparser'Access));
359362
end if;
360363
end;
361364
end loop;
@@ -1855,18 +1858,46 @@ package body Langkit_Support.Generic_API.Unparsing is
18551858
is
18561859
Pool : Document_Pool;
18571860

1861+
procedure Unparse_Tokens
1862+
(Tokens : Token_Sequence; Items : in out Document_Vectors.Vector);
1863+
-- Create template nodes for each element in ``Tokens`` and append them
1864+
-- to ``Items``.
1865+
18581866
function Unparse_Node (N : Lk_Node) return Document_Type;
18591867
-- Using the unparsing configuration for N, unparse it to a Prettier
18601868
-- document.
18611869

1862-
function Unparse_Field
1870+
procedure Unparse_Field
18631871
(Node : Lk_Node;
18641872
Node_Config : Node_Config_Record;
18651873
Child : Lk_Node;
1866-
Field_Ref : Struct_Member_Index) return Document_Type;
1867-
-- Unparse ``Child``, which is the ``Field_Ref`` field of ``Node``.
1868-
-- ``Node_Config`` must be the node unparsing configuration for
1869-
-- ``Node``.
1874+
Field_Ref : Struct_Member_Index;
1875+
Unparser : Field_Unparser_Impl;
1876+
Items : in out Document_Vectors.Vector);
1877+
-- Unparse ``Child``, which is the ``Field_Ref`` field of ``Node``. The
1878+
-- Resulting items are appended to ``Items``. ``Node_Config`` must be
1879+
-- the node unparsing configuration for ``Node``, and ``Unparser`` must
1880+
-- be the unparser for this field.
1881+
1882+
--------------------
1883+
-- Unparse_Tokens --
1884+
--------------------
1885+
1886+
procedure Unparse_Tokens
1887+
(Tokens : Token_Sequence; Items : in out Document_Vectors.Vector) is
1888+
begin
1889+
for T of Tokens.all loop
1890+
declare
1891+
Fragment : constant Unparsing_Fragment :=
1892+
Fragment_For (Config.Value.Language, T);
1893+
pragma Assert (Fragment.Kind = Token_Fragment);
1894+
begin
1895+
Items.Append
1896+
(Pool.Create_Token
1897+
(Fragment.Token_Kind, Fragment.Token_Text));
1898+
end;
1899+
end loop;
1900+
end Unparse_Tokens;
18701901

18711902
------------------
18721903
-- Unparse_Node --
@@ -1914,12 +1945,13 @@ package body Langkit_Support.Generic_API.Unparsing is
19141945
end;
19151946

19161947
when Field_Fragment =>
1917-
Items.Append
1918-
(Unparse_Field
1919-
(Node => N,
1920-
Node_Config => Node_Config,
1921-
Child => F.Node,
1922-
Field_Ref => To_Index (F.Field)));
1948+
Unparse_Field
1949+
(Node => N,
1950+
Node_Config => Node_Config,
1951+
Child => F.Node,
1952+
Field_Ref => To_Index (F.Field),
1953+
Unparser => F.Field_Unparser_Ref.all,
1954+
Items => Items);
19231955

19241956
when List_Child_Fragment =>
19251957
Items.Append (Unparse_Node (F.Node));
@@ -1966,22 +1998,13 @@ package body Langkit_Support.Generic_API.Unparsing is
19661998
begin
19671999
if Is_Field_Present (Child, Field_Unparser) then
19682000
Items.Clear;
1969-
1970-
for T of Field_Unparser.Pre_Tokens.all loop
1971-
Process_Fragment (Fragment_For (Id, T));
1972-
end loop;
1973-
1974-
Items.Append
1975-
(Unparse_Field
1976-
(Node => N,
1977-
Node_Config => Node_Config,
1978-
Child => Child,
1979-
Field_Ref => Field_Unparser.Member));
1980-
1981-
for T of Field_Unparser.Post_Tokens.all loop
1982-
Process_Fragment (Fragment_For (Id, T));
1983-
end loop;
1984-
2001+
Unparse_Field
2002+
(Node => N,
2003+
Node_Config => Node_Config,
2004+
Child => Child,
2005+
Field_Ref => Field_Unparser.Member,
2006+
Unparser => Field_Unparser,
2007+
Items => Items);
19852008
Arguments.Field_Docs.Append
19862009
(Pool.Create_List (Items));
19872010
else
@@ -2004,11 +2027,13 @@ package body Langkit_Support.Generic_API.Unparsing is
20042027
-- Unparse_Field --
20052028
-------------------
20062029

2007-
function Unparse_Field
2030+
procedure Unparse_Field
20082031
(Node : Lk_Node;
20092032
Node_Config : Node_Config_Record;
20102033
Child : Lk_Node;
2011-
Field_Ref : Struct_Member_Index) return Document_Type
2034+
Field_Ref : Struct_Member_Index;
2035+
Unparser : Field_Unparser_Impl;
2036+
Items : in out Document_Vectors.Vector)
20122037
is
20132038
Field_Template : constant Template_Type :=
20142039
Node_Config.Field_Configs.Element (Field_Ref);
@@ -2018,11 +2043,14 @@ package body Langkit_Support.Generic_API.Unparsing is
20182043
(Kind => With_Recurse,
20192044
With_Recurse_Doc => Unparse_Node (Child));
20202045
begin
2021-
return Instantiate_Template
2022-
(Pool => Pool,
2023-
Node => Node,
2024-
Template => Field_Template,
2025-
Arguments => Field_Template_Args);
2046+
Unparse_Tokens (Unparser.Pre_Tokens, Items);
2047+
Items.Append
2048+
(Instantiate_Template
2049+
(Pool => Pool,
2050+
Node => Node,
2051+
Template => Field_Template,
2052+
Arguments => Field_Template_Args));
2053+
Unparse_Tokens (Unparser.Post_Tokens, Items);
20262054
end Unparse_Field;
20272055

20282056
begin

0 commit comments

Comments
 (0)