xorp

xrl_parser.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxipc/xrl_parser.hh,v 1.12 2008/10/02 21:57:24 bms Exp $
00023 
00024 #ifndef __LIBXIPC_XRL_PARSER_HH__
00025 #define __LIBXIPC_XRL_PARSER_HH__
00026 
00027 #include <stdarg.h>
00028 
00029 #include "xrl.hh"
00030 #include "xrl_parser_input.hh"
00031 
00032 class XrlParseError {
00033 public:
00034 
00035     XrlParseError(const string& input, ssize_t offset, const string& reason)
00036     : _input(input), _offset(offset), _reason(reason) {}
00037 
00038     XrlParseError(const string& input, string::const_iterator pos,
00039           const string& reason)
00040     : _input(input), _offset(pos - input.begin()), _reason(reason) {}
00041 
00042     XrlParseError() : _input(""), _offset(0xffffffff), _reason("") {}
00043 
00044     virtual ~XrlParseError() {}
00045 
00046     const string& input() const     { return _input; }
00047     ssize_t offset() const      { return _offset; }
00048     const string& reason() const    { return _reason; }
00049 
00050     string pretty_print(size_t termwidth = 80u) const;
00051 
00052 protected:
00053     const string    _input;
00054     size_t      _offset;
00055     string      _reason;
00056 
00057     void get_coordinates(size_t& lineno, size_t& charno) const;
00058 };
00059 
00060 // XrlLocator - locates sub-strings that look like spaceless and
00061 // presentation Xrl's.  It does weak examination of syntax and leaves
00062 // finer grained examination to the Xrl instantiation routines.
00063 
00064 class XrlParser {
00065 public:
00066     XrlParser(XrlParserInput& xpi) : _xpi(xpi) {}
00067     virtual ~XrlParser() {}
00068 
00072     bool start_next() throw (XrlParserInputException);
00073 
00077     bool finished() const { return _xpi.eof(); }
00078 
00079     bool get(string&  protocol,
00080          string&  target,
00081          string&  command,
00082          XrlArgs& args)
00083     throw (XrlParseError);
00084 
00085     bool get(string& protocol,
00086          string& target,
00087          string& command,
00088          XrlArgs& args,
00089          list<XrlAtomSpell>& spells)
00090     throw (XrlParseError);
00091 
00092     bool get(string& xrl_c_str) throw (XrlParseError);
00093 
00094     bool get_return_specs(list<XrlAtomSpell>& spells);
00095 
00096     const string& input() const { return _input; }
00097 
00104     bool resync();
00105 
00106     const XrlParserInput& parser_input() const;
00107 
00108 protected:
00109 
00110     bool get(string&         protocol,
00111          string&         target,
00112          string&         command,
00113          XrlArgs*        args,
00114          list<XrlAtomSpell>* spells)
00115     throw (XrlParseError);
00116 
00117     bool parse_atoms_and_spells(XrlArgs*        args,
00118                 list<XrlAtomSpell>* spells);
00119 
00120     XrlParserInput&    _xpi;
00121     string         _input;
00122     string::const_iterator _pos;
00123 };
00124 
00125 #endif // __LIBXIPC_XRL_PARSER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations