SkSLParser.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright 2016 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. #ifndef SKSL_PARSER
  8. #define SKSL_PARSER
  9. #include <vector>
  10. #include <memory>
  11. #include <unordered_map>
  12. #include <unordered_set>
  13. #include "src/sksl/SkSLASTFile.h"
  14. #include "src/sksl/SkSLASTNode.h"
  15. #include "src/sksl/SkSLErrorReporter.h"
  16. #include "src/sksl/SkSLLexer.h"
  17. #include "src/sksl/ir/SkSLLayout.h"
  18. struct yy_buffer_state;
  19. #define YY_TYPEDEF_YY_BUFFER_STATE
  20. typedef struct yy_buffer_state *YY_BUFFER_STATE;
  21. namespace SkSL {
  22. struct Modifiers;
  23. class SymbolTable;
  24. /**
  25. * Consumes .sksl text and produces an abstract syntax tree describing the contents.
  26. */
  27. class Parser {
  28. public:
  29. enum class LayoutToken {
  30. LOCATION,
  31. OFFSET,
  32. BINDING,
  33. INDEX,
  34. SET,
  35. BUILTIN,
  36. INPUT_ATTACHMENT_INDEX,
  37. ORIGIN_UPPER_LEFT,
  38. OVERRIDE_COVERAGE,
  39. BLEND_SUPPORT_ALL_EQUATIONS,
  40. BLEND_SUPPORT_MULTIPLY,
  41. BLEND_SUPPORT_SCREEN,
  42. BLEND_SUPPORT_OVERLAY,
  43. BLEND_SUPPORT_DARKEN,
  44. BLEND_SUPPORT_LIGHTEN,
  45. BLEND_SUPPORT_COLORDODGE,
  46. BLEND_SUPPORT_COLORBURN,
  47. BLEND_SUPPORT_HARDLIGHT,
  48. BLEND_SUPPORT_SOFTLIGHT,
  49. BLEND_SUPPORT_DIFFERENCE,
  50. BLEND_SUPPORT_EXCLUSION,
  51. BLEND_SUPPORT_HSL_HUE,
  52. BLEND_SUPPORT_HSL_SATURATION,
  53. BLEND_SUPPORT_HSL_COLOR,
  54. BLEND_SUPPORT_HSL_LUMINOSITY,
  55. PUSH_CONSTANT,
  56. POINTS,
  57. LINES,
  58. LINE_STRIP,
  59. LINES_ADJACENCY,
  60. TRIANGLES,
  61. TRIANGLE_STRIP,
  62. TRIANGLES_ADJACENCY,
  63. MAX_VERTICES,
  64. INVOCATIONS,
  65. WHEN,
  66. KEY,
  67. TRACKED,
  68. CTYPE,
  69. SKPMCOLOR4F,
  70. SKVECTOR4,
  71. SKRECT,
  72. SKIRECT,
  73. SKPMCOLOR,
  74. SKMATRIX44,
  75. BOOL,
  76. INT,
  77. FLOAT,
  78. };
  79. Parser(const char* text, size_t length, SymbolTable& types, ErrorReporter& errors);
  80. /**
  81. * Consumes a complete .sksl file and returns the parse tree. Errors are reported via the
  82. * ErrorReporter; the return value may contain some declarations even when errors have occurred.
  83. */
  84. std::unique_ptr<ASTFile> file();
  85. StringFragment text(Token token);
  86. Position position(Token token);
  87. private:
  88. static void InitLayoutMap();
  89. /**
  90. * Return the next token, including whitespace tokens, from the parse stream.
  91. */
  92. Token nextRawToken();
  93. /**
  94. * Return the next non-whitespace token from the parse stream.
  95. */
  96. Token nextToken();
  97. /**
  98. * Push a token back onto the parse stream, so that it is the next one read. Only a single level
  99. * of pushback is supported (that is, it is an error to call pushback() twice in a row without
  100. * an intervening nextToken()).
  101. */
  102. void pushback(Token t);
  103. /**
  104. * Returns the next non-whitespace token without consuming it from the stream.
  105. */
  106. Token peek();
  107. /**
  108. * Checks to see if the next token is of the specified type. If so, stores it in result (if
  109. * result is non-null) and returns true. Otherwise, pushes it back and returns false.
  110. */
  111. bool checkNext(Token::Kind kind, Token* result = nullptr);
  112. /**
  113. * Reads the next non-whitespace token and generates an error if it is not the expected type.
  114. * The 'expected' string is part of the error message, which reads:
  115. *
  116. * "expected <expected>, but found '<actual text>'"
  117. *
  118. * If 'result' is non-null, it is set to point to the token that was read.
  119. * Returns true if the read token was as expected, false otherwise.
  120. */
  121. bool expect(Token::Kind kind, const char* expected, Token* result = nullptr);
  122. bool expect(Token::Kind kind, String expected, Token* result = nullptr);
  123. void error(Token token, String msg);
  124. void error(int offset, String msg);
  125. /**
  126. * Returns true if the 'name' identifier refers to a type name. For instance, isType("int") will
  127. * always return true.
  128. */
  129. bool isType(StringFragment name);
  130. // The pointer to the node may be invalidated by modifying the fNodes vector
  131. ASTNode& getNode(ASTNode::ID id) {
  132. SkASSERT(id.fValue >= 0 && id.fValue < (int) fFile->fNodes.size());
  133. return fFile->fNodes[id.fValue];
  134. }
  135. // these functions parse individual grammar rules from the current parse position; you probably
  136. // don't need to call any of these outside of the parser. The function declarations in the .cpp
  137. // file have comments describing the grammar rules.
  138. ASTNode::ID precision();
  139. ASTNode::ID directive();
  140. ASTNode::ID section();
  141. ASTNode::ID enumDeclaration();
  142. ASTNode::ID declaration();
  143. ASTNode::ID varDeclarations();
  144. ASTNode::ID structDeclaration();
  145. ASTNode::ID structVarDeclaration(Modifiers modifiers);
  146. ASTNode::ID varDeclarationEnd(Modifiers modifiers, ASTNode::ID type, StringFragment name);
  147. ASTNode::ID parameter();
  148. int layoutInt();
  149. StringFragment layoutIdentifier();
  150. StringFragment layoutCode();
  151. Layout::Key layoutKey();
  152. Layout::CType layoutCType();
  153. Layout layout();
  154. Modifiers modifiers();
  155. Modifiers modifiersWithDefaults(int defaultFlags);
  156. ASTNode::ID statement();
  157. ASTNode::ID type();
  158. ASTNode::ID interfaceBlock(Modifiers mods);
  159. ASTNode::ID ifStatement();
  160. ASTNode::ID doStatement();
  161. ASTNode::ID whileStatement();
  162. ASTNode::ID forStatement();
  163. ASTNode::ID switchCase();
  164. ASTNode::ID switchStatement();
  165. ASTNode::ID returnStatement();
  166. ASTNode::ID breakStatement();
  167. ASTNode::ID continueStatement();
  168. ASTNode::ID discardStatement();
  169. ASTNode::ID block();
  170. ASTNode::ID expressionStatement();
  171. ASTNode::ID expression();
  172. ASTNode::ID assignmentExpression();
  173. ASTNode::ID ternaryExpression();
  174. ASTNode::ID logicalOrExpression();
  175. ASTNode::ID logicalXorExpression();
  176. ASTNode::ID logicalAndExpression();
  177. ASTNode::ID bitwiseOrExpression();
  178. ASTNode::ID bitwiseXorExpression();
  179. ASTNode::ID bitwiseAndExpression();
  180. ASTNode::ID equalityExpression();
  181. ASTNode::ID relationalExpression();
  182. ASTNode::ID shiftExpression();
  183. ASTNode::ID additiveExpression();
  184. ASTNode::ID multiplicativeExpression();
  185. ASTNode::ID unaryExpression();
  186. ASTNode::ID postfixExpression();
  187. ASTNode::ID suffix(ASTNode::ID base);
  188. ASTNode::ID term();
  189. bool intLiteral(SKSL_INT* dest);
  190. bool floatLiteral(SKSL_FLOAT* dest);
  191. bool boolLiteral(bool* dest);
  192. bool identifier(StringFragment* dest);
  193. static std::unordered_map<String, LayoutToken>* layoutTokens;
  194. const char* fText;
  195. Lexer fLexer;
  196. YY_BUFFER_STATE fBuffer;
  197. // current parse depth, used to enforce a recursion limit to try to keep us from overflowing the
  198. // stack on pathological inputs
  199. int fDepth = 0;
  200. Token fPushback;
  201. SymbolTable& fTypes;
  202. ErrorReporter& fErrors;
  203. std::unique_ptr<ASTFile> fFile;
  204. friend class AutoDepth;
  205. friend class HCodeGenerator;
  206. };
  207. } // namespace
  208. #endif