e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
0533e21999-02-19Mirar (Pontus Hagland) #include "global.h"
7068e31999-06-12Mirar (Pontus Hagland) #include "stralloc.h" #include "global.h" #include "pike_macros.h" #include "interpret.h"
0533e21999-02-19Mirar (Pontus Hagland) #include "program.h"
6c4a1a1999-12-30Martin Stjernholm #include "program_id.h"
7068e31999-06-12Mirar (Pontus Hagland) #include "object.h" #include "operators.h"
51ef5c2002-10-21Marcus Comstedt #include "module.h"
0533e21999-02-19Mirar (Pontus Hagland)  #include "parser.h"
6dc2772000-07-28Fredrik Hübinette (Hubbe) 
531c172002-05-11Martin Nilsson #define sp Pike_sp #define fp Pike_fp
7068e31999-06-12Mirar (Pontus Hagland) #define PARSER_INITER
0533e21999-02-19Mirar (Pontus Hagland) 
7068e31999-06-12Mirar (Pontus Hagland) /*#define DEBUG*/
6c4a1a1999-12-30Martin Stjernholm #define PARSER_CLASS(name,init,exit,prog,id) \
7068e31999-06-12Mirar (Pontus Hagland)  void init(void); void exit(void); struct program *prog; #define PARSER_SUBMODULE(name,init,exit) \
13670c2015-05-25Martin Nilsson  void init(void); void exit(void);
7068e31999-06-12Mirar (Pontus Hagland) #define PARSER_SUBMODMAG(name,init,exit) \ void init(void); void exit(void); #define PARSER_FUNCTION(name,func,def0,def1) \ void func(INT32 args); #include "initstuff.h"
13670c2015-05-25Martin Nilsson static struct
7068e31999-06-12Mirar (Pontus Hagland) { char *name; void (*init)(void); void (*exit)(void); struct program **dest;
6c4a1a1999-12-30Martin Stjernholm  int id;
7068e31999-06-12Mirar (Pontus Hagland) } initclass[]= { #undef PARSER_CLASS #undef PARSER_SUBMODULE #undef PARSER_FUNCTION #undef PARSER_SUBMODMAG
13670c2015-05-25Martin Nilsson #define PARSER_SUBMODMAG(a,b,c)
7068e31999-06-12Mirar (Pontus Hagland) #define PARSER_FUNCTION(a,b,c,d)
6c4a1a1999-12-30Martin Stjernholm #define PARSER_CLASS(name,init,exit,prog,id) { name,init,exit,&prog,id },
7068e31999-06-12Mirar (Pontus Hagland) #define PARSER_SUBMODULE(a,b,c) #include "initstuff.h" };
13670c2015-05-25Martin Nilsson static struct
7068e31999-06-12Mirar (Pontus Hagland) { char *name; void (*init)(void); void (*exit)(void); } initsubmodule[]= { #undef PARSER_CLASS #undef PARSER_SUBMODULE
6c4a1a1999-12-30Martin Stjernholm #define PARSER_CLASS(name,init,exit,prog,id)
7068e31999-06-12Mirar (Pontus Hagland) #define PARSER_SUBMODULE(name,init,exit) { name,init,exit }, #include "initstuff.h"
07228a1999-06-19Fredrik Hübinette (Hubbe)  {0,0,0 }
7068e31999-06-12Mirar (Pontus Hagland) };
0533e21999-02-19Mirar (Pontus Hagland) 
13670c2015-05-25Martin Nilsson static struct
0533e21999-02-19Mirar (Pontus Hagland) { char *name;
7068e31999-06-12Mirar (Pontus Hagland)  void (*init)(void); void (*exit)(void); struct pike_string *ps; struct object *o; } submagic[]= { #undef PARSER_SUBMODULE #undef PARSER_SUBMODMAG #define PARSER_SUBMODULE(a,b,c) #define PARSER_SUBMODMAG(name,init,exit) { name,init,exit,NULL,NULL }, #include "initstuff.h"
07228a1999-06-19Fredrik Hübinette (Hubbe)  { 0,0,0,0,0 }
0533e21999-02-19Mirar (Pontus Hagland) };
7068e31999-06-12Mirar (Pontus Hagland) #ifdef PIKE_DEBUG
125e522000-08-16Henrik Grubbström (Grubba) #define PARSER_CHECK_STACK(X) do { \ if (save_sp != sp) { \
5aad932002-08-15Marcus Comstedt  Pike_fatal("%s:%d: %ld droppings on stack! previous init: %s\n", \
125e522000-08-16Henrik Grubbström (Grubba)  __FILE__, __LINE__, \
6f95902000-08-17Henrik Grubbström (Grubba)  PTRDIFF_T_TO_LONG(sp - save_sp), X); \
125e522000-08-16Henrik Grubbström (Grubba)  } \ } while(0)
7068e31999-06-12Mirar (Pontus Hagland) #else #define PARSER_CHECK_STACK(X) #endif /* PIKE_DEBUG */
493b3a2010-05-11Henrik Grubbström (Grubba) /*! @module Parser */ /*! @module _parser *! *! Low-level helpers for parsers. *! *! @note *! You probably don't want to use the modules contained in *! this module directly, but instead use the other @[Parser] *! modules. See instead the modules below. *! *! @seealso *! @[Parser], @[Parser.C], @[Parser.Pike], @[Parser.RCS], *! @[Parser.HTML], @[Parser.XML] */
7068e31999-06-12Mirar (Pontus Hagland)  static void parser_magic_index(INT32 args)
0533e21999-02-19Mirar (Pontus Hagland) {
7068e31999-06-12Mirar (Pontus Hagland)  int i;
13670c2015-05-25Martin Nilsson  if (args!=1)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Parser.`[]: Too few or too many arguments\n");
017b572011-10-28Henrik Grubbström (Grubba)  if (TYPEOF(sp[-1]) != T_STRING)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Parser.`[]: Illegal type of argument\n");
7068e31999-06-12Mirar (Pontus Hagland) 
07228a1999-06-19Fredrik Hübinette (Hubbe)  for (i=0; i<(int)NELEM(submagic)-1; i++)
7068e31999-06-12Mirar (Pontus Hagland)  if (sp[-1].u.string==submagic[i].ps) { #ifdef PIKE_DEBUG struct svalue *save_sp; #endif pop_stack(); #ifdef PIKE_DEBUG save_sp = sp; #endif if (!submagic[i].o) { struct program *p; start_new_program(); (submagic[i].init)(); PARSER_CHECK_STACK(submagic[i].name); p=end_program(); submagic[i].o=clone_object(p,0); free_program(p); }
13670c2015-05-25Martin Nilsson 
7068e31999-06-12Mirar (Pontus Hagland)  ref_push_object(submagic[i].o); return; } stack_dup(); ref_push_object(fp->current_object); stack_swap(); f_arrow(2);
017b572011-10-28Henrik Grubbström (Grubba)  if (TYPEOF(sp[-1]) == T_INT)
7068e31999-06-12Mirar (Pontus Hagland)  { pop_stack(); stack_dup();
9feeb72015-08-18Per Hedbor  push_static_text("_Parser_");
7068e31999-06-12Mirar (Pontus Hagland)  stack_swap(); f_add(2);
da1bd12003-12-01Martin Nilsson  SAFE_APPLY_MASTER("resolv",1);
7068e31999-06-12Mirar (Pontus Hagland)  }
017b572011-10-28Henrik Grubbström (Grubba)  if (TYPEOF(sp[-1]) == T_INT)
7068e31999-06-12Mirar (Pontus Hagland)  { pop_stack(); stack_dup();
9feeb72015-08-18Per Hedbor  push_static_text("_Parser");
da1bd12003-12-01Martin Nilsson  SAFE_APPLY_MASTER("resolv",1);
7068e31999-06-12Mirar (Pontus Hagland)  stack_swap();
017b572011-10-28Henrik Grubbström (Grubba)  if(TYPEOF(sp[-2]) == T_INT)
52c5792000-05-23Fredrik Hübinette (Hubbe)  { pop_stack(); }else{ f_index(2); }
7068e31999-06-12Mirar (Pontus Hagland)  } stack_swap(); pop_stack();
0533e21999-02-19Mirar (Pontus Hagland) }
493b3a2010-05-11Henrik Grubbström (Grubba) /*! @endmodule */ /*! @endmodule */
51ef5c2002-10-21Marcus Comstedt PIKE_MODULE_INIT
0533e21999-02-19Mirar (Pontus Hagland) {
7068e31999-06-12Mirar (Pontus Hagland) #ifdef PIKE_DEBUG struct svalue *save_sp = sp; #endif
0533e21999-02-19Mirar (Pontus Hagland)  int i;
7068e31999-06-12Mirar (Pontus Hagland)  for (i=0; i<(int)NELEM(initclass); i++) { start_new_program();
bad5162000-06-23Fredrik Hübinette (Hubbe)  if (initclass[i].id) Pike_compiler->new_program->id = initclass[i].id;
7068e31999-06-12Mirar (Pontus Hagland)  #ifdef DEBUG fprintf(stderr,"Parser: initiating class \"Parser.%s\"...\n", initclass[i].name); #endif (initclass[i].init)(); PARSER_CHECK_STACK(initclass[i].name); initclass[i].dest[0]=end_program(); add_program_constant(initclass[i].name,initclass[i].dest[0],0); }
07228a1999-06-19Fredrik Hübinette (Hubbe)  for (i=0; i<(int)NELEM(initsubmodule)-1; i++)
0533e21999-02-19Mirar (Pontus Hagland)  { struct program *p;
7068e31999-06-12Mirar (Pontus Hagland)  struct pike_string *s;
0533e21999-02-19Mirar (Pontus Hagland) 
7068e31999-06-12Mirar (Pontus Hagland) #ifdef DEBUG fprintf(stderr,"Parser: initiating submodule \"Parser.%s\"...\n", initsubmodule[i].name); #endif
13670c2015-05-25Martin Nilsson 
0533e21999-02-19Mirar (Pontus Hagland)  start_new_program();
7068e31999-06-12Mirar (Pontus Hagland)  (initsubmodule[i].init)(); PARSER_CHECK_STACK(initsubmodule[i].name);
0533e21999-02-19Mirar (Pontus Hagland)  p=end_program();
7068e31999-06-12Mirar (Pontus Hagland)  push_object(clone_object(p,0)); s=make_shared_string(initsubmodule[i].name); add_constant(s,sp-1,0); free_string(s);
0533e21999-02-19Mirar (Pontus Hagland)  free_program(p);
7068e31999-06-12Mirar (Pontus Hagland)  pop_stack(); }
07228a1999-06-19Fredrik Hübinette (Hubbe)  for (i=0; i<(int)NELEM(submagic)-1; i++)
7068e31999-06-12Mirar (Pontus Hagland)  submagic[i].ps=make_shared_string(submagic[i].name); #undef PARSER_FUNCTION #define PARSER_FUNCTION(name,func,def0,def1) ADD_FUNCTION(name,func,def0,def1); #include "initstuff.h" ADD_FUNCTION("`[]",parser_magic_index, tFunc(tString,tMixed),0); }
51ef5c2002-10-21Marcus Comstedt PIKE_MODULE_EXIT
7068e31999-06-12Mirar (Pontus Hagland) { int i; for (i=0; i<(int)NELEM(initclass); i++) { (initclass[i].exit)(); free_program(initclass[i].dest[0]);
0533e21999-02-19Mirar (Pontus Hagland)  }
07228a1999-06-19Fredrik Hübinette (Hubbe)  for (i=0; i<(int)NELEM(initsubmodule)-1; i++)
7068e31999-06-12Mirar (Pontus Hagland)  (initsubmodule[i].exit)();
07228a1999-06-19Fredrik Hübinette (Hubbe)  for (i=0; i<(int)NELEM(submagic)-1; i++)
7068e31999-06-12Mirar (Pontus Hagland)  if (submagic[i].o) { (submagic[i].exit)(); free_object(submagic[i].o); }
0533e21999-02-19Mirar (Pontus Hagland) }