diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespajlib/src/main/java/com/yahoo/tensor/TensorTypeParser.java |
Publish
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor/TensorTypeParser.java')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/tensor/TensorTypeParser.java | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/TensorTypeParser.java b/vespajlib/src/main/java/com/yahoo/tensor/TensorTypeParser.java new file mode 100644 index 00000000000..3d2e1663971 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/tensor/TensorTypeParser.java @@ -0,0 +1,70 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.tensor; + +import com.google.common.annotations.Beta; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Class for parsing a tensor type spec. + * + * @author <a href="mailto:geirst@yahoo-inc.com">Geir Storli</a> + */ +@Beta +class TensorTypeParser { + + private final static String START_STRING = "tensor("; + private final static String END_STRING = ")"; + + private static final Pattern indexedPattern = Pattern.compile("(\\w+)\\[(\\d*)\\]"); + private static final Pattern mappedPattern = Pattern.compile("(\\w+)\\{\\}"); + + static TensorType fromSpec(String specString) { + if (!specString.startsWith(START_STRING) || !specString.endsWith(END_STRING)) { + throw new IllegalArgumentException("Tensor type spec must start with '" + START_STRING + "'" + + " and end with '" + END_STRING + "', but was '" + specString + "'"); + } + TensorType.Builder builder = new TensorType.Builder(); + String dimensionsSpec = specString.substring(START_STRING.length(), specString.length() - END_STRING.length()); + if (dimensionsSpec.isEmpty()) { + return builder.build(); + } + for (String element : dimensionsSpec.split(",")) { + String trimmedElement = element.trim(); + if (tryParseIndexedDimension(trimmedElement, builder)) { + } else if (tryParseMappedDimension(trimmedElement, builder)) { + } else { + throw new IllegalArgumentException("Failed parsing element '" + element + + "' in type spec '" + specString + "'"); + } + } + return builder.build(); + } + + private static boolean tryParseIndexedDimension(String element, TensorType.Builder builder) { + Matcher matcher = indexedPattern.matcher(element); + if (matcher.matches()) { + String dimensionName = matcher.group(1); + String dimensionSize = matcher.group(2); + if (dimensionSize.isEmpty()) { + builder.indexedUnbound(dimensionName); + } else { + builder.indexedBound(dimensionName, Integer.valueOf(dimensionSize)); + } + return true; + } + return false; + } + + private static boolean tryParseMappedDimension(String element, TensorType.Builder builder) { + Matcher matcher = mappedPattern.matcher(element); + if (matcher.matches()) { + String dimensionName = matcher.group(1); + builder.mapped(dimensionName); + return true; + } + return false; + } +} + |