1 # Copyright (C) 2005-2008, Parrot Foundation.
6 data_json - parse JSON, a lightweight data-interchange format.
10 Given a valid JSON (JavaScript Object Notation) string, the compiler will
11 return a sub that when called will produce the appropriate values. For
14 .local pmc json, code, result
15 json = compreg 'data_json'
16 code = json.'compile'('[1,2,3]')
19 will create a PMC that C<does> C<array> containing the values 1, 2, and 3,
20 and store it in the C<result>.
22 For more information about the structure of the JSON representation, see
23 the documentation at L<http://www.json.org/>.
30 load_bytecode 'PGE.pbc'
31 load_bytecode 'PGE/Util.pbc'
32 load_bytecode 'TGE.pbc'
34 $P1 = newclass ['JSON'; 'Compiler']
36 compreg 'data_json', $P2
48 set_root_global ['parrot'; 'data_json'], '$escapes', $P1
52 .namespace ['JSON';'Compiler']
54 .sub 'compile' :method
55 .param string json_string
57 .local pmc parse, match
58 parse = get_root_global ['parrot'; 'JSON'], 'value'
60 $P0 = get_root_global ['parrot'; 'PGE'], 'Match'
61 match = $P0.'new'(json_string)
64 unless match goto failed
66 .local pmc pirgrammar, pirbuilder, pir
67 pirgrammar = new ['JSON'; 'PIR']
68 pirbuilder = pirgrammar.'apply'(match)
69 pir = pirbuilder.'get'('result')
71 .local pmc pirc, result
78 $P0[0] = "Invalid JSON value"
85 .include 'compilers/data_json/data_json/grammar.pir'
86 .include 'compilers/data_json/data_json/pge2pir.pir'
93 # vim: expandtab shiftwidth=4 ft=pir: