michael@0: /* michael@0: // michael@0: // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: This file contains the Yacc grammar for GLSL ES preprocessor expression. michael@0: michael@0: IF YOU MODIFY THIS FILE YOU ALSO NEED TO RUN generate_parser.sh, michael@0: WHICH GENERATES THE GLSL ES preprocessor expression parser. michael@0: */ michael@0: michael@0: %{ michael@0: // michael@0: // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // This file is auto-generated by generate_parser.sh. DO NOT EDIT! michael@0: michael@0: #if defined(__GNUC__) michael@0: // Triggered by the auto-generated pplval variable. michael@0: #if !defined(__clang__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) michael@0: #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" michael@0: #else michael@0: #pragma GCC diagnostic ignored "-Wuninitialized" michael@0: #endif michael@0: #elif defined(_MSC_VER) michael@0: #pragma warning(disable: 4065 4701) michael@0: #endif michael@0: michael@0: #include "ExpressionParser.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "DiagnosticsBase.h" michael@0: #include "Lexer.h" michael@0: #include "Token.h" michael@0: michael@0: #if defined(_MSC_VER) michael@0: typedef __int64 YYSTYPE; michael@0: #else michael@0: #include michael@0: typedef intmax_t YYSTYPE; michael@0: #endif // _MSC_VER michael@0: #define YYENABLE_NLS 0 michael@0: #define YYLTYPE_IS_TRIVIAL 1 michael@0: #define YYSTYPE_IS_TRIVIAL 1 michael@0: #define YYSTYPE_IS_DECLARED 1 michael@0: michael@0: namespace { michael@0: struct Context michael@0: { michael@0: pp::Diagnostics* diagnostics; michael@0: pp::Lexer* lexer; michael@0: pp::Token* token; michael@0: int* result; michael@0: }; michael@0: } // namespace michael@0: %} michael@0: michael@0: %pure-parser michael@0: %name-prefix="pp" michael@0: %parse-param {Context *context} michael@0: %lex-param {Context *context} michael@0: michael@0: %{ michael@0: static int yylex(YYSTYPE* lvalp, Context* context); michael@0: static void yyerror(Context* context, const char* reason); michael@0: %} michael@0: michael@0: %token TOK_CONST_INT michael@0: %left TOK_OP_OR michael@0: %left TOK_OP_AND michael@0: %left '|' michael@0: %left '^' michael@0: %left '&' michael@0: %left TOK_OP_EQ TOK_OP_NE michael@0: %left '<' '>' TOK_OP_LE TOK_OP_GE michael@0: %left TOK_OP_LEFT TOK_OP_RIGHT michael@0: %left '+' '-' michael@0: %left '*' '/' '%' michael@0: %right TOK_UNARY michael@0: michael@0: %% michael@0: michael@0: input michael@0: : expression { michael@0: *(context->result) = static_cast($1); michael@0: YYACCEPT; michael@0: } michael@0: ; michael@0: michael@0: expression michael@0: : TOK_CONST_INT michael@0: | expression TOK_OP_OR expression { michael@0: $$ = $1 || $3; michael@0: } michael@0: | expression TOK_OP_AND expression { michael@0: $$ = $1 && $3; michael@0: } michael@0: | expression '|' expression { michael@0: $$ = $1 | $3; michael@0: } michael@0: | expression '^' expression { michael@0: $$ = $1 ^ $3; michael@0: } michael@0: | expression '&' expression { michael@0: $$ = $1 & $3; michael@0: } michael@0: | expression TOK_OP_NE expression { michael@0: $$ = $1 != $3; michael@0: } michael@0: | expression TOK_OP_EQ expression { michael@0: $$ = $1 == $3; michael@0: } michael@0: | expression TOK_OP_GE expression { michael@0: $$ = $1 >= $3; michael@0: } michael@0: | expression TOK_OP_LE expression { michael@0: $$ = $1 <= $3; michael@0: } michael@0: | expression '>' expression { michael@0: $$ = $1 > $3; michael@0: } michael@0: | expression '<' expression { michael@0: $$ = $1 < $3; michael@0: } michael@0: | expression TOK_OP_RIGHT expression { michael@0: $$ = $1 >> $3; michael@0: } michael@0: | expression TOK_OP_LEFT expression { michael@0: $$ = $1 << $3; michael@0: } michael@0: | expression '-' expression { michael@0: $$ = $1 - $3; michael@0: } michael@0: | expression '+' expression { michael@0: $$ = $1 + $3; michael@0: } michael@0: | expression '%' expression { michael@0: if ($3 == 0) { michael@0: std::ostringstream stream; michael@0: stream << $1 << " % " << $3; michael@0: std::string text = stream.str(); michael@0: context->diagnostics->report(pp::Diagnostics::DIVISION_BY_ZERO, michael@0: context->token->location, michael@0: text.c_str()); michael@0: YYABORT; michael@0: } else { michael@0: $$ = $1 % $3; michael@0: } michael@0: } michael@0: | expression '/' expression { michael@0: if ($3 == 0) { michael@0: std::ostringstream stream; michael@0: stream << $1 << " / " << $3; michael@0: std::string text = stream.str(); michael@0: context->diagnostics->report(pp::Diagnostics::DIVISION_BY_ZERO, michael@0: context->token->location, michael@0: text.c_str()); michael@0: YYABORT; michael@0: } else { michael@0: $$ = $1 / $3; michael@0: } michael@0: } michael@0: | expression '*' expression { michael@0: $$ = $1 * $3; michael@0: } michael@0: | '!' expression %prec TOK_UNARY { michael@0: $$ = ! $2; michael@0: } michael@0: | '~' expression %prec TOK_UNARY { michael@0: $$ = ~ $2; michael@0: } michael@0: | '-' expression %prec TOK_UNARY { michael@0: $$ = - $2; michael@0: } michael@0: | '+' expression %prec TOK_UNARY { michael@0: $$ = + $2; michael@0: } michael@0: | '(' expression ')' { michael@0: $$ = $2; michael@0: } michael@0: ; michael@0: michael@0: %% michael@0: michael@0: int yylex(YYSTYPE* lvalp, Context* context) michael@0: { michael@0: int type = 0; michael@0: michael@0: pp::Token* token = context->token; michael@0: switch (token->type) michael@0: { michael@0: case pp::Token::CONST_INT: michael@0: { michael@0: unsigned int val = 0; michael@0: if (!token->uValue(&val)) michael@0: { michael@0: context->diagnostics->report(pp::Diagnostics::INTEGER_OVERFLOW, michael@0: token->location, token->text); michael@0: } michael@0: *lvalp = static_cast(val); michael@0: type = TOK_CONST_INT; michael@0: break; michael@0: } michael@0: case pp::Token::OP_OR: type = TOK_OP_OR; break; michael@0: case pp::Token::OP_AND: type = TOK_OP_AND; break; michael@0: case pp::Token::OP_NE: type = TOK_OP_NE; break; michael@0: case pp::Token::OP_EQ: type = TOK_OP_EQ; break; michael@0: case pp::Token::OP_GE: type = TOK_OP_GE; break; michael@0: case pp::Token::OP_LE: type = TOK_OP_LE; break; michael@0: case pp::Token::OP_RIGHT: type = TOK_OP_RIGHT; break; michael@0: case pp::Token::OP_LEFT: type = TOK_OP_LEFT; break; michael@0: case '|': type = '|'; break; michael@0: case '^': type = '^'; break; michael@0: case '&': type = '&'; break; michael@0: case '>': type = '>'; break; michael@0: case '<': type = '<'; break; michael@0: case '-': type = '-'; break; michael@0: case '+': type = '+'; break; michael@0: case '%': type = '%'; break; michael@0: case '/': type = '/'; break; michael@0: case '*': type = '*'; break; michael@0: case '!': type = '!'; break; michael@0: case '~': type = '~'; break; michael@0: case '(': type = '('; break; michael@0: case ')': type = ')'; break; michael@0: michael@0: default: break; michael@0: } michael@0: michael@0: // Advance to the next token if the current one is valid. michael@0: if (type != 0) context->lexer->lex(token); michael@0: michael@0: return type; michael@0: } michael@0: michael@0: void yyerror(Context* context, const char* reason) michael@0: { michael@0: context->diagnostics->report(pp::Diagnostics::INVALID_EXPRESSION, michael@0: context->token->location, michael@0: reason); michael@0: } michael@0: michael@0: namespace pp { michael@0: michael@0: ExpressionParser::ExpressionParser(Lexer* lexer, Diagnostics* diagnostics) : michael@0: mLexer(lexer), michael@0: mDiagnostics(diagnostics) michael@0: { michael@0: } michael@0: michael@0: bool ExpressionParser::parse(Token* token, int* result) michael@0: { michael@0: Context context; michael@0: context.diagnostics = mDiagnostics; michael@0: context.lexer = mLexer; michael@0: context.token = token; michael@0: context.result = result; michael@0: int ret = yyparse(&context); michael@0: switch (ret) michael@0: { michael@0: case 0: michael@0: case 1: michael@0: break; michael@0: michael@0: case 2: michael@0: mDiagnostics->report(Diagnostics::OUT_OF_MEMORY, token->location, ""); michael@0: break; michael@0: michael@0: default: michael@0: assert(false); michael@0: mDiagnostics->report(Diagnostics::INTERNAL_ERROR, token->location, ""); michael@0: break; michael@0: } michael@0: michael@0: return ret == 0; michael@0: } michael@0: michael@0: } // namespace pp