Skip to content

Commit dba90ec

Browse files
committed
Add support for "text" nodes in field templates
1 parent 81afbbe commit dba90ec

File tree

8 files changed

+331
-81
lines changed

8 files changed

+331
-81
lines changed

langkit/support/langkit_support-generic_api-unparsing.adb

Lines changed: 248 additions & 76 deletions
Large diffs are not rendered by default.

langkit/support/langkit_support-generic_api-unparsing.ads

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,14 @@ package Langkit_Support.Generic_API.Unparsing is
245245
--
246246
-- {"kind": "text", "text": "some_text_to_unparse"}
247247
--
248-
-- It is valid only when used with "recurse_field" template: see its
249-
-- description.
248+
-- Using this template is valid in specific contexts only:
249+
--
250+
-- * For "node" templates, when used with "recurse_field" template: see
251+
-- the documentation for "recurse_field";
252+
--
253+
-- * For "fields" templates: in this case, the linearized template must
254+
-- reflect how the field is unparsed. See the documentation for
255+
-- "recurse_field" to have more information about linearization.
250256
--
251257
-- * A JSON list yields the corresponding "list" Prettier document::
252258
--

langkit/support/langkit_support-prettier_utils.ads

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,37 @@ private package Langkit_Support.Prettier_Utils is
157157
(Document : Document_Type) return Prettier.Document_Type;
158158
-- Turn an unparsing document into an actual Prettier document
159159

160-
type Template_Kind is (No_Template_Kind, With_Recurse, With_Recurse_Field);
160+
-- Templates have different kinds depending on how they should be
161+
-- instantiated:
162+
--
163+
-- * No_Template_Kind: Special value to designate the absence of template.
164+
--
165+
-- * With_Recurse: Node or field template to instantiate with a single node
166+
-- document argument. For node templates, the argument must embed all the
167+
-- tokens for that node. For field templates, the argument must embed
168+
-- only the unparsing of the node that the field contains.
169+
--
170+
-- * With_Recurse_Field: Node template to instantiate with one argument per
171+
-- field.
172+
--
173+
-- * With_Text_Recurse: Field template to instantiate with a single node
174+
-- argument (the field). The argument must embed all the tokens for the
175+
-- field (pre/post tokens plus the unparsing of the node that the field
176+
-- contains).
177+
178+
type Template_Kind is
179+
(No_Template_Kind,
180+
With_Recurse,
181+
With_Recurse_Field,
182+
With_Text_Recurse);
161183
subtype Some_Template_Kind is
162-
Template_Kind range With_Recurse .. With_Recurse_Field;
184+
Template_Kind range With_Recurse .. With_Text_Recurse;
163185
type Template_Type (Kind : Template_Kind := No_Template_Kind) is record
164186
case Kind is
165187
when No_Template_Kind =>
166188
null;
167189

168-
when With_Recurse | With_Recurse_Field =>
190+
when Some_Template_Kind =>
169191
Root : Document_Type;
170192
end case;
171193
end record;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"node_configs": {
3+
"CallArg": {
4+
"fields": {
5+
"f_name": [
6+
"recurse",
7+
{"kind": "whitespace", "length": 1},
8+
{"kind": "text", "text": "="},
9+
{"kind": "whitespace", "length": 2}
10+
]
11+
}
12+
}
13+
}
14+
}

testsuite/tests/ada_api/unparsing/commands.adb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ begin
165165
Check ("cmd_markasroot.json");
166166
Check ("cmd_innerroot.json");
167167
Check ("cmd_recurse.json");
168+
Check
169+
("cmd_recurse_in_field.json",
170+
"var v:Int=f(a=1);");
168171
Check
169172
("cmd_recurse_field.json",
170173
"var i: Int = 0;" & ASCII.LF

testsuite/tests/ada_api/unparsing/invalid_config.adb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ begin
8484

8585
Put_Line ("== Invalid templates (too many/few ""recurse"") ==");
8686
New_Line;
87+
Check ("recurse_in_field_too_many.json");
8788
Check ("recurse_list_too_few.json");
8889
Check ("recurse_list_too_many.json");
8990
Check ("recurse_line.json");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"node_configs": {
3+
"CallArg": {
4+
"fields": {
5+
"f_name": [
6+
"recurse",
7+
{"kind": "text", "text": "="},
8+
"recurse"
9+
]
10+
}
11+
}
12+
}
13+
}

testsuite/tests/ada_api/unparsing/test.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ a: Int=2
134134

135135
== Invalid templates (too many/few "recurse") ==
136136

137+
# recurse_in_field_too_many.json
138+
template for CallArg.f_name: unexpected extra template item: recurse
139+
Expected:
140+
* recurse
141+
* token "="
137142
# recurse_list_too_few.json
138143
"sep" template for StmtList: recursion is missing
139144
# recurse_list_too_many.json
@@ -790,6 +795,20 @@ Done.
790795
]
791796
}
792797

798+
== cmd_recurse_in_field.json ==
799+
800+
{
801+
"id": 1,
802+
"kind": "list",
803+
"list": [
804+
{
805+
"id": 2,
806+
"kind": "text",
807+
"text": "var v:Int=f(a = 1);"
808+
}
809+
]
810+
}
811+
793812
== cmd_recurse_field.json ==
794813

795814
{

0 commit comments

Comments
 (0)