Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: openjava/syntax/SeparatedListRule.java


1   /*
2    * SeparatedListRule.java
3    *
4    * comments here.
5    *
6    * @author   Michiaki Tatsubori
7    * @version  %VERSION% %DATE%
8    * @see      java.lang.Object
9    *
10   * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED.
11   */
12  package openjava.syntax;
13  
14  import openjava.ptree.ParseTree;
15  
16  /**
17   * The class <code>SeparatedListRule</code> represents the syntax
18   * rule of a list separated by an separator.
19   * <p>
20   * Suppose there's a syntax rule A and token t.  This class can
21   * represents the syntax A ( t A )*.
22   * <p>
23   *
24   * @author   Michiaki Tatsubori
25   * @version  1.0
26   * @since    $Id: SeparatedListRule.java,v 1.2 2003/02/19 02:54:32 tatsubori Exp $
27   * @see java.lang.Object
28   */
29  public abstract class SeparatedListRule extends AbstractSyntaxRule {
30    private SyntaxRule elementRule;
31    private int separator;
32    private boolean allowsEmpty;
33  
34    protected abstract void initList();
35    protected abstract void addListElement(Object elem);
36    protected abstract ParseTree getList();
37  
38    /**
39     * Allocates a new rule representing a list of a give rule
40     * separeted by a given separator.
41     *
42     * @param elementRule a rule of each element of the list
43     * @param separator_token  the id of a token to be separator
44     * @param allowEmpty a flag to allow 0 iteration if it is true.
45     * @see openjava.syntax.TokenID
46     */
47    public SeparatedListRule(
48      SyntaxRule elementRule,
49      int separator_token,
50      boolean allowsEmpty) {
51      this.elementRule = elementRule;
52      this.separator = separator_token;
53      this.allowsEmpty = allowsEmpty;
54    }
55  
56    /**
57     * Allocates a new rule representing a list of a give rule
58     * separeted by a given separator.
59     *
60     * @param elementRule a rule of each element of the list
61     * @param separator_token  the id of a token to be separator
62     * @see openjava.syntax.TokenID
63     */
64    public SeparatedListRule(SyntaxRule elementRule, int separator_token) {
65      this(elementRule, separator_token, false);
66    }
67  
68    /**
69     * Consumes token source.
70     *
71     * @param token_src  token source.
72     * @return  null if this fails to consume a syntax tree represented
73     * by this object.  Otherwise it returns <code>ObjectList</code> object.
74     */
75    public final ParseTree consume(TokenSource token_src)
76      throws SyntaxException {
77      initList();
78      ParseTree elem;
79      if (!allowsEmpty) {
80        elem = elementRule.consume(token_src);
81        addListElement(elem);
82      }
83      CompositeRule spy =
84        new CompositeRule(new TokenRule(separator), elementRule);
85      while (spy.lookahead(token_src)) {
86        elem = consumeSepAndElem(token_src);
87        addListElement(elem);
88      }
89      return getList();
90    }
91  
92    private ParseTree consumeSepAndElem(TokenSource token_src)
93      throws SyntaxException {
94      token_src.getNextToken(); /* separator */
95      return elementRule.consume(token_src);
96    }
97  
98  }