001: /*
002: * @(#)ConstraintsTransformer.java 1.2 04/12/06
003: *
004: * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution of
007: * this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.lang;
010:
011: import pnuts.lang.SimpleNode;
012: import pnuts.lang.PnutsParserTreeConstants;
013:
014: public class ConstraintsTransformer {
015:
016: protected ConstraintsTransformer() {
017: }
018:
019: public static boolean isPredicate(SimpleNode node) {
020: switch (node.id) {
021: case PnutsParserTreeConstants.JJTLOGANDNODE:
022: case PnutsParserTreeConstants.JJTLOGORNODE:
023: case PnutsParserTreeConstants.JJTLOGNOTNODE:
024: case PnutsParserTreeConstants.JJTEQUALNODE:
025: case PnutsParserTreeConstants.JJTNOTEQNODE:
026: case PnutsParserTreeConstants.JJTINSTANCEOFEXPRESSION:
027: case PnutsParserTreeConstants.JJTLTNODE:
028: case PnutsParserTreeConstants.JJTGTNODE:
029: case PnutsParserTreeConstants.JJTLENODE:
030: case PnutsParserTreeConstants.JJTGENODE:
031: return true;
032: default:
033: return false;
034: }
035: }
036:
037: static void insertId(SimpleNode predicateNode, SimpleNode idNode) {
038: switch (predicateNode.id) {
039: case PnutsParserTreeConstants.JJTLOGANDNODE:
040: case PnutsParserTreeConstants.JJTLOGORNODE:
041: insertId(predicateNode.jjtGetChild(0), idNode);
042: insertId(predicateNode.jjtGetChild(1), idNode);
043: break;
044: case PnutsParserTreeConstants.JJTLOGNOTNODE:
045: case PnutsParserTreeConstants.JJTEQUALNODE:
046: case PnutsParserTreeConstants.JJTNOTEQNODE:
047: case PnutsParserTreeConstants.JJTINSTANCEOFEXPRESSION:
048: case PnutsParserTreeConstants.JJTLTNODE:
049: case PnutsParserTreeConstants.JJTGTNODE:
050: case PnutsParserTreeConstants.JJTLENODE:
051: case PnutsParserTreeConstants.JJTGENODE:
052: SimpleNode left = predicateNode.jjtGetChild(0);
053: while (left != null) {
054: if (left.id == PnutsParserTreeConstants.JJTIDNODE) {
055: SimpleNode parent = left.jjtGetParent();
056: SimpleNode member = new SimpleNode(
057: PnutsParserTreeConstants.JJTMEMBERNODE);
058: member.jjtSetParent(parent);
059: member.jjtAddChild(left, 0);
060: member.str = left.str;
061: left.str = ID_SYMBOL;
062: member.beginLine = parent.beginLine;
063: member.endLine = parent.endLine;
064: left.jjtSetParent(member);
065: parent.jjtAddChild(member, 0);
066: break;
067: }
068: if (left.jjtGetNumChildren() > 0) {
069: left = left.jjtGetChild(0);
070: } else {
071: break;
072: }
073: }
074: }
075: }
076:
077: final static String ID_SYMBOL = "i".intern();
078:
079: public static SimpleNode buildFunc(SimpleNode pred) {
080: SimpleNode fn = new SimpleNode(
081: PnutsParserTreeConstants.JJTFUNCTIONSTATEMENT);
082: SimpleNode p = new SimpleNode(PnutsParserTreeConstants.JJTPARAM);
083: p.str = ID_SYMBOL;
084: SimpleNode pl = new SimpleNode(
085: PnutsParserTreeConstants.JJTPARAMLIST);
086: pl.jjtAddChild(p, 0);
087: fn.jjtAddChild(pl, 0);
088:
089: SimpleNode blockNode = new SimpleNode(
090: PnutsParserTreeConstants.JJTBLOCK);
091: fn.jjtAddChild(blockNode, 1);
092: blockNode.jjtAddChild(pred, 0);
093:
094: SimpleNode idNode = new SimpleNode(
095: PnutsParserTreeConstants.JJTIDNODE);
096: idNode.jjtSetParent(blockNode);
097: idNode.str = ID_SYMBOL;
098:
099: if (isPredicate(pred)) {
100: insertId(pred, idNode);
101: }
102: return fn;
103: }
104:
105: public static SimpleNode buildExpression(SimpleNode startSet) {
106: if (startSet.jjtGetNumChildren() != 1) {
107: throw new IllegalArgumentException();
108: }
109: SimpleNode el = startSet.jjtGetChild(0);
110: if (el.jjtGetNumChildren() != 1) {
111: throw new IllegalArgumentException();
112: }
113: SimpleNode pred = el.jjtGetChild(0);
114: SimpleNode fn = buildFunc(pred);
115: startSet.jjtAddChild(el, 0);
116: el.jjtAddChild(fn, 0);
117: fn.jjtSetParent(el);
118: el.jjtSetParent(startSet);
119: return startSet;
120: }
121: }
|