1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: AlternativePattern.java,v 1.5 2004/02/16 22:24:29 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import com.sun.org.apache.bcel.internal.generic.GOTO;
  21. import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
  22. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  23. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  24. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  27. /**
  28. * @author Jacek Ambroziak
  29. * @author Santiago Pericas-Geertsen
  30. */
  31. final class AlternativePattern extends Pattern {
  32. private final Pattern _left;
  33. private final Pattern _right;
  34. /**
  35. * Construct an alternative pattern. The method <code>setParent</code>
  36. * should not be called in this case.
  37. */
  38. public AlternativePattern(Pattern left, Pattern right) {
  39. _left = left;
  40. _right = right;
  41. }
  42. public void setParser(Parser parser) {
  43. super.setParser(parser);
  44. _left.setParser(parser);
  45. _right.setParser(parser);
  46. }
  47. public Pattern getLeft() {
  48. return _left;
  49. }
  50. public Pattern getRight() {
  51. return _right;
  52. }
  53. /**
  54. * The type of an '|' is not really defined, hence null is returned.
  55. */
  56. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  57. _left.typeCheck(stable);
  58. _right.typeCheck(stable);
  59. return null;
  60. }
  61. public double getPriority() {
  62. double left = _left.getPriority();
  63. double right = _right.getPriority();
  64. if (left < right)
  65. return(left);
  66. else
  67. return(right);
  68. }
  69. public String toString() {
  70. return "alternative(" + _left + ", " + _right + ')';
  71. }
  72. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  73. final InstructionList il = methodGen.getInstructionList();
  74. _left.translate(classGen, methodGen);
  75. final InstructionHandle gotot = il.append(new GOTO(null));
  76. il.append(methodGen.loadContextNode());
  77. _right.translate(classGen, methodGen);
  78. _left._trueList.backPatch(gotot);
  79. _left._falseList.backPatch(gotot.getNext());
  80. _trueList.append(_right._trueList.add(gotot));
  81. _falseList.append(_right._falseList);
  82. }
  83. }