summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/syntax/SeparatedListRule.java
blob: 3ff5cc9ec68cd946c3cfe92ba3ff006308bbf22b (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * SeparatedListRule.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 class <code>SeparatedListRule</code> represents the syntax
 * rule of a list separated by an separator.
 * <p>
 * Suppose there's a syntax rule A and token t.  This class can
 * represents the syntax A ( t A )*.
 * <p>
 *
 * @author   Michiaki Tatsubori
 * @version  1.0
 * @since    $Id: SeparatedListRule.java,v 1.2 2003/02/19 02:54:32 tatsubori Exp $
 * @see java.lang.Object
 */
public abstract class SeparatedListRule extends AbstractSyntaxRule {
    private SyntaxRule elementRule;
    private int separator;
    private boolean allowsEmpty;

    protected abstract void initList();
    protected abstract void addListElement(Object elem);
    protected abstract ParseTree getList();

    /**
     * Allocates a new rule representing a list of a give rule
     * separeted by a given separator.
     *
     * @param elementRule a rule of each element of the list
     * @param separator_token  the id of a token to be separator
     * @param allowEmpty a flag to allow 0 iteration if it is true.
     * @see io.devnulllabs.openjava.syntax.TokenID
     */
    public SeparatedListRule(
        SyntaxRule elementRule,
        int separator_token,
        boolean allowsEmpty) {
        this.elementRule = elementRule;
        this.separator = separator_token;
        this.allowsEmpty = allowsEmpty;
    }

    /**
     * Allocates a new rule representing a list of a give rule
     * separeted by a given separator.
     *
     * @param elementRule a rule of each element of the list
     * @param separator_token  the id of a token to be separator
     * @see io.devnulllabs.openjava.syntax.TokenID
     */
    public SeparatedListRule(SyntaxRule elementRule, int separator_token) {
        this(elementRule, separator_token, false);
    }

    /**
     * Consumes token source.
     *
     * @param token_src  token source.
     * @return  null if this fails to consume a syntax tree represented
     * by this object.  Otherwise it returns <code>ObjectList</code> object.
     */
    public final ParseTree consume(TokenSource token_src)
        throws SyntaxException {
        initList();
        ParseTree elem;
        if (!allowsEmpty) {
            elem = elementRule.consume(token_src);
            addListElement(elem);
        }
        CompositeRule spy =
            new CompositeRule(new TokenRule(separator), elementRule);
        while (spy.lookahead(token_src)) {
            elem = consumeSepAndElem(token_src);
            addListElement(elem);
        }
        return getList();
    }

    private ParseTree consumeSepAndElem(TokenSource token_src)
        throws SyntaxException {
        token_src.getNextToken(); /* separator */
        return elementRule.consume(token_src);
    }

}