uk.me.nxg.unity
Class UnitParser

java.lang.Object
  extended by uk.me.nxg.unity.UnitParser

public class UnitParser
extends Object

A parser for unit strings. The parser will parse a single string.

There are a number of different parsers available:

fits
Parses unit string according to the prescriptions in the FITS specification, v3.0, section 4.3 (W.D. Pence et al., A&A 524, A42, 2010. doi:10.1051/0004-6361/201015362).
ogip
The format defined in OGIP memo OGIP/93-001, 1993 (postscript via FTP)
cds
A syntax based on the CDS document Standards for Astronomical Catalogues, Version 2.0, 2000, specifically section 3.2. See http://cdsweb.u-strasbg.fr/doc/catstd-3.2.htx
vou
This is intended to be as nearly as possible in the intersection of the various grammars above. It is a strict subset of the FITS and CDS grammars (in the sense that any VOUnit unit string is a valid FITS and CDS string, too), and it is almost a subset of the OGIP grammar, except that it uses the dot for multiplication rather than star. See IVOA VOUnits Proposed Recommendation.

See the uk.me.nxg.unity package documentation for fuller discussion of the grammars.


Nested Class Summary
static class UnitParser.Lexeme
          A single lexeme.
 
Field Summary
static String CDS
          Indicates the parser for CDS-format strings
static String FITS
          Indicates the parser for FITS-format strings
static String LATEX
          Indicates the formatter (not parser) for LaTeX/siunitx output
static String OGIP
          Indicates the parser for OGIP-format strings
static String VOUNITS
          Indicates the parser for VOUnit-format strings
 
Constructor Summary
UnitParser(String parserType, String unitString)
          Create a new unit parser.
 
Method Summary
 UnitParser.Lexeme getLexeme()
          Return successive lexemes from the input.
 UnitExpr getParsed()
          Retrieve the parsed result
static Set<String> getParserTypes()
          Indicate the set of available parsers.
static boolean isKnownParser(String parser)
          Indicates whether the argument is the name of a known parser.
static void main(String[] args)
          The library main program, for exploring the library's functionality.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VOUNITS

public static final String VOUNITS
Indicates the parser for VOUnit-format strings


FITS

public static final String FITS
Indicates the parser for FITS-format strings


OGIP

public static final String OGIP
Indicates the parser for OGIP-format strings


CDS

public static final String CDS
Indicates the parser for CDS-format strings


LATEX

public static final String LATEX
Indicates the formatter (not parser) for LaTeX/siunitx output

Constructor Detail

UnitParser

public UnitParser(String parserType,
                  String unitString)
           throws UnitParserException
Create a new unit parser. The possible parser syntaxes are available from getParserTypes(), or can alternatively be specified using the class's constant field values FITS, OGIP or CDS.

Parameters:
parserType - one of the known parser types
unitString - the string to be parsed
Throws:
UnitParserException - if no parser can be created (typically because the required syntax is unknown)
Method Detail

getParsed

public UnitExpr getParsed()
                   throws UnitParserException
Retrieve the parsed result

Throws:
UnitParserException - if the parse fails

getParserTypes

public static Set<String> getParserTypes()
Indicate the set of available parsers. The returned syntaxes are all valid as arguments to SyntaxFactory.createParser(java.lang.String, java.io.Reader)

Returns:
a list of parser names

isKnownParser

public static boolean isKnownParser(String parser)
Indicates whether the argument is the name of a known parser.

Parameters:
parser - the string name of a parser (eg "fits")
Returns:
true if the parser is one of the types returned by getParserTypes()

getLexeme

public UnitParser.Lexeme getLexeme()
Return successive lexemes from the input. Primarily for debugging.

Returns:
a Lexeme object, or null when the lexemes are exhausted

main

public static void main(String[] args)
The library main program, for exploring the library's functionality.

Usage:

 UnitParser [-isyntax] [-osyntax] [-v] unit-expression
 

Parse and redisplay the string expression given as argument. The -i and -o flags specify the input and output syntaxes.

 UnitParser -S
 

List the set of available syntaxes