--- /dev/null
+/*--------------------------------------------------------------------------
+ * Copyright 2009 Taro L. Saito
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *--------------------------------------------------------------------------*/
+//--------------------------------------
+// XerialJ
+//
+// SilkNode.java
+// Since: Feb 2, 2009 10:41:58 AM
+//
+// $URL$
+// $Author$
+//--------------------------------------
+package org.xerial.silk;
+
+import org.xerial.silk.impl.SilkElement;
+
+/**
+ * Event data of {@link SilkPullParser}
+ *
+ * @author leo
+ *
+ */
+public class SilkEvent
+{
+ private final SilkEventType type;
+ private SilkElement element;
+
+ public SilkEvent(SilkEventType type, SilkElement element)
+ {
+ this.type = type;
+ this.element = element;
+ }
+
+ public SilkEventType getType()
+ {
+ return type;
+ }
+
+ public SilkElement getElement()
+ {
+ return element;
+ }
+
+ @Override
+ public String toString()
+ {
+ if (element == null)
+ return String.format("[%s]", type);
+ else
+ return String.format("[%s] %s", type, element);
+ }
+
+}
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.tree.Tree;
+import org.xerial.core.XerialErrorCode;
import org.xerial.core.XerialException;
+import org.xerial.silk.impl.SilkDataLine;
import org.xerial.silk.impl.SilkElement;
import org.xerial.silk.impl.SilkFunction;
import org.xerial.silk.impl.SilkLexer;
import org.xerial.silk.impl.SilkNode;
import org.xerial.silk.impl.SilkParser;
import org.xerial.silk.impl.SilkParser.silkLine_return;
-import org.xerial.util.ArrayDeque;
-import org.xerial.util.Deque;
import org.xerial.util.bean.impl.BeanUtilImpl;
import org.xerial.util.log.Logger;
public class SilkPullParser
{
private static Logger _logger = Logger.getLogger(SilkPullParser.class);
+ private static final SilkEvent EOFEvent = new SilkEvent(SilkEventType.END_OF_FILE, null);
+ private static final SilkEvent BlankLineEvent = new SilkEvent(SilkEventType.BLANK_LINE, null);
private final SilkLexer lexer;
private CommonTokenStream tokenStream;
private SilkParser parser;
- private Deque<EventItem> eventQueue = new ArrayDeque<EventItem>();
+
+ private boolean foundEOF = false;
/**
* SilkEvents
public boolean hasNext()
{
- return false;
+ return !foundEOF;
}
- private void refill()
+ public SilkEvent next() throws XerialException
{
+ if (foundEOF)
+ return EOFEvent;
- }
-
- public SilkEventType next()
- {
if (tokenStream.LT(1) == Token.EOF_TOKEN)
- return SilkEventType.END_OF_FILE;
+ {
+ foundEOF = true;
+ return EOFEvent;
+ }
+
try
{
silkLine_return ret = parser.silkLine();
case SilkParser.Function:
{
SilkFunction func = BeanUtilImpl.createBeanFromParseTree(SilkFunction.class, t, SilkParser.tokenNames);
- _logger.info(func);
- return SilkEventType.FUNCTION;
+ return new SilkEvent(SilkEventType.FUNCTION, func);
}
case SilkParser.SilkNode:
{
SilkNode node = BeanUtilImpl.createBeanFromParseTree(SilkNode.class, t, SilkParser.tokenNames);
- _logger.info(node);
- return SilkEventType.NODE;
+ return new SilkEvent(SilkEventType.NODE, node);
}
case SilkParser.BlankLine:
{
- return SilkEventType.BLANK_LINE;
+ return BlankLineEvent;
}
case SilkParser.DataLine:
{
- String dataLine = t.getText();
- _logger.info("data line: " + dataLine);
- return SilkEventType.DATA_LINE;
+ SilkDataLine dataLine = new SilkDataLine(t.getText());
+ return new SilkEvent(SilkEventType.DATA_LINE, dataLine);
}
+ default:
+ throw new XerialException(XerialErrorCode.INVALID_INPUT, "invalid data type");
}
}
catch (RecognitionException e)
{
- // TODO Auto-generated catch block
- e.printStackTrace();
+ throw new XerialException(XerialErrorCode.INVALID_INPUT, "parse error: " + e.getMessage());
}
catch (XerialException e)
{
- // TODO Auto-generated catch block
- e.printStackTrace();
+ throw new XerialException(XerialErrorCode.INVALID_INPUT, "parse error: " + e.getMessage());
}
- return null;
}
}
--- /dev/null
+/*--------------------------------------------------------------------------
+ * Copyright 2009 Taro L. Saito
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *--------------------------------------------------------------------------*/
+//--------------------------------------
+// XerialJ
+//
+// SilkWalker.java
+// Since: Feb 2, 2009 11:29:02 AM
+//
+// $URL$
+// $Author$
+//--------------------------------------
+package org.xerial.silk;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+
+import org.xerial.core.XerialException;
+import org.xerial.util.tree.TreeNode;
+import org.xerial.util.tree.TreeVisitor;
+import org.xerial.util.tree.TreeWalker;
+
+/**
+ * {@link TreeWalker} implementation of the Silk format.
+ *
+ * @author leo
+ *
+ */
+public class SilkWalker implements TreeWalker
+{
+ private final SilkPullParser parser;
+
+ public SilkWalker(InputStream input) throws IOException
+ {
+ this.parser = new SilkPullParser(input);
+ }
+
+ public SilkWalker(Reader input) throws IOException
+ {
+ this.parser = new SilkPullParser(input);
+ }
+
+ public TreeNode getSubTree() throws XerialException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void skipDescendants()
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void walk(TreeVisitor visitor) throws XerialException
+ {
+ while (parser.hasNext())
+ {
+ SilkEvent currentEvent = parser.next();
+
+ }
+
+ }
+
+}
--- /dev/null
+/*--------------------------------------------------------------------------
+ * Copyright 2009 Taro L. Saito
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *--------------------------------------------------------------------------*/
+//--------------------------------------
+// XerialJ
+//
+// SilkDataLine.java
+// Since: Feb 2, 2009 11:16:05 AM
+//
+// $URL$
+// $Author$
+//--------------------------------------
+package org.xerial.silk.impl;
+
+/**
+ * dataline
+ *
+ * @author leo
+ *
+ */
+public class SilkDataLine implements SilkElement
+{
+ private final String dataLine;
+
+ public SilkDataLine(String dataLine)
+ {
+ this.dataLine = dataLine;
+ }
+
+ public String getDataLine()
+ {
+ return dataLine;
+ }
+
+ @Override
+ public String toString()
+ {
+ return dataLine;
+ }
+}
{
SilkPullParser p = new SilkPullParser(FileResource.open(SilkPullParserTest.class, "small.silk"));
- SilkEvent e = SilkEvent.UNKNOWN;
- while (e != SilkEvent.END_OF_FILE)
+ while (p.hasNext())
{
- e = p.next();
+ SilkEvent e = p.next();
_logger.info(e);
}
--object\r
+-node1\r
\r
--node:value\r
+-node2:value\r
-p1:hello\r
-p2: long text value spareted by some white spaces.\r
\r