@ValueConverter(rule = "EString") public IValueConverter<String> EString() { return new AbstractNullSafeConverter<String>() { @Override protected String internalToValue(String string, INode node) { if((string.startsWith("'") && string.endsWith("'"))||(string.startsWith("\"") && string.endsWith("\""))) { return STRING().toValue(string, node); } return ID().toValue(string, node); } @Override protected String internalToString(String value) { if(ID_PATTERN.matcher(value).matches()) { return ID().toString(value); } else { return STRING().toString(value); } } }; }
@ValueConverter(rule = "ID") public IValueConverter<String> ID() { return new AbstractNullSafeConverter<String>() { @Override protected String internalToValue(String string, INode node) { return string.startsWith("^") ? string.substring(1) : string; } @Override protected String internalToString(String value) { if (GrammarUtil.getAllKeywords(getGrammar()).contains(value)) { return "^"+value; } return value; } }; }
@ValueConverter(rule = "Fraction") public IValueConverter<BigDecimal> Fraction() { return new AbstractNullSafeConverter<BigDecimal>(){ @Override protected BigDecimal internalToValue(String string, INode node) { String[] splitted = string.split("/"); if (splitted.length > 1) { return new BigDecimal(splitted[0].trim()).divide(new BigDecimal(splitted[1].trim())); } return new BigDecimal(string); } @Override protected String internalToString(BigDecimal value) { BigDecimal bd = value; int scale = bd.scale(); if (scale <= 0) { return bd.toPlainString(); } bd = bd.multiply(BigDecimal.valueOf(1, -1 * scale)); return bd.toPlainString() + '/' + BigDecimal.valueOf(1, -1 * scale).toPlainString(); }}; }
@ValueConverter(rule = "FQN") public IValueConverter<String> FQN() { return new AbstractNullSafeConverter<String>() { @Override protected String internalToString(String value) { return value; } @Override protected String internalToValue(String string, INode node) { if (!string.equals(string.trim())) throw new RuntimeException(); StringBuffer b = new StringBuffer(); for (ILeafNode leaf : node.getLeafNodes()) { if (!leaf.isHidden()) { b.append(leaf.getText()); } } return b.toString(); } }; }
@ValueConverter(rule = "WrappingDataType") public IValueConverter<String> WrappingDataType() { return new AbstractNullSafeConverter<String>() { @Override protected String internalToString(String value) { return value; } @Override protected String internalToValue(String string, INode node) throws ValueConverterException { return node.getText().trim(); } }; }
@ValueConverter(rule = "DatEnum") public IValueConverter<TestEnum> DatEnum() { return new IValueConverter<TestEnum>() { @Override public TestEnum toValue(String string, INode node) throws ValueConverterException { if ("lit3".equals(string)) return TestEnum.LIT3; else throw new ValueConverterException(null, null, null); } @Override public String toString(TestEnum value) throws ValueConverterException { if (value == TestEnum.LIT3) return TestEnum.LIT3.getName(); else throw new ValueConverterException(null, null, null); } }; }
@ValueConverter(rule = "NULL_STRING") public IValueConverter<String> NULL_STRING() { return new IValueConverter<String>() { @Override public String toString(String value) throws ValueConverterException { if (value == null) return "''"; return "'" + value + "'"; } @Override public String toValue(String string, INode node) throws ValueConverterException { if (string.length() <= 2) { return null; } return string.substring(1, string.length() - 1); } }; }
@Override @ValueConverter(rule = "MyId") public IValueConverter<String> ID() { return new IValueConverter<String>() { @Override public String toValue(String string, INode node) throws ValueConverterException { if (string != null && string.length() > 3) { throw new ValueConverterException("ID too long", node, null); } return string; } @Override public String toString(String value) throws ValueConverterException { return value; } }; }
@ValueConverter(rule = "FQN") // CHECKSTYLE:OFF public IValueConverter<String> FQN() { // NOPMD // CHECKSTYLE:ON return new AbstractNullSafeConverter<String>() { @Override protected String internalToValue(final String string, final INode node) { if (!string.equals(string.trim())) { throw new RuntimeException(); // NOPMD } StringBuffer b = new StringBuffer(); for (ILeafNode l : node.getLeafNodes()) { if (!l.isHidden()) { b.append(l.getText()); } } return b.toString(); } @Override protected String internalToString(final String value) { return value; } }; }
@ValueConverter(rule = "NameString") public IValueConverter<String> NameString() { return new AbstractNullSafeConverter<String>() { @Override protected String internalToString(String value) { return String.format("\"%s\"", value); } @Override protected String internalToValue(String string, INode node) throws ValueConverterException { if (string.matches("\\\".*\\\"")) { return string.subSequence(1, string.length() - 1).toString(); } return string; } }; }
@ValueConverter(rule = "FQN") public IValueConverter<String> FQN() { return new AbstractNullSafeConverter<String>() { @Override protected String internalToString(String value) { // this prohibits FQNs with spaces to be serialized if (value.contains(" ")) { throw new ValueConverterException("Spaces are not allowed inside FQNs.", null, null); } return value; } @Override protected String internalToValue(String string, INode node) throws ValueConverterException { return string; } }; }
@ValueConverter(rule = "CardinalityBound") public IValueConverter<Integer> convertCardinalityBound() { return new AbstractNullSafeConverter<Integer>() { @Override protected String internalToString(Integer value) { if (value < 0) { return "*"; } return value.toString(); } @Override protected Integer internalToValue(String string, INode node) throws ValueConverterException { if ("*".equals(string)) { return -1; } return Integer.parseInt(string); } }; }
@ValueConverter(rule = "DESCRIPTION") public IValueConverter<String> DESCRIPTION() { return new IValueConverter<String>() { @Override public String toValue(String string, INode node) throws ValueConverterException { return toString(string); } @Override public String toString(String value) throws ValueConverterException { return value.substring(3, value.length() - 2); } }; }
@ValueConverter(rule = "SHORT_LONG") public IValueConverter<Integer> SHORT_LONG() { return new IValueConverter<Integer>() { public Integer toValue(String string, INode node) { if (Strings.isEmpty(string)) throw new ValueConverterException("Couldn't convert empty string to int", node, null); else if ("short".equals(string.trim())) return 1; else if ("long".equals(string.trim())) return 5; try { return Integer.parseInt(string); } catch (NumberFormatException e) { throw new ValueConverterException("Couldn't convert '"+string+"' to int", node, e); } } public String toString(Integer value) { return Integer.toString(value); } }; }
@ValueConverter(rule = "QualifiedName") public IValueConverter<String> EString() { return new AbstractNullSafeConverter<String>() { @Override protected String internalToValue(String string, INode node) { if((string.startsWith("'") && string.endsWith("'"))||(string.startsWith("\"") && string.endsWith("\""))) { return STRING().toValue(string, node); } return ID().toValue(string, node); } @Override protected String internalToString(String value) { if(ID_PATTERN.matcher(value).matches()) { return ID().toString(value); } else { return STRING().toString(value); } } }; }
@ValueConverter(rule = "ElementBound") public IValueConverter<Integer> ElementBound() { return new IValueConverter<Integer>() { @Override public Integer toValue(String string, INode node) throws ValueConverterException { if (Strings.isEmpty(string)) throw new ValueConverterException("Couldn't convert empty string to int", node, null); else if ("*".equals(string.trim())) return -1; try { return Integer.parseInt(string); } catch (NumberFormatException e) { throw new ValueConverterException("Couldn't convert '"+string+"' to int", node, e); } } @Override public String toString(Integer value) throws ValueConverterException { return ((value == -1) ? "*" : Integer.toString(value)); } }; }
@ValueConverter(rule = "LiteralValue") public IValueConverter<Boolean> LiteralValue() { return new AbstractNullSafeConverter<Boolean>() { @Override protected Boolean internalToValue(String string, INode node) throws ValueConverterException { return "+".equals(string); } @Override protected String internalToString(Boolean value) { return value.booleanValue() ? "+" : "!"; } }; }
@ValueConverter(rule = "ESCAPED_CHAR") public IValueConverter<String> forFragment() { return new AbstractNullSafeConverter<String>() { @Override protected String internalToValue(String string, INode node) { return string; } @Override protected String internalToString(String value) { return value; } }; }
@ValueConverter(rule = "STRING") public IValueConverter<String> STRING() { return new AbstractNullSafeConverter<String>() { @Override protected String internalToValue(String string, INode node) { return Strings.convertFromJavaString(string.substring(1, string.length() - 1), false); } @Override protected String internalToString(String value) { return '"' + Strings.convertToJavaString(value, false) + '"'; } }; }
@ValueConverter(rule = "IdDatatype") public IValueConverter<String> IdDatatype() { return new AbstractToStringConverter<String>() { @Override protected String internalToValue(String string, INode node) { return "IdDatatype: " + string; } }; }
@ValueConverter(rule = "StringDatatype") public IValueConverter<String> StringDatatype() { return new AbstractToStringConverter<String>() { @Override protected String internalToValue(String string, INode node) { return "StringDatatype: " + string; } }; }
@ValueConverter(rule = "REAL") public IValueConverter<Double> REAL() { return new AbstractToStringConverter<Double>(){ @Override protected Double internalToValue(String string, INode node) { return Double.valueOf(string); } }; }
@ValueConverter(rule = "Datatype") public IValueConverter<String> Datatype() { return new AbstractNullSafeConverter<String>() { @Override protected String internalToValue(String string, INode node) { return "str"; } @Override protected String internalToString(String value) { throw new UnsupportedOperationException(); } }; }
@ValueConverter(rule = "NestedDatatype") public IValueConverter<String> NestedDatatype() { return new AbstractNullSafeConverter<String>() { @Override protected String internalToValue(String string, INode node) { return "str"; } @Override protected String internalToString(String value) { throw new UnsupportedOperationException(); } }; }
@ValueConverter(rule = "longdouble_type") public IValueConverter<PrimitiveKind> longdouble_type() { return new AbstractValueConverter<PrimitiveKind>() { public PrimitiveKind toValue(String string, INode node) { return PrimitiveKind.PK_LONGDOUBLE; } public String toString(PrimitiveKind value) { return "long double"; } }; }
@ValueConverter(rule = "longlong_type") public IValueConverter<PrimitiveKind> longlong_type() { return new AbstractValueConverter<PrimitiveKind>() { public PrimitiveKind toValue(String string, INode node) { return PrimitiveKind.PK_LONGLONG; } public String toString(PrimitiveKind value) { return "long long"; } }; }
@ValueConverter(rule = "ushort_type") public IValueConverter<PrimitiveKind> ushort_type() { return new AbstractValueConverter<PrimitiveKind>() { public PrimitiveKind toValue(String string, INode node) { return PrimitiveKind.PK_USHORT; } public String toString(PrimitiveKind value) { return "unsigned short"; } }; }
@ValueConverter(rule = "ulong_type") public IValueConverter<PrimitiveKind> ulong_type() { return new AbstractValueConverter<PrimitiveKind>() { public PrimitiveKind toValue(String string, INode node) { return PrimitiveKind.PK_ULONG; } public String toString(PrimitiveKind value) { return "unsigned long"; } }; }
@ValueConverter(rule = "ulonglong_type") public IValueConverter<PrimitiveKind> ulonglong_type() { return new AbstractValueConverter<PrimitiveKind>() { public PrimitiveKind toValue(String string, INode node) { return PrimitiveKind.PK_ULONGLONG; } public String toString(PrimitiveKind value) { return "unsigned long long"; } }; }
@ValueConverter(rule = "ID") public IValueConverter<String> ID() { return new AbstractValueConverter<String>() { public String toValue(String string, INode node) { return string.startsWith("_") ? string.substring(1) : string; } public String toString(String value) { return value; } }; }
@Override @ValueConverter(rule = "INT") public IValueConverter<Integer> INT() { return new IValueConverter<Integer>(){ @Override public Integer toValue(String string, INode node) throws ValueConverterException { if(string.startsWith("0x") || string.startsWith("0X")){ String stripped = string.substring(2, string.length()); return Integer.parseInt(stripped,RADIX_16); } else if(string.startsWith("0")){ return Integer.parseInt(string,RADIX_8); } else { return Integer.parseInt(string); } } @Override public String toString(Integer value) throws ValueConverterException { return value.toString(); } }; }
/** * Registers the value converter for the rule {@code TypesIdentifier}. */ @ValueConverter(rule = "TypesIdentifier") public IValueConverter<String> TypesIdentifier() { return identifierDelegateValueConverter; }
/** * Registers the value converter for the rule {@code TypesComputedPropertyName}. */ @ValueConverter(rule = "TypesComputedPropertyName") public IValueConverter<String> TypesComputedPropertyName() { return typesComputedPropertyNameValueConverter; }
/** * @return the registered value converter for the rule {@code NO_SUBSTITUTION_TEMPLATE_LITERAL} */ @ValueConverter(rule = "NO_SUBSTITUTION_TEMPLATE_LITERAL") public IValueConverter<String> noSubstitutionTemplateConverter() { return noSubstitutionTemplateConverter; }
/** * @return the registered value converter for the rule {@code TEMPLATE_HEAD} */ @ValueConverter(rule = "TEMPLATE_HEAD") public IValueConverter<String> templateHeadValueConverter() { return templateHeadValueConverter; }
/** * @return the registered value converter for the rule {@code TemplateTailLiteral} */ @ValueConverter(rule = "TemplateTailLiteral") public IValueConverter<String> templateEndValueConverter() { return templateEndValueConverter; }
/** * @return the registered value converter for the rule {@code TemplateMiddleLiteral} */ @ValueConverter(rule = "TemplateMiddleLiteral") public IValueConverter<String> templateMiddleValueConverter() { return templateMiddleValueConverter; }
/** * @return the registered value converter for the rule {@code BINARY_INT} */ @ValueConverter(rule = "BINARY_INT") public IValueConverter<BigDecimal> BinaryInt() { return binaryIntValueConverter; }
/** * @return the registered value converter for the rule {@code HEX_INT} */ @ValueConverter(rule = "HEX_INT") public IValueConverter<BigDecimal> HexInt() { return hexIntValueConverter; }
/** * @return the registered value converter for the rule {@code LEGACY_OCTAL_INT} */ @ValueConverter(rule = "LEGACY_OCTAL_INT") public IValueConverter<BigDecimal> LegacyOctalInt() { return legacyOctalIntValueConverter; }