OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "gl/GrGLSLPrettyPrint.h" | 7 #include "gl/GrGLSLPrettyPrint.h" |
8 | 8 |
9 namespace GrGLSLPrettyPrint { | 9 namespace GrGLSLPrettyPrint { |
10 | 10 |
11 class GLSLPrettyPrint { | 11 class GLSLPrettyPrint { |
12 public: | 12 public: |
13 GLSLPrettyPrint() {} | 13 GLSLPrettyPrint() {} |
14 | 14 |
15 SkString prettify(const char** strings, | 15 SkString prettify(const SkString& input, bool countlines) { |
16 int* lengths, | 16 // setup pretty state |
17 int count, | 17 fIndex = 0; |
18 bool countlines) { | 18 fLength = input.size(); |
| 19 fInput = input; |
19 fCountlines = countlines; | 20 fCountlines = countlines; |
20 fTabs = 0; | 21 fTabs = 0; |
21 fLinecount = 1; | 22 fLinecount = 1; |
22 fFreshline = true; | 23 fFreshline = true; |
23 | 24 |
24 // If a string breaks while in the middle 'parse until' we need to conti
nue parsing on the | |
25 // next string | |
26 fInParseUntilNewline = false; | |
27 fInParseUntil = false; | |
28 | |
29 int parensDepth = 0; | 25 int parensDepth = 0; |
30 | |
31 // number 1st line | 26 // number 1st line |
32 this->lineNumbering(); | 27 this->lineNumbering(); |
33 for (int i = 0; i < count; i++) { | 28 while (fLength > fIndex) { |
34 // setup pretty state | 29 /* the heart and soul of our prettification algorithm. The rules sh
ould hopefully be |
35 fIndex = 0; | 30 * self explanatory. For '#' and '//' tokens we parse until we reac
h a newline. |
36 fLength = lengths[i]; | 31 * |
37 fInput = strings[i]; | 32 * For long style comments like this one, we search for the ending t
oken. We also |
38 | 33 * preserve whitespace in these comments WITH THE CAVEAT that we do
the newlines |
39 while (fLength > fIndex) { | 34 * ourselves. This allows us to remain in control of line numbers,
and matching tabs |
40 /* the heart and soul of our prettification algorithm. The rule
s should hopefully | 35 * Existing tabs in the input string are copied over too, but this w
ill look funny |
41 * be self explanatory. For '#' and '//' tokens we parse until
we reach a newline. | 36 * |
42 * | 37 * '{' and '}' are handled in basically the same way. We add a newl
ine if we aren't |
43 * For long style comments like this one, we search for the endi
ng token. We also | 38 * on a fresh line, dirty the line, then add a second newline, ie br
aces are always |
44 * preserve whitespace in these comments WITH THE CAVEAT that we
do the newlines | 39 * on their own lines indented properly. The one funkiness here is
structs print with |
45 * ourselves. This allows us to remain in control of line numbe
rs, and matching | 40 * the semicolon on its own line. Its not a problem for a glsl comp
iler though |
46 * tabs Existing tabs in the input string are copied over too, b
ut this will look | 41 * |
47 * funny | 42 * '(' and ')' are basically ignored, except as a sign we need to ig
nore ';' ala |
48 * | 43 * in for loops. |
49 * '{' and '}' are handled in basically the same way. We add a
newline if we aren't | 44 * |
50 * on a fresh line, dirty the line, then add a second newline, i
e braces are always | 45 * ';' means add a new line |
51 * on their own lines indented properly. The one funkiness here
is structs print | 46 * |
52 * with the semicolon on its own line. Its not a problem for a
glsl compiler though | 47 * '\t' and '\n' are ignored in general parsing for backwards compat
ability with |
53 * | 48 * existing shader code and we also have a special case for handling
whitespace |
54 * '(' and ')' are basically ignored, except as a sign we need t
o ignore ';' ala | 49 * at the beginning of fresh lines. |
55 * in for loops. | 50 * |
56 * | 51 * Otherwise just add the new character to the pretty string, indent
ing if necessary. |
57 * ';' means add a new line | 52 */ |
58 * | 53 if (this->hasToken("#") || this->hasToken("//")) { |
59 * '\t' and '\n' are ignored in general parsing for backwards co
mpatability with | 54 this->parseUntilNewline(); |
60 * existing shader code and we also have a special case for hand
ling whitespace | 55 } else if (this->hasToken("/*")) { |
61 * at the beginning of fresh lines. | 56 this->parseUntil("*/"); |
62 * | 57 } else if ('{' == fInput[fIndex]) { |
63 * Otherwise just add the new character to the pretty string, in
denting if necessary. | 58 this->newline(); |
64 */ | 59 this->appendChar('{'); |
65 if (fInParseUntilNewline) { | 60 fTabs++; |
66 this->parseUntilNewline(); | 61 this->newline(); |
67 } else if (fInParseUntil) { | 62 } else if ('}' == fInput[fIndex]) { |
68 this->parseUntil(fInParseUntilToken); | 63 fTabs--; |
69 } else if (this->hasToken("#") || this->hasToken("//")) { | 64 this->newline(); |
70 this->parseUntilNewline(); | 65 this->appendChar('}'); |
71 } else if (this->hasToken("/*")) { | 66 this->newline(); |
72 this->parseUntil("*/"); | 67 } else if (this->hasToken(")")) { |
73 } else if ('{' == fInput[fIndex]) { | 68 parensDepth--; |
74 this->newline(); | 69 } else if (this->hasToken("(")) { |
75 this->appendChar('{'); | 70 parensDepth++; |
76 fTabs++; | 71 } else if (!parensDepth && this->hasToken(";")) { |
77 this->newline(); | 72 this->newline(); |
78 } else if ('}' == fInput[fIndex]) { | 73 } else if ('\t' == fInput[fIndex] || '\n' == fInput[fIndex] || |
79 fTabs--; | 74 (fFreshline && ' ' == fInput[fIndex])) { |
80 this->newline(); | 75 fIndex++; |
81 this->appendChar('}'); | 76 } else { |
82 this->newline(); | 77 this->appendChar(input[fIndex]); |
83 } else if (this->hasToken(")")) { | |
84 parensDepth--; | |
85 } else if (this->hasToken("(")) { | |
86 parensDepth++; | |
87 } else if (!parensDepth && this->hasToken(";")) { | |
88 this->newline(); | |
89 } else if ('\t' == fInput[fIndex] || '\n' == fInput[fIndex] || | |
90 (fFreshline && ' ' == fInput[fIndex])) { | |
91 fIndex++; | |
92 } else { | |
93 this->appendChar(fInput[fIndex]); | |
94 } | |
95 } | 78 } |
96 } | 79 } |
97 return fPretty; | 80 return fPretty; |
98 } | 81 } |
99 private: | 82 private: |
100 void appendChar(char c) { | 83 void appendChar(char c) { |
101 this->tabString(); | 84 this->tabString(); |
102 fPretty.appendf("%c", fInput[fIndex++]); | 85 fPretty.appendf("%c", fInput[fIndex++]); |
103 fFreshline = false; | 86 fFreshline = false; |
104 } | 87 } |
(...skipping 12 matching lines...) Expand all Loading... |
117 fPretty.append(token); | 100 fPretty.append(token); |
118 fFreshline = false; | 101 fFreshline = false; |
119 return true; | 102 return true; |
120 } | 103 } |
121 | 104 |
122 void parseUntilNewline() { | 105 void parseUntilNewline() { |
123 while (fLength > fIndex) { | 106 while (fLength > fIndex) { |
124 if ('\n' == fInput[fIndex]) { | 107 if ('\n' == fInput[fIndex]) { |
125 fIndex++; | 108 fIndex++; |
126 this->newline(); | 109 this->newline(); |
127 fInParseUntilNewline = false; | |
128 break; | 110 break; |
129 } | 111 } |
130 fPretty.appendf("%c", fInput[fIndex++]); | 112 fPretty.appendf("%c", fInput[fIndex++]); |
131 fInParseUntilNewline = true; | |
132 } | 113 } |
133 } | 114 } |
134 | 115 |
135 // this code assumes it is not actually searching for a newline. If you nee
d to search for a | 116 // this code assumes it is not actually searching for a newline. If you nee
d to search for a |
136 // newline, then use the function above. If you do search for a newline wit
h this function | 117 // newline, then use the function above. If you do search for a newline wit
h this function |
137 // it will consume the entire string and the output will certainly not be pr
ettified | 118 // it will consume the entire string and the output will certainly not be pr
ettified |
138 void parseUntil(const char* token) { | 119 void parseUntil(const char* token) { |
139 while (fLength > fIndex) { | 120 while (fLength > fIndex) { |
140 // For embedded newlines, this code will make sure to embed the new
line in the | 121 // For embedded newlines, this code will make sure to embed the new
line in the |
141 // pretty string, increase the linecount, and tab out the next line
to the appropriate | 122 // pretty string, increase the linecount, and tab out the next line
to the appropriate |
142 // place | 123 // place |
143 if ('\n' == fInput[fIndex]) { | 124 if ('\n' == fInput[fIndex]) { |
144 this->newline(); | 125 this->newline(); |
145 this->tabString(); | 126 this->tabString(); |
146 fIndex++; | 127 fIndex++; |
147 } | 128 } |
148 if (this->hasToken(token)) { | 129 if (this->hasToken(token)) { |
149 fInParseUntil = false; | |
150 break; | 130 break; |
151 } | 131 } |
152 fFreshline = false; | 132 fFreshline = false; |
153 fPretty.appendf("%c", fInput[fIndex++]); | 133 fPretty.appendf("%c", fInput[fIndex++]); |
154 fInParseUntil = true; | |
155 fInParseUntilToken = token; | |
156 } | 134 } |
157 } | 135 } |
158 | 136 |
159 // We only tab if on a newline, otherwise consider the line tabbed | 137 // We only tab if on a newline, otherwise consider the line tabbed |
160 void tabString() { | 138 void tabString() { |
161 if (fFreshline) { | 139 if (fFreshline) { |
162 for (int t = 0; t < fTabs; t++) { | 140 for (int t = 0; t < fTabs; t++) { |
163 fPretty.append("\t"); | 141 fPretty.append("\t"); |
164 } | 142 } |
165 } | 143 } |
(...skipping 11 matching lines...) Expand all Loading... |
177 | 155 |
178 void lineNumbering() { | 156 void lineNumbering() { |
179 if (fCountlines) { | 157 if (fCountlines) { |
180 fPretty.appendf("%4d\t", fLinecount++); | 158 fPretty.appendf("%4d\t", fLinecount++); |
181 } | 159 } |
182 } | 160 } |
183 | 161 |
184 bool fCountlines, fFreshline; | 162 bool fCountlines, fFreshline; |
185 int fTabs, fLinecount; | 163 int fTabs, fLinecount; |
186 size_t fIndex, fLength; | 164 size_t fIndex, fLength; |
187 const char* fInput; | 165 SkString fInput, fPretty; |
188 SkString fPretty; | |
189 | |
190 // Some helpers for parseUntil when we go over a string length | |
191 bool fInParseUntilNewline; | |
192 bool fInParseUntil; | |
193 const char* fInParseUntilToken; | |
194 }; | 166 }; |
195 | 167 |
196 SkString PrettyPrintGLSL(const char** strings, | 168 SkString PrettyPrintGLSL(const SkString& input, bool countlines) { |
197 int* lengths, | |
198 int count, | |
199 bool countlines) { | |
200 GLSLPrettyPrint pp; | 169 GLSLPrettyPrint pp; |
201 return pp.prettify(strings, lengths, count, countlines); | 170 return pp.prettify(input, countlines); |
202 } | 171 } |
203 | 172 |
204 } // end namespace | 173 } // end namespace |
OLD | NEW |