Class Parser


  • public class Parser
    extends Object
    Parser for units.

    This fairly basic parser uses recursive descent with the following grammar, where '*' can in fact be either '*', '×', '.', or '·', '/' can be either '/' or '⁄' and '^' can be either '^', "**" or implicit with switch to superscripts, and fraction are either unicode fractions like ½ or ⅞ or the decimal value 0.5. The special cases "n/a" returns a null list. It is intended to manage the special unit Unit.NONE.

       unit         ::=  "n/a" | chain
       chain        ::=  operand { ('*' | '/') operand }
       operand      ::=  integer | integer term | term
       term         ::=  '√' base | base power
       power        ::=  '^' exponent | ε
       exponent     ::=  'fraction'   | integer | '(' integer denominator ')'
       denominator  ::=  '/' integer  | ε
       base         ::=  identifier | '(' chain ')'
     

    This parses correctly units like MHz, km/√d, kg.m.s⁻¹, µas^⅖/(h**(2)×m)³, km/√(kg.s), √kg*km** (3/2) /(µs^2*Ω⁻⁷), km**0.5/s, #/y, 2rev/d², 1/s.

    Note that we don't accept combining square roots and power on the same operand; km/√d³ is refused (but km/√(d³) is accepted). We also accept a single integer prefix and only at the start of the specification.

    Since:
    11.0
    Author:
    Luc Maisonobe
    • Method Detail

      • buildTermsList

        public static List<PowerTerm> buildTermsList​(String unitsSpecification)
        Build the list of terms corresponding to a units specification.
        Parameters:
        unitsSpecification - units specification to parse
        Returns:
        parse tree