From ba838c8b0e9967db229e091ae9bd408770af4fd7 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 2 Feb 2009 02:38:45 +0000 Subject: [PATCH] pull parsing done git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@2926 ae02f08e-27ec-0310-ae8c-8ba02fe2eafd --- src/main/java/org/xerial/silk/SilkEvent.java | 65 ++++++++++++++++++ src/main/java/org/xerial/silk/SilkPullParser.java | 47 ++++++------- src/main/java/org/xerial/silk/SilkWalker.java | 78 ++++++++++++++++++++++ .../java/org/xerial/silk/impl/SilkDataLine.java | 52 +++++++++++++++ .../java/org/xerial/silk/SilkPullParserTest.java | 5 +- src/test/java/org/xerial/silk/small.silk | 4 +- 6 files changed, 223 insertions(+), 28 deletions(-) create mode 100644 src/main/java/org/xerial/silk/SilkEvent.java create mode 100644 src/main/java/org/xerial/silk/SilkWalker.java create mode 100644 src/main/java/org/xerial/silk/impl/SilkDataLine.java diff --git a/src/main/java/org/xerial/silk/SilkEvent.java b/src/main/java/org/xerial/silk/SilkEvent.java new file mode 100644 index 0000000..48f93a6 --- /dev/null +++ b/src/main/java/org/xerial/silk/SilkEvent.java @@ -0,0 +1,65 @@ +/*-------------------------------------------------------------------------- + * 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); + } + +} diff --git a/src/main/java/org/xerial/silk/SilkPullParser.java b/src/main/java/org/xerial/silk/SilkPullParser.java index 381c241..61c73f3 100644 --- a/src/main/java/org/xerial/silk/SilkPullParser.java +++ b/src/main/java/org/xerial/silk/SilkPullParser.java @@ -34,15 +34,15 @@ import org.antlr.runtime.CommonTokenStream; 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; @@ -56,11 +56,14 @@ 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 eventQueue = new ArrayDeque(); + + private boolean foundEOF = false; /** * SilkEvents @@ -106,18 +109,20 @@ public class SilkPullParser 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(); @@ -127,39 +132,35 @@ public class SilkPullParser 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; } } diff --git a/src/main/java/org/xerial/silk/SilkWalker.java b/src/main/java/org/xerial/silk/SilkWalker.java new file mode 100644 index 0000000..675a18f --- /dev/null +++ b/src/main/java/org/xerial/silk/SilkWalker.java @@ -0,0 +1,78 @@ +/*-------------------------------------------------------------------------- + * 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(); + + } + + } + +} diff --git a/src/main/java/org/xerial/silk/impl/SilkDataLine.java b/src/main/java/org/xerial/silk/impl/SilkDataLine.java new file mode 100644 index 0000000..b39afcf --- /dev/null +++ b/src/main/java/org/xerial/silk/impl/SilkDataLine.java @@ -0,0 +1,52 @@ +/*-------------------------------------------------------------------------- + * 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; + } +} diff --git a/src/test/java/org/xerial/silk/SilkPullParserTest.java b/src/test/java/org/xerial/silk/SilkPullParserTest.java index 344a018..b797d05 100644 --- a/src/test/java/org/xerial/silk/SilkPullParserTest.java +++ b/src/test/java/org/xerial/silk/SilkPullParserTest.java @@ -47,10 +47,9 @@ public class SilkPullParserTest { 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); } diff --git a/src/test/java/org/xerial/silk/small.silk b/src/test/java/org/xerial/silk/small.silk index 933f4b1..f63abd5 100644 --- a/src/test/java/org/xerial/silk/small.silk +++ b/src/test/java/org/xerial/silk/small.silk @@ -1,6 +1,6 @@ --object +-node1 --node:value +-node2:value -p1:hello -p2: long text value spareted by some white spaces. -- 2.11.0