summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/syntax/AbstractSyntaxRule.java
blob: 33c602938e61b32f58aa835033d50686fcf98657 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
    }

}