@@ -78,6 +78,9 @@ package body Langkit_Support.Generic_API.Unparsing is
78
78
-- Syntax field for this fragment (the parent node is a
79
79
-- regular node).
80
80
81
+ Field_Unparser_Ref : Field_Unparser;
82
+ -- Unparser for this field
83
+
81
84
when List_Child_Fragment =>
82
85
Child_Index : Positive;
83
86
-- Index of this field for this fragment (the parent node is
@@ -350,12 +353,12 @@ package body Langkit_Support.Generic_API.Unparsing is
350
353
-- Then append fragments for the field itself, if present
351
354
352
355
if Is_Field_Present (Child, Field_Unparser) then
353
- Append (Field_Unparser.Pre_Tokens);
354
356
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 ));
359
362
end if ;
360
363
end ;
361
364
end loop ;
@@ -1855,18 +1858,46 @@ package body Langkit_Support.Generic_API.Unparsing is
1855
1858
is
1856
1859
Pool : Document_Pool;
1857
1860
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
+
1858
1866
function Unparse_Node (N : Lk_Node) return Document_Type;
1859
1867
-- Using the unparsing configuration for N, unparse it to a Prettier
1860
1868
-- document.
1861
1869
1862
- function Unparse_Field
1870
+ procedure Unparse_Field
1863
1871
(Node : Lk_Node;
1864
1872
Node_Config : Node_Config_Record;
1865
1873
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 ;
1870
1901
1871
1902
-- ----------------
1872
1903
-- Unparse_Node --
@@ -1914,12 +1945,13 @@ package body Langkit_Support.Generic_API.Unparsing is
1914
1945
end ;
1915
1946
1916
1947
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);
1923
1955
1924
1956
when List_Child_Fragment =>
1925
1957
Items.Append (Unparse_Node (F.Node));
@@ -1966,22 +1998,13 @@ package body Langkit_Support.Generic_API.Unparsing is
1966
1998
begin
1967
1999
if Is_Field_Present (Child, Field_Unparser) then
1968
2000
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);
1985
2008
Arguments.Field_Docs.Append
1986
2009
(Pool.Create_List (Items));
1987
2010
else
@@ -2004,11 +2027,13 @@ package body Langkit_Support.Generic_API.Unparsing is
2004
2027
-- Unparse_Field --
2005
2028
-- -----------------
2006
2029
2007
- function Unparse_Field
2030
+ procedure Unparse_Field
2008
2031
(Node : Lk_Node;
2009
2032
Node_Config : Node_Config_Record;
2010
2033
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)
2012
2037
is
2013
2038
Field_Template : constant Template_Type :=
2014
2039
Node_Config.Field_Configs.Element (Field_Ref);
@@ -2018,11 +2043,14 @@ package body Langkit_Support.Generic_API.Unparsing is
2018
2043
(Kind => With_Recurse,
2019
2044
With_Recurse_Doc => Unparse_Node (Child));
2020
2045
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);
2026
2054
end Unparse_Field ;
2027
2055
2028
2056
begin
0 commit comments