using System.Diagnostics.Contracts;

namespace Microsoft.Dafny;

public class AutoGeneratedToken : TokenWrapper {
  public AutoGeneratedToken(IToken wrappedToken)
    : base(wrappedToken) {
    Contract.Requires(wrappedToken != null);
  }

  public static IToken Unwrap(IToken token) {
    if (token is AutoGeneratedToken autoGeneratedToken) {
      return Unwrap(autoGeneratedToken.WrappedToken);
    }

    return token;
  }

  public static bool Is(IToken tok) {
    while (tok is TokenWrapper w) {
      if (w is AutoGeneratedToken) {
        return true;
      }
      tok = w.WrappedToken;
    }
    return false;
  }

  public static Expression WrapExpression(Expression expr) {
    return Expression.CreateParensExpression(new AutoGeneratedToken(expr.tok), expr);
  }

  public override IToken WithVal(string newVal) {
    return new AutoGeneratedToken(WrappedToken.WithVal(newVal));
  }
}