From 537312c73514afc7b1794e7b3e74373851cc03f9 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 5 Jun 2005 05:27:02 +0000 Subject: [PATCH] *** empty log message *** --- ase/stx/misc.h | 12 +++- ase/stx/parser.c | 118 +++++++++++++++++++++++++++++++++----- ase/stx/parser.h | 25 +++++--- ase/stx/stx.h | 7 ++- ase/test/stx/makefile.lcc | 14 +++-- ase/test/stx/parser.c | 59 +++++++++++++++++++ 6 files changed, 205 insertions(+), 30 deletions(-) create mode 100644 ase/test/stx/parser.c diff --git a/ase/stx/misc.h b/ase/stx/misc.h index 6797912c..b889b3eb 100644 --- a/ase/stx/misc.h +++ b/ase/stx/misc.h @@ -1,5 +1,5 @@ /* - * $Id: misc.h,v 1.3 2005-05-21 07:27:32 bacon Exp $ + * $Id: misc.h,v 1.4 2005-06-05 05:26:24 bacon Exp $ */ #ifndef _XP_STX_MISC_H_ @@ -12,6 +12,7 @@ #include #include #include + #include #define xp_stx_assert assert #define xp_stx_malloc malloc @@ -21,10 +22,15 @@ #define xp_stx_va_start va_start #define xp_stx_va_end va_end #define xp_stx_va_arg va_arg + #define xp_stx_isspace isspace + #define xp_stx_isdigit isdigit + #define xp_stx_isalpha isalpha + #define xp_stx_isalnum isalnum #else #include #include #include + #include #define xp_stx_assert xp_assert #define xp_stx_malloc xp_malloc @@ -34,6 +40,10 @@ #define xp_stx_va_start xp_va_start #define xp_stx_va_end xp_va_end #define xp_stx_va_arg xp_va_arg + #define xp_stx_isspace xp_isspace + #define xp_stx_isdigit xp_isdigit + #define xp_stx_isalpha xp_isalpha + #define xp_stx_isalnum xp_isalnum #endif #ifdef __cplusplus diff --git a/ase/stx/parser.c b/ase/stx/parser.c index b2cfe20a..ad278009 100644 --- a/ase/stx/parser.c +++ b/ase/stx/parser.c @@ -1,11 +1,18 @@ /* - * $Id: parser.c,v 1.11 2005-06-04 16:27:30 bacon Exp $ + * $Id: parser.c,v 1.12 2005-06-05 05:26:24 bacon Exp $ */ #include #include #include +static int __get_token (xp_stx_parser_t* parser); +static int __skip_spaces (xp_stx_parser_t* parser); +static int __skip_comment (xp_stx_parser_t* parser); +static int __get_char (xp_stx_parser_t* parser); +static int __unget_char (xp_stx_parser_t* parser, xp_cint_t c); +static int __reset_input (xp_stx_parser_t* parser, void* input); + xp_stx_parser_t* xp_stx_parser_open (xp_stx_parser_t* parser) { if (parser == XP_NULL) { @@ -21,11 +28,13 @@ xp_stx_parser_t* xp_stx_parser_open (xp_stx_parser_t* parser) return XP_NULL; } - parser->error_code = 0; - parser->text = XP_NULL; - parser->curc = XP_CHAR_EOF; - parser->curp = XP_NULL; + parser->error_code = XP_STX_PARSER_ERROR_NONE; + parser->curc = XP_STX_CHAR_EOF; + parser->ungotc_count = 0; + parser->input = XP_NULL; + parser->input_reset = XP_NULL; + parser->input_consume = XP_NULL; return parser; } @@ -35,19 +44,101 @@ void xp_stx_parser_close (xp_stx_parser_t* parser) if (parser->__malloced) xp_stx_free (parser); } -int xp_stx_parser_parse_method (xp_stx_parser_t* parser, - xp_stx_word_t method_class, xp_stx_char_t* method_text) -{ - xp_stx_assert (method_text != XP_NULL); +#define RESET_INPUT(parser,in) \ + do { if (__reset_input(parser,in) == -1) return -1; } while (0) +#define GET_CHAR(parser) \ + do { if (__get_char(parser) == -1) return -1; } while (0) +#define UNGET_CHAR(parser,c) \ + do { if (__unget_char(parser,c) == -1) return -1; } while (0) - parser->error_code = 0; - parser->text = method_text; - parser->curp = method_text; - NEXT_CHAR (parser); +int xp_stx_parser_parse_method ( + xp_stx_parser_t* parser, xp_stx_word_t method_class, void* input) +{ + if (parser->input == XP_NULL || + parser->input_reset == XP_NULL || + parser->input_consume == XP_NULL) { + parser->error_code = XP_STX_PARSER_ERROR_INVALID; + return -1; + } + + RESET_INPUT (parser, input); + GET_CHAR (parser); return 0; } +static int __get_token (xp_stx_parser_t* parser) +{ + xp_cint_t c; + + do { + if (__skip_spaces(parser) == -1) return -1; + if (parser->curc == XP_STX_CHAR('"')) { + GET_CHAR (parser); + if (__skip_comment(parser) == -1) return -1; + } + else break; + } while (1); + + c = parser->curc; + + if (xp_stx_isalpha(c)) { + } + else if (xp_stx_isdigit(c)) { + } + else if (c == XP_STX_CHAR('\'')) { + } + else { + parser->error_code = XP_STX_PARSER_ERROR_CHAR; + return -1; + } + + return 0; +} + +static int __skip_spaces (xp_stx_parser_t* parser) +{ + while (xp_stx_isspace(parser->curc)) GET_CHAR (parser); + return 0; +} + +static int __skip_comment (xp_stx_parser_t* parser) +{ + while (parser->curc != XP_STX_CHAR('"')) GET_CHAR (parser); + GET_CHAR (parser); + return 0; +} + +static int __get_char (xp_stx_parser_t* parser) +{ + xp_cint_t c; + if (parser->input_consume (parser, &c) == -1) { + parser->error_code = XP_STX_PARSER_ERROR_INPUT; + return -1; + } + parser->curc = c; + return 0; +} + +static int __unget_char (xp_stx_parser_t* parser, xp_cint_t c) +{ + if (parser->ungotc_count >= xp_countof(parser->ungotc)) return -1; + parser->ungotc[parser->ungotc_count++] = c; + return 0; +} + +static int __reset_input (xp_stx_parser_t* parser, void* input) +{ + parser->input = input; + if (parser->input_reset(parser) == -1) return -1; + + parser->error_code = XP_STX_PARSER_ERROR_NONE; + parser->curc = XP_STX_CHAR_EOF; + parser->ungotc_count = 0; + return 0; +} + +/* static int __get_token (xp_stx_parser_t* parser) { xp_cint_t c = parser->curc; @@ -81,3 +172,4 @@ static int __skip_spaces (xp_stx_parser_t* parser) { while (xp_stx_isspace(parser->curc)) __get_char } +*/ diff --git a/ase/stx/parser.h b/ase/stx/parser.h index 6b5f896a..c6348e6f 100644 --- a/ase/stx/parser.h +++ b/ase/stx/parser.h @@ -1,5 +1,5 @@ /* - * $Id: parser.h,v 1.8 2005-06-04 07:01:57 bacon Exp $ + * $Id: parser.h,v 1.9 2005-06-05 05:26:24 bacon Exp $ */ #ifndef _XP_STX_PARSER_H_ @@ -8,21 +8,32 @@ #include #include +enum +{ + XP_STX_PARSER_ERROR_NONE = 0, + XP_STX_PARSER_ERROR_INPUT, + XP_STX_PARSER_ERROR_INVALID, + XP_STX_PARSER_ERROR_CHAR +}; + +typedef struct xp_stx_parser_t xp_stx_parser_t; + struct xp_stx_parser_t { int error_code; xp_stx_token_t token; - /* lexer data */ - const xp_char_t* text; - const xp_cint_t curc; - const xp_char_t* curp; + xp_stx_cint_t curc; + xp_stx_cint_t ungotc[5]; + xp_size_t ungotc_count; + + void* input; + int (*input_reset) (xp_stx_parser_t*); + int (*input_consume) (xp_stx_parser_t*, xp_stx_cint_t*); xp_bool_t __malloced; }; -typedef struct xp_stx_parser_t xp_stx_parser_t; - #ifdef __cplusplus extern "C" { #endif diff --git a/ase/stx/stx.h b/ase/stx/stx.h index 73aa94ad..828d0427 100644 --- a/ase/stx/stx.h +++ b/ase/stx/stx.h @@ -1,5 +1,5 @@ /* - * $Id: stx.h,v 1.23 2005-05-29 16:51:16 bacon Exp $ + * $Id: stx.h,v 1.24 2005-06-05 05:26:24 bacon Exp $ */ #ifndef _XP_STX_STX_H_ @@ -21,8 +21,9 @@ typedef struct xp_stx_char_object_t xp_stx_char_object_t; typedef struct xp_stx_memory_t xp_stx_memory_t; typedef struct xp_stx_t xp_stx_t; -#define XP_STX_CHAR(x) XP_CHAR(x) -#define XP_STX_TEXT(x) XP_TEXT(x) +#define XP_STX_CHAR(x) XP_CHAR(x) +#define XP_STX_TEXT(x) XP_TEXT(x) +#define XP_STX_CHAR_EOF XP_CHAR_EOF /* common object structure */ struct xp_stx_objhdr_t diff --git a/ase/test/stx/makefile.lcc b/ase/test/stx/makefile.lcc index 74a72c70..91e993c5 100644 --- a/ase/test/stx/makefile.lcc +++ b/ase/test/stx/makefile.lcc @@ -1,7 +1,3 @@ -SRCS = stx.c -OBJS = stx.obj -OUT = stx.exe - CC = lcc CFLAGS = -I../../.. -A -ansic -libcdll #LDFLAGS = -L../../../xp/bas -L../../../xp/stx @@ -9,8 +5,14 @@ CFLAGS = -I../../.. -A -ansic -libcdll LDFLAGS = -subsystem console -dynamic -s LIBS = ..\..\..\xp\bas\xpbas.lib ..\..\..\xp\stx\xpstx.lib -all: $(OBJS) - lcclnk $(LDFLAGS) -o $(OUT) $(OBJS) $(LIBS) +all: stx parser + +stx: stx.obj + lcclnk $(LDFLAGS) -o stx.exe stx.obj $(LIBS) + +parser: parser.obj + lcclnk $(LDFLAGS) -o parser.exe parser.obj $(LIBS) + clean: del $(OBJS) *.obj $(OUT) diff --git a/ase/test/stx/parser.c b/ase/test/stx/parser.c new file mode 100644 index 00000000..9e163d00 --- /dev/null +++ b/ase/test/stx/parser.c @@ -0,0 +1,59 @@ +#include + +#ifdef _DOS + #include + #define xp_printf printf +#else + #include + #include +#endif + +#include + +struct ss_t +{ + xp_stx_char_t* text; + xp_size_t size; + xp_size_t index; +}; + +typedef struct ss_t ss_t; + +int ss_reset (xp_stx_parser_t* parser) +{ + return 0; +} + +int ss_consume (xp_stx_parser_t* parser, xp_stx_cint_t* c) +{ + ss_t* ss = (ss_t*)parser->input; + if (ss->index < ss->size) *c = ss->text[ss->index++]; + else *c = XP_STX_CHAR_EOF; + return 0; +} + +int xp_main (int argc, xp_char_t* argv[]) +{ + xp_stx_parser_t parser; + xp_stx_word_t i; + +#ifndef _DOS + if (xp_setlocale () == -1) { + printf ("cannot set locale\n"); + return -1; + } +#endif + + if (xp_stx_parser_open(&parser) == XP_NULL) { + xp_printf (XP_TEXT("cannot open parser\n")); + return -1; + } + + parser.input_reset = ss_reset; + parser.input_consume = ss_consume; + + xp_stx_parser_close (&parser); + xp_printf (XP_TEXT("== End of program ==\n")); + return 0; +} +