langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Position.java
author jlahoda
Wed, 27 Aug 2014 07:44:00 +0200
changeset 26266 2d24bda701dc
parent 25874 83c19f00452c
permissions -rw-r--r--
8056061: Mark implementations of public interfaces with an annotation Summary: Adding @DefinedBy annotation to mark methods that implement public API methods; annotating the methods; adding a coding rules analyzer to enforce all such methods are annotated. Reviewed-by: jjg, mcimadamore, jfranck Contributed-by: jan.lahoda@oracle.com, jonathan.gibbons@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
     2
 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.BitSet;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    29
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    30
import com.sun.tools.javac.util.DefinedBy.Api;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    31
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import static com.sun.tools.javac.util.LayoutCharacters.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
/** A class that defines source code positions as simple character
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *  offsets from the beginning of the file. The first character
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *  is at position 0.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *  Support is also provided for (line,column) coordinates, but tab
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *  expansion is optional and no Unicode excape translation is considered.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *  The first character is at location (1,1).
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    42
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    43
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
public class Position {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    public static final int NOPOS        = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    public static final int FIRSTPOS     = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    public static final int FIRSTLINE    = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    public static final int FIRSTCOLUMN  = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    public static final int LINESHIFT    = 10;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    public static final int MAXCOLUMN    = (1<<LINESHIFT) - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    public static final int MAXLINE      = (1<<(Integer.SIZE-LINESHIFT)) - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public static final int MAXPOS       = Integer.MAX_VALUE;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     * This is class is not supposed to be instantiated.
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private Position() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    /** A two-way map between line/column numbers and positions,
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     *  derived from a scan done at creation time.  Tab expansion is
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     *  optionally supported via a character map.  Text content
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     *  is not retained.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     *<p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     *  Notes:  The first character position FIRSTPOS is at
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     *  (FIRSTLINE,FIRSTCOLUMN).  No account is taken of Unicode escapes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * @param   src         Source characters
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * @param   max         Number of characters to read
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     * @param   expandTabs  If true, expand tabs when calculating columns
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    public static LineMap makeLineMap(char[] src, int max, boolean expandTabs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        LineMapImpl lineMap = expandTabs ?
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            new LineTabMapImpl(max) : new LineMapImpl();
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        lineMap.build(src, max);
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        return lineMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    /** Encode line and column numbers in an integer as:
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
    85
     *  {@code line-number << LINESHIFT + column-number }.
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 13844
diff changeset
    86
     *  {@link Position#NOPOS} represents an undefined position.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * @param  line  number of line (first is 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * @param  col   number of character on line (first is 1)
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 13844
diff changeset
    90
     * @return       an encoded position or {@link Position#NOPOS}
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     *               if the line or column number is too big to
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     *               represent in the encoded format
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * @throws IllegalArgumentException if line or col is less than 1
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    public static int encodePosition(int line, int col) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        if (line < 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            throw new IllegalArgumentException("line must be greater than 0");
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        if (col < 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            throw new IllegalArgumentException("column must be greater than 0");
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        if (line > MAXLINE || col > MAXCOLUMN) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            return NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        return (line << LINESHIFT) + col;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    public static interface LineMap extends com.sun.source.tree.LineMap {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        /** Find the start position of a line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
         * @param line number of line (first is 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
         * @return     position of first character in line
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
         * @throws  ArrayIndexOutOfBoundsException
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
   113
         *           if {@code lineNumber < 1}
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
   114
         *           if {@code lineNumber > no. of lines}
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        int getStartPosition(int line);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        /** Find the position corresponding to a (line,column).
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
         * @param   line    number of line (first is 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
         * @param   column  number of character on line (first is 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
         * @return  position of character
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
         * @throws  ArrayIndexOutOfBoundsException
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
   125
         *           if {@code line < 1}
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
   126
         *           if {@code line > no. of lines}
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        int getPosition(int line, int column);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        /** Find the line containing a position; a line termination
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
         * character is on the line it terminates.
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
         * @param   pos  character offset of the position
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
         * @return the line number on which pos occurs (first line is 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        int getLineNumber(int pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        /** Find the column for a character position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
         *  Note:  this method does not handle tab expansion.
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
         *  If tab expansion is needed, use a LineTabMap instead.
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
         * @param  pos   character offset of the position
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
         * @return       the column number at which pos occurs
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        int getColumnNumber(int pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    static class LineMapImpl implements LineMap {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        protected int[] startPosition; // start position of each line
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        protected LineMapImpl() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        protected void build(char[] src, int max) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            int c = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            int[] linebuf = new int[max];
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            while (i < max) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                linebuf[c++] = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                do {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                    char ch = src[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                    if (ch == '\r' || ch == '\n') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                        if (ch == '\r' && (i+1) < max && src[i+1] == '\n')
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                            i += 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                            ++i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                    else if (ch == '\t')
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                        setTabPosition(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                } while (++i < max);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            this.startPosition = new int[c];
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            System.arraycopy(linebuf, 0, startPosition, 0, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        public int getStartPosition(int line) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            return startPosition[line - FIRSTLINE];
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   180
        @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        public long getStartPosition(long line) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            return getStartPosition(longToInt(line));
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        public int getPosition(int line, int column) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            return startPosition[line - FIRSTLINE] + column - FIRSTCOLUMN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   189
        @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        public long getPosition(long line, long column) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            return getPosition(longToInt(line), longToInt(column));
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        // Cache of last line number lookup
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        private int lastPosition = Position.FIRSTPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        private int lastLine = Position.FIRSTLINE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        public int getLineNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            if (pos == lastPosition) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                return lastLine;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
            lastPosition = pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            int low = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            int high = startPosition.length-1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            while (low <= high) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                int mid = (low + high) >> 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                int midVal = startPosition[mid];
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                if (midVal < pos)
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                    low = mid + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                else if (midVal > pos)
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                    high = mid - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                    lastLine = mid + 1; // pos is at beginning of this line
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                    return lastLine;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            lastLine = low;
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
            return lastLine;  // pos is on this line
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   223
        @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        public long getLineNumber(long pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            return getLineNumber(longToInt(pos));
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        public int getColumnNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            return pos - startPosition[getLineNumber(pos) - FIRSTLINE] + FIRSTCOLUMN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   232
        @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        public long getColumnNumber(long pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            return getColumnNumber(longToInt(pos));
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        private static int longToInt(long longValue) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            int intValue = (int)longValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            if (intValue != longValue)
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                throw new IndexOutOfBoundsException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            return intValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        protected void setTabPosition(int offset) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
     * A LineMap that handles tab expansion correctly.  The cost is
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
     * an additional bit per character in the source array.
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    public static class LineTabMapImpl extends LineMapImpl {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        private BitSet tabMap;       // bits set for tab positions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        public LineTabMapImpl(int max) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            super();
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            tabMap = new BitSet(max);
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        protected void setTabPosition(int offset) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            tabMap.set(offset);
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        public int getColumnNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            int lineStart = startPosition[getLineNumber(pos) - FIRSTLINE];
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            int column = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            for (int bp = lineStart; bp < pos; bp++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                if (tabMap.get(bp))
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                    column = (column / TabInc * TabInc) + TabInc;
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                    column++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            return column + FIRSTCOLUMN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        public int getPosition(int line, int column) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            int pos = startPosition[line - FIRSTLINE];
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            column -= FIRSTCOLUMN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            int col = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            while (col < column) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                pos++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                if (tabMap.get(pos))
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                    col = (col / TabInc * TabInc) + TabInc;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                    col++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            return pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
}