156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson/* 256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Copyright (C) 2010 Google Inc. 356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * 456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Licensed under the Apache License, Version 2.0 (the "License"); 556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * you may not use this file except in compliance with the License. 656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * You may obtain a copy of the License at 756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * 856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * http://www.apache.org/licenses/LICENSE-2.0 956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * 1056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Unless required by applicable law or agreed to in writing, software 1156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * distributed under the License is distributed on an "AS IS" BASIS, 1256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * See the License for the specific language governing permissions and 1456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * limitations under the License. 1556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson */ 1656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson 1756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonpackage com.google.clearsilver.jsilver.syntax; 1856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson 1956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonimport com.google.clearsilver.jsilver.syntax.analysis.DepthFirstAdapter; 2056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonimport com.google.clearsilver.jsilver.syntax.node.AMultipleCommand; 2156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonimport com.google.clearsilver.jsilver.syntax.node.AOptimizedMultipleCommand; 2256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson 2356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson/** 2456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Visitor that can be applied to the AST to optimize it by replacing nodes with more efficient 2556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * implementations than the default SableCC generated versions. 2656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson */ 2756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonpublic class SyntaxTreeOptimizer extends DepthFirstAdapter { 2856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson 2956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson /** 3056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Replace AMultipleCommand nodes with AOptimizedMultipleCommands, which iterates over children 3156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * faster. 3256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson */ 3356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson @Override 3456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson public void caseAMultipleCommand(AMultipleCommand originalNode) { 3556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson // Recurse through child nodes first. Because the optimised node doesn't 3656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson // handle replacement, go leaves-first. 3756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson super.caseAMultipleCommand(originalNode); 3856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson // Replace this node with the optimized version. 3956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson originalNode.replaceBy(new AOptimizedMultipleCommand(originalNode)); 4056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson } 4156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson 4256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson} 43