Search in sources :

Example 1 with UnitFormatParser

use of tec.uom.se.internal.format.UnitFormatParser in project uom-se by unitsofmeasurement.

the class EBNFUnitFormat method parse.

@Override
protected Unit<? extends Quantity<?>> parse(CharSequence csq, ParsePosition cursor) throws ParserException {
    // 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 ParserException(e);
    } catch (TokenMgrError e) {
        cursor.setErrorIndex(start);
        throw new IllegalArgumentException(e.getMessage());
    }
}
Also used : ParserException(javax.measure.format.ParserException) TokenException(tec.uom.se.internal.format.TokenException) StringReader(java.io.StringReader) TokenMgrError(tec.uom.se.internal.format.TokenMgrError) UnitFormatParser(tec.uom.se.internal.format.UnitFormatParser)

Aggregations

StringReader (java.io.StringReader)1 ParserException (javax.measure.format.ParserException)1 TokenException (tec.uom.se.internal.format.TokenException)1 TokenMgrError (tec.uom.se.internal.format.TokenMgrError)1 UnitFormatParser (tec.uom.se.internal.format.UnitFormatParser)1