All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.extendj.ast.Literal Maven / Gradle / Ivy

The newest version!
/* This file was generated with JastAdd2 (http://jastadd.org) version 2.3.0 */
package org.extendj.ast;
import java.util.ArrayList;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.util.Set;
import beaver.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.LinkedHashSet;
import java.util.*;
import org.jastadd.util.PrettyPrintable;
import org.jastadd.util.PrettyPrinter;
import java.util.zip.*;
import java.io.*;
import org.jastadd.util.*;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
/**
 * The abstract base class for all literals.
 * @ast node
 * @declaredat /home/jesper/git/extendj/java4/grammar/Literals.ast:4
 * @astdecl Literal : PrimaryExpr ::= ;
 * @production Literal : {@link PrimaryExpr} ::= <LITERAL:String>;

 */
public abstract class Literal extends PrimaryExpr implements Cloneable {
  /**
   * @aspect Java4PrettyPrint
   * @declaredat /home/jesper/git/extendj/java4/frontend/PrettyPrint.jadd:442
   */
  public void prettyPrint(PrettyPrinter out) {
    out.print(getLITERAL());
  }
  /**
   * @aspect BytecodeCONSTANT
   * @declaredat /home/jesper/git/extendj/java4/frontend/BytecodeCONSTANT.jrag:80
   */
  public static Literal buildDoubleLiteral(double value) {
    return new DoubleLiteral(Double.toString(value));
  }
  /**
   * @aspect BytecodeCONSTANT
   * @declaredat /home/jesper/git/extendj/java4/frontend/BytecodeCONSTANT.jrag:84
   */
  public static Literal buildFloatLiteral(float value) {
    return new FloatingPointLiteral(Double.toString(value));
  }
  /**
   * @aspect BytecodeCONSTANT
   * @declaredat /home/jesper/git/extendj/java4/frontend/BytecodeCONSTANT.jrag:88
   */
  public static Literal buildIntegerLiteral(int value) {
    return new IntegerLiteral("0x"+Integer.toHexString(value));
  }
  /**
   * @aspect BytecodeCONSTANT
   * @declaredat /home/jesper/git/extendj/java4/frontend/BytecodeCONSTANT.jrag:92
   */
  public static Literal buildLongLiteral(long value) {
    return new LongLiteral("0x"+Long.toHexString(value));
  }
  /**
   * @aspect BytecodeCONSTANT
   * @declaredat /home/jesper/git/extendj/java4/frontend/BytecodeCONSTANT.jrag:96
   */
  public static Literal buildBooleanLiteral(boolean value) {
    return new BooleanLiteral(value ? "true" : "false");
  }
  /**
   * @aspect BytecodeCONSTANT
   * @declaredat /home/jesper/git/extendj/java4/frontend/BytecodeCONSTANT.jrag:100
   */
  public static Literal buildStringLiteral(String value) {
    return new StringLiteral(value);
  }
  /**
   * @aspect Literals
   * @declaredat /home/jesper/git/extendj/java4/frontend/Literals.jrag:73
   */
  static long parseLong(String s) {
    long x = 0L;
    s = s.toLowerCase();
    if (s.endsWith("l")) {
      s = s.substring(0, s.length() - 1);
    }
    boolean neg = false;
    if (s.startsWith("-")) {
      s = s.substring(1);
      neg = true;
    }
    if (s.startsWith("0x")) {
      s = s.substring(2);
      if (s.length() > 16) {
        for (int i = 0; i < s.length() - 16; i++) {
          if (s.charAt(i) != '0') {
            throw new NumberFormatException("");
          }
        }
      }
      for (int i = 0; i < s.length(); i++) {
        int c = s.charAt(i);
        if (c >= 'a' && c <= 'f') {
          c = c - 'a' + 10;
        } else if (c >= '0' && c <= '9') {
          c = c - '0';
        } else {
          throw new NumberFormatException("");
        }
        x = x * 16 + c;
      }
    } else if (s.startsWith("0")) {
      s = s.substring(1);
      // Octal literals larger than 01777777777777777777777L are not valid.
      if (s.length() > 21) {
        for (int i = 0; i < s.length() - 21; i++) {
          if (i == s.length() - 21 - 1) {
            if (s.charAt(i) != '0' && s.charAt(i) != '1') {
              throw new NumberFormatException("");
            }
          } else {
            if (s.charAt(i) != '0') {
              throw new NumberFormatException("");
            }
          }
        }
      }
      for (int i = 0; i < s.length(); i++) {
        int c = s.charAt(i);
        if (c >= '0' && c <= '7') {
          c = c - '0';
        } else {
          throw new NumberFormatException("");
        }
        x = x * 8 + c;
      }
    } else {
      long oldx = 0;
      for (int i = 0; i < s.length(); i++) {
        int c = s.charAt(i);
        if (c >= '0' && c <= '9') {
          c = c - '0';
        } else {
          throw new NumberFormatException("");
        }
        x = x * 10 + c;
        if (x < oldx) {
          boolean negMinValue = i == (s.length()-1) && neg && x == Long.MIN_VALUE;
          if (!negMinValue) {
            throw new NumberFormatException("");
          }
        }
        oldx = x;
      }
      if (x == Long.MIN_VALUE) {
        return x;
      }
      if (x < 0) {
        throw new NumberFormatException("");
      }
    }
    return neg ? -x : x;
  }
  /**
   * @aspect PrettyPrintUtil
   * @declaredat /home/jesper/git/extendj/java4/frontend/PrettyPrintUtil.jrag:117
   */
  @Override public String toString() {
    return getLITERAL();
  }
  /**
   * Escapes a string literal.
   * @param s string to escape
   * @return escaped string literal
   * @aspect PrettyPrintUtil
   * @declaredat /home/jesper/git/extendj/java4/frontend/PrettyPrintUtil.jrag:406
   */
  protected static String escape(String s) {
    StringBuilder result = new StringBuilder();
    for (int i=0; i < s.length(); i++) {
      switch(s.charAt(i)) {
        case '\b':
          result.append("\\b");
          break;
        case '\t':
          result.append("\\t");
          break;
        case '\n':
          result.append("\\n");
          break;
        case '\f':
          result.append("\\f");
          break;
        case '\r':
          result.append("\\r");
          break;
        case '\"':
          result.append("\\\"");
          break;
        case '\'':
          result.append("\\\'");
          break;
        case '\\':
          result.append("\\\\");
          break;
        default:
          int value = (int) s.charAt(i);
          if (value < 0x20 || (value > 0x7e)) {
            result.append(asEscape(value));
          } else {
            result.append(s.charAt(i));
          }
      }
    }
    return result.toString();
  }
  /**
   * @aspect PrettyPrintUtil
   * @declaredat /home/jesper/git/extendj/java4/frontend/PrettyPrintUtil.jrag:446
   */
  protected static String asEscape(int value) {
    StringBuilder s = new StringBuilder("\\u");
    String hex = Integer.toHexString(value);
    for (int i = 0; i < 4-hex.length(); i++) {
      s.append("0");
    }
    s.append(hex);
    return s.toString();
  }
  /**
   * @aspect CreateBCode
   * @declaredat /home/jesper/git/extendj/java4/backend/CreateBCode.jrag:285
   */
  public void createBCode(CodeGeneration gen) {
    emitPushConstant(gen);
  }
  /**
   * @aspect CodeGeneration
   * @declaredat /home/jesper/git/extendj/java4/backend/CodeGeneration.jrag:117
   */
  public void emitPushConstant(CodeGeneration gen) {
    System.err.println("ERROR: Tried to generate bytecode for: " + getClass().getName());
  }
  /**
   * @declaredat ASTNode:1
   */
  public Literal() {
    super();
  }
  /**
   * Initializes the child array to the correct size.
   * Initializes List and Opt nta children.
   * @apilevel internal
   * @ast method
   * @declaredat ASTNode:10
   */
  public void init$Children() {
  }
  /**
   * @declaredat ASTNode:12
   */
  @ASTNodeAnnotation.Constructor(
    name = {"LITERAL"},
    type = {"String"},
    kind = {"Token"}
  )
  public Literal(String p0) {
    setLITERAL(p0);
  }
  /**
   * @declaredat ASTNode:20
   */
  public Literal(beaver.Symbol p0) {
    setLITERAL(p0);
  }
  /** @apilevel low-level 
   * @declaredat ASTNode:24
   */
  protected int numChildren() {
    return 0;
  }
  /**
   * @apilevel internal
   * @declaredat ASTNode:30
   */
  public boolean mayHaveRewrite() {
    return false;
  }
  /** @apilevel internal 
   * @declaredat ASTNode:34
   */
  public void flushAttrCache() {
    super.flushAttrCache();
    constant_reset();
  }
  /** @apilevel internal 
   * @declaredat ASTNode:39
   */
  public void flushCollectionCache() {
    super.flushCollectionCache();
  }
  /** @apilevel internal 
   * @declaredat ASTNode:43
   */
  public Literal clone() throws CloneNotSupportedException {
    Literal node = (Literal) super.clone();
    return node;
  }
  /**
   * Create a deep copy of the AST subtree at this node.
   * The copy is dangling, i.e. has no parent.
   * @return dangling copy of the subtree at this node
   * @apilevel low-level
   * @deprecated Please use treeCopy or treeCopyNoTransform instead
   * @declaredat ASTNode:54
   */
  @Deprecated
  public abstract Literal fullCopy();
  /**
   * Create a deep copy of the AST subtree at this node.
   * The copy is dangling, i.e. has no parent.
   * @return dangling copy of the subtree at this node
   * @apilevel low-level
   * @declaredat ASTNode:62
   */
  public abstract Literal treeCopyNoTransform();
  /**
   * Create a deep copy of the AST subtree at this node.
   * The subtree of this node is traversed to trigger rewrites before copy.
   * The copy is dangling, i.e. has no parent.
   * @return dangling copy of the subtree at this node
   * @apilevel low-level
   * @declaredat ASTNode:70
   */
  public abstract Literal treeCopy();
  /**
   * Replaces the lexeme LITERAL.
   * @param value The new value for the lexeme LITERAL.
   * @apilevel high-level
   */
  public void setLITERAL(String value) {
    tokenString_LITERAL = value;
  }
  /** @apilevel internal 
   */
  protected String tokenString_LITERAL;
  /**
   */
  public int LITERALstart;
  /**
   */
  public int LITERALend;
  /**
   * JastAdd-internal setter for lexeme LITERAL using the Beaver parser.
   * @param symbol Symbol containing the new value for the lexeme LITERAL
   * @apilevel internal
   */
  public void setLITERAL(beaver.Symbol symbol) {
    if (symbol.value != null && !(symbol.value instanceof String))
    throw new UnsupportedOperationException("setLITERAL is only valid for String lexemes");
    tokenString_LITERAL = (String)symbol.value;
    LITERALstart = symbol.getStart();
    LITERALend = symbol.getEnd();
  }
  /**
   * Retrieves the value for the lexeme LITERAL.
   * @return The value for the lexeme LITERAL.
   * @apilevel high-level
   */
  @ASTNodeAnnotation.Token(name="LITERAL")
  public String getLITERAL() {
    return tokenString_LITERAL != null ? tokenString_LITERAL : "";
  }
  /** @apilevel internal */
  private void constant_reset() {
    constant_computed = null;
    constant_value = null;
  }
  /** @apilevel internal */
  protected ASTState.Cycle constant_computed = null;

  /** @apilevel internal */
  protected Constant constant_value;

  /**
   * @attribute syn
   * @aspect ConstantExpression
   * @declaredat /home/jesper/git/extendj/java4/frontend/ConstantExpression.jrag:38
   */
  @ASTNodeAnnotation.Attribute(kind=ASTNodeAnnotation.Kind.SYN)
  @ASTNodeAnnotation.Source(aspect="ConstantExpression", declaredAt="/home/jesper/git/extendj/java4/frontend/ConstantExpression.jrag:38")
  public Constant constant() {
    ASTState state = state();
    if (constant_computed == ASTState.NON_CYCLE || constant_computed == state().cycle()) {
      return constant_value;
    }
    constant_value = constant_compute();
    if (state().inCircle()) {
      constant_computed = state().cycle();
    
    } else {
      constant_computed = ASTState.NON_CYCLE;
    
    }
    return constant_value;
  }
  /** @apilevel internal */
  private Constant constant_compute() {
      throw new UnsupportedOperationException("ConstantExpression operation constant"
          + " not supported for type " + getClass().getName());
    }
  /**
   * @attribute syn
   * @aspect ConstantExpression
   * @declaredat /home/jesper/git/extendj/java4/frontend/ConstantExpression.jrag:383
   */
  @ASTNodeAnnotation.Attribute(kind=ASTNodeAnnotation.Kind.SYN)
  @ASTNodeAnnotation.Source(aspect="ConstantExpression", declaredAt="/home/jesper/git/extendj/java4/frontend/ConstantExpression.jrag:383")
  public boolean isConstant() {
    boolean isConstant_value = true;
    return isConstant_value;
  }
  /**
   * @attribute syn
   * @aspect ConstantExpression
   * @declaredat /home/jesper/git/extendj/java4/frontend/ConstantExpression.jrag:435
   */
  @ASTNodeAnnotation.Attribute(kind=ASTNodeAnnotation.Kind.SYN)
  @ASTNodeAnnotation.Source(aspect="ConstantExpression", declaredAt="/home/jesper/git/extendj/java4/frontend/ConstantExpression.jrag:435")
  public boolean isTrue() {
    boolean isTrue_value = false;
    return isTrue_value;
  }
  /**
   * @attribute syn
   * @aspect ConstantExpression
   * @declaredat /home/jesper/git/extendj/java4/frontend/ConstantExpression.jrag:438
   */
  @ASTNodeAnnotation.Attribute(kind=ASTNodeAnnotation.Kind.SYN)
  @ASTNodeAnnotation.Source(aspect="ConstantExpression", declaredAt="/home/jesper/git/extendj/java4/frontend/ConstantExpression.jrag:438")
  public boolean isFalse() {
    boolean isFalse_value = false;
    return isFalse_value;
  }
  /** @apilevel internal */
  public ASTNode rewriteTo() {
    return super.rewriteTo();
  }
  /** @apilevel internal */
  public boolean canRewrite() {
    return false;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy