pike.git / lib / modules / Parser.pmod / Tabular.pike

version» Context lines:

pike.git/lib/modules/Parser.pmod/Tabular.pike:1:   //! This is a parser for line and block oriented data.   //! It provides a flexible yet concise record-description language to parse   //! character/column/delimiter-organised records.   //!   //! @seealso   //! @[Parser.LR]      #pike __REAL_VERSION__    - private Stdio.FILE in; + Stdio.FILE _in;   private int prefetch=1024; // TODO: Document and make this available    // through compile().   private String.Buffer alread=String.Buffer(prefetch);   private mapping|array fms;   private int eol;   private Regexp simple=Regexp("^[^[\\](){}<>^$|+*?\\\\]+$");   private Regexp emptyline=Regexp("^[ \t\v\r\x1a]*$");   private mixed severity=1;   private int verb=0;   private int recordcount=1;
pike.git/lib/modules/Parser.pmod/Tabular.pike:56:   { if(zero_type(verbose)&&intp(format))    verbose=format;    else    fms=stringp(format)||objectp(format)?compile(format):format;    verb=verbose==1?70:verbose;    if(!input)    input=" ";    if(stringp(input))    input=Stdio.FakeFile(input);    if(!input->unread) -  (in=Stdio.FILE())->assign(input); +  (_in=Stdio.FILE())->assign(input);    else -  in=input; +  _in=input;   }      #if 0 // Currently unused function   private int getchar() - { int c=in->getchar(); + { int c=_in->getchar();    if(c<0)    throw(severity);    alread->putchar(c);    return c;   }   #endif      private string read(int n)   { string s; -  s=in->read(n); +  s=_in->read(n);    alread->add(s);    if(sizeof(s)!=n)    throw(severity);    return s;   }      private string gets(int n)   { string s;    if(n)    { s=read(n);    if(has_value(s,"\n")||has_value(s,"\r"))    throw(severity);    }    else -  { s=in->gets(); +  { s=_in->gets();    if(!s)    throw(severity);    if(has_value(s,"\r")) // Retrofix \r-only line endings    { array t;    t=s/"\r"; -  s=t[0];in->unread(t[1..]*"\n"); +  s=t[0];_in->unread(t[1..]*"\n");    }    alread->add(s);alread->putchar('\n');    if(has_suffix(s,"\r"))    s=s[..<1];    eol=1;    }    return s;   }      private class checkpoint
pike.git/lib/modules/Parser.pmod/Tabular.pike:122:    { string s=alread->get();    alread->add(oldalread);    alread->add(s);    oldalread=0;    }       protected void destroy()    { if(oldalread)    { string back=alread->get();    if(sizeof(back)) -  { in->unread(back); +  { _in->unread(back);    if(verb<0)    { back-="\n";    if(sizeof(back))    werror("Backtracking %O\n",back);    }    }    alread->add(oldalread);    }    }   }
pike.git/lib/modules/Parser.pmod/Tabular.pike:188:    if(m->delim)    { multiset delim=m->delim;    int i,pref=m->prefetch || prefetch;    String.Buffer word=String.Buffer(pref);    string buf,skipclass;    skipclass="%[^"+(string)indices(delim)+"\"\r\x1a\n]";    if(sizeof(delim-(<',',';','\t',' '>)))   delimready:    for(;;)    { i=0; -  buf=in->read(pref); +  buf=_in->read(pref);    int c;    FETCHAR(c,buf,i);    while(c>=0)    { if(delim[c])    break delimready;    else switch(c)    { default:    { string s;    sscanf(buf[--i..],skipclass,s);    word->add(s);
pike.git/lib/modules/Parser.pmod/Tabular.pike:229:    }    if(!sizeof(buf))    throw(severity);    alread->add(buf);    }    else    { int leadspace=1,inquotes=0;   csvready:    for(;;)    { i=0; -  buf=in->read(pref); +  buf=_in->read(pref);    int c;    FETCHAR(c,buf,i);    while(c>=0)    { if(delim[c])    { if(!inquotes)    break csvready;    word->putchar(c);    }    else switch(c)    { case '"':leadspace=0;
pike.git/lib/modules/Parser.pmod/Tabular.pike:289:    case '\x1a':;    }    FETCHAR(c,buf,i);    }    if(!sizeof(buf))    throw(severity);    alread->add(buf);    }    }    alread->add(buf[..i-1]); -  in->unread(buf[i..]); +  _in->unread(buf[i..]);    value=word->get();    }    if(m->match)    { Regexp rgx;    if(stringp(m->match))    { if(!value && simple->match(m->match))    { m->width=sizeof(m->match);    value=gets(m->width);    }    m->match=Regexp("^("+m->match+")"+(value?"$":""));
pike.git/lib/modules/Parser.pmod/Tabular.pike:311:    rgx=m->match;    if(value)    { if(!rgx->match(value))    { if(verb<-3)    werror(sprintf("Mismatch %O!=%O\n",value,rgx)    -"Regexp.SimpleRegexp");    throw(severity);    }    }    else -  { string buf=in->read(m->prefetch || prefetch); +  { string buf=_in->read(m->prefetch || prefetch);    { array spr;    if(!buf || !(spr=rgx->split(buf)))    { alread->add(buf);    if(verb<-3)    werror(sprintf("Mismatch %O!=%O\n",buf[..32],rgx)    -"Regexp.SimpleRegexp");    throw(severity);    } -  in->unread(buf[sizeof(value=spr[0])..]); +  _in->unread(buf[sizeof(value=spr[0])..]);    }    alread->add(value);    value-="\r";    if(has_suffix(value,"\n"))    value=value[..<1];    }    }    if(!m->drop)    ret[m->name]=value;    }
pike.git/lib/modules/Parser.pmod/Tabular.pike:379:   //! the input. This is unnecessary if no argument is   //! specified for @[fetch()].   //!   //! @returns   //! It returns true if EOF has been reached.   //!   //! @seealso   //! @[fetch()]   int skipemptylines()   { string line; int eof=1; -  while((line=in->gets()) && String.width(line)==8 && emptyline->match(line)) +  while((line=_in->gets()) && String.width(line)==8 && emptyline->match(line))    recordcount++;    if(line) -  eof=0,in->unread(line+"\n"); +  eof=0,_in->unread(line+"\n");    return eof;   }      //! This function consumes as much input as needed to parse   //! the full tabular structures at once.   //!   //! @param format   //! Describes (precompiled only) formats to be parsed.   //! If no format is specified,   //! the format specified on @[create()] is used, and empty lines are
pike.git/lib/modules/Parser.pmod/Tabular.pike:485:      //! @param content   //! Is injected into the input stream.   //!   //! @returns   //! This object.   //!   //! @seealso   //! @[fetch()]   object feed(string content) - { in->unread(content); + { _in->unread(content);    return this;   }      //! @param format   //! Replaces the default (precompiled only) format.   //!   //! @returns   //! The previous default format.   //!   //! @seealso