Commits

PioneerAxon  committed 23221e3

Error reporting in lexer and rework

Fixed: Removed temp error reporting code from lexer.c and use set_error() to set proper error.
Fixed: Set error_token pointer to NULL in ParserState.
Fixed: Make "Full string not parsed" condition more full-proof.

  • Participants
  • Parent commits b9e07a1

Comments (0)

Files changed (2)

 
 #include <lexer.h>
 #include <parserfunc.h>
-
-
-/*------------TEMP code----------*/
-#include<stdio.h>
-#define PR_ERROR(x) printf("ERROR : %s at string \"%s\"...\n",x,pl_get_marked_substring (state)); return l_insert_token (lstate, T_UNKNOWN);
-
-/*---------END TEMP code---------*/
+#include "mp-equation.h"
 
 static gint
 l_check_if_function (LexerState* state)
 	/* [PL_SUPER_MINUS][PL_SUPER_DIGIT]+ */
 	if (type == PL_SUPER_MINUS) {
 		if ((type = pl_get_next_token (state)) != PL_SUPER_DIGIT) {
-			//TODO: ERROR
-			PR_ERROR ("expected PL_SUP_DIGIT");
+			/* ERROR: expected PL_SUP_DIGIT */
+			set_error (lstate->parent, PARSER_ERR_MP, tmp = pl_get_marked_substring (state));
+			free (tmp);
+			return l_insert_token (lstate, T_UNKNOWN);
 		}
 		while (pl_get_next_token (state) == PL_SUPER_DIGIT);
 		pl_roll_back (state);
 							return l_insert_token (lstate, T_NUMBER);
 						}
 						else {
-							//TODO: ERROR
-							PR_ERROR ("expected PL_SECOND.");
+							/* ERROR: expected PL_SECOND */
+							set_error (lstate->parent, PARSER_ERR_MP, tmp = pl_get_marked_substring (state));
+							free (tmp);
+							return l_insert_token (lstate, T_UNKNOWN);
 						}
 					}
 					else if (type == PL_DECIMAL) {
 ANGLE_NUM_DMS_STATE:
 						if ((type = pl_get_next_token (state)) != PL_DIGIT) {
-							//TODO: ERROR
-							PR_ERROR ("expected PL_DIGIT.");
+							/* ERROR: expected PL_DIGIT */
+							set_error (lstate->parent, PARSER_ERR_MP, tmp = pl_get_marked_substring (state));
+							free (tmp);
+							return l_insert_token (lstate, T_UNKNOWN);
 						}
 						while ((type = pl_get_next_token (state)) == PL_DIGIT);
 						if (type == PL_SECOND) {
 							return l_insert_token (lstate, T_NUMBER);
 						}
 						else {
-							//TODO: ERROR
-							PR_ERROR ("expected PL_SECOND.");
+							/* ERROR: expected PL_SECOND */
+							set_error (lstate->parent, PARSER_ERR_MP, tmp = pl_get_marked_substring (state));
+							free (tmp);
+							return l_insert_token (lstate, T_UNKNOWN);
 						}
 					}
 					else {
 					}
 				}
 				else {
-					//TODO: ERROR
-					PR_ERROR ("expected PL_MINUTE | PL_DIGIT.");
+					/* ERROR: expected PL_MINUTE | PL_DIGIT */
+					set_error (lstate->parent, PARSER_ERR_MP, tmp = pl_get_marked_substring (state));
+					free (tmp);
+					return l_insert_token (lstate, T_UNKNOWN);
 				}
 			}
 			else if (type == PL_DECIMAL) {
 ANGLE_NUM_DM_STATE:
 				if ((type = pl_get_next_token (state)) != PL_DIGIT) {
-					//TODO: ERROR
-					PR_ERROR ("expected PL_DIGIT.");
+					/* ERROR: expected PL_DIGIT */
+					set_error (lstate->parent, PARSER_ERR_MP, tmp = pl_get_marked_substring (state));
+					free (tmp);
+					return l_insert_token (lstate, T_UNKNOWN);
 				}
 				while ((type = pl_get_next_token (state)) == PL_DIGIT);
 				if (type == PL_MINUTE) {
 					return l_insert_token (lstate, T_NUMBER);
 				}
 				else {
-					//TODO: ERROR
-					PR_ERROR ("expected PL_MINUTE.");
+					/* ERROR: expected PL_MINUTE */
+					set_error (lstate->parent, PARSER_ERR_MP, tmp = pl_get_marked_substring (state));
+					free (tmp);
+					return l_insert_token (lstate, T_UNKNOWN);
 				}
 			}
 			else {
 			goto DECIMAL_HEX_STATE;
 		}
 		else {
-			//TODO: ERROR
-			PR_ERROR ("expected PL_DIGIT | PL_HEX.");
+			/* ERROR: expected PL_DIGIT | PL_HEX */
+			set_error (lstate->parent, PARSER_ERR_MP, tmp = pl_get_marked_substring (state));
+			free (tmp);
+			return l_insert_token (lstate, T_UNKNOWN);
 		}
 	}
 	if (type == PL_HEX) {
 				else {
 					if (l_check_if_number (lstate))
 						return l_insert_token (lstate, T_NUMBER);
-					//TODO: ERROR
-					PR_ERROR ("expected PL_DECIMAL | PL_DIGIT | PL_HEX.");
+					/* ERROR: expected PL_DECIMAL | PL_DIGIT | PL_HEX */
+					set_error (lstate->parent, PARSER_ERR_MP, tmp = pl_get_marked_substring (state));
+					free (tmp);
+					return l_insert_token (lstate, T_UNKNOWN);
 				}
 			}
 			
 DECIMAL_HEX_STATE:
 			type = pl_get_next_token (state);
 			if (!(type == PL_DIGIT || type == PL_HEX)) {
-				//TODO: ERROR
-				PR_ERROR ("expected PL_DIGIT | PL_HEX.");
+				/* ERROR: expected PL_DIGIT | PL_HEX */
+				set_error (lstate->parent, PARSER_ERR_MP, tmp = pl_get_marked_substring (state));
+				free (tmp);
+				return l_insert_token (lstate, T_UNKNOWN);
 			}
 			while (1) {
 				type = pl_get_next_token (state);
 		else if (type == PL_SUB_DIGIT) {
 			while (pl_get_next_token (state) == PL_SUB_DIGIT);
 			pl_roll_back (state);
-			//TODO: check variable/number
 			if (l_check_if_number (lstate)) {
 				/* NUMBER */
 				return l_insert_token (lstate, T_NUMBER);
 			}
 			else {
 				/* VARIABLE */
-				//TODO: check function / variable
 				if (l_check_if_function (lstate)) {
 					return l_insert_token (lstate, T_FUNCTION);
 				}
 		}
 		else {
 			pl_roll_back (state);
-			//TODO: check variable/number
 			if (l_check_if_number (lstate)) {
 				/* NUMBER */
 				return l_insert_token (lstate, T_NUMBER);
 			}
 			else {
 				/* VARIABLE */
-				//TODO: check function / variable
 				if (l_check_if_function (lstate)) {
 					return l_insert_token (lstate, T_FUNCTION);
 				}
 				if (g_strcmp0 (tmp, "in") == 0) {
 					return l_insert_token (lstate, T_IN);
 				}
-				//TODO:check function or variable
 				if (l_check_if_function (lstate)) {
 					return l_insert_token (lstate, T_FUNCTION);
 				}
 				if (g_strcmp0 (tmp, "in") == 0) {
 					return l_insert_token (lstate, T_IN);
 				}
-				//TODO:check function or variable
 				if (l_check_if_function (lstate)) {
 					return l_insert_token (lstate, T_FUNCTION);
 				}
 	if (type == PL_EOS) {
 		return l_insert_token (lstate, PL_EOS);
 	}
-	/* Parse error.. ;) */
-	PR_ERROR ("Unexpected token :(.");
+	/* ERROR: Unexpected token.. X( */
+	set_error (lstate->parent, PARSER_ERR_INVALID, tmp = pl_get_marked_substring (state));
+	free (tmp);
 	return l_insert_token (lstate, T_UNKNOWN);
 }
 

File src/parser.c

 static guint
 p_get_precedence (LexerTokenType type)
 {
-	/* WARNING: This function doesn't work for Unary Plus and Unary Minus. */
+	/* WARNING: This function doesn't work for Unary Plus and Unary Minus. Use their precedence directly while inserting them in tree. */
 	if (type == T_ADD
 	 || type == T_SUBTRACT)
 		return P_AddSubtract;
 	state->right_most = NULL;
 	state->options = options;
 	state->error = 0;
+	state->error_token = NULL;
 	return state;
 }
 
 	l_insert_all_tokens (state->lexer);
 	ret = statement (state);
 	token = l_get_next_token (state->lexer);
-	if (token->token_type != PL_EOS && token->token_type != T_ASSIGN) {
+	if (token->token_type == T_ASSIGN) {
+		token = l_get_next_token (state->lexer);
+		if (token->token_type != PL_EOS) {
 		/* Full string is not parsed. */
+			if (!state->error)
+				set_error (state, PARSER_ERR_INVALID, token->string);
+			return PARSER_ERR_INVALID;
+		}
+	}
+	if (token->token_type != PL_EOS) {
+		/* Full string is not parsed. */
+		if (!state->error)
+			set_error (state, PARSER_ERR_INVALID, token->string);
 		return PARSER_ERR_INVALID;
 	}
 	if (ret == 0)
-	{
 		/* Input can't be parsed with grammar. */
 		return PARSER_ERR_INVALID;
-	}
 	ans = (MPNumber *) (*(state->root->evaluate)) (state->root);
 	if (ans) {
 		mp_set_from_mp (ans, &state->ret);