Skip to content

Commit 7e4b960

Browse files
authored
Merge pull request swiftlang#10424 from harlanhaskins/breakfast-jokes
2 parents 8232001 + a273ca4 commit 7e4b960

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

include/swift/Syntax/Serialization/SyntaxSerialization.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,39 @@ struct TokenDescription {
132132
};
133133

134134
/// Serialization traits for TokenDescription.
135-
/// TokenDescriptions are always serialized this way:
135+
/// TokenDescriptions always serialized with a token kind, which is
136+
/// the stringified version of their name in the tok:: enum.
136137
/// ```
137138
/// {
138139
/// "kind": <token name, e.g. "kw_struct">,
139-
/// "text": <token text, e.g. "struct">
140140
/// }
141141
/// ```
142+
///
143+
/// For tokens that have some kind of text attached, like literals or
144+
/// identifiers, the serialized form will also have a "text" key containing
145+
/// that text as the value.
142146
template<>
143147
struct ObjectTraits<TokenDescription> {
144148
static void mapping(Output &out, TokenDescription &value) {
145149
out.mapRequired("kind", value.Kind);
146-
out.mapRequired("text", value.Text);
150+
switch (value.Kind) {
151+
case tok::integer_literal:
152+
case tok::floating_literal:
153+
case tok::string_literal:
154+
case tok::unknown:
155+
case tok::code_complete:
156+
case tok::identifier:
157+
case tok::oper_binary_unspaced:
158+
case tok::oper_binary_spaced:
159+
case tok::oper_postfix:
160+
case tok::oper_prefix:
161+
case tok::dollarident:
162+
case tok::comment:
163+
out.mapRequired("text", value.Text);
164+
break;
165+
default:
166+
break;
167+
}
147168
}
148169
};
149170

test/Syntax/Inputs/serialize_multiple_decls.json

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
"layout": [
55
{
66
"tokenKind": {
7-
"kind": "kw_struct",
8-
"text": "struct"
7+
"kind": "kw_struct"
98
},
109
"leadingTrivia": [
1110
{
@@ -51,8 +50,7 @@
5150
},
5251
{
5352
"tokenKind": {
54-
"kind": "l_brace",
55-
"text": "{"
53+
"kind": "l_brace"
5654
},
5755
"leadingTrivia": [
5856

@@ -64,8 +62,7 @@
6462
},
6563
{
6664
"tokenKind": {
67-
"kind": "r_brace",
68-
"text": "}"
65+
"kind": "r_brace"
6966
},
7067
"leadingTrivia": [
7168
{
@@ -86,8 +83,7 @@
8683
"layout": [
8784
{
8885
"tokenKind": {
89-
"kind": "kw_struct",
90-
"text": "struct"
86+
"kind": "kw_struct"
9187
},
9288
"leadingTrivia": [
9389
{
@@ -121,8 +117,7 @@
121117
},
122118
{
123119
"tokenKind": {
124-
"kind": "l_brace",
125-
"text": "{"
120+
"kind": "l_brace"
126121
},
127122
"leadingTrivia": [
128123

@@ -134,8 +129,7 @@
134129
},
135130
{
136131
"tokenKind": {
137-
"kind": "r_brace",
138-
"text": "}"
132+
"kind": "r_brace"
139133
},
140134
"leadingTrivia": [
141135
{

test/Syntax/Inputs/serialize_struct_decl.json

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
"layout": [
55
{
66
"tokenKind": {
7-
"kind": "kw_struct",
8-
"text": "struct"
7+
"kind": "kw_struct"
98
},
109
"leadingTrivia": [
1110
{
@@ -51,8 +50,7 @@
5150
},
5251
{
5352
"tokenKind": {
54-
"kind": "l_brace",
55-
"text": "{"
53+
"kind": "l_brace"
5654
},
5755
"leadingTrivia": [
5856

@@ -64,8 +62,7 @@
6462
},
6563
{
6664
"tokenKind": {
67-
"kind": "kw_let",
68-
"text": "let"
65+
"kind": "kw_let"
6966
},
7067
"leadingTrivia": [
7168
{
@@ -103,8 +100,7 @@
103100
},
104101
{
105102
"tokenKind": {
106-
"kind": "colon",
107-
"text": ":"
103+
"kind": "colon"
108104
},
109105
"leadingTrivia": [
110106

@@ -132,8 +128,7 @@
132128
},
133129
{
134130
"tokenKind": {
135-
"kind": "kw_let",
136-
"text": "let"
131+
"kind": "kw_let"
137132
},
138133
"leadingTrivia": [
139134
{
@@ -171,8 +166,7 @@
171166
},
172167
{
173168
"tokenKind": {
174-
"kind": "colon",
175-
"text": ":"
169+
"kind": "colon"
176170
},
177171
"leadingTrivia": [
178172

@@ -248,8 +242,7 @@
248242
},
249243
{
250244
"tokenKind": {
251-
"kind": "r_brace",
252-
"text": "}"
245+
"kind": "r_brace"
253246
},
254247
"leadingTrivia": [
255248
{

0 commit comments

Comments
 (0)