view anagram/agcore/ftpar.h @ 21:1c9dac05d040

Add lint-style FALLTHROUGH annotations to fallthrough cases. (in the parse engine and thus the output code) Document this, because the old output causes warnings with gcc10.
author David A. Holland
date Mon, 13 Jun 2022 00:04:38 -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 */