Skip to content

Commit bc57b61

Browse files
committed
[Java] Tidy up of generators.
1 parent 510f1c3 commit bc57b61

File tree

4 files changed

+110
-65
lines changed

4 files changed

+110
-65
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2013-2017 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package uk.co.real_logic.sbe.generation;
17+
18+
import uk.co.real_logic.sbe.ir.Token;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import static uk.co.real_logic.sbe.generation.java.JavaUtil.forEachField;
24+
25+
public class NamedToken
26+
{
27+
private final String name;
28+
private final Token typeToken;
29+
30+
public NamedToken(final String name, final Token typeToken)
31+
{
32+
this.name = name;
33+
this.typeToken = typeToken;
34+
}
35+
36+
public String name()
37+
{
38+
return name;
39+
}
40+
41+
public Token typeToken()
42+
{
43+
return typeToken;
44+
}
45+
46+
public static List<NamedToken> gatherNamedFieldTokens(final List<Token> fields)
47+
{
48+
final List<NamedToken> namedTokens = new ArrayList<>();
49+
forEachField(fields, (f, t) -> namedTokens.add(new NamedToken(f.name(), t)));
50+
51+
return namedTokens;
52+
}
53+
}

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/NamedToken.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.agrona.generation.OutputManager;
55
import uk.co.real_logic.sbe.PrimitiveType;
66
import uk.co.real_logic.sbe.generation.CodeGenerator;
7+
import uk.co.real_logic.sbe.generation.NamedToken;
78
import uk.co.real_logic.sbe.ir.*;
89

910
import java.io.IOException;
@@ -218,11 +219,9 @@ private static void generateMessageEncoder(
218219
final String messageTypeName = formatTypeName(msgToken.name());
219220
final RustCodecType codecType = RustCodecType.Encoder;
220221
String topType = codecType.generateDoneCoderType(outputManager, messageTypeName);
221-
topType = generateTopVarDataCoders(messageTypeName, components.varData,
222-
outputManager, topType, codecType);
222+
topType = generateTopVarDataCoders(messageTypeName, components.varData, outputManager, topType, codecType);
223223
topType = generateGroupsCoders(groupTree, outputManager, topType, codecType);
224-
topType = generateFixedFieldCoder(messageTypeName, outputManager, topType,
225-
fieldsRepresentation, codecType);
224+
topType = generateFixedFieldCoder(messageTypeName, outputManager, topType, fieldsRepresentation, codecType);
226225
topType = codecType.generateMessageHeaderCoder(messageTypeName, outputManager, topType, headerSize);
227226
generateEntryPoint(messageTypeName, outputManager, topType, codecType);
228227
}
@@ -240,11 +239,9 @@ private static void generateMessageDecoder(
240239
final String messageTypeName = formatTypeName(msgToken.name());
241240
final RustCodecType codecType = RustCodecType.Decoder;
242241
String topType = codecType.generateDoneCoderType(outputManager, messageTypeName);
243-
topType = generateTopVarDataCoders(messageTypeName, components.varData,
244-
outputManager, topType, codecType);
242+
topType = generateTopVarDataCoders(messageTypeName, components.varData, outputManager, topType, codecType);
245243
topType = generateGroupsCoders(groupTree, outputManager, topType, codecType);
246-
topType = generateFixedFieldCoder(messageTypeName, outputManager, topType,
247-
fieldsRepresentation, codecType);
244+
topType = generateFixedFieldCoder(messageTypeName, outputManager, topType, fieldsRepresentation, codecType);
248245
topType = codecType.generateMessageHeaderCoder(messageTypeName, outputManager, topType, headerSize);
249246
generateEntryPoint(messageTypeName, outputManager, topType, codecType);
250247
}
@@ -363,8 +360,14 @@ private static String generateGroupNodeEncoders(
363360
atEndOfCurrentLevel = false;
364361
}
365362

366-
return writeGroupEncoderTopTypes(outputManager, afterGroupCoderType, node, atEndOfParent,
367-
atEndOfCurrentLevel, memberCoderType, nextCoderType);
363+
return writeGroupEncoderTopTypes(
364+
outputManager,
365+
afterGroupCoderType,
366+
node,
367+
atEndOfParent,
368+
atEndOfCurrentLevel,
369+
memberCoderType,
370+
nextCoderType);
368371
}
369372

370373
private static String generateGroupNodeDecoders(
@@ -709,22 +712,21 @@ private static String toScratchChain(final int depth)
709712

710713
private static <T> Iterable<T> reversedList(final List<T> list)
711714
{
712-
return () ->
713-
{
714-
if (list.isEmpty())
715+
return
716+
() ->
715717
{
716-
return list.stream().iterator();
717-
}
718+
if (list.isEmpty())
719+
{
720+
return list.stream().iterator();
721+
}
718722

719-
final int maxIndex = list.size() - 1;
723+
final int maxIndex = list.size() - 1;
720724

721-
return IntStream.rangeClosed(0, maxIndex).mapToObj((i) -> list.get(maxIndex - i)).iterator();
722-
};
725+
return IntStream.rangeClosed(0, maxIndex).mapToObj((i) -> list.get(maxIndex - i)).iterator();
726+
};
723727
}
724728

725-
private static List<GroupTreeNode> buildGroupTrees(
726-
final String parentTypeName,
727-
final List<Token> groupsTokens)
729+
private static List<GroupTreeNode> buildGroupTrees(final String parentTypeName, final List<Token> groupsTokens)
728730
{
729731
return buildGroupTrees(parentTypeName, groupsTokens, Optional.empty());
730732
}
@@ -971,13 +973,15 @@ String generateVarDataDecoder(
971973
{
972974
goToNext = format("%s::wrap(self.%s)", nextDecoderType, contentPropertyName);
973975
}
976+
974977
indent(writer, 2,
975978
"Ok((%s.read_slice::<%s>(count as usize, %s)?, %s))\n", toScratchChain(groupDepth),
976979
rustTypeName(this.dataType), this.dataType.size(), goToNext);
977980
indent(writer).append("}\n");
978981

979982
writer.append("}\n");
980983
}
984+
981985
return decoderType;
982986
}
983987

@@ -1000,6 +1004,7 @@ static List<VarDataSummary> gatherVarDataSummaries(final List<Token> tokens)
10001004
summaries.add(new VarDataSummary(beginToken.name(), lengthType, dataType));
10011005
i += headerTokenCount;
10021006
}
1007+
10031008
return summaries;
10041009
}
10051010
}
@@ -1008,8 +1013,9 @@ static String generateTopVarDataCoders(
10081013
final String messageTypeName,
10091014
final List<Token> tokens,
10101015
final OutputManager outputManager,
1011-
final String initialNextType, final RustCodecType codecType) throws
1012-
IOException
1016+
final String initialNextType,
1017+
final RustCodecType codecType)
1018+
throws IOException
10131019
{
10141020
final List<VarDataSummary> summaries = VarDataSummary.gatherVarDataSummaries(tokens);
10151021

@@ -1051,16 +1057,19 @@ private static void generateEnums(final Ir ir, final OutputManager outputManager
10511057
{
10521058
continue;
10531059
}
1060+
10541061
final Token beginToken = tokens.get(0);
10551062
if (beginToken.signal() != BEGIN_ENUM)
10561063
{
10571064
continue;
10581065
}
1066+
10591067
final String typeName = beginToken.applicableTypeName();
10601068
if (enumTypeNames.contains(typeName))
10611069
{
10621070
continue;
10631071
}
1072+
10641073
generateEnum(tokens, outputManager);
10651074
enumTypeNames.add(typeName);
10661075
}
@@ -1297,8 +1306,8 @@ private static void generateComposites(final Ir ir, final OutputManager outputMa
12971306
}
12981307
}
12991308

1300-
private static void generateSingleComposite(final List<Token> tokens, final OutputManager outputManager) throws
1301-
IOException
1309+
private static void generateSingleComposite(final List<Token> tokens, final OutputManager outputManager)
1310+
throws IOException
13021311
{
13031312
final Token beginToken = tokens.get(0);
13041313
final String originalTypeName = beginToken.applicableTypeName();
@@ -1307,7 +1316,7 @@ private static void generateSingleComposite(final List<Token> tokens, final Outp
13071316
try (Writer writer = outputManager.createOutput(formattedTypeName))
13081317
{
13091318
appendStructHeader(writer, formattedTypeName, true);
1310-
appendStructFields(writer, splitTokens.nonConstantEncodingTokens);
1319+
appendStructFields(writer, splitTokens.nonConstantEncodingTokens());
13111320
writer.append("}\n");
13121321

13131322
generateConstantAccessorImpl(writer, formattedTypeName, getMessageBody(tokens));
@@ -1319,13 +1328,13 @@ private static void appendStructFields(final Appendable appendable, final List<N
13191328
{
13201329
for (final NamedToken namedToken : namedTokens)
13211330
{
1322-
final Token typeToken = namedToken.typeToken;
1331+
final Token typeToken = namedToken.typeToken();
13231332
if (typeToken.isConstantEncoding())
13241333
{
13251334
continue;
13261335
}
13271336

1328-
final String propertyName = formatMethodName(namedToken.name);
1337+
final String propertyName = formatMethodName(namedToken.name());
13291338
indent(appendable).append("pub ").append(propertyName).append(":");
13301339

13311340
switch (typeToken.signal())
@@ -1387,14 +1396,14 @@ private void generateMessageHeaderDefault(
13871396
"version"));
13881397

13891398
final List<NamedToken> nonReservedNamedTokens = SplitCompositeTokens.splitInnerTokens(header.tokens())
1390-
.nonConstantEncodingTokens
1399+
.nonConstantEncodingTokens()
13911400
.stream()
1392-
.filter((namedToken) -> !reserved.contains(namedToken.name))
1401+
.filter((namedToken) -> !reserved.contains(namedToken.name()))
13931402
.collect(Collectors.toList());
13941403

13951404
for (final NamedToken namedToken : nonReservedNamedTokens)
13961405
{
1397-
indent(writer, 4, "%s: Default::default(),\n", formatMethodName(namedToken.name));
1406+
indent(writer, 4, "%s: Default::default(),\n", formatMethodName(namedToken.name()));
13981407
}
13991408

14001409
indent(writer, 3, "}\n");
@@ -1517,8 +1526,7 @@ private static void generateConstantAccessorImpl(
15171526
case BEGIN_SET:
15181527
case BEGIN_COMPOSITE:
15191528
default:
1520-
throw new IllegalStateException(format("Unsupported constant presence property " +
1521-
"%s", fieldToken.toString()));
1529+
throw new IllegalStateException("Unsupported constant presence property " + fieldToken);
15221530
}
15231531

15241532
appendConstAccessor(writer, name, constantRustTypeName, constantRustExpression);

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/SplitCompositeTokens.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
package uk.co.real_logic.sbe.generation.rust;
22

3+
import uk.co.real_logic.sbe.generation.NamedToken;
34
import uk.co.real_logic.sbe.ir.Token;
45

56
import java.util.ArrayList;
67
import java.util.List;
78

89
final class SplitCompositeTokens
910
{
10-
final List<Token> constantEncodingTokens;
11-
final List<NamedToken> nonConstantEncodingTokens;
11+
private final List<Token> constantEncodingTokens;
12+
private final List<NamedToken> nonConstantEncodingTokens;
1213

13-
private SplitCompositeTokens(final List<Token> constantEncodingTokens, final List<NamedToken>
14-
nonConstantEncodingTokens)
14+
private SplitCompositeTokens(
15+
final List<Token> constantEncodingTokens, final List<NamedToken> nonConstantEncodingTokens)
1516
{
1617
this.constantEncodingTokens = constantEncodingTokens;
1718
this.nonConstantEncodingTokens = nonConstantEncodingTokens;
1819
}
1920

20-
static SplitCompositeTokens splitInnerTokens(final List<Token> tokens)
21+
public List<Token> constantEncodingTokens()
22+
{
23+
return constantEncodingTokens;
24+
}
25+
26+
public List<NamedToken> nonConstantEncodingTokens()
27+
{
28+
return nonConstantEncodingTokens;
29+
}
30+
31+
public static SplitCompositeTokens splitInnerTokens(final List<Token> tokens)
2132
{
2233
final List<Token> constantTokens = new ArrayList<>();
2334
final List<NamedToken> namedNonConstantTokens = new ArrayList<>();

0 commit comments

Comments
 (0)