Search in sources :

Example 1 with TokenException

use of tech.units.indriya.internal.format.TokenException in project indriya by unitsofmeasurement.

the class EBNFUnitFormat method parse.

@Override
protected Unit<? extends Quantity<?>> parse(CharSequence csq, ParsePosition cursor) throws MeasurementParseException {
    // Parsing reads the whole character sequence from the parse position.
    int start = cursor != null ? cursor.getIndex() : 0;
    int end = csq.length();
    if (end <= start) {
        return AbstractUnit.ONE;
    }
    String source = csq.subSequence(start, end).toString().trim();
    if (source.length() == 0) {
        return AbstractUnit.ONE;
    }
    try {
        UnitFormatParser parser = new UnitFormatParser(symbolMap, new StringReader(source));
        Unit<?> result = parser.parseUnit();
        if (cursor != null)
            cursor.setIndex(end);
        return result;
    } catch (TokenException e) {
        if (e.currentToken != null) {
            cursor.setErrorIndex(start + e.currentToken.endColumn);
        } else {
            cursor.setErrorIndex(start);
        }
        throw new MeasurementParseException(e);
    } catch (TokenMgrError e) {
        cursor.setErrorIndex(start);
        throw new IllegalArgumentException(e.getMessage());
    }
}
Also used : TokenException(tech.units.indriya.internal.format.TokenException) StringReader(java.io.StringReader) TokenMgrError(tech.units.indriya.internal.format.TokenMgrError) UnitFormatParser(tech.units.indriya.internal.format.UnitFormatParser) MeasurementParseException(javax.measure.format.MeasurementParseException)

Example 2 with TokenException

use of tech.units.indriya.internal.format.TokenException in project indriya by unitsofmeasurement.

the class LocalUnitFormat method parse.

public Unit<?> parse(CharSequence csq, ParsePosition cursor) throws ParserException {
    // Parsing reads the whole character sequence from the parse position.
    int start = cursor.getIndex();
    int end = csq.length();
    if (end <= start) {
        return AbstractUnit.ONE;
    }
    String source = csq.subSequence(start, end).toString().trim();
    if (source.length() == 0) {
        return AbstractUnit.ONE;
    }
    try {
        LocalUnitFormatParser parser = new LocalUnitFormatParser(symbolMap, new StringReader(source));
        Unit<?> result = parser.parseUnit();
        cursor.setIndex(end);
        return result;
    } catch (TokenException e) {
        if (e.currentToken != null) {
            cursor.setErrorIndex(start + e.currentToken.endColumn);
        } else {
            cursor.setErrorIndex(start);
        }
        // TODO should we throw
        throw new IllegalArgumentException(e);
    // ParserException here,
    // too?
    } catch (TokenMgrError e) {
        cursor.setErrorIndex(start);
        throw new ParserException(e);
    }
}
Also used : ParserException(javax.measure.format.ParserException) TokenException(tech.units.indriya.internal.format.TokenException) StringReader(java.io.StringReader) LocalUnitFormatParser(tech.units.indriya.internal.format.LocalUnitFormatParser) TokenMgrError(tech.units.indriya.internal.format.TokenMgrError)

Aggregations

StringReader (java.io.StringReader)2 TokenException (tech.units.indriya.internal.format.TokenException)2 TokenMgrError (tech.units.indriya.internal.format.TokenMgrError)2 MeasurementParseException (javax.measure.format.MeasurementParseException)1 ParserException (javax.measure.format.ParserException)1 LocalUnitFormatParser (tech.units.indriya.internal.format.LocalUnitFormatParser)1 UnitFormatParser (tech.units.indriya.internal.format.UnitFormatParser)1