| 1 | /* |
|---|
| 2 | * This program is free software; you can redistribute it and/or modify |
|---|
| 3 | * it under the terms of the GNU General Public License as published by |
|---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 5 | * (at your option) any later version. |
|---|
| 6 | * |
|---|
| 7 | * This program is distributed in the hope that it will be useful, |
|---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 10 | * GNU General Public License for more details. |
|---|
| 11 | * |
|---|
| 12 | * You should have received a copy of the GNU General Public License |
|---|
| 13 | * along with this program; if not, write to the Free Software |
|---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | * Scanner.java |
|---|
| 19 | * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | package weka.core.json; |
|---|
| 23 | |
|---|
| 24 | import java_cup.runtime.SymbolFactory; |
|---|
| 25 | import java.io.*; |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * A scanner for JSON data files. |
|---|
| 29 | * |
|---|
| 30 | * @author FracPete (fracpete at waikato dot ac dot nz) |
|---|
| 31 | * @version $Revision: 5786 $ |
|---|
| 32 | */ |
|---|
| 33 | %% |
|---|
| 34 | %cup |
|---|
| 35 | %public |
|---|
| 36 | %class Scanner |
|---|
| 37 | %{ |
|---|
| 38 | // Author: FracPete (fracpete at waikato dot ac dot nz) |
|---|
| 39 | // Version: $Revision: 5786 $ |
|---|
| 40 | protected SymbolFactory m_SF; |
|---|
| 41 | |
|---|
| 42 | protected StringBuffer m_String = new StringBuffer(); |
|---|
| 43 | |
|---|
| 44 | public Scanner(InputStream r, SymbolFactory sf) { |
|---|
| 45 | this(r); |
|---|
| 46 | m_SF = sf; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | public Scanner(Reader r, SymbolFactory sf) { |
|---|
| 50 | this(r); |
|---|
| 51 | m_SF = sf; |
|---|
| 52 | } |
|---|
| 53 | %} |
|---|
| 54 | %eofval{ |
|---|
| 55 | return m_SF.newSymbol("EOF", sym.EOF); |
|---|
| 56 | %eofval} |
|---|
| 57 | |
|---|
| 58 | %state STRING |
|---|
| 59 | |
|---|
| 60 | %% |
|---|
| 61 | <YYINITIAL> "{" { return m_SF.newSymbol("Left curly bracket", sym.LCURLY); } |
|---|
| 62 | <YYINITIAL> "}" { return m_SF.newSymbol("Right curly bracket", sym.RCURLY); } |
|---|
| 63 | |
|---|
| 64 | <YYINITIAL> { |
|---|
| 65 | "[" { return m_SF.newSymbol("Left square bracket", sym.LSQUARE); } |
|---|
| 66 | "]" { return m_SF.newSymbol("Right square bracket", sym.RSQUARE); } |
|---|
| 67 | "," { return m_SF.newSymbol("Comma", sym.COMMA); } |
|---|
| 68 | ":" { return m_SF.newSymbol("Colon", sym.COLON); } |
|---|
| 69 | "null" { return m_SF.newSymbol("Null", sym.NULL); } |
|---|
| 70 | "true" { return m_SF.newSymbol("Boolean", sym.BOOLEAN, new Boolean(yytext())); } |
|---|
| 71 | "false" { return m_SF.newSymbol("Boolean", sym.BOOLEAN, new Boolean(yytext())); } |
|---|
| 72 | [0-9][0-9]* { return m_SF.newSymbol("Integer", sym.INTEGER, new Integer(yytext())); } |
|---|
| 73 | [0-9][0-9]*\.?[0-9]* { return m_SF.newSymbol("Double", sym.DOUBLE, new Double(yytext())); } |
|---|
| 74 | -[0-9][0-9]*\.?[0-9]* { return m_SF.newSymbol("Double", sym.DOUBLE, new Double(yytext())); } |
|---|
| 75 | \" { m_String.setLength(0); yybegin(STRING); } |
|---|
| 76 | [ \r\n\t\f] { /* ignore white space. */ } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | <STRING> { |
|---|
| 80 | \" { yybegin(YYINITIAL); return m_SF.newSymbol("String", sym.STRING, m_String.toString()); } |
|---|
| 81 | [^\n\r\"\\]+ { m_String.append(yytext()); } |
|---|
| 82 | \\\" { m_String.append('\"'); } |
|---|
| 83 | \\b { m_String.append('\b'); } |
|---|
| 84 | \\f { m_String.append('\f'); } |
|---|
| 85 | \\n { m_String.append('\n'); } |
|---|
| 86 | \\r { m_String.append('\r'); } |
|---|
| 87 | \\t { m_String.append('\t'); } |
|---|
| 88 | \\ { m_String.append('\\'); } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | // catch all |
|---|
| 92 | . { System.err.println("Illegal character: " + yytext()); } |
|---|