diff options
Diffstat (limited to 'src/main/java/io/devnulllabs/openjava/syntax/AbstractSyntaxRule.java')
-rw-r--r-- | src/main/java/io/devnulllabs/openjava/syntax/AbstractSyntaxRule.java | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/main/java/io/devnulllabs/openjava/syntax/AbstractSyntaxRule.java b/src/main/java/io/devnulllabs/openjava/syntax/AbstractSyntaxRule.java new file mode 100644 index 0000000..33c6029 --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/syntax/AbstractSyntaxRule.java @@ -0,0 +1,80 @@ +/* + * AbstractSyntaxRule.java + * + * comments here. + * + * @author Michiaki Tatsubori + * @version %VERSION% %DATE% + * @see java.lang.Object + * + * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED. + */ +package io.devnulllabs.openjava.syntax; + + +import io.devnulllabs.openjava.ptree.ParseTree; + + +/** + * The interface <code>AbstractSyntaxRule</code> represents a syntax rule. + * <p> + * + * @author Michiaki Tatsubori + * @version 1.0 + * @since $Id: AbstractSyntaxRule.java,v 1.2 2003/02/19 02:54:31 tatsubori Exp $ + * @see java.lang.Object + */ +public abstract class AbstractSyntaxRule implements SyntaxRule +{ + /** + * Consumes tokens from the given token source following + * the rule. To be overridden. + * + * @param token_src token source to consume. + * @return a parse tree object consumed by following this rule. + */ + public abstract ParseTree consume( TokenSource token_src ) + throws SyntaxException; + + /** + * Tests if the given token source follows this rule. + * + * @param token_src token source to consume. + * @return true if the given token source can be consumed safely. + */ + public final boolean lookahead( TokenSource token_src ) { + try { + RestorableTokenSource dummy + = new RestorableTokenSource( token_src ); + consume( dummy ); + return true; + } catch (SyntaxException e) { + setSyntaxException( e ); + return false; + } + } + + private SyntaxException lastException = null; + + /** + * Obtains the syntax exception at the last lookahead. + * through the method <tt>lookahead(TokenSource)</tt>. + * + * @return the syntax exception. + */ + public final SyntaxException getSyntaxException() { + return lastException; + } + + /** + * Sets the last syntax exception in consuming token source + * through the method <tt>consume(TokenSource)</tt>. + * + * @return the syntax exception. + */ + private final void setSyntaxException( SyntaxException e ) { + lastException = e; + } + +} + |