001: package org.drools.rule.builder.dialect.java;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025:
026: import org.antlr.runtime.ANTLRStringStream;
027: import org.antlr.runtime.CharStream;
028: import org.antlr.runtime.CommonTokenStream;
029: import org.antlr.runtime.RecognitionException;
030: import org.antlr.runtime.TokenStream;
031: import org.drools.rule.builder.dialect.java.parser.JavaLexer;
032: import org.drools.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr;
033: import org.drools.rule.builder.dialect.java.parser.JavaParser;
034:
035: /**
036: * Expression analyzer.
037: *
038: * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a>
039: */
040: public class JavaExprAnalyzer {
041: // ------------------------------------------------------------
042: // Constructors
043: // ------------------------------------------------------------
044:
045: /**
046: * Construct.
047: */
048: public JavaExprAnalyzer() {
049: // intentionally left blank.
050: }
051:
052: // ------------------------------------------------------------
053: // Instance methods
054: // ------------------------------------------------------------
055:
056: /**
057: * Analyze an expression.
058: *
059: * @param expr
060: * The expression to analyze.
061: * @param availDecls
062: * Total set of declarations available.
063: *
064: * @return The <code>Set</code> of declarations used by the expression.
065: * @throws RecognitionException
066: * If an error occurs in the parser.
067: */
068: public JavaAnalysisResult analyzeExpression(final String expr,
069: final Set[] availableIdentifiers)
070: throws RecognitionException {
071: final CharStream charStream = new ANTLRStringStream(expr);
072: final JavaLexer lexer = new JavaLexer(charStream);
073: final TokenStream tokenStream = new CommonTokenStream(lexer);
074: final JavaParser parser = new JavaParser(tokenStream);
075:
076: parser.conditionalOrExpression();
077:
078: JavaAnalysisResult result = new JavaAnalysisResult();
079: result.setIdentifiers(parser.getIdentifiers());
080: return analyze(result, availableIdentifiers);
081: }
082:
083: public JavaAnalysisResult analyzeBlock(final String expr,
084: final Set[] availableIdentifiers)
085: throws RecognitionException {
086: final CharStream charStream = new ANTLRStringStream("{" + expr
087: + "}");
088: final JavaLexer lexer = new JavaLexer(charStream);
089: final TokenStream tokenStream = new CommonTokenStream(lexer);
090: final JavaParser parser = new JavaParser(tokenStream);
091:
092: parser.block();
093:
094: JavaAnalysisResult result = new JavaAnalysisResult();
095: result.setIdentifiers(parser.getIdentifiers());
096: result.setLocalVariables(new HashMap());
097: for (Iterator it = parser.getLocalDeclarations().iterator(); it
098: .hasNext();) {
099: JavaLocalDeclarationDescr descr = (JavaLocalDeclarationDescr) it
100: .next();
101: for (Iterator identIt = descr.getIdentifiers().iterator(); identIt
102: .hasNext();) {
103: JavaLocalDeclarationDescr.IdentifierDescr ident = (JavaLocalDeclarationDescr.IdentifierDescr) identIt
104: .next();
105: result.addLocalVariable(ident.getIdentifier(), descr);
106: }
107: }
108:
109: return analyze(result, availableIdentifiers);
110: }
111:
112: /**
113: * Analyze an expression.
114: *
115: * @param availDecls
116: * Total set of declarations available.
117: * @param ast
118: * The AST for the expression.
119: *
120: * @return The <code>Set</code> of declarations used by the expression.
121: *
122: * @throws RecognitionException
123: * If an error occurs in the parser.
124: */
125: private JavaAnalysisResult analyze(final JavaAnalysisResult result,
126: final Set[] availableIdentifiers)
127: throws RecognitionException {
128: final List identifiers = result.getIdentifiers();
129: final Set notBound = new HashSet(identifiers);
130: final List[] used = new List[availableIdentifiers.length];
131: for (int i = 0, length = used.length; i < length; i++) {
132: used[i] = new ArrayList();
133: }
134:
135: for (int i = 0, length = availableIdentifiers.length; i < length; i++) {
136: final Set set = availableIdentifiers[i];
137: for (final Iterator it = set.iterator(); it.hasNext();) {
138: final String eachDecl = (String) it.next();
139: if (identifiers.contains(eachDecl)) {
140: used[i].add(eachDecl);
141: notBound.remove(eachDecl);
142: }
143: }
144: }
145: result.setBoundIdentifiers(used);
146: result.setNotBoundedIdentifiers(new ArrayList(notBound));
147:
148: return result;
149: }
150: }
|