1 /*--------------------------------------------------------------------------
2 * Copyright 2009 Taro L. Saito
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 //--------------------------------------
19 // SilkPullParser.java
20 // Since: Jan 28, 2009 1:00:02 PM
24 //--------------------------------------
25 package org.xerial.silk;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.Reader;
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;
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.
53 public class SilkPullParser
55 private static Logger _logger = Logger.getLogger(SilkPullParser.class);
57 private final SilkLexer lexer;
58 private CommonTokenStream tokenStream;
59 private SilkParser parser;
61 public SilkPullParser(InputStream input) throws IOException
63 lexer = new SilkLexer(new ANTLRInputStream(input));
67 public SilkPullParser(Reader input) throws IOException
69 lexer = new SilkLexer(new ANTLRReaderStream(input));
75 tokenStream = new CommonTokenStream(lexer);
76 parser = new SilkParser(tokenStream);
79 public SilkEvent next()
81 if (tokenStream.LT(1) == Token.EOF_TOKEN)
82 return SilkEvent.END_OF_FILE;
85 silkLine_return ret = parser.silkLine();
86 Tree t = (Tree) ret.getTree();
89 case SilkParser.Function:
91 SilkFunction func = BeanUtilImpl.createBeanFromParseTree(SilkFunction.class, t, SilkParser.tokenNames);
92 return SilkEvent.FUNCTION;
94 case SilkParser.SilkNode:
96 SilkNode node = BeanUtilImpl.createBeanFromParseTree(SilkNode.class, t, SilkParser.tokenNames);
97 return SilkEvent.NODE;
99 case SilkParser.BlankLine:
101 return SilkEvent.BLANK_LINE;
103 case SilkParser.DataLine:
105 String dataLine = t.getText();
106 return SilkEvent.DATA_LINE;
111 catch (RecognitionException e)
113 // TODO Auto-generated catch block
116 catch (XerialException e)
118 // TODO Auto-generated catch block