Ejemplo n.º 1
0
 /**
  * Constructor.
  * <p>
  * Creates a new instance for a certain literal type.
  *
  * @param pTree  The tree node representing the literal.
  * @param pLiteralType  One of the 'LITERAL_???' constants defined by the
  *                      interface 'Literal'. Note that it's up to the caller
  *                      to ensure that the literal type matches the token
  *                      type.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2Literal(AST2JSOMTree pTree, LiteralType pLiteralType,
     TokenRewriteStream pTokenRewriteStream)
     : base(pTree, ExpressionType.LITERAL, pTokenRewriteStream)
 {
     mLiteralType = pLiteralType;
 }
Ejemplo n.º 2
0
        /**
         * Changes <code>this</code> to become a <code>Literal</code> object
         * representing a literal stated as string.
         * <p>
         * Literals like numbers, bool values, the <code>null</code> literal,
         * characters and so on can be stated as expected, i.e. like
         * <ul>
         *  <li>
         *      <i>"123"</i> (an integer)
         *  </li>
         *  <li>
         *      <i>"true"</i> (a bool literal)
         *  </li>
         *  <li>
         *      <i>"null"</i> (the <code>null</code> literal)
         *  </li>
         *  <li>
         *      <i>"'a'"</i> (the character literal <code>a</code>)
         *  </li>
         *  <li>
         *      <i>"'\''"</i> (the character literal <code>'</code>)
         *  </li>
         * </ul>
         * For string literals it must be considered that they must be stated
         * including the quotation marks that belong to the string literal. Examples
         * are
         * <ul>
         *  <li>
         *      <i>"\"\""</i> (the empty string)
         *  </li>
         *  <li>
         *      <i>"\"anyString\""</i> (a simple non-empty string)
         *  </li>
         *  <li>
         *      <i>"\"with \\\" character\""</i> (a string containing one escaped
         *      <code>'"'</code> character)
         *  </li>
         * </ul>
         *
         * @param pLiteral  The new literal stated as string.
         *
         * @return  The old literal.
         *
         * @  if the given literal isn't a valid
         *                                     literal; note that the passed literal
         *                                     string must be parsable by the
         *                                     unmarshaller.
         *
         * @see #setLiteralChecked(String)
         */
        public String setLiteral(String pLiteral)
        {
            if (pLiteral == null)
            {
                throw new JSourceObjectizerException(
                        CommonErrorMessages.getArgumentIsNullMessage("pLiteral"));
            }
            // Remember the old literal string.
            AST2JSOMTree thisRootNode = (AST2JSOMTree)getTreeNode();
            String oldLiteral = thisRootNode.Text;
            // Create a temporary literal object for the new literal.
            List<String> messages = new List<String>();
            AST2Literal tempLiteral = null;
            try
            {
                tempLiteral =
                    (AST2Literal)getUnmarshaller().unmarshalAST2Expression(
                        pLiteral, messages);
            }
            catch (Exception pException)
            {
                // Catch any kind of exception.
                throw new JSourceObjectizerException(
                        CommonErrorMessages.getInvalidArgumentValueMessage(
                                "pLiteral", pLiteral),
                                pException);
            }
            if (tempLiteral == null || messages.Count > 0)
            {
                throw new JSourceObjectizerException(
                        CommonErrorMessages.getInvalidArgumentValueMessage(
                                "pLiteral", pLiteral));
            }
            // Get the data from the temporary literal.
            AST2JSOMTree tempRootNode = (AST2JSOMTree)tempLiteral.getTreeNode();
            mLiteralType = tempLiteral.mLiteralType;
            IToken token = thisRootNode.Token;
            token.Type = tempRootNode.Type;
            token.Text = tempRootNode.Text;

            return oldLiteral;
        }