File: LineMapping.java

package info (click to toggle)
doctorj 5.0.0-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,156 kB
  • ctags: 3,454
  • sloc: java: 30,027; xml: 331; makefile: 82; sh: 61
file content (131 lines) | stat: -rw-r--r-- 3,971 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package org.incava.java;

import java.awt.Point;
import java.io.*;
import java.util.*;
import org.incava.log.Log;


/**
 * Converts from 0-indexed string positions to line:column values. Lines and
 * columns are 1-indexed, matching the Java parser.
 */
public class LineMapping extends ArrayList
{
    public class PositionToLocation 
    {
        public int position;
        
        public int line;

        public int column;

        public PositionToLocation(int position, int line, int column)
        {
            this.position = position;
            this.line = line;
            this.column = column;
        }

        public String toString()
        {
            return "{ position: " + position + " => { line: " + line + ", column: " + column + " } }";
        }
    }

    public LineMapping(String text, int startLine, int startColumn)
    {
        // Log.log("position: " + 0 + " => { line: " + startLine + ", column: " + startColumn + " }");
        add(new PositionToLocation(0, startLine, startColumn));

        int len = text.length();

        // figure out where the lines start:
        for (int pos = 0, line = startLine; pos < len; ++pos) {
            // Mac: \r, Unix: \n, DOS: \r\n
            if (text.charAt(pos) == '\r') {
                // might be DOS:
                if (pos + 1 < len && text.charAt(pos + 1) == '\n') {
                    ++pos;
                }
                // otherwise, it's Mac (at least, before OS X)
            }
            else if (text.charAt(pos) == '\n') {
                // Unix
            }
            else {
                // we're not at a newline, so go to the next character.
                continue;
            }

            ++line;

            // Log.log("position: " + pos + " => { line: " + line + ", column: " + 1 + " }");
            add(new PositionToLocation(pos + 1, line, 1));
        }

        // Log.log("line positions: " + this);
    }

    /**
     * Converts the string position to a line:column start and end location.
     */
    public Location[] getLocations(Point pos)
    {
        return getLocations(pos.x, pos.y);
    }

    /**
     * Converts the string position to a line:column start and end location.
     */
    public Location[] getLocations(int startPos, int endPos)
    {
        // Log.log("parsing description");
        Location start = null;
        Location end   = null;

        // Log.log("position: " + startPos + ", " + endPos);
        
        // go backward
        ListIterator lit = listIterator(size());
        while ((start == null || end == null) && lit.hasPrevious()) {
            PositionToLocation pl = (PositionToLocation)lit.previous();
            // Log.log("considering position/location " + pl);
            if (end == null && endPos >= pl.position) {
                // Log.log("assigning end");
                end = new Location(pl.line, pl.column + endPos - pl.position);
            }
            if (start == null && startPos >= pl.position) {
                // Log.log("assigning start");
                start = new Location(pl.line, pl.column + startPos - pl.position);
            }
        }

        // attn Sun: tuples, please!
        return new Location[] { start, end };
    }
    
    /**
     * Converts the string position to a line:column location.
     */
    public Location getLocation(int pos)
    {
        Location location = null;

        // Log.log("position: " + pos);
        
        // go backward
        ListIterator lit = listIterator(size());
        while (location == null && lit.hasPrevious()) {
            PositionToLocation pl = (PositionToLocation)lit.previous();
            // Log.log("considering position/location " + pl);
            if (location == null && pos >= pl.position) {
                // Log.log("creating location");
                return new Location(pl.line, pl.column + pos - pl.position);
            }
        }

        return null;
    }
    
}