Package tech.units.indriya.format
Class EBNFUnitFormat
- java.lang.Object
-
- tech.units.indriya.format.AbstractUnitFormat
-
- tech.units.indriya.format.EBNFUnitFormat
-
- All Implemented Interfaces:
UnitFormat
public class EBNFUnitFormat extends AbstractUnitFormat
This class represents the local neutral format.
Here is the grammar for Units in Extended Backus-Naur Form (EBNF)
Note that the grammar has been left-factored to be suitable for use by a top-down parser generator such as JavaCC
Lexical Entities: <sign> := "+" | "-" <digit> := "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" <superscript_digit> := "⁰" | "¹" | "²" | "³" | "⁴" | "⁵" | "⁶" | "⁷" | "⁸" | "⁹" <integer> := (<digit>)+ <number> := (<sign>)? (<digit>)* (".")? (<digit>)+ (("e" | "E") (<sign>)? (<digit>)+)? <exponent> := ( "^" ( <sign> )? <integer> )
| ( "^(" (<sign>)? <integer> ( "/" (<sign>)? <integer> )? ")" )
| ( <superscript_digit> )+<initial_char> := ? Any Unicode character excluding the following: ASCII control & whitespace (\u0000 - \u0020), decimal digits '0'-'9', '(' (\u0028), ')' (\u0029), '*' (\u002A), '+' (\u002B), '-' (\u002D), '.' (\u002E), '/' (\u005C), ':' (\u003A), '^' (\u005E), '²' (\u00B2), '³' (\u00B3), '·' (\u00B7), '¹' (\u00B9), '⁰' (\u2070), '⁴' (\u2074), '⁵' (\u2075), '⁶' (\u2076), '⁷' (\u2077), '⁸' (\u2078), '⁹' (\u2079) ? <unit_identifier> := <initial_char> ( <initial_char> | <digit> )* Non-Terminals: <unit_expr> := <mix_expr> <mix_expr> := <add_expr> ( ":" <add_expr> )* <add_expr> := ( <number> <sign> )? <mul_expr> ( <sign> <number> )? <mul_expr> := <exponent_expr> ( ( ( "*" | "·" ) <exponent_expr> ) | ( "/" <exponent_expr> ) )* <exponent_expr> := ( <atomic_expr> ( <exponent> )? )
| (<integer> "^" <atomic_expr>)
| ( ( "log" ( <integer> )? ) | "ln" ) "(" <add_expr> ")" )<atomic_expr> := <number>
| <unit_identifier>
| ( "(" <add_expr> ")" )- Since:
- 1.0
- Version:
- 2.2, October 12, 2024
- Author:
- Eric Russell, Werner Keil
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidalias(Unit<?> unit, String alias)Attaches a system-wide alias to this unit.Appendableformat(Unit<?> unit, Appendable appendable)Formats the specified unit.static EBNFUnitFormatgetInstance()Returns the instance for the current default locale (non-ascii characters are allowed)static EBNFUnitFormatgetInstance(SymbolMap symbols)Returns a new instance for the given symbol map.static EBNFUnitFormatgetNewInstance()Similar togetInstance(), but returns a new, non-shared unit format instance, instead of a shared singleton instance.protected SymbolMapgetSymbols()Get the symbol map used by this instance to map betweenUnits andStrings, etc...voidlabel(Unit<?> unit, String label)Unit<?>parse(CharSequence csq)protected Unit<?>parse(CharSequence csq, int index)Parses a portion of the specifiedCharSequencefrom the specified position to produce a unit.Unit<? extends Quantity<?>>parse(CharSequence csq, ParsePosition cursor)Parses a portion of the specifiedCharSequencefrom the specified position to produce a unit.StringtoString()-
Methods inherited from class tech.units.indriya.format.AbstractUnitFormat
format
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface javax.measure.format.UnitFormat
isLocaleSensitive
-
-
-
-
Method Detail
-
getInstance
public static EBNFUnitFormat getInstance()
Returns the instance for the current default locale (non-ascii characters are allowed)
-
getInstance
public static EBNFUnitFormat getInstance(SymbolMap symbols)
Returns a new instance for the given symbol map.
-
getNewInstance
public static EBNFUnitFormat getNewInstance()
Similar togetInstance(), but returns a new, non-shared unit format instance, instead of a shared singleton instance.- Returns:
- a new instance of the default unit format.
- Since:
- 2.0
- See Also:
getInstance()
-
getSymbols
protected SymbolMap getSymbols()
Get the symbol map used by this instance to map betweenUnits andStrings, etc...- Returns:
- SymbolMap the current symbol map
-
alias
public void alias(Unit<?> unit, String alias)
Attaches a system-wide alias to this unit. Multiple aliases may be attached to the same unit. Aliases are used during parsing to recognize different variants of the same unit. For example:EBNFUnitFormat.getInstance().alias(METRE.multiply(0.3048), "foot"); EBNFUnitFormat.getInstance().alias(METRE.multiply(0.3048), "feet"); EBNFUnitFormat.getInstance().alias(METRE, "meter");If the specified alias is already associated to a unit or applied as a label, the association is replaced by the new one.- Parameters:
unit- the unit being aliased.alias- the alias attached to this unit.
-
label
public void label(Unit<?> unit, String label)
- Specified by:
labelin interfaceUnitFormat- Overrides:
labelin classAbstractUnitFormat
-
format
public Appendable format(Unit<?> unit, Appendable appendable) throws IOException
Description copied from class:AbstractUnitFormatFormats the specified unit.- Specified by:
formatin interfaceUnitFormat- Specified by:
formatin classAbstractUnitFormat- Parameters:
unit- the unit to format.appendable- the appendable destination.- Returns:
- The appendable destination passed in as
appendable, with formatted text appended. - Throws:
IOException- if an error occurs.
-
parse
public Unit<? extends Quantity<?>> parse(CharSequence csq, ParsePosition cursor) throws MeasurementParseException
Description copied from class:AbstractUnitFormatParses a portion of the specifiedCharSequencefrom the specified position to produce a unit. If there is no unit to parseAbstractUnit.ONEis returned.- Specified by:
parsein interfaceUnitFormat- Specified by:
parsein classAbstractUnitFormat- Parameters:
csq- theCharSequenceto parse.cursor- the cursor holding the current parsing index.- Returns:
- the unit parsed from the specified character sub-sequence.
- Throws:
MeasurementParseException
-
parse
protected Unit<?> parse(CharSequence csq, int index) throws IllegalArgumentException
Description copied from class:AbstractUnitFormatParses a portion of the specifiedCharSequencefrom the specified position to produce a unit. If there is no unit to parseAbstractUnit.ONEis returned.- Specified by:
parsein classAbstractUnitFormat- Parameters:
csq- theCharSequenceto parse.index- the current parsing index.- Returns:
- the unit parsed from the specified character sub-sequence.
- Throws:
IllegalArgumentException- if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
-
parse
public Unit<?> parse(CharSequence csq) throws MeasurementParseException
- Throws:
MeasurementParseException
-
-