Skip to content

Instantly share code, notes, and snippets.

@hildjj
Created August 22, 2023 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hildjj/518d2e6919126d01f8ca9e79702e4419 to your computer and use it in GitHub Desktop.
Save hildjj/518d2e6919126d01f8ca9e79702e4419 to your computer and use it in GitHub Desktop.
Aggressively accurate ISO 6093 parser
{{
// The signed representation of the numerical value zero shall contain a PLUS
// SIGN or a SPACE, but not a MINUS SIGN.
const NZERO1 = /^-0+$/
// The signed representation of the numerical value zero shall contain a PLUS
// SIGN or a SPACE, but not a MINUS SIGN.
const NZERO2 = /^-0*\.0*$/
// If the exponent has the value Zero, its sign shall be a PLUS SIGN.
const NZERO3e = /^-?0+$/
// The representation of the numerical value zero shall contain a
// PLUS SIGN or a SPACE, only ZEROS in the significand, and a
// PLUS SIGN and only ZEROS in the exponent.
const NZERO3 = /^ *[ +]0*.0*[eE]\+0+$/
}}
Number
= number:NR3 { return {type: "NR3", number} }
/ number:NR2 { return {type: "NR2", number} }
/ number:NR1 { return {type: "NR1", number} }
// 6.2
NR1
= unsigned_NR1
/ @num:signed_NR1 !{ return NZERO1.test(num) }
unsigned_NR1 = space* @$digit+
signed_NR1 = space* @$((sign/space) digit+)
// 7.2
NR2
= unsigned_NR2
/ @num:signed_NR2 !{ return NZERO2.test(num) }
unsigned_NR2
= space* @$(digit+ decimal_mark digit*)
/ space* @$(digit* decimal_mark digit+)
signed_NR2
= space* @$((sign/space) digit+ decimal_mark digit*)
/ space* @$((sign/space) digit* decimal_mark digit+)
// 8.2
NR3
= num:(unsigned_NR3 / signed_NR3) &{
return (parseFloat(num) !== 0) || NZERO3.test(num)
} { return num.trim() }
unsigned_NR3 = $(space* significand exponent_mark exponent)
signed_NR3 = space* @$((sign/space) significand exponent_mark exponent)
significand
= $(digit+ decimal_mark digit*)
/ $(digit* decimal_mark digit+)
exponent = num:$(sign? digit+) !{ return NZERO3e.test(num) }
// 5.2 Syntax
digit = [0-9]
sign = "+" / "-"
decimal_mark = "," / "."
space = " "
exponent_mark = "E" / "e"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment