[gtk+/wip/otte/shader: 41/150] gsksl: Make expression parsing never fail
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/wip/otte/shader: 41/150] gsksl: Make expression parsing never fail
- Date: Sat, 21 Oct 2017 02:29:37 +0000 (UTC)
commit 05db43ee32821fdb84767fbf9efbafdaaa647cbc
Author: Benjamin Otte <otte redhat com>
Date: Wed Sep 27 17:34:02 2017 +0200
gsksl: Make expression parsing never fail
Code will always return a valid expression - for an invlaid token
stream, that might be a very random expression, but still, it's an
expression.
gsk/gskslexpression.c | 229 ++++++++++++++-----------------------------------
gsk/gsksltype.c | 2 +-
2 files changed, 65 insertions(+), 166 deletions(-)
---
diff --git a/gsk/gskslexpression.c b/gsk/gskslexpression.c
index 4bd676f..ceba4e8 100644
--- a/gsk/gskslexpression.c
+++ b/gsk/gskslexpression.c
@@ -1025,6 +1025,18 @@ static const GskSlExpressionClass GSK_SL_EXPRESSION_CONSTANT = {
gsk_sl_expression_constant_write_spv
};
+/* If parsing fails completely, just assume 1.0 */
+static GskSlExpression *
+gsk_sl_expression_error_new (void)
+{
+ GskSlExpressionConstant *constant;
+
+ constant = gsk_sl_expression_new (GskSlExpressionConstant, &GSK_SL_EXPRESSION_CONSTANT);
+ constant->value = gsk_sl_value_new (gsk_sl_type_get_scalar (GSK_SL_FLOAT));
+
+ return (GskSlExpression *) constant;
+}
+
GskSlExpression *
gsk_sl_expression_parse_constructor_call (GskSlScope *scope,
GskSlPreprocessor *stream,
@@ -1033,8 +1045,7 @@ gsk_sl_expression_parse_constructor_call (GskSlScope *scope,
GskSlExpressionFunctionCall *call;
const GskSlToken *token;
GskSlType **types;
- GError *err = NULL;
- gboolean fail = FALSE;
+ GError *error = NULL;
guint i;
call = gsk_sl_expression_new (GskSlExpressionFunctionCall, &GSK_SL_EXPRESSION_FUNCTION_CALL);
@@ -1045,8 +1056,7 @@ gsk_sl_expression_parse_constructor_call (GskSlScope *scope,
if (!gsk_sl_token_is (token, GSK_SL_TOKEN_LEFT_PAREN))
{
gsk_sl_preprocessor_error (stream, SYNTAX, "Expected opening \"(\" when calling constructor");
- gsk_sl_expression_unref ((GskSlExpression *) call);
- return NULL;
+ return (GskSlExpression *) call;
}
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) call);
@@ -1060,10 +1070,7 @@ gsk_sl_expression_parse_constructor_call (GskSlScope *scope,
{
GskSlExpression *expression = gsk_sl_expression_parse_assignment (scope, stream);
- if (expression != NULL)
- g_ptr_array_add (arguments, expression);
- else
- fail = TRUE;
+ g_ptr_array_add (arguments, expression);
token = gsk_sl_preprocessor_get (stream);
if (!gsk_sl_token_is (token, GSK_SL_TOKEN_COMMA))
@@ -1078,27 +1085,19 @@ gsk_sl_expression_parse_constructor_call (GskSlScope *scope,
types = g_newa (GskSlType *, call->n_arguments);
for (i = 0; i < call->n_arguments; i++)
types[i] = gsk_sl_expression_get_return_type (call->arguments[i]);
- if (!gsk_sl_function_matches (call->function, types, call->n_arguments, &err))
+ if (!gsk_sl_function_matches (call->function, types, call->n_arguments, &error))
{
- gsk_sl_preprocessor_emit_error (stream, TRUE, gsk_sl_preprocessor_get_location (stream), err);
- g_clear_error (&err);
- fail = TRUE;
+ gsk_sl_preprocessor_emit_error (stream, TRUE, gsk_sl_preprocessor_get_location (stream), error);
+ g_clear_error (&error);
}
if (!gsk_sl_token_is (token, GSK_SL_TOKEN_RIGHT_PAREN))
{
gsk_sl_preprocessor_error (stream, SYNTAX, "Expected closing \")\" after arguments.");
- gsk_sl_expression_unref ((GskSlExpression *) call);
- return NULL;
+ return (GskSlExpression *) call;
}
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) call);
- if (fail)
- {
- gsk_sl_expression_unref ((GskSlExpression *) call);
- return NULL;
- }
-
return (GskSlExpression *) call;
}
@@ -1122,7 +1121,7 @@ gsk_sl_expression_parse_primary (GskSlScope *scope,
{
gsk_sl_preprocessor_error (stream, DECLARATION, "No variable named \"%s\".", token->str);
gsk_sl_preprocessor_consume (stream, NULL);
- return NULL;
+ return gsk_sl_expression_error_new ();
}
reference = gsk_sl_expression_new (GskSlExpressionReference, &GSK_SL_EXPRESSION_REFERENCE);
@@ -1215,8 +1214,6 @@ gsk_sl_expression_parse_primary (GskSlScope *scope,
GskSlType *type;
type = gsk_sl_type_new_parse (stream);
- if (type == NULL)
- return NULL;
return gsk_sl_expression_parse_constructor_call (scope, stream, type);
}
@@ -1224,7 +1221,7 @@ gsk_sl_expression_parse_primary (GskSlScope *scope,
default:
gsk_sl_preprocessor_error (stream, SYNTAX, "Expected an expression.");
gsk_sl_preprocessor_consume (stream, NULL);
- return NULL;
+ return gsk_sl_expression_error_new ();
}
}
@@ -1232,15 +1229,13 @@ static GskSlExpression *
gsk_sl_expression_parse_field_selection (GskSlScope *scope,
GskSlPreprocessor *stream,
GskSlExpression *expr,
- const char *name,
- gboolean *success)
+ const char *name)
{
GskSlType *type;
if (g_str_equal (name, "length"))
{
gsk_sl_preprocessor_error (stream, UNSUPPORTED, ".length() is not implemented yet.");
- *success = FALSE;
return expr;
}
@@ -1263,11 +1258,8 @@ gsk_sl_expression_parse_field_selection (GskSlScope *scope,
{
gsk_sl_preprocessor_error (stream, TYPE_MISMATCH, "Type %s has no member named \"%s\".",
gsk_sl_type_get_name (type), name);
gsk_sl_expression_unref ((GskSlExpression *) swizzle);
- *success = FALSE;
return expr;
}
- swizzle->expr = expr;
-
for (swizzle->length = 0; swizzle->length < 4 && name[swizzle->length]; swizzle->length++)
{
const char *found = strchr (swizzle_options[swizzle->name], name[swizzle->length]);
@@ -1276,8 +1268,8 @@ gsk_sl_expression_parse_field_selection (GskSlScope *scope,
gsk_sl_preprocessor_error (stream, SYNTAX,
"Character '%c' is not valid for swizzle. Must be one of \"%s\".",
name[swizzle->length], swizzle_options[swizzle->name]);
- *success = FALSE;
- return (GskSlExpression *) swizzle;
+ gsk_sl_expression_unref ((GskSlExpression *) swizzle);
+ return expr;
}
swizzle->indexes[swizzle->length] = found - swizzle_options[swizzle->name];
if (swizzle->indexes[swizzle->length] >= type_length)
@@ -1285,14 +1277,15 @@ gsk_sl_expression_parse_field_selection (GskSlScope *scope,
gsk_sl_preprocessor_error (stream, SYNTAX,
"Swizzle index '%c' not allowed for type %s",
name[swizzle->length], gsk_sl_type_get_name (type));
- *success = FALSE;
- return (GskSlExpression *) swizzle;
+ gsk_sl_expression_unref ((GskSlExpression *) swizzle);
+ return expr;
}
}
+ swizzle->expr = expr;
+
if (name[swizzle->length])
{
gsk_sl_preprocessor_error (stream, SYNTAX, "Too many swizzle options. A maximum of 4 characters
are allowed.");
- *success = FALSE;
return (GskSlExpression *) swizzle;
}
@@ -1301,7 +1294,6 @@ gsk_sl_expression_parse_field_selection (GskSlScope *scope,
else
{
gsk_sl_preprocessor_error (stream, TYPE_MISMATCH, "Type %s has no fields to select.",
gsk_sl_type_get_name (type));
- *success = FALSE;
return expr;
}
}
@@ -1312,7 +1304,6 @@ gsk_sl_expression_parse_postfix (GskSlScope *scope,
{
GskSlExpression *expr;
const GskSlToken *token;
- gboolean success = TRUE;
expr = gsk_sl_expression_parse_primary (scope, stream);
@@ -1327,13 +1318,12 @@ gsk_sl_expression_parse_postfix (GskSlScope *scope,
{
char *field = g_strdup (token->str);
gsk_sl_preprocessor_consume (stream, NULL);
- expr = gsk_sl_expression_parse_field_selection (scope, stream, expr, field, &success);
+ expr = gsk_sl_expression_parse_field_selection (scope, stream, expr, field);
g_free (field);
}
else
{
gsk_sl_preprocessor_error (stream, SYNTAX, "Expected an identifier to select a field.");
- success = FALSE;
continue;
}
}
@@ -1343,12 +1333,6 @@ gsk_sl_expression_parse_postfix (GskSlScope *scope,
}
}
- if (!success)
- {
- gsk_sl_expression_unref (expr);
- return NULL;
- }
-
return expr;
}
@@ -1369,8 +1353,6 @@ gsk_sl_expression_parse_multiplicative (GskSlScope *scope,
GskSlOperation op;
expression = gsk_sl_expression_parse_unary (scope, stream);
- if (expression == NULL)
- return NULL;
while (TRUE)
{
@@ -1389,20 +1371,15 @@ gsk_sl_expression_parse_multiplicative (GskSlScope *scope,
operation->op = op;
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) operation);
operation->right = gsk_sl_expression_parse_unary (scope, stream);
- if (operation->right == NULL)
- {
- gsk_sl_expression_ref (expression);
- gsk_sl_expression_unref ((GskSlExpression *) operation);
- }
- else if ((op == GSK_SL_OPERATION_MOD &&
- !gsk_sl_expression_bitwise_type_check (stream,
- gsk_sl_expression_get_return_type (operation->left),
- gsk_sl_expression_get_return_type (operation->right))) ||
- (op != GSK_SL_OPERATION_MOD &&
- !gsk_sl_expression_arithmetic_type_check (stream,
- FALSE,
- gsk_sl_expression_get_return_type (operation->left),
- gsk_sl_expression_get_return_type (operation->right))))
+ if ((op == GSK_SL_OPERATION_MOD &&
+ !gsk_sl_expression_bitwise_type_check (stream,
+ gsk_sl_expression_get_return_type (operation->left),
+ gsk_sl_expression_get_return_type (operation->right))) ||
+ (op != GSK_SL_OPERATION_MOD &&
+ !gsk_sl_expression_arithmetic_type_check (stream,
+ FALSE,
+ gsk_sl_expression_get_return_type (operation->left),
+ gsk_sl_expression_get_return_type (operation->right))))
{
gsk_sl_expression_ref (expression);
gsk_sl_expression_unref ((GskSlExpression *) operation);
@@ -1444,15 +1421,10 @@ gsk_sl_expression_parse_additive (GskSlScope *scope,
operation->op = op;
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) operation);
operation->right = gsk_sl_expression_parse_additive (scope, stream);
- if (operation->right == NULL)
- {
- gsk_sl_expression_ref (expression);
- gsk_sl_expression_unref ((GskSlExpression *) operation);
- }
- else if (!gsk_sl_expression_arithmetic_type_check (stream,
- FALSE,
- gsk_sl_expression_get_return_type (operation->left),
- gsk_sl_expression_get_return_type (operation->right)))
+ if (!gsk_sl_expression_arithmetic_type_check (stream,
+ FALSE,
+ gsk_sl_expression_get_return_type (operation->left),
+ gsk_sl_expression_get_return_type (operation->right)))
{
gsk_sl_expression_ref (expression);
gsk_sl_expression_unref ((GskSlExpression *) operation);
@@ -1476,8 +1448,6 @@ gsk_sl_expression_parse_shift (GskSlScope *scope,
GskSlOperation op;
expression = gsk_sl_expression_parse_additive (scope, stream);
- if (expression == NULL)
- return NULL;
while (TRUE)
{
@@ -1494,14 +1464,9 @@ gsk_sl_expression_parse_shift (GskSlScope *scope,
operation->op = op;
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) operation);
operation->right = gsk_sl_expression_parse_additive (scope, stream);
- if (operation->right == NULL)
- {
- gsk_sl_expression_ref (expression);
- gsk_sl_expression_unref ((GskSlExpression *) operation);
- }
- else if (!gsk_sl_expression_shift_type_check (stream,
- gsk_sl_expression_get_return_type (operation->left),
- gsk_sl_expression_get_return_type (operation->right)))
+ if (!gsk_sl_expression_shift_type_check (stream,
+ gsk_sl_expression_get_return_type (operation->left),
+ gsk_sl_expression_get_return_type (operation->right)))
{
gsk_sl_expression_ref (expression);
gsk_sl_expression_unref ((GskSlExpression *) operation);
@@ -1525,8 +1490,6 @@ gsk_sl_expression_parse_relational (GskSlScope *scope,
GskSlOperation op;
expression = gsk_sl_expression_parse_shift (scope, stream);
- if (expression == NULL)
- return NULL;
while (TRUE)
{
@@ -1547,14 +1510,9 @@ gsk_sl_expression_parse_relational (GskSlScope *scope,
operation->op = op;
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) operation);
operation->right = gsk_sl_expression_parse_shift (scope, stream);
- if (operation->right == NULL)
- {
- gsk_sl_expression_ref (expression);
- gsk_sl_expression_unref ((GskSlExpression *) operation);
- }
- else if (!gsk_sl_expression_relational_type_check (stream,
- gsk_sl_expression_get_return_type (operation->left),
- gsk_sl_expression_get_return_type (operation->right)))
+ if (!gsk_sl_expression_relational_type_check (stream,
+ gsk_sl_expression_get_return_type (operation->left),
+ gsk_sl_expression_get_return_type (operation->right)))
{
gsk_sl_expression_ref (expression);
gsk_sl_expression_unref ((GskSlExpression *) operation);
@@ -1578,8 +1536,6 @@ gsk_sl_expression_parse_equality (GskSlScope *scope,
GskSlOperation op;
expression = gsk_sl_expression_parse_relational (scope, stream);
- if (expression == NULL)
- return NULL;
while (TRUE)
{
@@ -1596,15 +1552,7 @@ gsk_sl_expression_parse_equality (GskSlScope *scope,
operation->op = op;
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) operation);
operation->right = gsk_sl_expression_parse_relational (scope, stream);
- if (operation->right == NULL)
- {
- gsk_sl_expression_ref (expression);
- gsk_sl_expression_unref ((GskSlExpression *) operation);
- }
- else
- {
- expression = (GskSlExpression *) operation;
- }
+ expression = (GskSlExpression *) operation;
}
return expression;
@@ -1619,8 +1567,6 @@ gsk_sl_expression_parse_and (GskSlScope *scope,
GskSlExpressionOperation *operation;
expression = gsk_sl_expression_parse_equality (scope, stream);
- if (expression == NULL)
- return NULL;
while (TRUE)
{
@@ -1633,14 +1579,9 @@ gsk_sl_expression_parse_and (GskSlScope *scope,
operation->op = GSK_SL_OPERATION_AND;
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) operation);
operation->right = gsk_sl_expression_parse_equality (scope, stream);
- if (operation->right == NULL)
- {
- gsk_sl_expression_ref (expression);
- gsk_sl_expression_unref ((GskSlExpression *) operation);
- }
- else if (!gsk_sl_expression_bitwise_type_check (stream,
- gsk_sl_expression_get_return_type (operation->left),
- gsk_sl_expression_get_return_type (operation->right)))
+ if (!gsk_sl_expression_bitwise_type_check (stream,
+ gsk_sl_expression_get_return_type (operation->left),
+ gsk_sl_expression_get_return_type (operation->right)))
{
gsk_sl_expression_ref (expression);
gsk_sl_expression_unref ((GskSlExpression *) operation);
@@ -1663,8 +1604,6 @@ gsk_sl_expression_parse_xor (GskSlScope *scope,
GskSlExpressionOperation *operation;
expression = gsk_sl_expression_parse_and (scope, stream);
- if (expression == NULL)
- return NULL;
while (TRUE)
{
@@ -1677,14 +1616,9 @@ gsk_sl_expression_parse_xor (GskSlScope *scope,
operation->op = GSK_SL_OPERATION_XOR;
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) operation);
operation->right = gsk_sl_expression_parse_and (scope, stream);
- if (operation->right == NULL)
- {
- gsk_sl_expression_ref (expression);
- gsk_sl_expression_unref ((GskSlExpression *) operation);
- }
- else if (!gsk_sl_expression_bitwise_type_check (stream,
- gsk_sl_expression_get_return_type (operation->left),
- gsk_sl_expression_get_return_type (operation->right)))
+ if (!gsk_sl_expression_bitwise_type_check (stream,
+ gsk_sl_expression_get_return_type (operation->left),
+ gsk_sl_expression_get_return_type (operation->right)))
{
gsk_sl_expression_ref (expression);
gsk_sl_expression_unref ((GskSlExpression *) operation);
@@ -1707,8 +1641,6 @@ gsk_sl_expression_parse_or (GskSlScope *scope,
GskSlExpressionOperation *operation;
expression = gsk_sl_expression_parse_xor (scope, stream);
- if (expression == NULL)
- return NULL;
while (TRUE)
{
@@ -1721,14 +1653,9 @@ gsk_sl_expression_parse_or (GskSlScope *scope,
operation->op = GSK_SL_OPERATION_OR;
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) operation);
operation->right = gsk_sl_expression_parse_xor (scope, stream);
- if (operation->right == NULL)
- {
- gsk_sl_expression_ref (expression);
- gsk_sl_expression_unref ((GskSlExpression *) operation);
- }
- else if (!gsk_sl_expression_bitwise_type_check (stream,
- gsk_sl_expression_get_return_type (operation->left),
- gsk_sl_expression_get_return_type (operation->right)))
+ if (!gsk_sl_expression_bitwise_type_check (stream,
+ gsk_sl_expression_get_return_type (operation->left),
+ gsk_sl_expression_get_return_type (operation->right)))
{
gsk_sl_expression_ref (expression);
gsk_sl_expression_unref ((GskSlExpression *) operation);
@@ -1751,8 +1678,6 @@ gsk_sl_expression_parse_logical_and (GskSlScope *scope,
GskSlExpressionOperation *operation;
expression = gsk_sl_expression_parse_or (scope, stream);
- if (expression == NULL)
- return NULL;
while (TRUE)
{
@@ -1765,13 +1690,8 @@ gsk_sl_expression_parse_logical_and (GskSlScope *scope,
operation->op = GSK_SL_OPERATION_LOGICAL_AND;
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) operation);
operation->right = gsk_sl_expression_parse_or (scope, stream);
- if (operation->right == NULL)
- {
- gsk_sl_expression_ref (expression);
- gsk_sl_expression_unref ((GskSlExpression *) operation);
- }
- else if (!gsk_sl_type_can_convert (gsk_sl_type_get_scalar (GSK_SL_BOOL),
- gsk_sl_expression_get_return_type (operation->right)))
+ if (!gsk_sl_type_can_convert (gsk_sl_type_get_scalar (GSK_SL_BOOL),
+ gsk_sl_expression_get_return_type (operation->right)))
{
gsk_sl_preprocessor_error (stream, TYPE_MISMATCH,
"Right operand of && expression is not bool but %s",
@@ -1807,8 +1727,6 @@ gsk_sl_expression_parse_logical_xor (GskSlScope *scope,
GskSlExpressionOperation *operation;
expression = gsk_sl_expression_parse_logical_and (scope, stream);
- if (expression == NULL)
- return NULL;
while (TRUE)
{
@@ -1821,13 +1739,8 @@ gsk_sl_expression_parse_logical_xor (GskSlScope *scope,
operation->op = GSK_SL_OPERATION_LOGICAL_XOR;
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) operation);
operation->right = gsk_sl_expression_parse_logical_and (scope, stream);
- if (operation->right == NULL)
- {
- gsk_sl_expression_ref (expression);
- gsk_sl_expression_unref ((GskSlExpression *) operation);
- }
- else if (!gsk_sl_type_can_convert (gsk_sl_type_get_scalar (GSK_SL_BOOL),
- gsk_sl_expression_get_return_type (operation->right)))
+ if (!gsk_sl_type_can_convert (gsk_sl_type_get_scalar (GSK_SL_BOOL),
+ gsk_sl_expression_get_return_type (operation->right)))
{
gsk_sl_preprocessor_error (stream, TYPE_MISMATCH,
"Right operand of ^^ expression is not bool but %s",
@@ -1863,8 +1776,6 @@ gsk_sl_expression_parse_logical_or (GskSlScope *scope,
GskSlExpressionOperation *operation;
expression = gsk_sl_expression_parse_logical_xor (scope, stream);
- if (expression == NULL)
- return NULL;
while (TRUE)
{
@@ -1877,13 +1788,8 @@ gsk_sl_expression_parse_logical_or (GskSlScope *scope,
operation->op = GSK_SL_OPERATION_LOGICAL_OR;
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) operation);
operation->right = gsk_sl_expression_parse_logical_xor (scope, stream);
- if (operation->right == NULL)
- {
- gsk_sl_expression_ref (expression);
- gsk_sl_expression_unref ((GskSlExpression *) operation);
- }
- else if (!gsk_sl_type_can_convert (gsk_sl_type_get_scalar (GSK_SL_BOOL),
- gsk_sl_expression_get_return_type (operation->right)))
+ if (!gsk_sl_type_can_convert (gsk_sl_type_get_scalar (GSK_SL_BOOL),
+ gsk_sl_expression_get_return_type (operation->right)))
{
gsk_sl_preprocessor_error (stream, TYPE_MISMATCH,
"Right operand of || expression is not bool but %s",
@@ -1934,8 +1840,6 @@ gsk_sl_expression_parse_assignment (GskSlScope *scope,
GskSlExpressionAssignment *assign;
lvalue = gsk_sl_expression_parse_conditional (scope, stream);
- if (lvalue == NULL)
- return NULL;
token = gsk_sl_preprocessor_get (stream);
switch ((guint) token->type)
@@ -1977,11 +1881,6 @@ gsk_sl_expression_parse_assignment (GskSlScope *scope,
gsk_sl_preprocessor_consume (stream, (GskSlExpression *) assign);
assign->rvalue = gsk_sl_expression_parse_assignment (scope, stream);
- if (assign->rvalue == NULL)
- {
- gsk_sl_expression_unref ((GskSlExpression *) assign);
- return lvalue;
- }
return (GskSlExpression *) assign;
}
diff --git a/gsk/gsksltype.c b/gsk/gsksltype.c
index 78e29d9..062cd4c 100644
--- a/gsk/gsksltype.c
+++ b/gsk/gsksltype.c
@@ -971,7 +971,7 @@ gsk_sl_type_new_parse (GskSlPreprocessor *stream)
break;
default:
gsk_sl_preprocessor_error (stream, SYNTAX, "Expected type specifier");
- return NULL;
+ return gsk_sl_type_ref (gsk_sl_type_get_scalar (GSK_SL_FLOAT));
}
gsk_sl_preprocessor_consume (stream, NULL);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]