-
Notifications
You must be signed in to change notification settings - Fork 843
/
Input.java
147 lines (121 loc) · 4.13 KB
/
Input.java
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.googlejavaformat;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableRangeMap;
/** An input to the formatter. */
public abstract class Input extends InputOutput {
/**
* A {@code Tok} ("tock") is a token, or a comment, or a newline, or a maximal string of blanks. A
* token {@code Tok} underlies a {@link Token}, and each other {@code Tok} is attached to a single
* {@code Token}. Tokens and comments have indices; white space {@code Tok}s do not.
*/
public interface Tok {
/**
* Return the {@code Tok}'s index.
*
* @return its index
*/
int getIndex();
/**
* Return the {@code Tok}'s {@code 0}-based position.
*
* @return its position
*/
int getPosition();
/**
* Return the {@code Tok}'s {@code 0}-based column number.
*
* @return its column number
*/
int getColumn();
/** The {@code Tok}'s text. */
String getText();
/** The {@code Tok}'s original text (before processing escapes). */
String getOriginalText();
/** The length of the {@code Tok}'s original text. */
int length();
/** Is the {@code Tok} a newline? */
boolean isNewline();
/** Is the {@code Tok} a "//" comment? */
boolean isSlashSlashComment();
/** Is the {@code Tok} a "//" comment? */
boolean isSlashStarComment();
/** Is the {@code Tok} a javadoc comment? */
boolean isJavadocComment();
/** Is the {@code Tok} a comment? */
boolean isComment();
}
/** A {@code Token} is a language-level token. */
public interface Token {
/**
* Get the token's {@link Tok}.
*
* @return the token's {@link Tok}
*/
Tok getTok();
/**
* Get the earlier {@link Tok}s assigned to this {@code Token}.
*
* @return the earlier {@link Tok}s assigned to this {@code Token}
*/
ImmutableList<? extends Tok> getToksBefore();
/**
* Get the later {@link Tok}s assigned to this {@code Token}.
*
* @return the later {@link Tok}s assigned to this {@code Token}
*/
ImmutableList<? extends Tok> getToksAfter();
}
/**
* Get the input tokens.
*
* @return the input tokens
*/
public abstract ImmutableList<? extends Token> getTokens();
/** A map from [start, end] position ranges to {@link Token}s. */
public abstract ImmutableRangeMap<Integer, ? extends Token> getPositionTokenMap();
public abstract ImmutableMap<Integer, Integer> getPositionToColumnMap();
public abstract String getText();
/**
* Get the number of toks.
*
* @return the number of toks, including the EOF tok
*/
public abstract int getkN();
/**
* Get the Token by index.
*
* @param k the Tok index
*/
public abstract Token getToken(int k);
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("super", super.toString()).toString();
}
/** Converts a character offset in the input to a line number. */
public abstract int getLineNumber(int inputPosition);
/** Converts a character offset in the input to a 0-based column number. */
public abstract int getColumnNumber(int inputPosition);
/**
* Construct a diagnostic. Populates the input filename, and converts character offsets to
* numbers.
*/
public FormatterDiagnostic createDiagnostic(int inputPosition, String message) {
return FormatterDiagnostic.create(
getLineNumber(inputPosition), getColumnNumber(inputPosition), message);
}
}