summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/syntax/NameRule.java
blob: 5d7ffd0c32bf49662133180f4e21d9da57ac9f56 (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
/*
 * NameRule.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;
import io.devnulllabs.openjava.ptree.Variable;
import io.devnulllabs.openjava.tools.parser.Token;


/**
 * The interface <code>NameRule</code>
 * <p>
 * For example
 * <pre>
 * </pre>
 * <p>
 *
 * @author   Michiaki Tatsubori
 * @version  1.0
 * @since    $Id: NameRule.java,v 1.2 2003/02/19 02:54:31 tatsubori Exp $
 * @see java.lang.Object
 */
public class NameRule extends AbstractSyntaxRule
{
    /**
     * Consumes a dot-separated name like <tt>java.lang.String</tt>.
     *
     * @param  token_src  token source
     * @return  a dummy <code>Variable</code> object.
     * @see io.devnulllabs.openjava.ptree.Variable
     */
    public final ParseTree consume( TokenSource token_src )
    throws SyntaxException
    {
    return consumeQualifiedName( token_src );
    }

    /**
     * To override for modifying rule.
     */
    public Variable consumeQualifiedName( TokenSource token_src )
    throws SyntaxException
    {
    IdentifierRule rule = new IdentifierRule();
    Variable ident = rule.consumeIdentifier( token_src );
    StringBuffer buf = new StringBuffer( ident.toString() );
    while (lookaheadRest( token_src )) {
        buf.append( token_src.getNextToken().image ); /* DOT */
        buf.append( token_src.getNextToken().image ); /* IDENTIFIER */
    }
    return new Variable( buf.toString() );
    }

    /**
     * A hard-coded lookahead for performance reason.
     *   (. <IDENTIFIER>)
     */
    protected static final boolean lookaheadRest( TokenSource token_src ) {
    Token t1 = token_src.getToken( 1 );
    Token t2 = token_src.getToken( 2 );
    return (t1.kind == DOT && t2.kind == IDENTIFIER);
    }

}