001: /*
002: * FindBugs - Find bugs in Java programs
003: * Copyright (C) 2004 University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs.tools.patcomp;
021:
022: import java.io.IOException;
023:
024: /**
025: * From a parsed FindBugs Pattern (.fbp) file,
026: * generate Java source code for a bug pattern detector.
027: *
028: * @author David Hovemeyer
029: */
030: public class CodeGen implements PatternCompilerTreeConstants {
031: private CodeEmitter emitter;
032:
033: public CodeGen() {
034: }
035:
036: public void generate(SimpleNode root, CodeEmitter emitter)
037: throws IOException {
038: this .emitter = emitter;
039: visit(root);
040: emitter.finish();
041: }
042:
043: private void visit(SimpleNode node) throws IOException {
044: switch (node.getId()) {
045: case JJTPRESCREEN:
046: generatePrescreen(node);
047: break;
048: default:
049: generateDefault(node);
050: break;
051: }
052: }
053:
054: public void generateDefault(SimpleNode node) throws IOException {
055: // System.out.println("START " + node.toString() +
056: // ": first=" + node.getFirstToken() + ", last=" + node.getLastToken());
057:
058: int numChildren = node.jjtGetNumChildren();
059:
060: SimpleNode child = null;
061: Token t;
062:
063: // For each child, emit tokens preceeding that
064: // child not covered by previous children,
065: // then visit the child.
066: int childNum = 0;
067: while (childNum < numChildren) {
068: SimpleNode nextChild = (SimpleNode) node
069: .jjtGetChild(childNum);
070:
071: if (child == null)
072: t = node.getFirstToken();
073: else if (child.getLastToken() != nextChild.getFirstToken())
074: t = child.getLastToken().next;
075: else
076: t = nextChild.getFirstToken();
077:
078: while (t != nextChild.getFirstToken()) {
079: emitter.emitToken(t.image);
080: t = t.next;
081: }
082:
083: child = nextChild;
084:
085: visit(child);
086:
087: ++childNum;
088: }
089:
090: // Emit rest of tokens
091: if (child == null)
092: t = node.getFirstToken();
093: else if (child.getLastToken() != node.getLastToken())
094: t = child.getLastToken().next;
095: else
096: t = null;
097:
098: while (t != null) {
099: emitter.emitToken(t.image);
100: if (t == node.getLastToken())
101: break;
102: t = t.next;
103: }
104:
105: // System.out.println("FINISH " + node.toString() +
106: // ": first=" + node.getFirstToken() + ", last=" + node.getLastToken());
107: }
108:
109: public void generatePrescreen(SimpleNode node) throws IOException {
110: System.out.println("Generating prescreen code");
111: }
112: }
113:
114: // vim:ts=4
|