@@ -249,15 +249,28 @@ package body Langkit_Support.Generic_API.Unparsing is
249
249
-- Node configurations for all node types in Language
250
250
end record ;
251
251
252
+ type Single_Template_Instantiation_Argument is record
253
+ Document : Document_Type;
254
+ -- Document to substitute to "recurse*" nodes when instantiating a
255
+ -- template.
256
+
257
+ Node : Lk_Node;
258
+ -- Node from which ``Document`` was generated. Keeping track of this is
259
+ -- necessary in order to implement instantiation for "recurse_flatten".
260
+ end record ;
261
+
262
+ package Template_Instantiation_Arg_Vectors is new Ada.Containers.Vectors
263
+ (Positive, Single_Template_Instantiation_Argument);
264
+
252
265
type Template_Instantiation_Args (Kind : Some_Template_Kind) is record
253
266
case Kind is
254
267
when With_Recurse | With_Text_Recurse =>
255
- With_Recurse_Doc : Document_Type ;
268
+ With_Recurse_Doc : Single_Template_Instantiation_Argument ;
256
269
-- Document to use in order to replace "recurse"/"recurse_flatten"
257
270
-- templates.
258
271
259
272
when With_Recurse_Field =>
260
- Field_Docs : Document_Vectors .Vector;
273
+ Field_Docs : Template_Instantiation_Arg_Vectors .Vector;
261
274
-- Documents to use in order to replace "recurse_field" templates
262
275
end case ;
263
276
end record ;
@@ -1882,25 +1895,22 @@ package body Langkit_Support.Generic_API.Unparsing is
1882
1895
return Pool.Create_Align
1883
1896
(Template.Align_Data,
1884
1897
Instantiate_Template_Helper
1885
- (Pool, Node, Template.Align_Contents, Arguments),
1886
- Node);
1898
+ (Pool, Node, Template.Align_Contents, Arguments));
1887
1899
1888
1900
when Break_Parent =>
1889
1901
return Pool.Create_Break_Parent;
1890
1902
1891
1903
when Fill =>
1892
1904
return Pool.Create_Fill
1893
1905
(Instantiate_Template_Helper
1894
- (Pool, Node, Template.Fill_Document, Arguments),
1895
- Node);
1906
+ (Pool, Node, Template.Fill_Document, Arguments));
1896
1907
1897
1908
when Group =>
1898
1909
return Pool.Create_Group
1899
1910
(Instantiate_Template_Helper
1900
1911
(Pool, Node, Template.Group_Document, Arguments),
1901
1912
Template.Group_Should_Break,
1902
- Template.Group_Id,
1903
- Node);
1913
+ Template.Group_Id);
1904
1914
1905
1915
when Hard_Line =>
1906
1916
return Pool.Create_Hard_Line;
@@ -1919,8 +1929,7 @@ package body Langkit_Support.Generic_API.Unparsing is
1919
1929
when Indent =>
1920
1930
return Pool.Create_Indent
1921
1931
(Instantiate_Template_Helper
1922
- (Pool, Node, Template.Indent_Document, Arguments),
1923
- Node);
1932
+ (Pool, Node, Template.Indent_Document, Arguments));
1924
1933
1925
1934
when Line =>
1926
1935
return Pool.Create_Line;
@@ -1937,51 +1946,58 @@ package body Langkit_Support.Generic_API.Unparsing is
1937
1946
Template.List_Documents.Element (I),
1938
1947
Arguments));
1939
1948
end loop ;
1940
- return Pool.Create_List (Items, Node );
1949
+ return Pool.Create_List (Items);
1941
1950
end ;
1942
1951
1943
1952
when Literal_Line =>
1944
1953
return Pool.Create_Literal_Line;
1945
1954
1946
1955
when Recurse =>
1947
- return Arguments.With_Recurse_Doc;
1956
+ return Arguments.With_Recurse_Doc.Document ;
1948
1957
1949
1958
when Recurse_Field =>
1950
- return Arguments.Field_Docs (Template.Recurse_Field_Position);
1959
+ return Arguments
1960
+ .Field_Docs (Template.Recurse_Field_Position)
1961
+ .Document;
1951
1962
1952
1963
when Recurse_Flatten =>
1953
- return Result : Document_Type := Arguments.With_Recurse_Doc do
1964
+ declare
1965
+ Arg : constant Single_Template_Instantiation_Argument :=
1966
+ Arguments.With_Recurse_Doc;
1967
+ begin
1968
+ return Result : Document_Type := Arg.Document do
1954
1969
1955
- -- As long as Result is a document we can flatten and that was
1956
- -- created by a node that passes the flattening guard, unwrap
1957
- -- it.
1970
+ -- As long as Result is a document we can flatten and that
1971
+ -- was created by a node that passes the flattening guard,
1972
+ -- unwrap it.
1958
1973
1959
- while not Result .Node.Is_Null
1960
- and then Node_Matches
1961
- (Result .Node, Template.Recurse_Flatten_Types)
1962
- loop
1963
- case Result.Kind is
1964
- when Align =>
1965
- Result := Result.Align_Contents;
1974
+ while not Arg .Node.Is_Null
1975
+ and then Node_Matches
1976
+ (Arg .Node, Template.Recurse_Flatten_Types)
1977
+ loop
1978
+ case Result.Kind is
1979
+ when Align =>
1980
+ Result := Result.Align_Contents;
1966
1981
1967
- when Fill =>
1968
- Result := Result.Fill_Document;
1982
+ when Fill =>
1983
+ Result := Result.Fill_Document;
1969
1984
1970
- when Group =>
1971
- Result := Result.Group_Document;
1985
+ when Group =>
1986
+ Result := Result.Group_Document;
1972
1987
1973
- when Indent =>
1974
- Result := Result.Indent_Document;
1988
+ when Indent =>
1989
+ Result := Result.Indent_Document;
1975
1990
1976
- when List =>
1977
- exit when Result.List_Documents.Length /= 1 ;
1978
- Result := Result.List_Documents.First_Element;
1991
+ when List =>
1992
+ exit when Result.List_Documents.Length /= 1 ;
1993
+ Result := Result.List_Documents.First_Element;
1979
1994
1980
- when others =>
1981
- exit ;
1982
- end case ;
1983
- end loop ;
1984
- end return ;
1995
+ when others =>
1996
+ exit ;
1997
+ end case ;
1998
+ end loop ;
1999
+ end return ;
2000
+ end ;
1985
2001
1986
2002
when Soft_Line =>
1987
2003
return Pool.Create_Soft_Line;
@@ -2083,7 +2099,8 @@ package body Langkit_Support.Generic_API.Unparsing is
2083
2099
declare
2084
2100
Args : constant Template_Instantiation_Args :=
2085
2101
(Kind => With_Recurse,
2086
- With_Recurse_Doc => Token);
2102
+ With_Recurse_Doc =>
2103
+ (Document => Token, Node => N));
2087
2104
begin
2088
2105
Token := Instantiate_Template
2089
2106
(Pool => Pool,
@@ -2123,7 +2140,11 @@ package body Langkit_Support.Generic_API.Unparsing is
2123
2140
(Pool => Pool,
2124
2141
Node => N,
2125
2142
Template => Template,
2126
- Arguments => (With_Recurse, Pool.Create_List (Items)));
2143
+ Arguments =>
2144
+ (Kind => With_Recurse,
2145
+ With_Recurse_Doc =>
2146
+ (Document => Pool.Create_List (Items),
2147
+ Node => N)));
2127
2148
2128
2149
when With_Recurse_Field =>
2129
2150
@@ -2146,6 +2167,7 @@ package body Langkit_Support.Generic_API.Unparsing is
2146
2167
Child : constant Lk_Node := N.Child (I);
2147
2168
Field_Unparser : Field_Unparser_Impl renames
2148
2169
Node_Unparser.Field_Unparsers.Field_Unparsers (I);
2170
+ Child_Doc : Document_Type;
2149
2171
begin
2150
2172
if Is_Field_Present (Child, Field_Unparser) then
2151
2173
Items.Clear;
@@ -2156,12 +2178,14 @@ package body Langkit_Support.Generic_API.Unparsing is
2156
2178
Field_Ref => Field_Unparser.Member,
2157
2179
Unparser => Field_Unparser,
2158
2180
Items => Items);
2159
- Arguments.Field_Docs.Append
2160
- (Pool.Create_List (Items));
2181
+ Child_Doc := Pool.Create_List (Items);
2161
2182
else
2162
- Arguments.Field_Docs.Append
2163
- (Pool.Create_Empty_List);
2183
+ Child_Doc := Pool.Create_Empty_List;
2164
2184
end if ;
2185
+ Arguments.Field_Docs.Append
2186
+ (Single_Template_Instantiation_Argument'
2187
+ (Document => Child_Doc,
2188
+ Node => Child));
2165
2189
end ;
2166
2190
end loop ;
2167
2191
@@ -2207,7 +2231,9 @@ package body Langkit_Support.Generic_API.Unparsing is
2207
2231
Field_Template_Args : Template_Instantiation_Args
2208
2232
(Field_Template.Kind);
2209
2233
begin
2210
- Field_Template_Args.With_Recurse_Doc := Unparse_Node (Child);
2234
+ Field_Template_Args.With_Recurse_Doc :=
2235
+ (Document => Unparse_Node (Child),
2236
+ Node => Child);
2211
2237
2212
2238
if Handle_Tokens then
2213
2239
Unparse_Tokens (Unparser.Pre_Tokens, Items);
0 commit comments