// Generated by Bisonc++ V2.09.04 on Mon, 16 Jan 2012 21:40:37 +0100

#ifndef Parser_h_included
#define Parser_h_included

#include <string>

#include "../state/state.h"
#include "../block/block.h"
#include "../options/options.h"

// $insert baseclass
#include "parserbase.h"
// $insert scanner.h
#include "../scanner/scanner.h"

class States;
class Rules;

#undef Parser
class Parser: public ParserBase, public FlexTypes
{
    using Pair = std::pair<size_t, size_t>;

    Options &d_options;

    // $insert scannerobject
    Scanner d_scanner;
    std::string const &d_matched;   // text matched at the last lex() call.
    std::string d_rawStringContent;    // only valid when RAWSTRING was
                                        // returned 
    Block   d_block;

    bool d_nlAtEOF          = false;    // lex.cc: compensate missing final \n
    bool d_boln             = false;    // rule starts at boln
    bool d_doError          = true;     // use the error() function at ERRORs
    bool d_warnCarets       = false;    // carets in a RE
    bool d_warnDollars      = false;    // dollars in a RE
    bool d_usesLOP          = false;    // true while inside a regex using the
                                        // LOP: see lookahead.cc
    bool d_nullMatchingRules = false;
    bool d_printTokens;

    size_t d_parentheses = 0;
    size_t d_tokenCount = 0;
    size_t d_atEOF          = 0;        // counts 0-tokens in sequence

    std::string d_expect;

    Rules &d_rules;
    States &d_states;

    size_t d_nLOPstartConditions;           // startconditions index for 
                                            // the next LOP, initialized in
                                            // 'grammar'

    static int s_ignoreToken;
    static std::string s_lastMsg;

    public:
        Parser(Rules &rules, States &states);
        int parse();
        void addLopStartConditions();
        void cleanup();     // prepare Mstream tags, define accessor variables
                            // and show filenames
    private:
        FPattern eolnDollar();

        void orAction();    // sets the action of the last rule but one to 
                            // the last rule's action

        void reset();       // prepare the parser for a new regex 
                            // (resetting tokencount and warning flags)

        FPattern boln();
        FPattern dollar();
        FPattern quotes();
        FPattern lookahead(FPattern const &left, FPattern const &right);
        FPattern interval(FPattern &regex, Interval const &interval);

        FPattern escape();                                   // .ih
        FPattern str();                                      // .ih
        FPattern rawText();                                  // .ih
        FPattern rawText(std::string const &str);            // .ih

        void assignBlock();
        void noActions();

        void addRule(FPattern const &rule, bool resetMs = false);

        TextType rawString();           // returns the raw string content

        void block();
        void error();                   // called on (syntax) errors
        int lex();                      // returns the next token from the
                                        // lexical scanner. 
        void print();  

    // support functions for parse():
        void executeAction_(int ruleNr);
        void errorRecovery_();
        void nextCycle_();
        void nextToken_();
        void print_();
        void exceptionHandler(std::exception const &exc);
};

#endif







