summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/syntax/JavaSyntaxRules.java
blob: 76d38dcdbdae383b58135b54d37f26a4c4122e20 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * JavaSyntaxRules.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.mop.Environment;
import io.devnulllabs.openjava.ptree.Block;
import io.devnulllabs.openjava.ptree.Expression;
import io.devnulllabs.openjava.ptree.ObjectList;
import io.devnulllabs.openjava.ptree.ParseTree;
import io.devnulllabs.openjava.ptree.ParseTreeException;
import io.devnulllabs.openjava.ptree.Statement;
import io.devnulllabs.openjava.ptree.TypeName;
import io.devnulllabs.openjava.ptree.util.MemberAccessCorrector;
import io.devnulllabs.openjava.tools.parser.ParseException;
import io.devnulllabs.openjava.tools.parser.Parser;
import io.devnulllabs.openjava.tools.parser.Token;


/**
 * The class <code>JavaSyntaxRules</code>
 * <p>
 * For example
 * <pre>
 * </pre>
 * <p>
 *
 * @author   Michiaki Tatsubori
 * @version  1.0
 * @since    $Id: JavaSyntaxRules.java,v 1.2 2003/02/19 02:54:31 tatsubori Exp $
 * @see java.lang.Object
 */
public class JavaSyntaxRules implements TokenID
{
    private JavaSyntaxRules() {}

    private static ParseException lastException = null;
    public static SyntaxException getLastException() {
    return new SyntaxException( lastException );
    }

    private static ParseTree correct( ParseTree ptree, Environment env ) {
    MemberAccessCorrector corrector
        = new MemberAccessCorrector( env );
    ObjectList holder = new ObjectList( ptree );
    try {
        ptree.accept( corrector );
    } catch ( ParseTreeException e ) {
        System.err.println( e.getMessage() );
    }
    return (ParseTree) holder.get( 0 );
    }

    private static void
    adjustTokenSource( TokenSource token_src, Token last_token ) {
    Token token = token_src.getToken( 0 );
    while (token != last_token) {
        token = token_src.getNextToken();
    }
    return;
    }

    /**
     * Consumes a single expression from given token source.
     *
     * @param token_src token source
     * @param env environment
     * @return  null if this fail, otherwise an expression.
     */
    public static final Expression
    consumeExpression( TokenSource token_src, Environment env ) {
    if (env == null)  env = token_src.getEnvironment();
    RestorableTokenSource dummy = new RestorableTokenSource( token_src );
    Parser parser = new Parser( dummy );
    try {
        Expression ptree = parser.Expression( env );
        adjustTokenSource( token_src, parser.getToken( 0 ) );
        return (Expression) correct( ptree, env );
    } catch ( ParseException e ) {
        lastException = e;
        return null;
    }
    }
    public static final Expression
    consumeExpression( TokenSource token_src ) {
    return consumeExpression(  token_src, null );
    }

    /**
     * Consumes a statement.
     * This consumes only a pure statement excluding local variable
     * declarations and local class declarations.
     *
     * @param token_src token source
     * @return  null if this fail, otherwise a statement.
     */
    public static final Statement
    consumeStatement( TokenSource token_src, Environment env ) {
    if (env == null)  env = token_src.getEnvironment();
    RestorableTokenSource dummy = new RestorableTokenSource( token_src );
    Parser parser = new Parser( dummy );
    try {
        Statement ptree = parser.Statement( env );
        adjustTokenSource( token_src, parser.getToken( 0 ) );
        return (Statement) correct( ptree, env );
    } catch ( ParseException e ) {
        lastException = e;
        return null;
    }
    }
    public static final Statement consumeStatement( TokenSource token_src ) {
    return consumeStatement(  token_src, token_src.getEnvironment() );
    }

    /**
     * Consumes a block.
     *
     * @param token_src token source
     * @return  null if this fail, otherwise a block.
     */
    public static final Block
    consumeBlock( TokenSource token_src, Environment env ) {
    if (env == null)  env = token_src.getEnvironment();
    RestorableTokenSource dummy = new RestorableTokenSource( token_src );
    Parser parser = new Parser( dummy );
    try {
        Block ptree = parser.Block( env );
        adjustTokenSource( token_src, parser.getToken( 0 ) );
        return (Block) correct( ptree, env );
    } catch ( ParseException e ) {
        lastException = e;
        return null;
    }
    }
    public static final Block consumeBlock( TokenSource token_src ) {
    return consumeBlock(  token_src, token_src.getEnvironment() );
    }

    /**
     * Consumes a type name.
     * Including primitive type and reference type.
     *
     * @param token_src token source
     * @return  null if this fail, otherwise a block.
     */
    public static final TypeName
    consumeTypeName( TokenSource token_src ) {
    Environment env = token_src.getEnvironment();
    RestorableTokenSource dummy = new RestorableTokenSource( token_src );
    Parser parser = new Parser( dummy );
    try {
        TypeName ptree = parser.Type( env );
        adjustTokenSource( token_src, parser.getToken( 0 ) );
        return (TypeName) correct( ptree, env );
    } catch ( ParseException e ) {
        lastException = e;
        return null;
    }
    }

}