3709192002-03-20Martin Nilsson #pike __REAL_VERSION__
35eee72001-01-08David Norlin // This module contains utility functions for XML creation and // some other useful stuff common to all the modules.
d47c322001-03-06David Norlin #include "./debug.h"
9eaf1d2008-06-28Martin Nilsson protected constant DOC_COMMENT = "//!";
35eee72001-01-08David Norlin 
520d8e2011-11-16Henrik Grubbström (Grubba) //! Flags affecting autodoc extractor behaviour. //! //! @seealso //! @[ProcessXML.extractXML()], @[MirarDocParser], //! @[Tools.Standalone.extract_autodoc()] enum Flags { FLAG_QUIET = 0, //! Keep quiet about non-fatal errors. FLAG_NORMAL = 1, //! Normal verbosity. FLAG_VERBOSE = 2, //! Extra verbosity. FLAG_DEBUG = 3, //! Full verbosity. FLAG_VERB_MASK = 3, //! Verbosity mask. FLAG_KEEP_GOING = 4, //! Attempt to keep going after errors. FLAG_COMPAT = 8, //! Attempt to be compatible with old Pike. FLAG_NO_DYNAMIC = 16, //! Reduce the amount of dynamic information //! in the generated files (line numbers, //! extractor version, extraction time, etc). }
9eaf1d2008-06-28Martin Nilsson protected int isDigit(int c) { return '0' <= c && c <= '9'; }
35eee72001-01-08David Norlin 
9eaf1d2008-06-28Martin Nilsson protected int isDocComment(string s) {
456f4b2001-01-08Henrik Grubbström (Grubba)  return has_prefix(s, DOC_COMMENT);
35eee72001-01-08David Norlin } //FIXME: should it return 0 for keywords ??
9eaf1d2008-06-28Martin Nilsson protected int isIdent(string s) {
35eee72001-01-08David Norlin  if (!s || s == "") return 0; int first = s[0]; switch(s[0]) { case 'a'..'z': case 'A'..'Z': case '_': case '`': break; default: if (s[0] < 128) return 0; } return 1; }
9eaf1d2008-06-28Martin Nilsson protected int(0..1) isFloat(string s)
ade4e12002-12-23Henrik Grubbström (Grubba) { int n; sscanf(s, "%*[0-9].%*[0-9]%n", n); return sizeof(s) && n == sizeof(s); }
9eaf1d2008-06-28Martin Nilsson protected string xmlquote(string s) {
35eee72001-01-08David Norlin  return replace(s, ({ "<", ">", "&" }), ({ "&lt;", "&gt;", "&amp;" })); }
9eaf1d2008-06-28Martin Nilsson protected string attributequote(string s) {
35eee72001-01-08David Norlin  return replace(s, ({ "<", ">", "\"", "'", "&" }), ({ "&lt;", "&gt;", "&#34;", "&#39;", "&amp;" })); }
9eaf1d2008-06-28Martin Nilsson protected string writeattributes(mapping(string:string) attrs)
d5569c2001-05-01Johan Sundström {
35eee72001-01-08David Norlin  string s = "";
d5569c2001-05-01Johan Sundström  foreach(sort(indices(attrs || ([]))), string attr)
35eee72001-01-08David Norlin  s += sprintf(" %s='%s'", attr, attributequote(attrs[attr])); return s; }
9eaf1d2008-06-28Martin Nilsson protected string opentag(string t, mapping(string:string)|void attributes) {
35eee72001-01-08David Norlin  return "<" + t + writeattributes(attributes) + ">"; }
9eaf1d2008-06-28Martin Nilsson protected string closetag(string t) { return "</" + t + ">"; }
35eee72001-01-08David Norlin 
9eaf1d2008-06-28Martin Nilsson protected string xmltag(string t, string|mapping(string:string)|void arg1,
35eee72001-01-08David Norlin  string|void arg2) { mapping attributes = mappingp(arg1) ? arg1 : 0; string content = stringp(arg1) ? arg1 : stringp(arg2) ? arg2 : 0; if (content && content != "") return opentag(t, attributes) + content + closetag(t);
d47c322001-03-06David Norlin  string s = "<" + t + writeattributes(attributes) + "/>"; return s;
35eee72001-01-08David Norlin }
dd2d3b2001-01-09David Norlin 
6944d22010-08-29Henrik Grubbström (Grubba) //! Class used to keep track of where in the source a piece of //! documentation originated.
dd2d3b2001-01-09David Norlin class SourcePosition {
6944d22010-08-29Henrik Grubbström (Grubba)  //!
dd2d3b2001-01-09David Norlin  string filename;
6944d22010-08-29Henrik Grubbström (Grubba)  //! Range of lines.
dd2d3b2001-01-09David Norlin  int firstline; int lastline;
6944d22010-08-29Henrik Grubbström (Grubba)  //!
9eaf1d2008-06-28Martin Nilsson  protected void create(string filename, int firstline,
dd2d3b2001-01-09David Norlin  int|void lastline) {
d47c322001-03-06David Norlin  if (!firstline) { werror("**********************************************************\n"); werror("* NO FIRST LINE !!!!! \n"); werror("**********************************************************\n"); werror("%s", describe_backtrace(backtrace())); werror("**********************************************************\n"); werror("**********************************************************\n"); }
8e06a32014-09-30Martin Nilsson  this::filename = filename; this::firstline = firstline; this::lastline = lastline;
dd2d3b2001-01-09David Norlin  }
6944d22010-08-29Henrik Grubbström (Grubba)  //! @returns //! Returns a copy of the current object.
d47c322001-03-06David Norlin  SourcePosition copy() { return SourcePosition(filename, firstline, lastline); }
4507e92002-11-29Martin Nilsson  string _sprintf(int t) { if(t!='O') return 0;
dd2d3b2001-01-09David Norlin  string res = "SourcePosition(File: " + (filename ? filename : "?"); if (firstline) if (lastline) res += sprintf(", lines: %d..%d", firstline, lastline); else res += sprintf(", line: %d", firstline); return res + ")"; }
d47c322001-03-06David Norlin 
6944d22010-08-29Henrik Grubbström (Grubba)  //! @returns //! Returns a string with an XML-fragment describing the source position.
14fcb52011-12-04Henrik Grubbström (Grubba)  string xml(Flags|void flags) { if (flags & FLAG_NO_DYNAMIC) return "";
d47c322001-03-06David Norlin  mapping(string:string) m = ([]); m["file"] = filename || "?"; if (firstline) m["first-line"] = (string) firstline; if (lastline) m["last-line"] = (string) lastline; return xmltag("source-position", m); }
dd2d3b2001-01-09David Norlin }
6944d22010-08-29Henrik Grubbström (Grubba) //! Base class for errors generated by the autodoc extraction system. class AutoDocError ( //! SourcePosition position, //! Which part of the autodoc system. string part, //! Error message. string message ) {
4507e92002-11-29Martin Nilsson  string _sprintf(int t) { return t=='O' && sprintf("%O(%O, %O, %O)", this_program, position, part, message);
d47c322001-03-06David Norlin  } }