The following example parses floating-point numbers and integers. In both the scientific notation can be used.
entry {
<raw-num> = [(\d)+]
<num-sign> = [ '+' | '-' ]
<scientific-notation> = ['e' <num-sign>? <raw-num>]
<scientific-num> = [<raw-num> <scientific-notation>?]
default [object:
# Well that's optional
def sign = [<num-sign>]?
# Decimal
[
def num = [<raw-num>]? '.'
def decimal = [<scientific-num>]
] |
# Non-decimal
def num = [<scientific-num>]
]
}
The definition contains three tags:
num
contains the preceding number.decimal
contains the decimal-place.sign
contains (an optional) preceding sign (+
or-
).
Input | Output |
---|---|
parse('10') |
{sign: null, num: '10', decimal: null} |
parse('-25') |
{sign: '-', num: '25', decimal: null} |
parse('14.5') |
{sign: null, num: '14', decimal: '5'} |
parse('20e+55') |
{sign: null, num: '20e+55', decimal: null} |
parse('10.5e6') |
{sign: null, num: '10', decimal: '5e6'} |
parse('-.5e2') |
{sign: '-', num: null, decimal: '5e2'} |
parse('.23e-232') |
{sign: null, num: null, decimal: '23e-232'} |
Input | Output |
---|---|
parse(g) |
null (not a number) |
parse(.35.34) |
null (number cannot contain two decimal places) |
parse(2e5.3e5) |
null (scientific notation cannot be used in both places) |
parse(-+5) |
null (two preceding signs are not allowed) |