view anagram/agcore/ftpar.h @ 14:a02e9434072e

Fix friend declaration for gcc10. XXX: did not check it against the IBM compiler, might end up needing XXX: to be conditional.
author David A. Holland
date Tue, 31 May 2022 00:59:42 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*
 * AnaGram, A System for Syntax Directed Programming
 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
 * See the file COPYING for license and usage terms.
 *
 * ftpar.h
 */

#ifndef FTPAR_H
#define FTPAR_H

class Token; // from token.h
struct tsd; // from tsd.h
#include "agarray.h"
#include "agsignal.h"
#include "agstack.h"
#include "cint.h"


extern AgArray<unsigned> traceCounts;

int precedes(cint a, cint b);


class FtParser {
public:
  enum ProcessState {
    ready,
    running,
    finished,
    syntaxError,
    unexpectedEndOfFile,
    selectionRequired,
    selectionError
  };

  class State {
  public:
    friend class FtParser;
    friend class FtParserDc;
    friend class FtParserReductionDc;
    friend class FileTraceWindow;
    friend class GTView;
    friend class GTWindow;
    friend class TokenMenuDc;

    int number;
    int line, column, charPos;
    const unsigned char *pointer;
    unsigned token;

  public:
    State(char *p)
      : number(0)
      , line(0)
      , column(0)
      , charPos(0)
      , pointer((const unsigned char*) p)
      , token(0)
    {}
    State()
      : number(0)
      , line(0)
      , column(0)
      , charPos(0)
      , pointer(0)
      , token(0)
    {}
    State(unsigned sn, unsigned tn)
      : number(sn)
      , line(0)
      , column(0)
      , charPos(0)
      , pointer(0)
      , token(tn)
    {}

    cint position() { return cint(charPos,line); }
    int operator < (const State &s) const { return number < s.number; }
  };

  struct Transaction {
    int count;
    State state;

    Transaction() : count(0) {}
    Transaction(int c, State s) : count(c), state(s) {}
    int operator < (const Transaction &t) const { return count < t.count; }
  };

  struct StackChanged {
    int newSize;
    StackChanged() : newSize(0) {}
    StackChanged(int n) : newSize(n) {}
  };

  AgString text;
  State state;
  State reductionState;

  tsd *initialStack;

  AgStack<State> stateStack;
  AgStack<State> auxStack;
  AgStack<Transaction> transStack;
  const unsigned char *lookAhead;
  const unsigned char *endPointer;
  unsigned inputToken;
  int nNullShifts;
  ProcessState processState;
  static const char *processStateText[];

  unsigned actionParameter;

  int ruleLength;
  int ruleToReduce;
  int reductionIndex;
  int reductionSelection;

  //DcRef<FtParserDc> displayControl;
  struct BroadcastStackChanged : public Broadcaster<StackChanged> {
    FtParser &parser;
    BroadcastStackChanged(FtParser &p) : parser(p) {}
    void operator () (unsigned n) const {
      broadcast(FtParser::StackChanged(n));
    }
  };

  BroadcastStackChanged stackChanged;
  //DcRef<rule_stack_dc> ruleControl;
  //tsd *itemStack;

  int testingKeyword;

  void track(void);
  void dispatchReductionToken();

  void shiftTerminal();
  void shiftTerminalAndAccept(void);
  void shiftTerminalAndReduce();
  void reduce(void);
  void skip(void);
  void shiftNull(void);
  void error();
  void accept();

  int shiftNonterminalAndReduce();
  int shiftNonterminal();
  int shiftNonterminalAndAccept();

  static void (FtParser::*terminalAction[11])();
  static int  (FtParser::*nonterminalAction[4])();

  Token keyToken(void);
  void getToken();
  unsigned inspectToken();
  void completeReduction(int reductionToken);
  void completeReduction();
  int  validToken(unsigned, unsigned);
  int  validSelection(unsigned, unsigned);
  void requestSelection(int actionParameter);

public:
  FtParser &parseTo(unsigned char *target);
  FtParser &parseTo(cint *target);
  FtParser &parse(const char *fragment);
  FtParser &prime(const char *fragment);
  FtParser &parse();
  void parseAction();
  void stepToken(unsigned);
  void parseToken(unsigned);

  FtParser &step();
  FtParser &step(char *fragment);
  FtParser &reset();
  cint location() { return cint(state.charPos, state.line); }
  cint displayLocation() { return cint(state.column, state.line); }

  FtParser &ignoreKeyword(int k) {
    testingKeyword = k;
    return *this;
  }

  tsd *x1x_new();

  FtParser(AgString t);
  FtParser();
  FtParser(tsd *initialStack_);
  ~FtParser();
};


#endif /* FTPAR_H */