Symbol.java

/*
** Module   : Symbol.java
** Abstract : Represents a Progress preprocessor variable.
**
** Copyright (c) 2004-2020, Golden Code Development Corporation.
**
** -#- -I- --Date-- --JPRM-- ----------------------------Description-----------------------------
** 001 NVS 20041116   @18755 Created. This class represents Progress pre-
**                           processor symbols put into the multiscoped
**                           dictionary.
** 002 NVS 20050512   @21158 Added getOriginText() method.
** 003 GES 20070321   @32522 Switched methods to use wrappers.
** 004 GES 20070717   @34577 Moved to StringBuilder.
** 005 GES 20100226   @44692 JavaDoc update.           
** 006 EVL 20160223          Javadoc fixes to make compatible with Oracle Java 8 for Solaris 10.
** 007 GES 20190929          Removed defined() methods, they are no longer needed.
** 008 GES 20200626          Improved toString(). 
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.preproc;

import java.util.*;
import java.io.*;

import com.goldencode.p2j.util.*;

/**
 * Represents a Progress preprocessor variable put into one of the scopes
 * of the scoped symbol dictionary. The dictionary contains variable names
 * and the associated value objects. Value objects for the preprocessor
 * variables are Symbol objects.
 * <p>
 * Symbol objects are made of two pieces: origin of the variable definition
 * and its value as a string.
 * <p>
 * The origin field tells the source of the variable definition. Builtin
 * preprocessor variables are also presented. That allows for an unified
 * implementation of redefinitions of the builtins.
 * <p>
 * This class provides all methods needed for backing up the Progress
 * references like <code>{&amp;name}</code>, include file references like
 * <code>{file &amp;name=value}</code> and <code>DEFINED(name)</code> preprocessor
 * built-in function.
 * <p>
 * @author NVS
 */
public class Symbol
{
   /** origin definition constant for undefined symbols */
   public static final int UNDEFINED = 0;

   /** origin definition constant for symbols defined with &amp;GLOBAL-DEFINE */
   public static final int GLOBAL    = 1;

   /** origin definition constant for symbols defined with includes */
   public static final int ARGUMENT  = 2;

   /** origin definition constant for symbols defined with &amp;SCOPED-DEFINE */
   public static final int SCOPED    = 3;

   /** origin definition constant for built-in preprocessor symbols */
   public static final int BUILTIN   = 4;

   /** minimum origin definition constant */
   private static final int MINIMUM = 0;

   /** mamximum origin definition constant */
   private static final int MAXIMUM = 4;

   /** textual origin definition constants */
   private static final String[] orig = 
   { 
      "undefined",
      "global",
      "argument",
      "scoped",
      "builtin"
   };

   /** keeps the origin of this symbol */
   private final int origin;

   /** keeps the value string of this symbol */
   private final String value;

   /**
    * Constructor taking a <code>String</code>.
    *
    * @param    origin
    *           symbol's origin expressed as a definition constant.
    * @param    value
    *           a string representing symbol's value as a
    *           <code>String</code>
    */
   public Symbol(int origin, String value)
   {
      this.origin = origin;
      this.value = value;
   }

   /**
    * Constructor taking a <code>StringBuilder</code>.
    *
    * @param    origin
    *           symbol's origin expressed as a definition constant.
    * @param    value
    *           a string representing symbol's value
    */
   public Symbol(int origin, StringBuilder value)
   {
      this.origin = origin;
      this.value = value.toString();
   }

   /**
    * Gets the symbol origin of global preprocessor variables for
    * <code>DEFINED()</code>.
    *
    * @return   Always {@link #GLOBAL}.
    */
   public static integer globalVariable()
   {
      return new integer(GLOBAL);
   }

   /**
    * Gets the symbol origin of scoped preprocessor variables for
    * <code>DEFINED()</code>.
    *
    * @return   Always {@link #SCOPED}.
    */
   public static integer scopedVariable()
   {
      return new integer(SCOPED);
   }

   /**
    * Gets the symbol origin of include file arguments for
    * <code>DEFINED()</code>.
    *
    * @return   Always {@link #ARGUMENT}.
    */
   public static integer includeFileArgument()
   {
      return new integer(ARGUMENT);
   }

   /**
    * Fails the attempt to get the symbol origin of built-in variables
    * for <code>DEFINED()</code>.  In the Progress 4GL preprocessor, using
    * <code>DEFINED()</code> on a built-in variable causes a fatal error
    * so this implementation duplicates the same result (although it is
    * not displayed as a Progress style error message).
    *
    * @return   The idea is to block possible use of <code>DEFINED()</code>
    *           against a built-in preprocessor variable.
    */
   public static integer builtinVariable()
   {
      throw new IllegalArgumentException();
   }

   /**
    * Gets the symbol origin.
    *
    * @return   The symbol's origin:
    *           <ul>
    *              <li><code>Symbol.GLOBAL</code> for symbols defined with
    *                  <code>&amp;GLOBAL-DEFINE</code>;
    *              <li><code>Symbol.ARGUMENT</code> for symbols defined with
    *                  include file references;
    *              <li><code>Symbol.SCOPED</code> for symbols defined with
    *                  <code>&amp;SCOPED-DEFINE</code>;
    *              <li><code>Symbol.BUILTIN</code> for built-in preprocessor
    *                  symbols.
    *           </ul>
    */
   public int getOrigin()
   {
      return origin;
   }

   /**
    * Gets the symbol origin as a string.
    *
    * @return   The text representation of the symbol's origin:
    *           <ul>
    *              <li><code>"global"</code> for symbols defined with
    *                  <code>&amp;GLOBAL-DEFINE</code>;
    *              <li><code>"argument"</code> for symbols defined with
    *                  include file references;
    *              <li><code>"scoped"</code> for symbols defined with
    *                  <code>&amp;SCOPED-DEFINE</code>;
    *              <li><code>"builtin"</code> for built-in preprocessor
    *                  symbols.
    *           </ul>
    */
   public String getOriginText()
   {
      int o = origin;
      
      if (o < MINIMUM || o > MAXIMUM)
      {
         o = UNDEFINED;
      }

      return orig[o]; 
   }

   /**
    * Gets the symbol value
    *
    * @return   The symbol's value.
    */
   public String getValue()
   {
      return value;
   }

   /**
    * Converts the symbol into a string.
    *
    * @return   The text representation of the symbol's value.
    */
   public String toString()
   {
      return "(" + getOriginText() + ",\"" + value + "\")";
   }
}