OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 | 7 |
8 #include "stdio.h" | 8 #include "stdio.h" |
9 #include "SkSLParser.h" | 9 #include "SkSLParser.h" |
10 #include "SkSLToken.h" | 10 #include "SkSLToken.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 #include "ast/SkSLASTTernaryExpression.h" | 66 #include "ast/SkSLASTTernaryExpression.h" |
67 #include "ast/SkSLASTType.h" | 67 #include "ast/SkSLASTType.h" |
68 #include "ast/SkSLASTVarDeclaration.h" | 68 #include "ast/SkSLASTVarDeclaration.h" |
69 #include "ast/SkSLASTVarDeclarationStatement.h" | 69 #include "ast/SkSLASTVarDeclarationStatement.h" |
70 #include "ast/SkSLASTWhileStatement.h" | 70 #include "ast/SkSLASTWhileStatement.h" |
71 #include "ir/SkSLSymbolTable.h" | 71 #include "ir/SkSLSymbolTable.h" |
72 #include "ir/SkSLType.h" | 72 #include "ir/SkSLType.h" |
73 | 73 |
74 namespace SkSL { | 74 namespace SkSL { |
75 | 75 |
| 76 #define MAX_PARSE_DEPTH 50 |
| 77 |
| 78 class AutoDepth { |
| 79 public: |
| 80 AutoDepth(Parser* p) |
| 81 : fParser(p) { |
| 82 fParser->fDepth++; |
| 83 } |
| 84 |
| 85 ~AutoDepth() { |
| 86 fParser->fDepth--; |
| 87 } |
| 88 |
| 89 bool checkValid() { |
| 90 if (fParser->fDepth > MAX_PARSE_DEPTH) { |
| 91 fParser->error(fParser->peek().fPosition, "exceeded max parse depth"
); |
| 92 return false; |
| 93 } |
| 94 return true; |
| 95 } |
| 96 |
| 97 private: |
| 98 Parser* fParser; |
| 99 }; |
| 100 |
76 Parser::Parser(std::string text, SymbolTable& types, ErrorReporter& errors) | 101 Parser::Parser(std::string text, SymbolTable& types, ErrorReporter& errors) |
77 : fPushback(Position(-1, -1), Token::INVALID_TOKEN, "") | 102 : fPushback(Position(-1, -1), Token::INVALID_TOKEN, "") |
78 , fTypes(types) | 103 , fTypes(types) |
79 , fErrors(errors) { | 104 , fErrors(errors) { |
80 sksllex_init(&fScanner); | 105 sksllex_init(&fScanner); |
81 fBuffer = sksl_scan_string(text.c_str(), fScanner); | 106 fBuffer = sksl_scan_string(text.c_str(), fScanner); |
82 skslset_lineno(1, fScanner); | 107 skslset_lineno(1, fScanner); |
83 | 108 |
84 if (false) { | 109 if (false) { |
85 // avoid unused warning | 110 // avoid unused warning |
(...skipping 827 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
913 return nullptr; | 938 return nullptr; |
914 } | 939 } |
915 if (!this->expect(Token::SEMICOLON, "';'")) { | 940 if (!this->expect(Token::SEMICOLON, "';'")) { |
916 return nullptr; | 941 return nullptr; |
917 } | 942 } |
918 return std::unique_ptr<ASTDiscardStatement>(new ASTDiscardStatement(start.fP
osition)); | 943 return std::unique_ptr<ASTDiscardStatement>(new ASTDiscardStatement(start.fP
osition)); |
919 } | 944 } |
920 | 945 |
921 /* LBRACE statement* RBRACE */ | 946 /* LBRACE statement* RBRACE */ |
922 std::unique_ptr<ASTBlock> Parser::block() { | 947 std::unique_ptr<ASTBlock> Parser::block() { |
| 948 AutoDepth depth(this); |
| 949 if (!depth.checkValid()) { |
| 950 return nullptr; |
| 951 } |
923 Token start; | 952 Token start; |
924 if (!this->expect(Token::LBRACE, "'{'", &start)) { | 953 if (!this->expect(Token::LBRACE, "'{'", &start)) { |
925 return nullptr; | 954 return nullptr; |
926 } | 955 } |
927 std::vector<std::unique_ptr<ASTStatement>> statements; | 956 std::vector<std::unique_ptr<ASTStatement>> statements; |
928 for (;;) { | 957 for (;;) { |
929 switch (this->peek().fKind) { | 958 switch (this->peek().fKind) { |
930 case Token::RBRACE: | 959 case Token::RBRACE: |
931 this->nextToken(); | 960 this->nextToken(); |
932 return std::unique_ptr<ASTBlock>(new ASTBlock(start.fPosition, | 961 return std::unique_ptr<ASTBlock>(new ASTBlock(start.fPosition, |
(...skipping 19 matching lines...) Expand all Loading... |
952 if (this->expect(Token::SEMICOLON, "';'")) { | 981 if (this->expect(Token::SEMICOLON, "';'")) { |
953 ASTExpressionStatement* result = new ASTExpressionStatement(std::mov
e(expr)); | 982 ASTExpressionStatement* result = new ASTExpressionStatement(std::mov
e(expr)); |
954 return std::unique_ptr<ASTExpressionStatement>(result); | 983 return std::unique_ptr<ASTExpressionStatement>(result); |
955 } | 984 } |
956 } | 985 } |
957 return nullptr; | 986 return nullptr; |
958 } | 987 } |
959 | 988 |
960 /* assignmentExpression */ | 989 /* assignmentExpression */ |
961 std::unique_ptr<ASTExpression> Parser::expression() { | 990 std::unique_ptr<ASTExpression> Parser::expression() { |
| 991 AutoDepth depth(this); |
| 992 if (!depth.checkValid()) { |
| 993 return nullptr; |
| 994 } |
962 return this->assignmentExpression(); | 995 return this->assignmentExpression(); |
963 } | 996 } |
964 | 997 |
965 /* ternaryExpression ((EQEQ | STAREQ | SLASHEQ | PERCENTEQ | PLUSEQ | MINUSEQ |
SHLEQ | SHREQ | | 998 /* ternaryExpression ((EQEQ | STAREQ | SLASHEQ | PERCENTEQ | PLUSEQ | MINUSEQ |
SHLEQ | SHREQ | |
966 BITWISEANDEQ | BITWISEXOREQ | BITWISEOREQ | LOGICALANDEQ | LOGICALXOREQ | LOG
ICALOREQ) | 999 BITWISEANDEQ | BITWISEXOREQ | BITWISEOREQ | LOGICALANDEQ | LOGICALXOREQ | LOG
ICALOREQ) |
967 assignmentExpression)* | 1000 assignmentExpression)* |
968 */ | 1001 */ |
969 std::unique_ptr<ASTExpression> Parser::assignmentExpression() { | 1002 std::unique_ptr<ASTExpression> Parser::assignmentExpression() { |
970 std::unique_ptr<ASTExpression> result = this->ternaryExpression(); | 1003 std::unique_ptr<ASTExpression> result = this->ternaryExpression(); |
971 if (!result) { | 1004 if (!result) { |
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1445 bool Parser::identifier(std::string* dest) { | 1478 bool Parser::identifier(std::string* dest) { |
1446 Token t; | 1479 Token t; |
1447 if (this->expect(Token::IDENTIFIER, "identifier", &t)) { | 1480 if (this->expect(Token::IDENTIFIER, "identifier", &t)) { |
1448 *dest = t.fText; | 1481 *dest = t.fText; |
1449 return true; | 1482 return true; |
1450 } | 1483 } |
1451 return false; | 1484 return false; |
1452 } | 1485 } |
1453 | 1486 |
1454 } // namespace | 1487 } // namespace |
OLD | NEW |