Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: src/gpu/gl/GrGLSLPrettyPrint.cpp

Issue 437593004: Pretty print of shaders (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/gpu/gl/GrGLSLPrettyPrint.h ('k') | src/gpu/gl/GrGLShaderBuilder.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 #include "gl/GrGLSLPrettyPrint.h"
8
9 namespace GrGLSLPrettyPrint {
10
11 class GrGLSLPrettyPrint {
bsalomon 2014/08/04 15:20:01 For classes that are private to a particular trans
12 public:
13 GrGLSLPrettyPrint() {}
14
15 SkString prettify(const SkString& input, bool countlines) {
16 // setup pretty state
17 fIndex = 0;
18 fLength = input.size();
19 fInput = input;
20 fCountlines = countlines;
21 fTabs = 0;
22 fLinecount = 1;
23 fFreshline = true;
24
25 int parensDepth = 0;
26 // number 1st line
27 lineNumbering();
28 while (fLength > fIndex) {
29 /* the heart and soul of our prettification algorithm. The rules sh ould hopefully be
30 * self explanatory. For '#' and '//' tokens we parse until we reac h a newline.
31 *
32 * For long style comments like this one, we search for the ending t oken. We also
33 * preserve whitespace in these comments WITH THE CAVEAT that we do the newlines
34 * ourselves. This allows us to remain in control of line numbers, and matching tabs
35 * Existing tabs in the input string are copied over too, but this w ill look funny
36 *
37 * '{' and '}' are handled in basically the same way. We add a newl ine if we aren't
38 * on a fresh line, dirty the line, then add a second newline, ie br aces are always
39 * on their own lines indented properly. The one funkiness here is structs print with
40 * the semicolon on its own line. Its not a problem for a glsl comp iler though
41 *
42 * '(' and ')' are basically ignored, except as a sign we need to ig nore ';' ala
43 * in for loops.
44 *
45 * ';' means add a new line
46 *
47 * '\t' and '\n' are ignored in general parsing for backwards compat ability with
48 * existing shader code and we also have a special case for handling whitespace
49 * at the beginning of fresh lines.
50 *
51 * Otherwise just add the new character to the pretty string, indent ing if necessary.
52 */
53 if (hasToken("#") || hasToken("//")) {
Stephen White 2014/08/04 15:10:29 I guess ad hoc parsing is ok for this, since we kn
bsalomon 2014/08/04 15:20:01 Part of the advantage of having a separate functio
54 parseUntilNewline();
55 } else if (hasToken("/*")) {
bsalomon 2014/08/04 15:20:01 minor style nit: this->hasToken() (and similar in
56 parseUntil("*/");
57 } else if ('{' == fInput[fIndex]) {
58 newline();
59 appendChar('{');
60 fTabs++;
61 newline();
62 } else if ('}' == fInput[fIndex]) {
63 fTabs--;
64 newline();
65 appendChar('}');
66 newline();
67 } else if (hasToken(")")) {
68 parensDepth--;
69 } else if (hasToken("(")) {
70 parensDepth++;
71 } else if (!parensDepth && hasToken(";")) {
72 newline();
73 } else if ('\t' == fInput[fIndex] || '\n' == fInput[fIndex] ||
74 (fFreshline && ' ' == fInput[fIndex])) {
75 fIndex++;
76 continue;
77 } else {
78 appendChar(input[fIndex]);
79 }
80 }
81 return fPretty;
82 }
83 private:
84 void appendChar(char c) {
85 tabString();
86 fPretty.appendf("%c", fInput[fIndex++]);
87 fFreshline = false;
88 }
89
90 // hasToken automatically consumes the next token, if it is a match, and the n tabs
91 // if necessary, before inserting the token into the pretty string
92 bool hasToken(const char* token) {
93 size_t i = fIndex;
94 for (size_t j = 0; token[j] && fLength > i; i++, j++) {
95 if (token[j] != fInput[i]) {
96 return false;
97 }
98 }
99 tabString();
100 fIndex = i;
101 fPretty.append(token);
102 fFreshline = false;
103 return true;
104 }
105
106 void parseUntilNewline() {
107 while (fLength > fIndex) {
108 if ('\n' == fInput[fIndex]) {
109 fIndex++;
110 newline();
111 break;
112 }
113 fPretty.appendf("%c", fInput[fIndex++]);
114 }
115 }
116
117 // this code assumes it is not actually searching for a newline. If you nee d to search for a
118 // newline, then use the function above. If you do search for a newline wit h this function
119 // it will consume the entire string and the output will certainly not be pr ettified
120 void parseUntil(const char* token) {
121 while (fLength > fIndex) {
122 // For embedded newlines, this code will make sure to embed the new line in the
123 // pretty string, increase the linecount, and tab out the next line to the appropriate
124 // place
125 if ('\n' == fInput[fIndex]) {
126 newline();
127 tabString();
128 fIndex++;
129 }
130 if (hasToken(token)) {
131 break;
132 }
133 fFreshline = false;
134 fPretty.appendf("%c", fInput[fIndex++]);
135 }
136 }
137
138 // We only tab if on a newline, otherwise consider the line tabbed
139 void tabString() {
140 if (fFreshline) {
141 for (int t = 0; t < fTabs; t++) {
142 fPretty.append("\t");
143 }
144 }
145 }
146
147 // newline is really a request to add a newline, if we are on a fresh line t here is no reason
148 // to add another newline
149 void newline() {
150 if (!fFreshline) {
151 fFreshline = true;
152 fPretty.append("\n");
153 lineNumbering();
154 }
155 }
156
157 void lineNumbering() {
158 if (fCountlines) {
159 fPretty.appendf("%4d\t", fLinecount++);
160 }
161 }
162
163 bool fCountlines, fFreshline, fRequestNewline;
164 int fTabs, fLinecount;
165 size_t fIndex, fLength;
166 SkString fInput, fPretty;
167 };
168
169 SkString PrettyPrintGLSL(const SkString& input, bool countlines) {
170 GrGLSLPrettyPrint pp;
171 return pp.prettify(input, countlines);
172 }
173
174 } // end namespace
OLDNEW
« no previous file with comments | « include/gpu/gl/GrGLSLPrettyPrint.h ('k') | src/gpu/gl/GrGLShaderBuilder.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698