source: tags/MetisMQIDemo/src/main/java/weka/core/json/Parser.cup

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 8.8 KB
Line 
1/*
2 * STANDARD ML OF NEW JERSEY COPYRIGHT NOTICE, LICENSE AND DISCLAIMER.
3 *
4 * Copyright (c) 1989-1998 by Lucent Technologies
5 *
6 * Permission to use, copy, modify, and distribute this software and its
7 * documentation for any purpose and without fee is hereby granted, provided
8 * that the above copyright notice appear in all copies and that both the
9 * copyright notice and this permission notice and warranty disclaimer appear
10 * in supporting documentation, and that the name of Lucent Technologies, Bell
11 * Labs or any Lucent entity not be used in advertising or publicity pertaining
12 * to distribution of the software without specific, written prior permission.
13 *
14 * Lucent disclaims all warranties with regard to this software, including all
15 * implied warranties of merchantability and fitness. In no event shall Lucent
16 * be liable for any special, indirect or consequential damages or any damages
17 * whatsoever resulting from loss of use, data or profits, whether in an action
18 * of contract, negligence or other tortious action, arising out of or in
19 * connection with the use or performance of this software.
20 *
21 * Taken from this URL:
22 * http://www.smlnj.org/license.html
23 *
24 * This license is compatible with the GNU GPL (see section "Standard ML of New
25 * Jersey Copyright License"):
26 * http://www.gnu.org/licenses/license-list.html#StandardMLofNJ
27 */
28
29/*
30 * Copyright 1996-1999 by Scott Hudson, Frank Flannery, C. Scott Ananian
31 */
32
33package weka.core.json;
34
35import java_cup.runtime.*;
36
37import java.io.*;
38import java.util.*;
39
40/**
41 * A parser for parsing JSON files.
42 *
43 * @author FracPete (fracpete at waikato dot ac dot nz)
44 * @version $Revision: 5785 $
45 */
46
47parser code {:
48  /** variable - value relation. */
49  protected HashMap m_Symbols;
50
51  /** for storing the parsed JSON data structure. */
52  protected JSONNode m_Result;
53 
54  /** the stack for keeping track of the current parent node. */
55  protected Stack<JSONNode> m_Stack;
56
57  /**
58   * Returns the JSON data structure.
59   *
60   * @return the result
61   */
62  public JSONNode getResult() {
63    return m_Result;
64  }
65 
66  /**
67   * Returns the stack used internally for keeping track of the current
68   * parent node.
69   *
70   * @return the stack
71   */
72  protected Stack<JSONNode> getStack() {
73    return m_Stack;
74  }
75
76  /**
77   * Runs the parser from commandline. Expects a filename as first parameter,
78   * pointing to a JSON file.
79   *
80   * @param args the commandline arguments
81   * @throws Exception if something goes wrong
82   */
83  public static void main(String args[]) throws Exception {
84    if (args.length != 1) {
85      System.err.println("No JSON file specified!");
86      System.exit(1);
87    }
88   
89    FileInputStream stream = new FileInputStream(args[0]);
90    SymbolFactory sf = new DefaultSymbolFactory();
91    Parser parser = new Parser(new Scanner(stream, sf), sf);
92    parser.parse();
93    StringBuffer buffer = new StringBuffer();
94    parser.getResult().toString(buffer);
95    System.out.println(buffer.toString());
96  }
97:}
98
99init with {:
100  m_Symbols = new HashMap();
101  m_Result  = new JSONNode();
102  m_Stack   = new Stack<JSONNode>();
103  m_Stack.push(m_Result);
104:}
105
106terminal COMMA;
107terminal LSQUARE;
108terminal RSQUARE;
109terminal LCURLY;
110terminal RCURLY;
111terminal COLON;
112terminal NULL;
113terminal Boolean BOOLEAN;
114terminal Integer INTEGER;
115terminal Double DOUBLE;
116terminal String STRING;
117
118non terminal json;
119non terminal pairs;
120non terminal pair;
121non terminal primitive;
122non terminal null;
123non terminal Boolean boolean;
124non terminal Integer integer;
125non terminal Double double;
126non terminal String string;
127non terminal anon_object;
128non terminal named_object;
129non terminal named_object_start;
130non terminal anon_object_start;
131non terminal object_content;
132non terminal object_end;
133non terminal anon_array;
134non terminal named_array;
135non terminal named_array_start;
136non terminal anon_array_start;
137non terminal array_content;
138non terminal array_end;
139non terminal elements;
140non terminal element;
141
142json               ::=   LCURLY RCURLY
143                       | LCURLY pairs RCURLY
144                       ;
145
146pairs              ::=   pairs COMMA pair
147                       | pair
148                       ;
149
150pair               ::=   primitive
151                       | named_object
152                       | named_array
153                       ;
154
155primitive          ::=   null
156                       | boolean
157                       | integer
158                       | double
159                       | string
160                       ;
161                   
162null               ::= STRING:name COLON NULL
163                       {:
164                          parser.getStack().peek().addNull(name);
165                       :}
166                       ;
167                   
168boolean            ::= STRING:name COLON BOOLEAN:b
169                       {:
170                          parser.getStack().peek().addPrimitive(name, b);
171                       :}
172                       ;
173                   
174integer            ::= STRING:name COLON INTEGER:i
175                       {:
176                          parser.getStack().peek().addPrimitive(name, i);
177                       :}
178                       ;
179                   
180double             ::= STRING:name COLON DOUBLE:d
181                       {:
182                          parser.getStack().peek().addPrimitive(name, d);
183                       :}
184                       ;
185                   
186string             ::= STRING:name COLON STRING:s
187                       {:
188                          parser.getStack().peek().addPrimitive(name, s);
189                       :}
190                       ;
191                   
192named_object       ::=   named_object_start object_end
193                       | named_object_start object_content object_end
194                       ;
195                   
196named_object_start ::= STRING:name COLON LCURLY
197                       {:
198                          JSONNode node = parser.getStack().peek().addObject(name);
199                          parser.getStack().push(node);
200                       :}
201                       ;
202                   
203anon_object        ::=   anon_object_start object_end
204                       | anon_object_start object_content object_end
205                       ;
206                   
207anon_object_start  ::= LCURLY
208                       {:
209                          JSONNode node = parser.getStack().peek().addObject(null);
210                          parser.getStack().push(node);
211                       :}
212                       ;
213                   
214object_content     ::= pairs
215                       ;
216                   
217object_end         ::= RCURLY
218                       {:
219                          parser.getStack().pop();
220                       :}
221                       ;
222                   
223named_array        ::=   named_array_start array_end
224                       | named_array_start array_content array_end
225                       ;
226                   
227named_array_start  ::= STRING:name COLON LSQUARE
228                       {:
229                          JSONNode node = parser.getStack().peek().addArray(name);
230                          parser.getStack().push(node);
231                       :}
232                       ;
233                   
234anon_array         ::=   anon_array_start array_end
235                       | anon_array_start array_content array_end
236                       ;
237                   
238anon_array_start   ::= LSQUARE
239                       {:
240                          JSONNode node = parser.getStack().peek().addArray(null);
241                          parser.getStack().push(node);
242                       :}
243                       ;
244                   
245array_content      ::= elements
246                       ;
247                   
248array_end          ::= RSQUARE
249                       {:
250                          parser.getStack().pop();
251                       :}
252                       ;
253                   
254elements           ::=   elements COMMA element
255                       | element
256                       ;
257                   
258element            ::=   NULL
259                         {:
260                            parser.getStack().peek().addArrayElement(null);
261                         :}
262                       | BOOLEAN:b
263                         {:
264                            parser.getStack().peek().addArrayElement(b);
265                         :}
266                       | INTEGER:i
267                         {:
268                            parser.getStack().peek().addArrayElement(i);
269                         :}
270                       | DOUBLE:d
271                         {:
272                            parser.getStack().peek().addArrayElement(d);
273                         :}
274                       | STRING:s
275                         {:
276                            parser.getStack().peek().addArrayElement(s);
277                         :}
278                       | anon_object
279                       | anon_array
280                       ;
Note: See TracBrowser for help on using the repository browser.