Commit 64a0cbcd authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Properly support Enumeration values in MATLAB expressions.

By default, all Simulink enumerated values are formed as: enumerationtype.constant
The current MATLAB parser in jTET fails to recognize this.  Fix this so that
it recognizes these constants properly.  Also add a test to verify this mode.
Note that the original style of just the constant is still ok, thought in
a TET block that style may not work.
parent a7fb1754
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -350,7 +350,22 @@ final public class MatlabParser {

        @Override
        public void enterId_plus_indexers(Id_plus_indexersContext ctx) {
            addExpressionValue(m_variableParser.get(ctx.getText()));
            String variableName = ctx.getText();
            Variable variable;

            // MATLAB prefixes enumeration constants with the enumeration name and a dot.  Strip that out.
            if (variableName.contains(".")) {
                variable = m_variableParser.get(variableName.split("\\.")[1]);
                // Assume the type matches.  Due to the fact we allow type aliasing, we can't check the types match.
            } else {
                variable = m_variableParser.get(variableName);
            }

            if (variable == null) {
                throw new IllegalArgumentException("No variable called " + variableName + " found!");
            }

            addExpressionValue(variable);
        }

        public Expression m_rootExpression;
+17 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ package ca.mcscert.jtet.parsers.test;
import ca.mcscert.jtet.expression.BooleanVariableType;
import ca.mcscert.jtet.expression.CVC3Generator;
import ca.mcscert.jtet.expression.CheckerGenerator;
import ca.mcscert.jtet.expression.EnumerationVariableType;
import ca.mcscert.jtet.expression.Expression;
import ca.mcscert.jtet.expression.Literal;
import ca.mcscert.jtet.expression.VariableCollection;
@@ -16,6 +17,7 @@ import ca.mcscert.jtet.expression.SMTLIBGenerator;
import ca.mcscert.jtet.expression.Variable;
import ca.mcscert.jtet.parsers.VariableParser;
import ca.mcscert.jtet.expression.VariableType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -156,5 +158,20 @@ public class MatlabParserTest {
        assertEquals("(and (and (= z 1.0) (= x 0.0)) (= y 2.0))", expr.getCheckerOutput(generator, variableType));
    }

    @Test
    public void testEnumerationParse() {
        Expression expr = MatlabParser.parseMatlabCode(variableParser.parseVariables("x: Enumeration"), "x == Enumeration.e1");

        assertEquals("(x Equals e1)", expr.toString());
    }

    @Before
    public void setUp() {
        EnumerationVariableType enumType = new EnumerationVariableType("Enumeration");
        enumType.enumerationValues().add("e1");
        enumType.enumerationValues().add("e2");
        variableParser.addCustomType("Enumeration", enumType);
    }

    VariableParser variableParser = new VariableParser();
}