| 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.tools.javac.parser; |
| 27 | |
| 28 | import java.util.Map; |
| 29 | import java.util.HashMap; |
| 30 | import com.sun.tools.javac.tree.JCTree; |
| 31 | import com.sun.tools.javac.tree.TreeInfo; |
| 32 | import com.sun.tools.javac.util.Position; |
| 33 | import com.sun.tools.javac.util.List; |
| 34 | |
| 35 | import static com.sun.tools.javac.tree.JCTree.*; |
| 36 | |
| 37 | /** |
| 38 | * This class is similar to Parser except that it stores ending |
| 39 | * positions for the tree nodes. |
| 40 | * |
| 41 | * <p><b>This is NOT part of any API supported by Sun Microsystems. |
| 42 | * If you write code that depends on this, you do so at your own risk. |
| 43 | * This code and its internal interfaces are subject to change or |
| 44 | * deletion without notice.</b></p> |
| 45 | */ |
| 46 | public class EndPosParser extends Parser { |
| 47 | |
| 48 | public EndPosParser(Factory fac, Lexer S, boolean keepDocComments) { |
| 49 | super(fac, S, keepDocComments); |
| 50 | this.S = S; |
| 51 | endPositions = new HashMap<JCTree,Integer>(); |
| 52 | } |
| 53 | |
| 54 | private Lexer S; |
| 55 | |
| 56 | /** A hashtable to store ending positions |
| 57 | * of source ranges indexed by the tree nodes. |
| 58 | * Defined only if option flag genEndPos is set. |
| 59 | */ |
| 60 | Map<JCTree, Integer> endPositions; |
| 61 | |
| 62 | /** {@inheritDoc} */ |
| 63 | @Override |
| 64 | protected void storeEnd(JCTree tree, int endpos) { |
| 65 | int errorEndPos = getErrorEndPos(); |
| 66 | endPositions.put(tree, errorEndPos > endpos ? errorEndPos : endpos); |
| 67 | } |
| 68 | |
| 69 | /** {@inheritDoc} */ |
| 70 | @Override |
| 71 | protected <T extends JCTree> T to(T t) { |
| 72 | storeEnd(t, S.endPos()); |
| 73 | return t; |
| 74 | } |
| 75 | |
| 76 | /** {@inheritDoc} */ |
| 77 | @Override |
| 78 | protected <T extends JCTree> T toP(T t) { |
| 79 | storeEnd(t, S.prevEndPos()); |
| 80 | return t; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public JCCompilationUnit compilationUnit() { |
| 85 | JCCompilationUnit t = super.compilationUnit(); |
| 86 | t.endPositions = endPositions; |
| 87 | return t; |
| 88 | } |
| 89 | |
| 90 | /** {@inheritDoc} */ |
| 91 | @Override |
| 92 | JCExpression parExpression() { |
| 93 | int pos = S.pos(); |
| 94 | JCExpression t = super.parExpression(); |
| 95 | return toP(F.at(pos).Parens(t)); |
| 96 | } |
| 97 | |
| 98 | /** {@inheritDoc} */ |
| 99 | @Override |
| 100 | public int getEndPos(JCTree tree) { |
| 101 | return TreeInfo.getEndPos(tree, endPositions); |
| 102 | } |
| 103 | |
| 104 | } |