qse/ase/awk/awk.h

195 lines
3.8 KiB
C
Raw Normal View History

2005-11-05 17:54:00 +00:00
/*
2006-02-08 16:14:31 +00:00
* $Id: awk.h,v 1.28 2006-02-08 16:14:31 bacon Exp $
2005-11-05 17:54:00 +00:00
*/
#ifndef _XP_AWK_AWK_H_
#define _XP_AWK_AWK_H_
2006-01-18 15:16:01 +00:00
#ifdef __STAND_ALONE
#include <xp/awk/sa.h>
#else
2005-11-05 17:54:00 +00:00
#include <xp/types.h>
#include <xp/macros.h>
2005-12-29 12:04:51 +00:00
#include <xp/bas/str.h>
2006-01-18 15:16:01 +00:00
#endif
2006-01-09 12:51:47 +00:00
#include <xp/awk/tree.h>
2006-01-29 18:28:14 +00:00
#include <xp/awk/tab.h>
2006-01-30 14:34:47 +00:00
#include <xp/awk/hash.h>
2005-11-05 17:54:00 +00:00
2005-11-06 12:01:29 +00:00
enum
{
2005-11-07 16:02:44 +00:00
XP_AWK_ENOERR,
2005-11-14 15:23:54 +00:00
XP_AWK_ENOMEM, /* out of memory */
2006-01-31 16:57:45 +00:00
2005-11-07 16:02:44 +00:00
XP_AWK_ESRCOP,
2005-11-14 15:23:54 +00:00
XP_AWK_ESRCCL,
XP_AWK_ESRCDT, /* error in reading source */
2006-01-09 12:51:47 +00:00
2005-11-14 15:23:54 +00:00
XP_AWK_ELXCHR, /* lexer came accross an wrong character */
2006-01-09 12:51:47 +00:00
XP_AWK_ELXUNG, /* lexer failed to unget a character */
2006-01-09 16:03:56 +00:00
XP_AWK_EENDSRC, /* unexpected end of source */
XP_AWK_ELBRACE, /* left brace expected */
2006-01-14 16:09:58 +00:00
XP_AWK_ELPAREN, /* left parenthesis expected */
2006-01-11 14:03:17 +00:00
XP_AWK_ERPAREN, /* right parenthesis expected */
2006-01-19 16:28:21 +00:00
XP_AWK_ERBRACK, /* right bracket expected */
2006-01-13 14:16:56 +00:00
XP_AWK_ECOMMA, /* comma expected */
2006-01-10 13:57:54 +00:00
XP_AWK_ESEMICOLON, /* semicolon expected */
2006-01-19 10:56:35 +00:00
XP_AWK_EEXPR, /* expression expected */
2006-01-19 16:28:21 +00:00
XP_AWK_EWHILE, /* keyword 'while' is expected */
2006-01-20 07:33:46 +00:00
XP_AWK_EASSIGN, /* assignment statement expected */
2006-01-22 15:11:17 +00:00
XP_AWK_EIDENT, /* identifier expected */
XP_AWK_EDUPBEGIN, /* duplicate BEGIN */
2006-01-29 18:28:14 +00:00
XP_AWK_EDUPEND, /* duplicate END */
XP_AWK_EDUPFUNC, /* duplicate function name */
2006-01-31 16:57:45 +00:00
XP_AWK_EDUPPARAM, /* duplicate parameter name */
2006-02-04 19:31:51 +00:00
XP_AWK_EDUPVAR, /* duplicate variable name */
2006-02-05 13:45:59 +00:00
XP_AWK_EDUPNAME, /* duplicate name - function, variable, etc */
XP_AWK_EUNDEF /* undefined identifier */
2005-11-06 12:01:29 +00:00
};
2005-11-05 17:54:00 +00:00
/*
* TYPE: xp_awk_t
*/
typedef struct xp_awk_t xp_awk_t;
2006-02-07 15:28:05 +00:00
typedef struct xp_awk_chain_t xp_awk_chain_t;
2005-11-05 17:54:00 +00:00
/*
* TYPE: xp_awk_io_t
*/
typedef xp_ssize_t (*xp_awk_io_t) (
int cmd, void* arg, xp_char_t* data, xp_size_t count);
2006-01-19 16:28:21 +00:00
/* io function commands */
2005-11-05 17:54:00 +00:00
enum
{
XP_AWK_IO_OPEN,
XP_AWK_IO_CLOSE,
XP_AWK_IO_DATA
};
2006-01-31 16:57:45 +00:00
/* parse options */
2006-01-19 16:28:21 +00:00
enum
{
2006-02-05 13:45:59 +00:00
XP_AWK_IMPLICIT = (1 << 0), /* allow undeclared variables */
XP_AWK_EXPLICIT = (1 << 1), /* variable requires explicit declaration */
XP_AWK_UNIQUE = (1 << 2), /* a function name should not coincide to be a variable name */
2006-02-08 16:14:31 +00:00
XP_AWK_SHADING = (1 << 3), /* allow variable shading */
XP_AWK_SHIFT = (1 << 4) /* support shift operators */
2006-01-19 16:28:21 +00:00
};
2005-11-05 17:54:00 +00:00
struct xp_awk_t
{
2006-01-19 16:28:21 +00:00
/* options */
2006-01-31 16:57:45 +00:00
struct
{
int parse;
int run;
} opt;
2006-01-19 16:28:21 +00:00
2005-11-05 17:54:00 +00:00
/* io functions */
2005-12-29 12:04:51 +00:00
xp_awk_io_t src_func;
2006-01-19 16:28:21 +00:00
xp_awk_io_t in_func;
xp_awk_io_t out_func;
2005-11-07 16:02:44 +00:00
2005-12-29 12:04:51 +00:00
void* src_arg;
2006-01-19 16:28:21 +00:00
void* in_arg;
void* out_arg;
2005-11-05 17:54:00 +00:00
2006-01-22 15:11:17 +00:00
/* parse tree */
struct
{
2006-02-05 16:00:33 +00:00
xp_size_t nglobals;
2006-01-30 14:34:47 +00:00
xp_awk_hash_t funcs;
2006-01-22 15:11:17 +00:00
xp_awk_node_t* begin;
xp_awk_node_t* end;
2006-02-07 15:28:05 +00:00
xp_awk_chain_t* chain;
xp_awk_chain_t* chain_tail;
2006-01-22 15:11:17 +00:00
} tree;
2006-01-25 16:11:43 +00:00
/* temporary information that the parser needs */
struct
{
2006-02-04 19:31:51 +00:00
xp_awk_tab_t globals;
xp_awk_tab_t locals;
2006-01-31 16:57:45 +00:00
xp_awk_tab_t params;
2006-02-04 19:31:51 +00:00
xp_size_t nlocals_max;
2006-01-25 16:11:43 +00:00
} parse;
2005-11-07 16:02:44 +00:00
/* source buffer management */
2006-01-22 15:11:17 +00:00
struct
{
2005-11-07 16:02:44 +00:00
xp_cint_t curc;
xp_cint_t ungotc[5];
xp_size_t ungotc_count;
} lex;
2005-11-14 15:23:54 +00:00
/* token */
2006-01-22 15:11:17 +00:00
struct
{
2005-11-14 15:23:54 +00:00
int type;
xp_str_t name;
} token;
2005-11-05 17:54:00 +00:00
/* housekeeping */
2005-11-06 12:01:29 +00:00
int errnum;
2005-12-05 15:11:29 +00:00
xp_bool_t __dynamic;
2005-11-05 17:54:00 +00:00
};
2006-02-07 15:28:05 +00:00
struct xp_awk_chain_t
{
xp_awk_node_t* pattern;
xp_awk_node_t* action;
xp_awk_chain_t* next;
};
2005-11-05 17:54:00 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2005-12-29 12:04:51 +00:00
/*
* FUNCTION: xp_awk_open
*/
2005-11-05 17:54:00 +00:00
xp_awk_t* xp_awk_open (xp_awk_t* awk);
2005-12-29 12:04:51 +00:00
/*
* FUNCTION: xp_awk_close
*/
2005-11-05 17:54:00 +00:00
int xp_awk_close (xp_awk_t* awk);
2006-01-31 16:57:45 +00:00
int xp_awk_geterrnum (xp_awk_t* awk);
const xp_char_t* xp_awk_geterrstr (xp_awk_t* awk);
2006-01-24 16:14:28 +00:00
/*
* FUNCTION: xp_awk_clear
*/
void xp_awk_clear (xp_awk_t* awk);
2005-12-29 12:04:51 +00:00
/*
* FUNCTION: xp_awk_attsrc
*/
int xp_awk_attsrc (xp_awk_t* awk, xp_awk_io_t src, void* arg);
/*
* FUNCTION: xp_awk_detsrc
*/
int xp_awk_detsrc (xp_awk_t* awk);
2006-01-19 16:28:21 +00:00
int xp_awk_attin (xp_awk_t* awk, xp_awk_io_t in, void* arg);
int xp_awk_detin (xp_awk_t* awk);
2005-12-29 12:04:51 +00:00
2006-01-19 16:28:21 +00:00
int xp_awk_attout (xp_awk_t* awk, xp_awk_io_t out, void* arg);
int xp_awk_detout (xp_awk_t* awk);
2005-11-07 16:02:44 +00:00
2006-01-09 16:03:56 +00:00
int xp_awk_parse (xp_awk_t* awk);
2006-01-26 15:35:20 +00:00
int xp_awk_run (xp_awk_t* awk);
2006-01-09 16:03:56 +00:00
2005-11-05 17:54:00 +00:00
#ifdef __cplusplus
}
#endif
#endif