From a15cf65c44d5c224169c32ef5495b68c758134b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Sun, 18 May 2014 16:08:14 +0200 Subject: Imported Upstream version 3.3.0.2 --- libcult/cult/cli/scanner.hxx | 132 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 libcult/cult/cli/scanner.hxx (limited to 'libcult/cult/cli/scanner.hxx') diff --git a/libcult/cult/cli/scanner.hxx b/libcult/cult/cli/scanner.hxx new file mode 100644 index 0000000..13fa804 --- /dev/null +++ b/libcult/cult/cli/scanner.hxx @@ -0,0 +1,132 @@ +// file : cult/cli/scanner.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2010 Boris Kolpackov +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef CULT_CLI_SCANNER_HXX +#define CULT_CLI_SCANNER_HXX + +#include + +#include +#include + +namespace Cult +{ + namespace CLI + { + class Scanner: public NonCopyable + { + public: + class Action + { + public: + static Action const keep, erase; + + friend Boolean + operator== (Action const& a, Action const& b) + { + return a.v_ == b.v_; + } + + friend Boolean + operator!= (Action const& a, Action const& b) + { + return a.v_ != b.v_; + } + + private: + enum Value { keep_, erase_ } v_; + + Action (Value v) + : v_ (v) + { + } + }; + + public: + Scanner (Arguments& args, Action a = Action::keep, Index start = 1) + : cargs_ (args), + args_ (a == Action::erase ? &args : 0), + next_ (start) + { + } + + Scanner (Arguments const& args, Index start = 1) + : cargs_ (args), args_ (0), next_ (start) + { + } + + public: + static Char const* const eos; + + class PastEOS: public virtual Exception {}; + + Char const* + next () + { + if (next_ > cargs_.size ()) + { + throw PastEOS (); + } + else if (next_ == cargs_.size ()) + { + ++next_; + return eos; + } + else + { + Char const* r (cargs_[next_]); + + if (args_ != 0) + { + hold_ = r; + args_->erase (next_); + return hold_.c_str (); + } + else + { + ++next_; + return r; + } + } + } + + Char const* + peek () + { + if (next_ > cargs_.size ()) + { + throw PastEOS (); + } + else if (next_ == cargs_.size ()) + { + return eos; + } + else + { + return cargs_[next_]; + } + } + + Void + skip () + { + if (next_ > cargs_.size ()) + throw PastEOS (); + else + ++next_; + } + + private: + Arguments const& cargs_; + Arguments* args_; + Index next_; + String hold_; + }; + } +} + +#include + +#endif // CULT_CLI_SCANNER_HXX -- cgit v1.2.3