Package com.creativewidgetworks.goldparser.parser

Examples of com.creativewidgetworks.goldparser.parser.Variable


        String str = parser.getCurrentReduction() == null ? "\"\"" : parser.getCurrentReduction().get(0).asString();
       
        StringBuilder sb = new StringBuilder(str);
        sb.deleteCharAt(sb.length() - 1);
        sb.deleteCharAt(0);
        setValue(new Variable(sb.toString()));
    }
View Full Code Here


public class NumberLiteral extends Reduction {

    public NumberLiteral(GOLDParser parser) throws ParserException  {
        String literal = parser.getCurrentReduction() == null ? "<null>" : parser.getCurrentReduction().get(0).asString();
        try {
            setValue(new Variable(new BigDecimal(literal)));
        } catch (NumberFormatException e) {
            parser.raiseParserException(Simple3.formatMessage("error.token_nan", literal));
        }
    }
View Full Code Here

        }       
    }

    @Override
    public Variable getValue() throws ParserException {
        Variable result = null;
        Variable lValue = leftExpression.getValue();
        Variable rValue = rightExpression.getValue();

        boolean b = false;
        if (theOperator.equals("==")) {
           if (bothValuesAreNumbers()) {
               b = lValue.asNumber().compareTo(rValue.asNumber()) == 0;
               result = new Variable(Boolean.valueOf(b));
           } else if (bothValuesAreBooleans()) {
               b = lValue.asBool() == rValue.asBool();
               result = new Variable(Boolean.valueOf(b));
           } else if (bothValuesAreTimestamps()) {
               b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) == 0;
               result = new Variable(Boolean.valueOf(b));
           } else if (oneOrBothValuesAreStrings()) {
               b = lValue.toString().compareTo(rValue.toString()) == 0;
               result = new Variable(Boolean.valueOf(b));
           } else {
               theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
           }
        } else if (theOperator.equals("<>")) {
            if (bothValuesAreNumbers()) {
                b = lValue.asNumber().compareTo(rValue.asNumber()) != 0;
                result = new Variable(Boolean.valueOf(b));
            } else if (bothValuesAreBooleans()) {
                b = lValue.asBool() != rValue.asBool();
                result = new Variable(Boolean.valueOf(b));
            } else if (bothValuesAreTimestamps()) {
                b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) != 0;
                result = new Variable(Boolean.valueOf(b));
            } else if (oneOrBothValuesAreStrings()) {
                b = lValue.toString().compareTo(rValue.toString()) != 0;
                result = new Variable(Boolean.valueOf(b));
            } else {
                theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
            }
        } else if (theOperator.equals("<")) {
            if (bothValuesAreNumbers()) {
                b = lValue.asNumber().compareTo(rValue.asNumber()) < 0;
                result = new Variable(Boolean.valueOf(b));
            } else if (bothValuesAreTimestamps()) {
                b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) < 0;
                result = new Variable(Boolean.valueOf(b));
            } else if (oneOrBothValuesAreStrings()) {
                b = lValue.toString().compareTo(rValue.toString()) < 0;
                result = new Variable(Boolean.valueOf(b));
            } else {
                theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
            }
        } else if (theOperator.equals("<=")) {
            if (bothValuesAreNumbers()) {
                b = lValue.asNumber().compareTo(rValue.asNumber()) <= 0;
                result = new Variable(Boolean.valueOf(b));
            } else if (bothValuesAreTimestamps()) {
                b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) <= 0;
                result = new Variable(Boolean.valueOf(b));
            } else if (oneOrBothValuesAreStrings()) {
                b = lValue.toString().compareTo(rValue.toString()) <= 0;
                result = new Variable(Boolean.valueOf(b));
            } else {
                theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
            }
        } else if (theOperator.equals(">")) {
            if (bothValuesAreNumbers()) {
                b = lValue.asNumber().compareTo(rValue.asNumber()) > 0;
                result = new Variable(Boolean.valueOf(b));
            } else if (bothValuesAreTimestamps()) {
                b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) > 0;
                result = new Variable(Boolean.valueOf(b));
            } else if (oneOrBothValuesAreStrings()) {
                b = lValue.toString().compareTo(rValue.toString()) > 0;
                result = new Variable(Boolean.valueOf(b));
            } else {
                theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
            }
        } else if (theOperator.equals(">=")) {
            if (bothValuesAreNumbers()) {
                b = lValue.asNumber().compareTo(rValue.asNumber()) >= 0;
                result = new Variable(Boolean.valueOf(b));
            } else if (bothValuesAreTimestamps()) {
                b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) >= 0;
                result = new Variable(Boolean.valueOf(b));
            } else if (oneOrBothValuesAreStrings()) {
                b = lValue.toString().compareTo(rValue.toString()) >= 0;
                result = new Variable(Boolean.valueOf(b));
            } else {
                theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
            }
        } else if (theOperator.equals("+")) {
            if (bothValuesAreNumbers()) {
                result = new Variable(lValue.asNumber().add(rValue.asNumber()));
            } else {
                // I prefer to overload + depending upon type
                result = new Variable(lValue.toString() + rValue.toString());
            }
        } else if (theOperator.equals("-")) {
            if (bothValuesAreNumbers()) {
                result = new Variable(lValue.asNumber().subtract(rValue.asNumber()));
            } else {
                theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
            }
        } else if (theOperator.equals("&")) {
            // String concatenation
            if (oneOrBothValuesAreStrings()) {
                result = new Variable(lValue.toString() + rValue.toString());
            } else {
                theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
            }
        } else if (theOperator.equals("*")) {
            if (bothValuesAreNumbers()) {
                result = new Variable(lValue.asNumber().multiply(rValue.asNumber()));
            } else {
                theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
            }
        } else if (theOperator.equals("/")) {
            if (bothValuesAreNumbers()) {
                try {
                    // This path will strip off extraneous decimal places, but will fail
                    // with numbers that repeat (e.g.,  1/3 = .33333333333)
                    result = new Variable(lValue.asNumber().divide(rValue.asNumber()));
                } catch (Exception e) {
                    // This path will handle the repeating numbers
                    result = new Variable(lValue.asNumber().divide(rValue.asNumber(),PRECISION, ROUNDING_MODE));
                }
            } else {
                theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
            }
        }
View Full Code Here

        // Clean up the parameter call stack
        parser.clearProgramVariable(Parameters.KEY_PARAMETERS);

        // Make the function available to be called
        parser.setProgramVariable(FUNCTION_PREFIX + functionName, new Variable(this));
    }
View Full Code Here

public class Arguments extends Reduction {

    public Arguments(Reduction reduction) {
        List<Reduction> arguments = new ArrayList<Reduction>();
        arguments.add(reduction);
        setValue(new Variable(arguments));
    }
View Full Code Here

        Reduction reduction = parser.getCurrentReduction();

        List<Reduction> arguments = new ArrayList<Reduction>();
        buildArgumentList(reduction, arguments);

        setValue(new Variable(arguments));
    }
View Full Code Here

        BigDecimal bd = valueToNegate.getValue().asNumber();
        if (bd == null) {
            theParser.raiseParserException(Simple3.formatMessage("error.negation_number_expected"));
        }

        return new Variable(bd.negate());
    }
View Full Code Here

        return variableName;
    }
   
    @Override
    public Variable getValue() {
        Variable var = theParser.getProgramVariable(variableName);
        return var == null ? new Variable("") : var;
    }
View Full Code Here

        Scope newScope = new Scope(Function.FUNCTION_PREFIX + functionName, theParser.getCurrentScope());
        Scope oldScope = theParser.setCurrentScope(newScope);

        try {
            // Retrieve the function Reduction, set parameters, and execute the function
            Variable var = theParser.getProgramVariable(Function.FUNCTION_PREFIX + functionName);
            if (var != null) {
                Function fn = (Function)var.asObject();

                List<String> parameters = fn.getParameters();

                // Make sure we have the proper number of calling parameters
                if (parameters.size() != arguments.size()) {
                    throw new ParserException(Simple3.formatMessage("error.function_argument_count",
                        String.valueOf(parameters.size()), String.valueOf(arguments.size())));
                }

                // Set the function parameters -- these will be stored under the function's
                // scope and will not be visible outside of the function. Any variables
                // in other scopes having the same name will not be visible to the function.
                for (int i = 0; i < parameters.size(); i++) {
                    theParser.setProgramVariable(parameters.get(i), arguments.get(i).getValue());
                }

                Reduction statements = fn.getStatements();
                if (statements != null) {
                    statements.execute();
                    var = statements.getValue();
                }

                // Simple3 doesn't allow a simple RETURN without a value hence this
                // path will never be executed.  The code remains to support other
                // syntaxes that allow an empty return statement.  These lines of
                // code will be flagged as untested by the coverage tool when the
                // Simple3 tests are run.
                if (var == null) {
                    var = new Variable("");
                }
            } else {
                throw new ParserException(Simple3.formatMessage("error.function_undefined", functionName));
            }
View Full Code Here

        List<String> parameters = new ArrayList<String>();

        if (reduction.size() > 0) {
            Stack stack;

            Variable var = parser.getProgramVariable(KEY_PARAMETERS);
            if (var == null) {
                stack = new Stack();
                parser.setProgramVariable(KEY_PARAMETERS, new Variable(stack));
            } else {
                stack = (Stack)var.asObject();
            }

            stack.push(reduction.get(0).getData());

            // Copy the values of the current stack to the value object leaving
            // all the values on the stack
            int paramCount = stack.size();
            for (int i = paramCount - 1; i >= 0; i--) {
                parameters.add(stack.get(i).toString());
            }
        }

        setValue(new Variable(parameters));
    }
View Full Code Here

TOP

Related Classes of com.creativewidgetworks.goldparser.parser.Variable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.