Paste: json.read peg.ebnf problem

Author: rictic
Mode: factor
Date: Sat, 8 Nov 2008 01:56:07
Plain Text |
! Copyright (C) 2008 Peter Burns.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel peg peg.ebnf math.parser math.private strings math math.functions sequences
       arrays vectors hashtables prettyprint ;
IN: json.reader

SINGLETON: json-null

! Grammar for JSON from RFC 4627
EBNF: json>

ws = (" " | "\r" | "\t" | "\n")*

true = "true" => [[ t ]]
false = "false" => [[ f ]]
null = "null" => [[ json-null ]]

hex = [0-9a-fA-F]
char = '\\"'  [[ CHAR: "  ]]
     | "\\\\" [[ CHAR: \  ]]
     | "\\/"  [[ CHAR: /  ]]
     | "\\b"  [[ 8        ]]
     | "\\f"  [[ 12       ]]
     | "\\n"  [[ CHAR: \n ]]
     | "\\r"  [[ CHAR: \r ]]
     | "\\t"  [[ CHAR: \t ]]
     | "\\u" (hex hex hex hex) [[ hex> ]] => [[ 1 swap nth ]]
     | [^"\]
string = '"' char*:cs '"' => [[ cs >string ]]

sign = ("-" | "+")? => [[ "-" = [ "-" ] [ "" ] if ]]
digits = [0-9]+     => [[ >string ]]
decimal = "." digits  => [[ concat ]]
exp = ("e" | "E") sign digits => [[ concat ]]
number = sign digits decimal? exp? => [[ dup concat swap fourth [ [ string>float ] ] [ [ string>number ] ] if call ]]

elements = value:head ("," elements)?:tail => [[ head tail [ tail second ?push ] [ f ?push ] if ]]
array = "[" elements*:vec "]" => [[ vec first <reversed> >array ]]

pair = ws string:key ws ":" value:val => [[ { key val } ]]
members = pair:head ("," members)?:tail => [[ head tail [ tail second ?push ] [ f ?push ] if ]]
object = "{" (members)*:assoc "}" => [[ assoc first >hashtable ]]

val = true
    | false
    | null
    | string
    | number
    | array
    | object

value = ws val:v ws => [[ v ]]

;EBNF

New Annotation

Summary:
Author:
Mode:
Body: