src/share/classes/com/sun/tools/javac/parser/EndPosParser.java

Tue, 16 Sep 2008 18:35:18 -0700

author
jjg
date
Tue, 16 Sep 2008 18:35:18 -0700
changeset 113
eff38cc97183
parent 111
a92b756a888f
child 117
24a47c3062fe
permissions
-rw-r--r--

6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
Reviewed-by: darcy, mcimadamore

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

mercurial