/* * Copyright (c) 2005 Scott E. Johnston * Copyright (c) 1994, 1995, 1998 Vectaport Inc. * * Permission to use, copy, modify, distribute, and sell this software and * its documentation for any purpose is hereby granted without fee, provided * that the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the names of the copyright holders not be used in * advertising or publicity pertaining to distribution of the software * without specific, written prior permission. The copyright holders make * no representations about the suitability of this software for any purpose. * It is provided "as is" without express or implied warranty. * * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. * IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * */ #include #include #include #include #define TITLE "Parser" extern int _continuation_prompt; extern int _continuation_prompt_disabled; extern int _skip_shell_comments; extern infuncptr _oneshot_infunc; extern int _detail_matched_delims; extern int _ignore_numerics; extern int _angle_brackets; extern unsigned _token_state_save; extern void* parser_client; /* pointer to current client */ extern unsigned expecting; /* Type of operator expected next */ extern paren_stack *ParenStack; /* Stack to count args and keywords */ extern int TopOfParenStack; /* Top of ParenStack */ extern int SizeOfParenStack; /* Allocated size of ParenStack */ extern oper_stack *OperStack; /* Operator stack */ extern int TopOfOperStack; /* Top of OperStack */ extern int SizeOfOperStack; /* Allocated size of OperStack */ extern unsigned NextBufptr; /* Variables for look-ahead token */ extern char *NextToken; extern unsigned NextToklen; extern unsigned NextToktype; extern unsigned NextTokstart; extern unsigned NextLinenum; extern int NextOp_ids[OPTYPE_NUM]; #if __GNUC__>=3 static char newline; #endif /*****************************************************************************/ Parser::Parser() : ComTerpModule() { init(); } Parser::Parser(const char* path) : ComTerpModule(path) { init(); } Parser::Parser(void* inptr, char*(*infunc)(char*,int,void*), int(*eoffunc)(void*), int(*errfunc)(void*)) : ComTerpModule(inptr, infunc, eoffunc, errfunc) { init(); } Parser::Parser(istream& in) : ComTerpModule(&in, istream_fgets, istream_feof, istream_ferror) { init(); } void Parser::init() { /* Allocate block for initial postfix tokens to start with */ _pfsiz = 256; if(dmm_calloc((void**)&_pfbuf, _pfsiz, sizeof(postfix_token)) != 0) KANRET("error in call to dmm_calloc"); /* Create and load operator table */ if(opr_tbl_default() != 0) KANRET("error in creating and loading default operator table"); /* initialize the backup copies of all the globals */ __continuation_prompt = 0; __continuation_prompt_disabled = 0; __skip_shell_comments = 0; __detail_matched_delims = 0; __angle_brackets = 0; __token_state_save = TOK_WHITESPACE; __ignore_numerics = 0; } Parser::~Parser() { /* Free postfix token buffer */ if(dmm_free((void**)&_pfbuf) != 0) KANRET ("error in call to dmm_free"); } int Parser::print_next_expr() { int status = parser (_inptr, _infunc, _eoffunc, _errfunc, NULL, NULL, _buffer, _bufsiz, &_bufptr, _token, _toksiz, &_linenum, &_pfbuf, &_pfsiz, &_pfnum); if (status) err_print( stdout, "parser" ); else for (int i = 0; i < _pfnum; i++) print_pfbuf(_pfbuf,i); return _pfbuf[_pfnum-1].type != TOK_EOF; } postfix_token* Parser::copy_postfix_tokens(int& ntokens) { ntokens = _pfnum; postfix_token *pfcopy = new postfix_token[ntokens]; for (int i=0; i in.get(newline); #endif if (in.good()) { int i = 0; for (; i