/* ----------------------------------------------------------------------------- * s-exp.cxx * * A parse tree represented as Lisp s-expressions. * * Author(s) : Matthias Koeppe (mkoeppe@mail.math.uni-magdeburg.de) * * Copyright (C) 2002. The University of Chicago * See the file LICENSE for information on usage and redistribution. * ----------------------------------------------------------------------------- */ /* Derived from xml.cxx 1.1.2.2 */ char cvsroot_s_exp_cxx[] = "$Header$"; static const char *usage = "\ S-Exp Options (available with -sexp)\n\ -typemaplang lang - Typemap language.\n\n"; #include "swigmod.h" //static Node *view_top = 0; static File *out = 0; class Sexp : public Language { public: int indent_level; Sexp() : indent_level( 0 ) {} virtual ~Sexp() {} virtual void main(int argc, char *argv[]) { SWIG_typemap_lang("sexp"); for( int iX = 0; iX < argc; iX++ ) { if( strcmp( argv[iX], "-typemaplang" ) == 0 ) { Swig_mark_arg (iX); iX++; SWIG_typemap_lang(argv[iX]); Swig_mark_arg (iX); continue; } if( strcmp( argv[iX], "-help" ) == 0 ) { fputs( usage, stderr ); } } } DOHHash *print_circle_hash; int print_circle_count; int hanging_parens; bool need_whitespace; bool need_newline; /* Top of the parse tree */ virtual int top(Node *n) { if( out == 0 ) { String *outfile = Getattr(n,"outfile"); Replaceall(outfile,"_wrap.cxx", ".lisp"); out = NewFile(outfile,"w"); if (!out) { Printf(stderr,"*** Can't open '%s'\n", outfile); SWIG_exit(EXIT_FAILURE); } } Language::top(n); Printf( out, ";;; Lisp parse tree produced by SWIG\n" ); print_circle_hash = DohNewHash(); print_circle_count = 0; hanging_parens = 0; need_whitespace = 0; need_newline = 0; Sexp_print_node(n); flush_parens(); return SWIG_OK; } void print_indent() { int i; for (i = 0; i < indent_level; i++) { Printf(out, " "); } } void open_paren(const String *oper) { flush_parens(); Printf(out, "("); if (oper) Printf(out, "%s ", oper); indent_level += 2; } void close_paren(bool need_newline = false) { hanging_parens++; if (need_newline) print_lazy_whitespace(); indent_level -= 2; } void flush_parens() { int i; if (hanging_parens) { for (i = 0; i", obj); } } } } void Sexp_print_as_keyword(const DOH *k) { /* Print key, replacing ":" with "-" because : is CL's package prefix */ flush_parens(); String *key = NewString(k); Replaceall(key, ":", "-"); Replaceall(key, "_", "-"); Printf(out,":%s ", key); Delete(key); } void Sexp_print_plist_noparens(DOH *obj) { /* attributes map names to objects */ String *k; bool first; for (k = Firstkey(obj), first = true; k; k = Nextkey(obj), first=false) { if (!internal_key_p(k)) { DOH *value = Getattr(obj, k); flush_parens(); if (!first) { Printf(out, " "); } Sexp_print_as_keyword(k); /* Print value */ Sexp_print_value_of_key(value, k); } } } void Sexp_print_plist(DOH *obj) { flush_parens(); if (print_circle(obj, true)) { open_paren(NIL); Sexp_print_plist_noparens(obj); close_paren(); } } void Sexp_print_attributes(Node * obj) { Sexp_print_plist_noparens(obj); } void Sexp_print_node(Node *obj) { Node *cobj; open_paren(nodeType(obj)); /* A node has an attribute list... */ Sexp_print_attributes(obj); /* ... and child nodes. */ cobj = firstChild(obj); if (cobj) { print_lazy_newline(); flush_parens(); Sexp_print_as_keyword("children"); open_paren(NIL); for (; cobj; cobj = nextSibling(cobj)) { Sexp_print_node(cobj); } close_paren(); } close_paren(); } virtual int functionWrapper(Node *n) { ParmList *l = Getattr(n,"parms"); Wrapper *f = NewWrapper(); emit_attach_parmmaps(l,f); Setattr(n,"wrap:parms",l); return SWIG_OK; } }; extern "C" { Language * swig_sexp( void ) { return new Sexp(); } }