enahnced qse_awk_parsesimple()

This commit is contained in:
hyung-hwan 2009-02-22 06:28:02 +00:00
parent 770cff61c5
commit 82cbaab6f2
8 changed files with 314 additions and 151 deletions

View File

@ -51,9 +51,9 @@ static int app_debug = 0;
struct argout_t
{
void* isp; /* input source files or string */
int ist; /* input source type */
qse_awk_parsesimple_type_t ist; /* input source type */
qse_size_t isfl; /* the number of input source files */
int ost; /* output source type */
qse_awk_parsesimple_type_t ost; /* output source type */
qse_char_t* osf; /* output source file */
qse_char_t** icf; /* input console files */
qse_size_t icfl; /* the number of input console files */
@ -488,7 +488,7 @@ static int handle_args (int argc, qse_char_t* argv[], struct argout_t* ao)
}
/* the source code is the string, not from the file */
ao->ist = QSE_AWK_PARSESIMPLE_STRING;
ao->ist = QSE_AWK_PARSESIMPLE_STR;
ao->isp = argv[opt.ind++];
free (isf);
@ -520,7 +520,8 @@ static int handle_args (int argc, qse_char_t* argv[], struct argout_t* ao)
}
icf[icfl] = QSE_NULL;
ao->ost = QSE_AWK_PARSESIMPLE_FILE;
ao->ost = (osf == QSE_NULL)?
QSE_AWK_PARSESIMPLE_NONE: QSE_AWK_PARSESIMPLE_FILE;
ao->osf = osf;
ao->icf = icf;
@ -602,7 +603,9 @@ static int awk_main (int argc, qse_char_t* argv[])
awk = open_awk ();
if (awk == QSE_NULL) return -1;
if (qse_awk_parsesimple (awk, ao.ist, ao.isp, ao.osf) == -1)
/* TODO: change it to support multiple source files */
/*if (qse_awk_parsesimple (awk, ao.ist, ao.isp, ao.ost, ao.osf) == -1)*/
if (qse_awk_parsesimple (awk, ao.ist, ((qse_char_t**)ao.isp)[0], ao.ost, ao.osf) == -1)
{
qse_printf (
QSE_T("PARSE ERROR: CODE [%d] LINE [%u] %s\n"),
@ -658,7 +661,7 @@ static int awk_main (int argc, qse_char_t* argv[])
oops:
qse_awk_close (awk);
if (ao.ist == QSE_AWK_SOURCE_FILES && ao.isp != QSE_NULL) free (ao.isp);
if (ao.ist == QSE_AWK_PARSESIMPLE_FILE && ao.isp != QSE_NULL) free (ao.isp);
/*if (ao.osf != QSE_NULL) free (ao.osf);*/
if (ao.icf != QSE_NULL) free (ao.icf);
if (ao.vm != QSE_NULL) qse_map_close (ao.vm);

View File

@ -663,7 +663,8 @@ enum qse_awk_parsesimple_type_t
QSE_AWK_PARSESIMPLE_NONE = 0,
QSE_AWK_PARSESIMPLE_FILE = 1,
QSE_AWK_PARSESIMPLE_STR = 2,
QSE_AWK_PARSESIMPLE_STRL = 3
QSE_AWK_PARSESIMPLE_STRL = 3,
QSE_AWK_PARSESIMPLE_STDIO = 4
};
/******/
@ -1652,8 +1653,8 @@ int qse_awk_parsesimple (
*/
qse_awk_rtx_t* qse_awk_rtx_opensimple (
qse_awk_t* awk,
const qse_char_t* icf[],
const qse_char_t* ocf[]
const qse_char_t*const* icf,
const qse_char_t*const* ocf
);
/******/

View File

@ -33,24 +33,33 @@ typedef struct xtn_t
{
struct
{
int type;
qse_awk_parsesimple_type_t type;
union
{
const qse_char_t*const* files;
const qse_char_t* file;
const qse_char_t* str;
qse_cstr_t cstr;
struct
{
const qse_char_t* ptr;
const qse_char_t* end;
} strl;
} u;
qse_size_t index; /* current file index */
qse_sio_t* handle; /* the handle to an open file */
} in;
struct
{
qse_awk_parsesimple_type_t type;
union
{
const qse_char_t* file;
qse_char_t* str;
qse_cstr_t cstr;
struct
{
qse_xstr_t* osp;
qse_char_t* ptr;
qse_char_t* end;
} strl;
} u;
qse_sio_t* handle;
} out;
@ -170,30 +179,29 @@ static qse_ssize_t sf_in (
if (cmd == QSE_AWK_SIO_OPEN)
{
if (xtn->s.in.type == QSE_AWK_PARSESIMPLE_FILE)
switch (xtn->s.in.type)
{
if (xtn->s.in.u.files == QSE_NULL) return -1;
if (xtn->s.in.u.files[xtn->s.in.index] == QSE_NULL) return 0;
case QSE_AWK_PARSESIMPLE_FILE:
if (xtn->s.in.u.file == QSE_NULL) return -1;
if (xtn->s.in.u.files[xtn->s.in.index][0] == QSE_T('\0'))
{
xtn->s.in.handle = qse_sio_in;
}
else
{
xtn->s.in.handle = qse_sio_open (
awk->mmgr,
0,
xtn->s.in.u.files[xtn->s.in.index],
xtn->s.in.u.file,
QSE_SIO_READ
);
if (xtn->s.in.handle == QSE_NULL) return -1;
}
//qse_awk_setsource (awk, xtn->s.in.u.files[xtn->s.in.index]);
}
return 1;
case QSE_AWK_PARSESIMPLE_STDIO:
xtn->s.in.handle = qse_sio_in;
return 1;
case QSE_AWK_PARSESIMPLE_STR:
case QSE_AWK_PARSESIMPLE_STRL:
xtn->s.in.handle = QSE_NULL;
return 1;
}
}
else if (cmd == QSE_AWK_SIO_CLOSE)
{
@ -209,53 +217,36 @@ static qse_ssize_t sf_in (
}
else if (cmd == QSE_AWK_SIO_READ)
{
qse_ssize_t n = 0;
if (xtn->s.in.type == QSE_AWK_PARSESIMPLE_FILE)
switch (xtn->s.in.type)
{
qse_sio_t* sio;
case QSE_AWK_PARSESIMPLE_FILE:
case QSE_AWK_PARSESIMPLE_STDIO:
QSE_ASSERT (xtn->s.in.handle != QSE_NULL);
return qse_sio_getsn (xtn->s.in.handle, data, size);
retry:
sio = xtn->s.in.handle;
n = qse_sio_getsn (sio, data, size);
if (n == 0 && xtn->s.in.u.files[++xtn->s.in.index] != QSE_NULL)
case QSE_AWK_PARSESIMPLE_STR:
{
if (sio != qse_sio_in) qse_sio_close (sio);
if (xtn->s.in.u.files[xtn->s.in.index][0] == QSE_T('\0'))
qse_size_t n = 0;
while (n < size && *xtn->s.in.u.str != QSE_T('\0'))
{
xtn->s.in.handle = qse_sio_in;
data[n++] = *xtn->s.in.u.str++;
}
else
{
xtn->s.in.handle = qse_sio_open (
awk->mmgr,
0,
xtn->s.in.u.files[xtn->s.in.index],
QSE_SIO_READ
);
if (xtn->s.in.handle == QSE_NULL) return -1;
}
/* TODO: reset internal line counters...
set new source name....
qse_awk_setsinname ();
*/
goto retry;
}
}
else
{
while (n < size && xtn->s.in.u.str[xtn->s.in.index] != QSE_T('\0'))
{
data[n++] = xtn->s.in.u.str[xtn->s.in.index++];
}
}
return n;
}
case QSE_AWK_PARSESIMPLE_STRL:
{
qse_size_t n = 0;
while (n < size && xtn->s.in.u.strl.ptr < xtn->s.in.u.strl.end)
{
data[n++] = *xtn->s.in.u.strl.ptr++;
}
return n;
}
}
}
return -1;
}
@ -267,27 +258,39 @@ static qse_ssize_t sf_out (
if (cmd == QSE_AWK_SIO_OPEN)
{
if (xtn->s.out.file[0] == QSE_T('\0'))
{
xtn->s.out.handle = qse_sio_out;
}
else
switch (xtn->s.out.type)
{
case QSE_AWK_PARSESIMPLE_FILE:
if (xtn->s.out.u.file == QSE_NULL) return -1;
xtn->s.out.handle = qse_sio_open (
awk->mmgr,
0,
xtn->s.out.file,
QSE_SIO_WRITE|QSE_SIO_CREATE|QSE_SIO_TRUNCATE
xtn->s.out.u.file,
QSE_SIO_WRITE |
QSE_SIO_CREATE |
QSE_SIO_TRUNCATE
);
if (xtn->s.out.handle == QSE_NULL) return -1;
}
return 1;
case QSE_AWK_PARSESIMPLE_STDIO:
xtn->s.out.handle = qse_sio_out;
return 1;
case QSE_AWK_PARSESIMPLE_STR:
case QSE_AWK_PARSESIMPLE_STRL:
xtn->s.out.handle = QSE_NULL;
return 1;
}
}
else if (cmd == QSE_AWK_SIO_CLOSE)
{
if (xtn->s.out.handle != QSE_NULL)
switch (xtn->s.out.type)
{
case QSE_AWK_PARSESIMPLE_FILE:
case QSE_AWK_PARSESIMPLE_STDIO:
qse_sio_flush (xtn->s.out.handle);
if (xtn->s.out.handle != qse_sio_in &&
xtn->s.out.handle != qse_sio_out &&
@ -295,13 +298,48 @@ static qse_ssize_t sf_out (
{
qse_sio_close (xtn->s.out.handle);
}
}
return 0;
case QSE_AWK_PARSESIMPLE_STR:
*xtn->s.out.u.str = QSE_T('\0');
return 0;
case QSE_AWK_PARSESIMPLE_STRL:
xtn->s.out.u.strl.osp->len =
xtn->s.out.u.strl.ptr -
xtn->s.out.u.strl.osp->ptr;
return 0;
}
}
else if (cmd == QSE_AWK_SIO_WRITE)
{
switch (xtn->s.out.type)
{
case QSE_AWK_PARSESIMPLE_FILE:
case QSE_AWK_PARSESIMPLE_STDIO:
QSE_ASSERT (xtn->s.out.handle != QSE_NULL);
return qse_sio_putsn (xtn->s.out.handle, data, size);
case QSE_AWK_PARSESIMPLE_STR:
{
qse_size_t n = 0;
while (n < size && *xtn->s.out.u.str != QSE_T('\0'))
{
*xtn->s.out.u.str++ = data[n++];
}
return n;
}
case QSE_AWK_PARSESIMPLE_STRL:
{
qse_size_t n = 0;
while (n < size && xtn->s.out.u.strl.ptr < xtn->s.out.u.strl.end)
{
*xtn->s.out.u.strl.ptr++ = data[n++];
}
return n;
}
}
}
return -1;
@ -317,58 +355,64 @@ int qse_awk_parsesimple (
qse_awk_sio_t sio;
xtn_t* xtn = (xtn_t*) QSE_XTN (awk);
if (isp == QSE_NULL)
switch (ist)
{
qse_awk_seterrnum (awk, QSE_AWK_EINVAL);
return -1;
}
case QSE_AWK_PARSESIMPLE_FILE:
xtn->s.in.u.file = (const qse_char_t*)isp;
break;
if (ist == QSE_AWK_PARSESIMPLE_FILE)
{
xtn->s.in.u.files = (const qse_char_t*const*)isp;
}
else if (ist == QSE_AWK_PARSESIMPLE_STR)
{
case QSE_AWK_PARSESIMPLE_STR:
xtn->s.in.u.str = (const qse_char_t*)isp;
}
else if (ist == QSE_AWK_PARSESIMPLE_STRL)
{
xtn->s.in.u.cstr.ptr = (const qse_cstr_t*)isp->ptr;
xtn->s.in.u.cstr.len = (const qse_cstr_t*)isp->len;
}
else
{
break;
case QSE_AWK_PARSESIMPLE_STRL:
xtn->s.in.u.strl.ptr = ((const qse_cstr_t*)isp)->ptr;
xtn->s.in.u.strl.end = xtn->s.in.u.strl.ptr + ((const qse_cstr_t*)isp)->len;
break;
case QSE_AWK_PARSESIMPLE_STDIO:
/* nothing to do */
break;
default:
qse_awk_seterrnum (awk, QSE_AWK_EINVAL);
return -1;
}
if (ost == QSE_AWK_PARSESIMPLE_FILE)
{
}
else if (ost == QSE_AWK_PARSESIMPLE_STR)
{
}
else if (ost == QSE_AWK_PARSESIMPLE_STRL)
{
}
else if (ost == QSE_AWK_PARSESIMPLE_NONE)
{
}
else
switch (ost)
{
case QSE_AWK_PARSESIMPLE_FILE:
xtn->s.out.u.file = (const qse_char_t*)osp;
break;
case QSE_AWK_PARSESIMPLE_STR:
xtn->s.out.u.str = (qse_char_t*)osp;
break;
case QSE_AWK_PARSESIMPLE_STRL:
xtn->s.out.u.strl.osp = (qse_xstr_t*)osp;
xtn->s.out.u.strl.ptr = ((qse_xstr_t*)osp)->ptr;
xtn->s.out.u.strl.end = xtn->s.out.u.strl.ptr + ((qse_xstr_t*)osp)->len;
break;
case QSE_AWK_PARSESIMPLE_NONE:
case QSE_AWK_PARSESIMPLE_STDIO:
/* nothing to do */
break;
default:
qse_awk_seterrnum (awk, QSE_AWK_EINVAL);
return -1;
}
xtn->s.in.type = ist;
xtn->s.in.index = 0;
xtn->s.in.handle = QSE_NULL;
xtn->s.out.file = osf;
xtn->s.out.type = ost;
xtn->s.out.handle = QSE_NULL;
sio.in = sf_in;
sio.out = (osf == QSE_NULL)? QSE_NULL: sf_out;
sio.out = (ost == QSE_AWK_PARSESIMPLE_NONE)? QSE_NULL: sf_out;
return qse_awk_parse (awk, &sio);
}
@ -828,7 +872,9 @@ static qse_ssize_t awk_rio_console (
}
qse_awk_rtx_t* qse_awk_rtx_opensimple (
qse_awk_t* awk, const qse_char_t* icf[], const qse_char_t* ocf[])
qse_awk_t* awk,
const qse_char_t*const* icf,
const qse_char_t*const* ocf)
{
qse_awk_rtx_t* rtx;
qse_awk_rio_t rio;

View File

@ -1,9 +1,10 @@
AM_CPPFLAGS = -I$(top_srcdir)/include
bin_PROGRAMS = awk01 awk03
bin_PROGRAMS = awk01 awk02 awk03
LDFLAGS = -L../../lib/cmn -L../../lib/utl -L../../lib/awk
LDADD = -lqseawk -lqseutl -lqsecmn $(LIBM)
awk01_SOURCES = awk01.c
awk02_SOURCES = awk02.c
awk03_SOURCES = awk03.c

View File

@ -32,7 +32,7 @@ PRE_UNINSTALL = :
POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
bin_PROGRAMS = awk01$(EXEEXT) awk03$(EXEEXT)
bin_PROGRAMS = awk01$(EXEEXT) awk02$(EXEEXT) awk03$(EXEEXT)
subdir = test/awk
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@ -50,6 +50,10 @@ awk01_OBJECTS = $(am_awk01_OBJECTS)
awk01_LDADD = $(LDADD)
am__DEPENDENCIES_1 =
awk01_DEPENDENCIES = $(am__DEPENDENCIES_1)
am_awk02_OBJECTS = awk02.$(OBJEXT)
awk02_OBJECTS = $(am_awk02_OBJECTS)
awk02_LDADD = $(LDADD)
awk02_DEPENDENCIES = $(am__DEPENDENCIES_1)
am_awk03_OBJECTS = awk03.$(OBJEXT)
awk03_OBJECTS = $(am_awk03_OBJECTS)
awk03_LDADD = $(LDADD)
@ -66,8 +70,8 @@ CCLD = $(CC)
LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
--mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \
$(LDFLAGS) -o $@
SOURCES = $(awk01_SOURCES) $(awk03_SOURCES)
DIST_SOURCES = $(awk01_SOURCES) $(awk03_SOURCES)
SOURCES = $(awk01_SOURCES) $(awk02_SOURCES) $(awk03_SOURCES)
DIST_SOURCES = $(awk01_SOURCES) $(awk02_SOURCES) $(awk03_SOURCES)
ETAGS = etags
CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@ -195,6 +199,7 @@ top_srcdir = @top_srcdir@
AM_CPPFLAGS = -I$(top_srcdir)/include
LDADD = -lqseawk -lqseutl -lqsecmn $(LIBM)
awk01_SOURCES = awk01.c
awk02_SOURCES = awk02.c
awk03_SOURCES = awk03.c
all: all-am
@ -260,6 +265,9 @@ clean-binPROGRAMS:
awk01$(EXEEXT): $(awk01_OBJECTS) $(awk01_DEPENDENCIES)
@rm -f awk01$(EXEEXT)
$(LINK) $(awk01_OBJECTS) $(awk01_LDADD) $(LIBS)
awk02$(EXEEXT): $(awk02_OBJECTS) $(awk02_DEPENDENCIES)
@rm -f awk02$(EXEEXT)
$(LINK) $(awk02_OBJECTS) $(awk02_LDADD) $(LIBS)
awk03$(EXEEXT): $(awk03_OBJECTS) $(awk03_DEPENDENCIES)
@rm -f awk03$(EXEEXT)
$(LINK) $(awk03_OBJECTS) $(awk03_LDADD) $(LIBS)
@ -271,6 +279,7 @@ distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/awk01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/awk02.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/awk03.Po@am__quote@
.c.o:

View File

@ -41,7 +41,6 @@ int main ()
qse_awk_t* awk = QSE_NULL;
qse_awk_rtx_t* rtx = QSE_NULL;
int ret;
const qse_char_t* co[] = { QSE_T(""), QSE_NULL };
awk = qse_awk_opensimple ();
if (awk == QSE_NULL)
@ -52,8 +51,10 @@ int main ()
ret = qse_awk_parsesimple (
awk,
QSE_AWK_SOURCE_STRING, src, /* parse AWK source in a string */
QSE_NULL /* no parse output */
/* parse AWK source in a string */
QSE_AWK_PARSESIMPLE_STR, src,
/* no deparse output */
QSE_AWK_PARSESIMPLE_NONE, QSE_NULL
);
if (ret == -1)
{
@ -65,7 +66,7 @@ int main ()
rtx = qse_awk_rtx_opensimple (
awk,
QSE_NULL, /* no console input */
QSE_AWK_CONSOLE_STDIO /* use standard out for console output */
QSE_AWK_RTX_OPENSIMPLE_STDIO /* stdout for console output */
);
if (rtx == QSE_NULL)
{

102
qse/test/awk/awk02.c Normal file
View File

@ -0,0 +1,102 @@
/*
* $Id: awk.c 501 2008-12-17 08:39:15Z baconevi $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/****S* AWK/Basic Loop
* DESCRIPTION
* This program demonstrates how to use qse_awk_rtx_loop().
* SOURCE
*/
#include <qse/awk/awk.h>
#include <qse/cmn/mem.h>
#include <qse/utl/stdio.h>
static const qse_char_t* src = QSE_T(
"BEGIN {"
" for (i=2;i<=9;i++)"
" {"
" for (j=1;j<=9;j++)"
" print i \"*\" j \"=\" i * j;"
" print \"---------------------\";"
" }"
"}"
);
static qse_char_t srcout[5000];
int main ()
{
qse_awk_t* awk = QSE_NULL;
qse_awk_rtx_t* rtx = QSE_NULL;
int ret;
awk = qse_awk_opensimple ();
if (awk == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("error: cannot open awk\n"));
goto oops;
}
qse_memset (srcout, QSE_T(' '), QSE_COUNTOF(srcout)-1);
srcout[QSE_COUNTOF(srcout)-1] = QSE_T('\0');
ret = qse_awk_parsesimple (
awk,
/* parse the source in src */
QSE_AWK_PARSESIMPLE_STR, src,
/* deparse into srcout */
QSE_AWK_PARSESIMPLE_STR, srcout
);
if (ret == -1)
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_geterrmsg(awk));
goto oops;
}
qse_printf (QSE_T("DEPARSED SOURCE:\n%s\n"), srcout);
qse_printf (QSE_T("=================================\n"));
qse_fflush (QSE_STDOUT);
rtx = qse_awk_rtx_opensimple (
awk,
QSE_NULL, /* no console input */
QSE_AWK_RTX_OPENSIMPLE_STDIO /* stdout for console output */
);
if (rtx == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_geterrmsg(awk));
goto oops;
}
ret = qse_awk_rtx_loop (rtx);
if (ret == -1)
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_rtx_geterrmsg(rtx));
goto oops;
}
oops:
if (rtx != QSE_NULL) qse_awk_rtx_close (rtx);
if (awk != QSE_NULL) qse_awk_close (awk);
return -1;
}
/******/

View File

@ -63,9 +63,9 @@ int main ()
ret = qse_awk_parsesimple (
awk,
/* parse AWK source in a string */
QSE_AWK_SOURCE_STRING, src,
QSE_AWK_PARSESIMPLE_STR, src,
/* no parse output */
QSE_NULL
QSE_AWK_PARSESIMPLE_NONE, QSE_NULL
);
if (ret == -1)
{
@ -78,7 +78,7 @@ int main ()
rtx = qse_awk_rtx_opensimple (
awk,
QSE_NULL, /* no console input */
QSE_AWK_CONSOLE_STDIO /* console output */
QSE_AWK_RTX_OPENSIMPLE_STDIO /* stdout for console output */
);
if (rtx == QSE_NULL)
{