view anagram/agcore/csexp.h @ 12:aab9ff6af791

Strengthen the build hack for non-DOS targets.
author David A. Holland
date Tue, 31 May 2022 00:58: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.
 *
 * csexp.h
 */

#ifndef CSEXP_H
#define CSEXP_H

#include <stdlib.h>
#include "port.h"

class AgString; // from agstring.h
#include "bitmap.h"
#include "minmax.h"

//#define INCLUDE_LOGGING
//#include "log.h"


unsigned char agToUpper(unsigned char);

class CharSetExpression;
  class CharRange;
  class CodeRange;
  class IndividualChar;
  class IndividualCode;
  class NamedCharSet;
  class CharSetUnion;
  class CharSetIntersection;
  class CharSetDifference;
  class CharSetComplement;

class CharSetExpression {
protected:
  enum Precedence {
    additive=1,
    multiplicative,
    unary,
    atomic
  };

  Precedence precedence;

  class TranslateTable {
  private:
    int *table;
  public:
    TranslateTable() : table(new int[256]) {
      for (int i = 0; i < 256; i++) {
	table[i] = i;
      }
    }
    ~TranslateTable() {
      delete [] table;
    }
    int &operator [] (unsigned const x) {
      return table[x];
    }
  };

  static TranslateTable translateTable;

public:
  enum Type {
    notDefined = 1,
    charRange,
    codeRange,
    individualChar,
    individualCode,
    namedCharSet,
    charSetUnion,
    charSetIntersection,
    charSetDifference,
    charSetComplement,
  };

  Type type;

  void *operator new (size_t n) { return malloc(n); }
  void operator delete(void *p) { free(p); }

  static void checkMinimum(int f);

  CharSetExpression(Precedence p, Type t) : precedence(p), type(t) {}
  CharSetExpression(Type t) : precedence(atomic), type(t) {}
  CharSetExpression() : precedence(atomic), type(notDefined) {}
  virtual ~CharSetExpression() {}

  virtual int operator == (const CharSetExpression &) const {
    //LOGSECTION("CharSetExpression::operator==(CharSetExpression &)");
    return 0;
  }

  virtual int operator != (const CharSetExpression &x) const {
    return !(*this == x);
  }

  //virtual int operator > (const CharSetExpression &x) const = 0;

  virtual int operator < (const CharSetExpression &x) const {
    return type < x.type;
  }

  virtual AgString asString() const = 0;
  AgString asString(Precedence p) const;

  virtual int recursive(int) { return 0; }
  virtual int nameNode() { return 0; }
  virtual int char_node() { return 0; }

  virtual CharBitmap bitmap() const = 0;
  virtual void checkRange() {}
  virtual int sameType(const CharSetExpression &x) const {
    return type == x.type;
  }
};

class CharRange : public CharSetExpression {
private:
  int first;
  int last;
  CharRange(const CharRange &) : CharSetExpression() {}
public:
  CharRange(int f, int l)
    : CharSetExpression(charRange), first(min(f, l)), last(max(f, l))
  {
    //LOGSECTION("CharRange");
    //LOGV(first) LCV(last);
    checkRange();
  }
  ~CharRange() {}

  //virtual int operator == (const CharRange &x) const;
  virtual int operator == (const CharSetExpression &x) const;
  virtual int operator < (const CharSetExpression &x) const;
  virtual AgString asString() const;
  virtual CharBitmap bitmap() const;
  virtual void checkRange();
};

class CodeRange : public CharSetExpression {
private:
  int first;
  int last;
  CodeRange(const CharRange &) : CharSetExpression() {}
public:
  CodeRange(int f, int l)
    : CharSetExpression(codeRange), first(min(f, l)), last(max(f, l))
  {
    //LOGSECTION("CodeRange");
    //LOGV(first) LCV(last);
    checkRange();
    checkMinimum(f);
  }
  ~CodeRange() {}

  //virtual int operator == (const CodeRange &x) const;
  virtual int operator == (const CharSetExpression &x) const;
  virtual int operator < (const CharSetExpression &x) const;
  virtual AgString asString() const;
  virtual CharBitmap bitmap() const;
  virtual void checkRange();
};

class IndividualChar : public CharSetExpression {
private:
  IndividualChar(const IndividualChar &) : CharSetExpression() {}
public:
  int asciiValue;

  IndividualChar(int c) : CharSetExpression(individualChar), asciiValue(c) {
    //LOGSECTION("IndividualChar");
    //LOGV(asciiValue);
    checkRange();
  }
  ~IndividualChar() {}

  //virtual int operator == (const IndividualChar &x) const;
  virtual int operator == (const CharSetExpression &x) const;
  virtual int operator < (const CharSetExpression &x) const;
  virtual AgString asString() const;
  virtual CharBitmap bitmap() const;
  virtual void checkRange();
};

class IndividualCode : public CharSetExpression {
private:
  IndividualCode(const IndividualCode &) : CharSetExpression() {}
public:
  int value;

  IndividualCode(int c) : CharSetExpression(individualCode), value(c) {
    //LOGSECTION("IndividualCode");
    //LOGV(value);
    checkRange();
    checkMinimum(value);
  }
  ~IndividualCode() {}

  virtual int operator == (const CharSetExpression &x) const;
  virtual int operator < (const CharSetExpression &x) const;
  virtual AgString asString() const;
  virtual int char_node() { return 1; }
  virtual CharBitmap bitmap() const;
  virtual void checkRange();
};

class NamedCharSet : public CharSetExpression {
private:
  NamedCharSet(const NamedCharSet &) : CharSetExpression() {}
public:
  int name;

  NamedCharSet(int n) : CharSetExpression(namedCharSet), name(n) {
    //LOGSECTION("NamedCharSet");
    //LOGV(dict_str(tkn_dict, name));
  }
  ~NamedCharSet() {}

  virtual int operator == (const CharSetExpression &x) const;
  virtual int operator < (const CharSetExpression &x) const;
  virtual AgString asString() const;
  virtual int recursive(int n) { return n == name; }
  virtual int nameNode() { return 1; }
  virtual CharBitmap bitmap() const;
};

class CharSetUnion : public CharSetExpression {
private:
  CharSetExpression *left, *right;
  CharSetUnion(const CharSetUnion &) : CharSetExpression() {}
public:
  CharSetUnion(CharSetExpression *l, CharSetExpression *r)
    : CharSetExpression(additive, charSetUnion), left(l), right(r)
  {
    //LOGSECTION("CharSetUnion");
  }
  ~CharSetUnion() { delete left; delete right; }

  virtual int operator == (const CharSetExpression &x) const;
  virtual int operator < (const CharSetExpression &x) const;
  virtual AgString asString() const;
  virtual int recursive(int n) {
    return left->recursive(n) || right->recursive(n);
  }
  virtual CharBitmap bitmap() const;
  virtual void checkRange();
};

class CharSetIntersection : public CharSetExpression {
private:
  CharSetExpression *left, *right;
  CharSetIntersection(const CharSetIntersection &) : CharSetExpression() {}
public:
  CharSetIntersection(CharSetExpression *l, CharSetExpression *r)
    : CharSetExpression(multiplicative, charSetIntersection), left(l), right(r)
  {
    //LOGSECTION("CharSetIntersection");
  }
  ~CharSetIntersection() {delete left; delete right;}

  virtual int operator == (const CharSetExpression &x) const;
  virtual int operator < (const CharSetExpression &x) const;
  virtual AgString asString() const;
  virtual CharBitmap bitmap() const;
  virtual int recursive(int n) {
    return left->recursive(n) || right->recursive(n);
  }
  virtual void checkRange();
};

class CharSetDifference : public CharSetExpression {
private:
  CharSetExpression *left, *right;
  CharSetDifference(const CharSetDifference &) : CharSetExpression() {}
public:
  CharSetDifference(CharSetExpression *l, CharSetExpression *r)
    : CharSetExpression(additive, charSetDifference), left(l), right(r)
  {
    //LOGSECTION("CharSetDifference");
  }
  ~CharSetDifference() {delete left; delete right;}

  virtual int operator == (const CharSetExpression &x) const;
  virtual int operator < (const CharSetExpression &x) const;
  virtual AgString asString() const;
  virtual CharBitmap bitmap() const;
  virtual int recursive(int n) {
    return left->recursive(n) || right->recursive(n);
  }
  virtual void checkRange();
};

class CharSetComplement : public CharSetExpression {
private:
  CharSetExpression *operand;
  CharSetComplement(const CharSetComplement &) : CharSetExpression() {}
public:
  CharSetComplement(CharSetExpression *o)
    : CharSetExpression(unary, charSetComplement), operand(o)
  {
    //LOGSECTION("CharSetComplement");
  }
  ~CharSetComplement() {delete operand;}

  virtual int operator == (const CharSetExpression &x) const;
  virtual int operator < (const CharSetExpression &x) const;
  virtual AgString asString() const;
  virtual CharBitmap bitmap() const;
  virtual int recursive(int n) { return operand->recursive(n); }
  virtual void checkRange();
};


#endif /* CSEXP_H */