Skip to content

Simple Numeric Parsers

blancas edited this page Feb 2, 2013 · 4 revisions

Numeric parsers return values of the appropriate numeric type, not strings. These parsers support fewer numeric formats than those in the lexer namespace and won't skip over whitespace or comments. As such, they're intended for data formats or as lower-level support functions.

We first load the Kern core namespace.

(use 'blancas.kern.core)

value runs a parser and returns the parsed value, as opposed to a parser record type. At a minimum, it takes a parser and a string; optionally can take a string that describes the input (e.g. a filename) and an arbitrary value to be used by sub-parsers. The calls on this page illustrate the usage of this function.

dec-num parsers a decimal integer and returns a boxed long.

(value dec-num "8086")  ;; 8086

oct-num parsers an octal integer and returns a boxed long. A leading zero is optional.

(value oct-num "747")  ;; 487

hex-num parsers a hex integer and returns a boxed long. Note that the prefix 0x is not allowed.

(value hex-num "FEED")  ;; 65261

float-num parser a floating-point number and returns a boxed double. Scientific notation is not allowed (but it is in the lexer namespace with float-lit).

(value float-num "3.1415927")  ;; 3.1415927
(value float-num "0.333")  ;; 0.333
(value float-num "747")  ;; 747.0
Clone this wiki locally