changeset 31:574931d87abd

Created a function to prettyprint the current lexer token
author lost@l-w.ca
date Thu, 03 Feb 2011 21:28:24 -0700
parents bcd532a90e53
children 49d608aecc4d
files lwbasic/lexer.c lwbasic/lwbasic.h lwbasic/parser.c
diffstat 3 files changed, 28 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/lwbasic/lexer.c	Thu Feb 03 21:19:11 2011 -0700
+++ b/lwbasic/lexer.c	Thu Feb 03 21:28:24 2011 -0700
@@ -217,3 +217,25 @@
 	state -> lexer_token = token_char;
 	return;
 }
+
+char *lexer_return_token(cstate *state)
+{
+	static char *buffer = NULL;
+	static int buflen = 0;
+	int l;
+	
+	if (buflen == 0)
+	{
+		buffer = lw_alloc(128);
+		buflen = 128;
+	}
+
+	l = snprintf(buffer, buflen, "%s (%d)", state -> lexer_token_string, state -> lexer_token);
+	if (l >= buflen)
+	{
+		buffer = lw_realloc(buffer, l + 1);
+		buflen = l + 1;
+		snprintf(buffer, buflen, "%s (%d)", state -> lexer_token_string, state -> lexer_token);
+	}
+	return buffer;
+}
--- a/lwbasic/lwbasic.h	Thu Feb 03 21:19:11 2011 -0700
+++ b/lwbasic/lwbasic.h	Thu Feb 03 21:28:24 2011 -0700
@@ -97,6 +97,7 @@
 
 #ifndef __lexer_c_seen__
 extern void lexer(cstate *state);
+extern char *lexer_return_token(cstate *state);
 #endif
 
 #ifndef __emit_c_seen__
--- a/lwbasic/parser.c	Thu Feb 03 21:19:11 2011 -0700
+++ b/lwbasic/parser.c	Thu Feb 03 21:28:24 2011 -0700
@@ -84,7 +84,7 @@
 paramagain:
 	if (state -> lexer_token != token_identifier)
 	{
-		lwb_error("Parameter name expected, got %d, %s\n", state -> lexer_token, state -> lexer_token_string);
+		lwb_error("Parameter name expected, got %s\n", lexer_return_token(state));
 	}
 	printf("Got <param> = %s\n", state -> lexer_token_string);
 	lexer(state);
@@ -134,7 +134,7 @@
 	
 	if (state -> lexer_token != token_eol)
 	{
-		lwb_error("EOL expected; found %d, %s\n", state -> lexer_token, state -> lexer_token_string);
+		lwb_error("EOL expected; found %s\n", lexer_return_token(state));
 	}
 
 	
@@ -158,14 +158,14 @@
 	{
 		if (state -> lexer_token != token_kw_endsub)
 		{
-			lwb_error("Expecting ENDSUB, got %d (%s)\n", state -> lexer_token, state -> lexer_token_string);
+			lwb_error("Expecting ENDSUB, got %s\n", lexer_return_token(state));
 		}
 	}
 	else
 	{
 		if (state -> lexer_token != token_kw_endfunction)
 		{
-			lwb_error("Expecting ENDFUNCTION, got %d (%s)\n", state -> lexer_token, state -> lexer_token_string);
+			lwb_error("Expecting ENDFUNCTION, got %s\n", lexer_return_token(state));
 		}
 	}
 	/* output function/sub epilog */
@@ -206,7 +206,7 @@
 			return;
 
 		default:
-			lwb_error("Invalid token %d, %s in global state\n", state -> lexer_token, state -> lexer_token_string);
+			lwb_error("Invalid token '%s' in global state\n", lexer_return_token(state));
 		}
 	}	
 }