*** empty log message ***

This commit is contained in:
hyung-hwan 2005-06-05 05:27:02 +00:00
parent 33639ee40f
commit 537312c735
6 changed files with 205 additions and 30 deletions

View File

@ -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 <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
#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 <xp/bas/memory.h>
#include <xp/bas/assert.h>
#include <xp/bas/stdarg.h>
#include <xp/bas/ctype.h>
#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

View File

@ -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 <xp/stx/parser.h>
#include <xp/stx/token.h>
#include <xp/stx/misc.h>
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
}
*/

View File

@ -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 <xp/stx/stx.h>
#include <xp/stx/token.h>
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

View File

@ -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_
@ -23,6 +23,7 @@ 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_EOF XP_CHAR_EOF
/* common object structure */
struct xp_stx_objhdr_t

View File

@ -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)

59
ase/test/stx/parser.c Normal file
View File

@ -0,0 +1,59 @@
#include <xp/stx/stx.h>
#ifdef _DOS
#include <stdio.h>
#define xp_printf printf
#else
#include <xp/bas/stdio.h>
#include <xp/bas/locale.h>
#endif
#include <xp/stx/parser.h>
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;
}