• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. 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 distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.googlejavaformat;
16 
17 import com.google.common.base.MoreObjects;
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.collect.ImmutableRangeMap;
21 
22 /** An input to the formatter. */
23 public abstract class Input extends InputOutput {
24   /**
25    * A {@code Tok} ("tock") is a token, or a comment, or a newline, or a maximal string of blanks. A
26    * token {@code Tok} underlies a {@link Token}, and each other {@code Tok} is attached to a single
27    * {@code Token}. Tokens and comments have indices; white space {@code Tok}s do not.
28    */
29   public interface Tok {
30     /**
31      * Return the {@code Tok}'s index.
32      *
33      * @return its index
34      */
getIndex()35     int getIndex();
36 
37     /**
38      * Return the {@code Tok}'s {@code 0}-based position.
39      *
40      * @return its position
41      */
getPosition()42     int getPosition();
43 
44     /**
45      * Return the {@code Tok}'s {@code 0}-based column number.
46      *
47      * @return its column number
48      */
getColumn()49     int getColumn();
50 
51     /** The {@code Tok}'s text. */
getText()52     String getText();
53 
54     /** The {@code Tok}'s original text (before processing escapes). */
getOriginalText()55     String getOriginalText();
56 
57     /** The length of the {@code Tok}'s original text. */
length()58     int length();
59 
60     /** Is the {@code Tok} a newline? */
isNewline()61     boolean isNewline();
62 
63     /** Is the {@code Tok} a "//" comment? */
isSlashSlashComment()64     boolean isSlashSlashComment();
65 
66     /** Is the {@code Tok} a "//" comment? */
isSlashStarComment()67     boolean isSlashStarComment();
68 
69     /** Is the {@code Tok} a javadoc comment? */
isJavadocComment()70     boolean isJavadocComment();
71 
72     /** Is the {@code Tok} a comment? */
isComment()73     boolean isComment();
74   }
75 
76   /** A {@code Token} is a language-level token. */
77   public interface Token {
78     /**
79      * Get the token's {@link Tok}.
80      *
81      * @return the token's {@link Tok}
82      */
getTok()83     Tok getTok();
84 
85     /**
86      * Get the earlier {@link Tok}s assigned to this {@code Token}.
87      *
88      * @return the earlier {@link Tok}s assigned to this {@code Token}
89      */
getToksBefore()90     ImmutableList<? extends Tok> getToksBefore();
91 
92     /**
93      * Get the later {@link Tok}s assigned to this {@code Token}.
94      *
95      * @return the later {@link Tok}s assigned to this {@code Token}
96      */
getToksAfter()97     ImmutableList<? extends Tok> getToksAfter();
98   }
99 
100   /**
101    * Get the input tokens.
102    *
103    * @return the input tokens
104    */
getTokens()105   public abstract ImmutableList<? extends Token> getTokens();
106 
107   /** A map from [start, end] position ranges to {@link Token}s. */
getPositionTokenMap()108   public abstract ImmutableRangeMap<Integer, ? extends Token> getPositionTokenMap();
109 
getPositionToColumnMap()110   public abstract ImmutableMap<Integer, Integer> getPositionToColumnMap();
111 
getText()112   public abstract String getText();
113 
114   /**
115    * Get the number of toks.
116    *
117    * @return the number of toks, including the EOF tok
118    */
getkN()119   public abstract int getkN();
120 
121   /**
122    * Get the Token by index.
123    *
124    * @param k the token index
125    */
getToken(int k)126   public abstract Token getToken(int k);
127 
128   @Override
toString()129   public String toString() {
130     return MoreObjects.toStringHelper(this).add("super", super.toString()).toString();
131   }
132 
133   /** Converts a character offset in the input to a line number. */
getLineNumber(int inputPosition)134   public abstract int getLineNumber(int inputPosition);
135 
136   /** Converts a character offset in the input to a 0-based column number. */
getColumnNumber(int inputPosition)137   public abstract int getColumnNumber(int inputPosition);
138 
139   /**
140    * Construct a diagnostic. Populates the input filename, and converts character offsets to
141    * numbers.
142    */
createDiagnostic(int inputPosition, String message)143   public FormatterDiagnostic createDiagnostic(int inputPosition, String message) {
144     return FormatterDiagnostic.create(
145         getLineNumber(inputPosition), getColumnNumber(inputPosition), message);
146   }
147 }
148