Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.syntax; |
| 16 | |
mjhalupka | 374ad9e | 2018-06-29 12:35:11 -0700 | [diff] [blame] | 17 | import com.google.common.collect.Interner; |
| 18 | import com.google.devtools.build.lib.concurrent.BlazeInterners; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
| 20 | import com.google.devtools.build.lib.events.Location.LineAndColumn; |
shahan | 7ac7b63 | 2018-01-16 18:21:16 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import com.google.devtools.build.lib.util.Pair; |
Lukacs Berki | 4833822 | 2015-06-12 11:37:46 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import java.io.Serializable; |
Janak | 8251251 | 2015-07-13 14:35:14 +0000 | [diff] [blame] | 25 | import java.util.Arrays; |
Janak | 8251251 | 2015-07-13 14:35:14 +0000 | [diff] [blame] | 26 | import java.util.Objects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * A table to keep track of line numbers in source files. The client creates a LineNumberTable for |
| 30 | * their buffer using {@link #create}. The client can then ask for the line and column given a |
| 31 | * position using ({@link #getLineAndColumn(int)}). |
| 32 | */ |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 33 | @AutoCodec |
| 34 | @Immutable |
| 35 | public class LineNumberTable implements Serializable { |
mjhalupka | 374ad9e | 2018-06-29 12:35:11 -0700 | [diff] [blame] | 36 | private static final Interner<LineNumberTable> LINE_NUMBER_TABLE_INTERNER = |
| 37 | BlazeInterners.newWeakInterner(); |
| 38 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 39 | /** A mapping from line number (line >= 1) to character offset into the file. */ |
| 40 | private final int[] linestart; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 42 | private final PathFragment path; |
| 43 | private final int bufferLength; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 45 | public LineNumberTable(char[] buffer, PathFragment path) { |
| 46 | this(computeLinestart(buffer), path, buffer.length); |
| 47 | } |
| 48 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 49 | LineNumberTable(int[] linestart, PathFragment path, int bufferLength) { |
| 50 | this.linestart = linestart; |
| 51 | this.path = path; |
| 52 | this.bufferLength = bufferLength; |
| 53 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | |
Googler | 99cbc17 | 2018-11-08 07:43:00 -0800 | [diff] [blame] | 55 | @AutoCodec.Instantiator |
| 56 | static LineNumberTable createForSerialization( |
| 57 | int[] linestart, PathFragment path, int bufferLength) { |
| 58 | return LINE_NUMBER_TABLE_INTERNER.intern(new LineNumberTable(linestart, path, bufferLength)); |
| 59 | } |
| 60 | |
Lukacs Berki | 4833822 | 2015-06-12 11:37:46 +0000 | [diff] [blame] | 61 | static LineNumberTable create(char[] buffer, PathFragment path) { |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 62 | return new LineNumberTable(buffer, path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | } |
| 64 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 65 | private int getLineAt(int offset) { |
| 66 | if (offset < 0) { |
| 67 | throw new IllegalStateException("Illegal position: " + offset); |
shahan | 7ac7b63 | 2018-01-16 18:21:16 -0800 | [diff] [blame] | 68 | } |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 69 | int lowBoundary = 1; |
| 70 | int highBoundary = linestart.length - 1; |
| 71 | while (true) { |
| 72 | if ((highBoundary - lowBoundary) <= 1) { |
| 73 | if (linestart[highBoundary] > offset) { |
| 74 | return lowBoundary; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | } else { |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 76 | return highBoundary; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 79 | int medium = lowBoundary + ((highBoundary - lowBoundary) >> 1); |
| 80 | if (linestart[medium] > offset) { |
| 81 | highBoundary = medium; |
| 82 | } else { |
| 83 | lowBoundary = medium; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 84 | } |
shahan | 7ac7b63 | 2018-01-16 18:21:16 -0800 | [diff] [blame] | 85 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | } |
| 87 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 88 | LineAndColumn getLineAndColumn(int offset) { |
| 89 | int line = getLineAt(offset); |
| 90 | int column = offset - linestart[line] + 1; |
| 91 | return new LineAndColumn(line, column); |
| 92 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 93 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 94 | PathFragment getPath(int offset) { |
| 95 | return path; |
| 96 | } |
shahan | 7ac7b63 | 2018-01-16 18:21:16 -0800 | [diff] [blame] | 97 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 98 | Pair<Integer, Integer> getOffsetsForLine(int line) { |
| 99 | if (line <= 0 || line >= linestart.length) { |
| 100 | throw new IllegalArgumentException("Illegal line: " + line); |
| 101 | } |
| 102 | return Pair.of( |
| 103 | linestart[line], line < linestart.length - 1 ? linestart[line + 1] : bufferLength); |
| 104 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 105 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 106 | @Override |
| 107 | public int hashCode() { |
| 108 | return Objects.hash(Arrays.hashCode(linestart), path, bufferLength); |
| 109 | } |
shahan | 7ac7b63 | 2018-01-16 18:21:16 -0800 | [diff] [blame] | 110 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 111 | @Override |
| 112 | public boolean equals(Object other) { |
| 113 | if (other == null || !other.getClass().equals(getClass())) { |
| 114 | return false; |
| 115 | } |
| 116 | LineNumberTable that = (LineNumberTable) other; |
| 117 | return this.bufferLength == that.bufferLength |
| 118 | && Arrays.equals(this.linestart, that.linestart) |
| 119 | && Objects.equals(this.path, that.path); |
| 120 | } |
shahan | 7ac7b63 | 2018-01-16 18:21:16 -0800 | [diff] [blame] | 121 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 122 | private static int[] computeLinestart(char[] buffer) { |
| 123 | // Compute the size. |
| 124 | int size = 2; |
| 125 | for (int i = 0; i < buffer.length; i++) { |
| 126 | if (buffer[i] == '\n') { |
| 127 | size++; |
shahan | 7ac7b63 | 2018-01-16 18:21:16 -0800 | [diff] [blame] | 128 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | } |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 130 | int[] linestart = new int[size]; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 131 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 132 | int index = 0; |
| 133 | linestart[index++] = 0; // The 0th line does not exist - so we fill something in |
| 134 | // to make sure the start pos for the 1st line ends up at |
| 135 | // linestart[1]. Using 0 is useful for tables that are |
| 136 | // completely empty. |
| 137 | linestart[index++] = 0; // The first line ("line 1") starts at offset 0. |
Laurent Le Brun | e51a4d2 | 2016-10-11 18:04:16 +0000 | [diff] [blame] | 138 | |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 139 | // Scan the buffer and record the offset of each line start. Doing this |
| 140 | // once upfront is faster than checking each char as it is pulled from |
| 141 | // the buffer. |
| 142 | for (int i = 0; i < buffer.length; i++) { |
| 143 | if (buffer[i] == '\n') { |
| 144 | linestart[index++] = i + 1; |
Eric Fellheimer | 7ce73fb | 2015-04-07 13:18:12 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
laurentlb | 229ca18 | 2018-02-15 07:58:03 -0800 | [diff] [blame] | 147 | return linestart; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 148 | } |
| 149 | } |