1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | |
22 | package com.github.javaparser.printer.concretesyntaxmodel; |
23 | |
24 | import com.github.javaparser.GeneratedJavaParserConstants; |
25 | import com.github.javaparser.TokenTypes; |
26 | import com.github.javaparser.ast.Node; |
27 | import com.github.javaparser.printer.SourcePrinter; |
28 | import com.github.javaparser.utils.LineSeparator; |
29 | |
30 | import static com.github.javaparser.TokenTypes.*; |
31 | |
32 | public class CsmToken implements CsmElement { |
33 | private final int tokenType; |
34 | private String content; |
35 | private TokenContentCalculator tokenContentCalculator; |
36 | |
37 | public interface TokenContentCalculator { |
38 | String calculate(Node node); |
39 | } |
40 | |
41 | public int getTokenType() { |
42 | return tokenType; |
43 | } |
44 | |
45 | public String getContent(Node node) { |
46 | if (tokenContentCalculator != null) { |
47 | return tokenContentCalculator.calculate(node); |
48 | } |
49 | return content; |
50 | } |
51 | |
52 | public CsmToken(int tokenType) { |
53 | this.tokenType = tokenType; |
54 | this.content = GeneratedJavaParserConstants.tokenImage[tokenType]; |
55 | if (content.startsWith("\"")) { |
56 | content = content.substring(1, content.length() - 1); |
57 | } |
58 | |
59 | // Replace "raw" values with escaped textual counterparts (e.g. newlines {@code \r\n}) |
60 | // and "placeholder" values ({@code <SPACE>}) with their textual counterparts |
61 | if (isEndOfLineToken(tokenType)) { |
62 | // Use the unescaped version |
63 | content = LineSeparator.lookupEscaped(this.content).get().asRawString(); |
64 | } else if (isWhitespaceButNotEndOfLine(tokenType)) { |
65 | content = " "; |
66 | } |
67 | } |
68 | |
69 | public CsmToken(int tokenType, String content) { |
70 | this.tokenType = tokenType; |
71 | this.content = content; |
72 | } |
73 | |
74 | public CsmToken(int tokenType, TokenContentCalculator tokenContentCalculator) { |
75 | this.tokenType = tokenType; |
76 | this.tokenContentCalculator = tokenContentCalculator; |
77 | } |
78 | |
79 | @Override |
80 | public void prettyPrint(Node node, SourcePrinter printer) { |
81 | if (isEndOfLineToken(tokenType)) { |
82 | printer.println(); |
83 | } else { |
84 | printer.print(getContent(node)); |
85 | } |
86 | } |
87 | |
88 | @Override |
89 | public String toString() { |
90 | return "token(" + content + ")"; |
91 | } |
92 | |
93 | @Override |
94 | public boolean equals(Object o) { |
95 | if (this == o) return true; |
96 | if (o == null || getClass() != o.getClass()) return false; |
97 | |
98 | CsmToken csmToken = (CsmToken) o; |
99 | |
100 | if (tokenType != csmToken.tokenType) return false; |
101 | if (content != null ? !content.equals(csmToken.content) : csmToken.content != null) return false; |
102 | return tokenContentCalculator != null ? tokenContentCalculator.equals(csmToken.tokenContentCalculator) : csmToken.tokenContentCalculator == null; |
103 | } |
104 | |
105 | @Override |
106 | public int hashCode() { |
107 | int result = tokenType; |
108 | result = 31 * result + (content != null ? content.hashCode() : 0); |
109 | result = 31 * result + (tokenContentCalculator != null ? tokenContentCalculator.hashCode() : 0); |
110 | return result; |
111 | } |
112 | |
113 | public boolean isWhiteSpace() { |
114 | return TokenTypes.isWhitespace(tokenType); |
115 | } |
116 | |
117 | public boolean isNewLine() { |
118 | return TokenTypes.isEndOfLineToken(tokenType); |
119 | } |
120 | } |
121 |
Members