OSDN Git Service

da2b38551bcde158877383c2afd875dfce0e09a8
[xerial/xerial-core.git] / src / main / java / org / xerial / silk / SilkPullParser.java
1 /*--------------------------------------------------------------------------
2  *  Copyright 2009 Taro L. Saito
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *--------------------------------------------------------------------------*/
16 //--------------------------------------
17 // XerialJ
18 //
19 // SilkPullParser.java
20 // Since: Jan 28, 2009 1:00:02 PM
21 //
22 // $URL$
23 // $Author$
24 //--------------------------------------
25 package org.xerial.silk;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.Reader;
30
31 import org.antlr.runtime.ANTLRInputStream;
32 import org.antlr.runtime.ANTLRReaderStream;
33 import org.antlr.runtime.CommonTokenStream;
34 import org.antlr.runtime.RecognitionException;
35 import org.antlr.runtime.Token;
36 import org.antlr.runtime.tree.Tree;
37 import org.xerial.core.XerialException;
38 import org.xerial.silk.impl.SilkFunction;
39 import org.xerial.silk.impl.SilkLexer;
40 import org.xerial.silk.impl.SilkNode;
41 import org.xerial.silk.impl.SilkParser;
42 import org.xerial.silk.impl.SilkParser.silkLine_return;
43 import org.xerial.util.bean.impl.BeanUtilImpl;
44 import org.xerial.util.log.Logger;
45
46 /**
47  * Pull parser of the Silk format. Pull-style means each parsing event is
48  * generated when next() method is called, suited to stream processing.
49  * 
50  * @author leo
51  * 
52  */
53 public class SilkPullParser
54 {
55     private static Logger _logger = Logger.getLogger(SilkPullParser.class);
56
57     private final SilkLexer lexer;
58     private CommonTokenStream tokenStream;
59     private SilkParser parser;
60
61     public SilkPullParser(InputStream input) throws IOException
62     {
63         lexer = new SilkLexer(new ANTLRInputStream(input));
64         init();
65     }
66
67     public SilkPullParser(Reader input) throws IOException
68     {
69         lexer = new SilkLexer(new ANTLRReaderStream(input));
70         init();
71     }
72
73     private void init()
74     {
75         tokenStream = new CommonTokenStream(lexer);
76         parser = new SilkParser(tokenStream);
77     }
78
79     public SilkEvent next()
80     {
81         if (tokenStream.LT(1) == Token.EOF_TOKEN)
82             return SilkEvent.END_OF_FILE;
83         try
84         {
85             silkLine_return ret = parser.silkLine();
86             Tree t = (Tree) ret.getTree();
87             switch (t.getType())
88             {
89             case SilkParser.Function:
90             {
91                 SilkFunction func = BeanUtilImpl.createBeanFromParseTree(SilkFunction.class, t, SilkParser.tokenNames);
92                 return SilkEvent.FUNCTION;
93             }
94             case SilkParser.SilkNode:
95             {
96                 SilkNode node = BeanUtilImpl.createBeanFromParseTree(SilkNode.class, t, SilkParser.tokenNames);
97                 return SilkEvent.NODE;
98             }
99             case SilkParser.BlankLine:
100             {
101                 return SilkEvent.BLANK_LINE;
102             }
103             case SilkParser.DataLine:
104             {
105                 String dataLine = t.getText();
106                 return SilkEvent.DATA_LINE;
107             }
108             }
109
110         }
111         catch (RecognitionException e)
112         {
113             // TODO Auto-generated catch block
114             e.printStackTrace();
115         }
116         catch (XerialException e)
117         {
118             // TODO Auto-generated catch block
119             e.printStackTrace();
120         }
121         return null;
122     }
123
124 }