enhanced qse_awk_parsesimple() more
This commit is contained in:
parent
82cbaab6f2
commit
2e24e29e8c
@ -50,11 +50,18 @@ static int app_debug = 0;
|
||||
|
||||
struct argout_t
|
||||
{
|
||||
void* isp; /* input source files or string */
|
||||
qse_awk_parsesimple_type_t ist; /* input source type */
|
||||
union
|
||||
{
|
||||
const qse_char_t* str;
|
||||
qse_char_t** files;
|
||||
} isp;
|
||||
qse_size_t isfl; /* the number of input source files */
|
||||
|
||||
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 */
|
||||
qse_map_t* vm; /* global variable map */
|
||||
@ -489,14 +496,14 @@ 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_STR;
|
||||
ao->isp = argv[opt.ind++];
|
||||
ao->isp.str = argv[opt.ind++];
|
||||
|
||||
free (isf);
|
||||
}
|
||||
else
|
||||
{
|
||||
ao->ist = QSE_AWK_PARSESIMPLE_FILE;
|
||||
ao->isp = isf;
|
||||
ao->isp.files = isf;
|
||||
}
|
||||
|
||||
/* the remaining arguments are input console file names */
|
||||
@ -520,8 +527,7 @@ static int handle_args (int argc, qse_char_t* argv[], struct argout_t* ao)
|
||||
}
|
||||
icf[icfl] = QSE_NULL;
|
||||
|
||||
ao->ost = (osf == QSE_NULL)?
|
||||
QSE_AWK_PARSESIMPLE_NONE: QSE_AWK_PARSESIMPLE_FILE;
|
||||
ao->ost = QSE_AWK_PARSESIMPLE_FILE;
|
||||
ao->osf = osf;
|
||||
|
||||
ao->icf = icf;
|
||||
@ -604,8 +610,21 @@ static int awk_main (int argc, qse_char_t* argv[])
|
||||
if (awk == QSE_NULL) return -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_awk_parsesimple_in_t psin;
|
||||
qse_awk_parsesimple_out_t psout;
|
||||
|
||||
psin.type = ao.ist;
|
||||
if (ao.ist == QSE_AWK_PARSESIMPLE_STR) psin.u.str = ao.isp.str;
|
||||
else psin.u.file = ao.isp.files[0];
|
||||
|
||||
if (ao.osf != QSE_NULL)
|
||||
{
|
||||
psout.type = ao.ost;
|
||||
psout.u.file = ao.osf;
|
||||
}
|
||||
|
||||
if (qse_awk_parsesimple (awk, &psin,
|
||||
((ao.osf == QSE_NULL)? QSE_NULL: &psout)) == -1)
|
||||
{
|
||||
qse_printf (
|
||||
QSE_T("PARSE ERROR: CODE [%d] LINE [%u] %s\n"),
|
||||
@ -661,7 +680,8 @@ static int awk_main (int argc, qse_char_t* argv[])
|
||||
oops:
|
||||
qse_awk_close (awk);
|
||||
|
||||
if (ao.ist == QSE_AWK_PARSESIMPLE_FILE && ao.isp != QSE_NULL) free (ao.isp);
|
||||
if (ao.ist == QSE_AWK_PARSESIMPLE_FILE &&
|
||||
ao.isp.files != QSE_NULL) free (ao.isp.files);
|
||||
/*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);
|
||||
|
@ -660,15 +660,52 @@ struct qse_awk_val_ref_t
|
||||
*/
|
||||
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_STDIO = 4
|
||||
QSE_AWK_PARSESIMPLE_FILE = 0,
|
||||
QSE_AWK_PARSESIMPLE_STR = 1,
|
||||
QSE_AWK_PARSESIMPLE_STRL = 2,
|
||||
QSE_AWK_PARSESIMPLE_STDIO = 3
|
||||
};
|
||||
typedef enum qse_awk_parsesimple_type_t qse_awk_parsesimple_type_t;
|
||||
/******/
|
||||
|
||||
typedef enum qse_awk_parsesimple_type_t qse_awk_parsesimple_type_t;
|
||||
|
||||
/****s* AWK/qse_awk_parsesimple_in_t
|
||||
* NAME
|
||||
* qse_awk_parsesimple_in_t - define source input
|
||||
* SYNOPSIS
|
||||
*/
|
||||
struct qse_awk_parsesimple_in_t
|
||||
{
|
||||
qse_awk_parsesimple_type_t type;
|
||||
|
||||
union
|
||||
{
|
||||
const qse_char_t* file;
|
||||
const qse_char_t* str;
|
||||
qse_cstr_t strl;
|
||||
} u;
|
||||
};
|
||||
typedef struct qse_awk_parsesimple_in_t qse_awk_parsesimple_in_t;
|
||||
/******/
|
||||
|
||||
/****s* AWK/qse_awk_parsesimple_out_t
|
||||
* NAME
|
||||
* qse_awk_parsesimple_out_t - define source output
|
||||
* SYNOPSIS
|
||||
*/
|
||||
struct qse_awk_parsesimple_out_t
|
||||
{
|
||||
qse_awk_parsesimple_type_t type;
|
||||
|
||||
union
|
||||
{
|
||||
const qse_char_t* file;
|
||||
qse_char_t* str;
|
||||
qse_xstr_t strl;
|
||||
} u;
|
||||
};
|
||||
typedef struct qse_awk_parsesimple_out_t qse_awk_parsesimple_out_t;
|
||||
/******/
|
||||
|
||||
#define QSE_AWK_RTX_OPENSIMPLE_STDIO (qse_awk_rtx_opensimple_stdio)
|
||||
extern const qse_char_t* qse_awk_rtx_opensimple_stdio[];
|
||||
@ -1595,49 +1632,28 @@ qse_awk_t* qse_awk_opensimple (
|
||||
/****f* AWK/qse_awk_parsesimple
|
||||
* NAME
|
||||
* qse_awk_parsesimple - parse source code
|
||||
*
|
||||
* DESCRIPTION
|
||||
* If 'ist' is QSE_AWK_PARSESIMPLE_STR, 'isp' should be a null-terminated
|
||||
* string of the type 'const qse_char_t*' holding the source code; If 'ist'
|
||||
* is QSE_AWK_PARSESIMPLE_FILE, 'isp' should be a null-terminated string of
|
||||
* the type 'const qse_char_t*' holding the file name to read the source code
|
||||
* from; If 'ist' is QSE_AWK_PARSESIMPLE_STRL, 'isp' should be a const pointer
|
||||
* to a variable of the type 'qse_cstr_t'. It holds the pointer to the source
|
||||
* code and its length.
|
||||
*
|
||||
* If 'ost' is QSE_AWK_PARSESIMPLE_STR, 'osp' should be a pointer to a
|
||||
* 'qse_char_t' buffer whose end is marked with a null character. that is
|
||||
* the buffer should not contain a null character before its end; If 'ost'
|
||||
* is QSE_AWK_PARSESIMPLE_FILE, the deparsed source code is written to a file
|
||||
* indicated by 'osp' of the type 'const qse_char_t*';
|
||||
* If 'ost' is QSE_AWK_PARSESIMPLE_STRL, the buffer to be written is indicated
|
||||
* in the parameter 'osp' of the type 'qse_xstr_t*'.
|
||||
*
|
||||
* You can set 'ost' to QSE_AWK_PARSESIMPLE_NONE not to produce deparsed
|
||||
* source code while setting 'ist' TO QSE_AWK_PARSESIMPLE_NONE is an error.
|
||||
*
|
||||
* EXAMPLE
|
||||
* The following example parses the literal string 'BEGIN { print 10; }' and
|
||||
* deparses it out to a buffer 'buf'.
|
||||
* int n;
|
||||
* qse_awk_parsesimple_in_t in;
|
||||
* qse_awk_parsesimple_out_t out;
|
||||
* qse_char_t buf[1000];
|
||||
*
|
||||
* qse_memset (buf, QSE_T(' '), QSE_COUNTOF(buf));
|
||||
* buf[QSE_COUNTOF(buf)-1] = QSE_T('\0');
|
||||
* n = qse_awk_parsesimple (
|
||||
* awk,
|
||||
* QSE_AWK_PARSESIMPLE_STR,
|
||||
* QSE_T("BEGIN { print 10; }"),
|
||||
* QSE_AWK_PARSESIMPLE_STR,
|
||||
* buf
|
||||
* );
|
||||
* in.type = QSE_AWK_PARSESIMPLE_STR;
|
||||
* in.u.str = QSE_T("BEGIN { print 10; }");
|
||||
* out.type = QSE_AWK_PARSESIMPLE_STR;
|
||||
* out.u.str = buf;
|
||||
*
|
||||
* n = qse_awk_parsesimple (awk, &in, &out);
|
||||
* SYNOPSIS
|
||||
*/
|
||||
int qse_awk_parsesimple (
|
||||
qse_awk_t* awk,
|
||||
qse_awk_parsesimple_type_t ist,
|
||||
const void* isp,
|
||||
qse_awk_parsesimple_type_t ost,
|
||||
const void* osp
|
||||
const qse_awk_parsesimple_in_t* in,
|
||||
qse_awk_parsesimple_out_t* out
|
||||
);
|
||||
/******/
|
||||
|
||||
|
@ -347,27 +347,32 @@ static qse_ssize_t sf_out (
|
||||
|
||||
int qse_awk_parsesimple (
|
||||
qse_awk_t* awk,
|
||||
qse_awk_parsesimple_type_t ist,
|
||||
const void* isp,
|
||||
qse_awk_parsesimple_type_t ost,
|
||||
const void* osp)
|
||||
const qse_awk_parsesimple_in_t* in,
|
||||
qse_awk_parsesimple_out_t* out)
|
||||
{
|
||||
qse_awk_sio_t sio;
|
||||
xtn_t* xtn = (xtn_t*) QSE_XTN (awk);
|
||||
|
||||
switch (ist)
|
||||
if (in == QSE_NULL)
|
||||
{
|
||||
/* the input is a must */
|
||||
qse_awk_seterrnum (awk, QSE_AWK_EINVAL);
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (in->type)
|
||||
{
|
||||
case QSE_AWK_PARSESIMPLE_FILE:
|
||||
xtn->s.in.u.file = (const qse_char_t*)isp;
|
||||
xtn->s.in.u.file = in->u.file;
|
||||
break;
|
||||
|
||||
case QSE_AWK_PARSESIMPLE_STR:
|
||||
xtn->s.in.u.str = (const qse_char_t*)isp;
|
||||
xtn->s.in.u.str = in->u.str;
|
||||
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;
|
||||
xtn->s.in.u.strl.ptr = in->u.strl.ptr;
|
||||
xtn->s.in.u.strl.end = in->u.strl.ptr + in->u.strl.len;
|
||||
break;
|
||||
|
||||
case QSE_AWK_PARSESIMPLE_STDIO:
|
||||
@ -378,41 +383,41 @@ int qse_awk_parsesimple (
|
||||
qse_awk_seterrnum (awk, QSE_AWK_EINVAL);
|
||||
return -1;
|
||||
}
|
||||
|
||||
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.type = in->type;
|
||||
xtn->s.in.handle = QSE_NULL;
|
||||
|
||||
xtn->s.out.type = ost;
|
||||
xtn->s.out.handle = QSE_NULL;
|
||||
|
||||
sio.in = sf_in;
|
||||
sio.out = (ost == QSE_AWK_PARSESIMPLE_NONE)? QSE_NULL: sf_out;
|
||||
|
||||
if (out == QSE_NULL) sio.out = QSE_NULL;
|
||||
else
|
||||
{
|
||||
switch (out->type)
|
||||
{
|
||||
case QSE_AWK_PARSESIMPLE_FILE:
|
||||
xtn->s.out.u.file = out->u.file;
|
||||
break;
|
||||
|
||||
case QSE_AWK_PARSESIMPLE_STR:
|
||||
xtn->s.out.u.str = out->u.str;
|
||||
break;
|
||||
|
||||
case QSE_AWK_PARSESIMPLE_STRL:
|
||||
xtn->s.out.u.strl.osp = &out->u.strl;
|
||||
xtn->s.out.u.strl.ptr = out->u.strl.ptr;
|
||||
xtn->s.out.u.strl.end = out->u.strl.ptr + out->u.strl.len;
|
||||
break;
|
||||
|
||||
case QSE_AWK_PARSESIMPLE_STDIO:
|
||||
/* nothing to do */
|
||||
break;
|
||||
|
||||
default:
|
||||
qse_awk_seterrnum (awk, QSE_AWK_EINVAL);
|
||||
return -1;
|
||||
}
|
||||
xtn->s.out.type = out->type;
|
||||
xtn->s.out.handle = QSE_NULL;
|
||||
sio.out = sf_out;
|
||||
}
|
||||
|
||||
return qse_awk_parse (awk, &sio);
|
||||
}
|
||||
|
@ -40,6 +40,7 @@ int main ()
|
||||
{
|
||||
qse_awk_t* awk = QSE_NULL;
|
||||
qse_awk_rtx_t* rtx = QSE_NULL;
|
||||
qse_awk_parsesimple_in_t psin;
|
||||
int ret;
|
||||
|
||||
awk = qse_awk_opensimple ();
|
||||
@ -49,13 +50,10 @@ int main ()
|
||||
goto oops;
|
||||
}
|
||||
|
||||
ret = qse_awk_parsesimple (
|
||||
awk,
|
||||
/* parse AWK source in a string */
|
||||
QSE_AWK_PARSESIMPLE_STR, src,
|
||||
/* no deparse output */
|
||||
QSE_AWK_PARSESIMPLE_NONE, QSE_NULL
|
||||
);
|
||||
psin.type = QSE_AWK_PARSESIMPLE_STR;
|
||||
psin.u.str = src;
|
||||
|
||||
ret = qse_awk_parsesimple (awk, &psin, QSE_NULL);
|
||||
if (ret == -1)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
|
@ -43,6 +43,10 @@ int main ()
|
||||
{
|
||||
qse_awk_t* awk = QSE_NULL;
|
||||
qse_awk_rtx_t* rtx = QSE_NULL;
|
||||
|
||||
qse_awk_parsesimple_in_t psin;
|
||||
qse_awk_parsesimple_out_t psout;
|
||||
|
||||
int ret;
|
||||
|
||||
awk = qse_awk_opensimple ();
|
||||
@ -55,13 +59,12 @@ int main ()
|
||||
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
|
||||
);
|
||||
psin.type = QSE_AWK_PARSESIMPLE_STR;
|
||||
psin.u.str = src;
|
||||
psout.type = QSE_AWK_PARSESIMPLE_STR;
|
||||
psout.u.str = srcout;
|
||||
|
||||
ret = qse_awk_parsesimple (awk, &psin, &psout);
|
||||
if (ret == -1)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
|
@ -47,6 +47,9 @@ int main ()
|
||||
{
|
||||
qse_awk_t* awk = QSE_NULL;
|
||||
qse_awk_rtx_t* rtx = QSE_NULL;
|
||||
|
||||
qse_awk_parsesimple_in_t psin;
|
||||
|
||||
int ret, i;
|
||||
|
||||
/* create a main processor */
|
||||
@ -60,13 +63,10 @@ int main ()
|
||||
/* don't allow BEGIN, END, pattern-action blocks */
|
||||
qse_awk_setoption (awk, qse_awk_getoption(awk) & ~QSE_AWK_PABLOCK);
|
||||
|
||||
ret = qse_awk_parsesimple (
|
||||
awk,
|
||||
/* parse AWK source in a string */
|
||||
QSE_AWK_PARSESIMPLE_STR, src,
|
||||
/* no parse output */
|
||||
QSE_AWK_PARSESIMPLE_NONE, QSE_NULL
|
||||
);
|
||||
psin.type = QSE_AWK_PARSESIMPLE_STR;
|
||||
psin.u.str = src;
|
||||
|
||||
ret = qse_awk_parsesimple (awk, &psin, QSE_NULL);
|
||||
if (ret == -1)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
|
Loading…
x
Reference in New Issue
Block a user