Commit a2356756 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Similar to VariableParser, also make MatlabParser a single static method.

Since MatlabParser has no reason to have an instance of it managed by a
user, just make it use a single static method to access its functionality.
Overall it makes code simpler (avoiding the nasty getRootExpression call).

git-svn-id: https://groke.mcmaster.ca/svn/grad/colin/branches/TableTool_javization@10847 57e6efec-57d4-0310-aeb1-a6c144bb1a8b
parent 34e979de
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -98,7 +98,7 @@ final public class HierarchicalGridCVC3Generator implements HierarchcialGridChec
        if (m_currentlyRunning) {
            outputCells();
        }
        m_parentCellsCVC3Code.addFirst(new MatlabParser(m_variableDefinitions, cell.contents()).getRootExpression().getCheckerOutput(m_CVC3Generator, m_booleanType));
        m_parentCellsCVC3Code.addFirst(MatlabParser.parseMatlabCode(m_variableDefinitions, cell.contents()).getCheckerOutput(m_CVC3Generator, m_booleanType));
        m_currentlyRunning = true;
    }

@@ -118,7 +118,7 @@ final public class HierarchicalGridCVC3Generator implements HierarchcialGridChec
    @Override
    public void handleLeafCell(Cell cell) {
        // First get the appropriate CVC3 code from matlab.
        String newCellCode = new MatlabParser(m_variableDefinitions, cell.contents()).getRootExpression().getCheckerOutput(m_CVC3Generator, m_booleanType);
        String newCellCode = MatlabParser.parseMatlabCode(m_variableDefinitions, cell.contents()).getCheckerOutput(m_CVC3Generator, m_booleanType);

        // Next form the disjoint queries and add to the list.
        for(String otherCellCode : m_currentCompleteQueries) {
+80 −95
Original line number Diff line number Diff line
@@ -45,17 +45,14 @@ import org.antlr.v4.runtime.tree.ParseTreeWalker;
 *
 * @author matthew
 */
final public class MatlabParser implements MatlabExpressionWithSubExpression {
final public class MatlabParser {

    public MatlabParser(Map<String, Variable> variableListing, String matlabCode) {
        m_variableParser = variableListing;
        m_opStack.addFirst(new ExpressionStackContainer(0, this));
        parseMatlabCode(matlabCode + "\n");
    private MatlabParser() {
    }

    private void parseMatlabCode(String matlabCode) {
    public static MatlabExpression parseMatlabCode(Map<String, Variable> variableListing, String matlabCode) {
        // create a CharStream that reads from standard input
        ANTLRInputStream input = new ANTLRInputStream(matlabCode);
        ANTLRInputStream input = new ANTLRInputStream(matlabCode + "\n");

        // create a lexer that feeds off of input CharStream
        MatlabParserLexer lexer = new MatlabParserLexer(input);
@@ -68,12 +65,17 @@ final public class MatlabParser implements MatlabExpressionWithSubExpression {

        ParseTree tree = parser.expression();
        ParseTreeWalker walker = new ParseTreeWalker();
        MatlabParserListener listener = new MatlabParser.MatlabParserToMatlabExpressions();
        MatlabParserToMatlabExpressions listener = new MatlabParser.MatlabParserToMatlabExpressions(variableListing);
        walker.walk(listener, tree);

        return listener.m_rootExpression;
    }

    public MatlabExpression getRootExpression() {
        return m_rootExpression;
    private static class MatlabParserToMatlabExpressions extends MatlabParserBaseListener implements MatlabExpressionWithSubExpression {

        MatlabParserToMatlabExpressions(Map<String, Variable> variableListing) {
            m_variableParser = variableListing;
            m_opStack.addFirst(new ExpressionStackContainer(0, this));
        }

        private void incrementLevel() {
@@ -143,7 +145,6 @@ final public class MatlabParser implements MatlabExpressionWithSubExpression {
            m_rootExpression = subExpression;
        }

    private class MatlabParserToMatlabExpressions extends MatlabParserBaseListener {

        @Override
        public void enterE0(E0Context ctx) {
@@ -290,23 +291,6 @@ final public class MatlabParser implements MatlabExpressionWithSubExpression {
        public void enterG4(G4Context ctx) {
            addExpressionBinaryOperation(new MatlabExpressionBinaryOperation(null, MatlabBinaryOperation.getOpForSymbol(ctx.getText()), null));
        }

        @Override
        public void exitG1(G1Context ctx) {
        }

        @Override
        public void exitG2(G2Context ctx) {
        }

        @Override
        public void exitG3(G3Context ctx) {
        }

        @Override
        public void exitG4(G4Context ctx) {
        }

        @Override
        public void enterPrefix_operator(Prefix_operatorContext ctx) {
            addPrefixUnaryOperation(new MatlabExpressionUnaryOperation(MatlabUnaryOperation.getOpForSymbol(ctx.getText()), null));
@@ -325,8 +309,8 @@ final public class MatlabParser implements MatlabExpressionWithSubExpression {
        public void enterId_plus_indexers(Id_plus_indexersContext ctx) {
            addExpressionValue(m_variableParser.get(ctx.getText()));
        }
    }
    private MatlabExpression m_rootExpression;

        public MatlabExpression m_rootExpression;
        private int m_level = 0;
        private Map<String, Variable> m_variableParser;

@@ -340,7 +324,7 @@ final public class MatlabParser implements MatlabExpressionWithSubExpression {
                this.op = op;
            }
        }
    Deque<ExpressionStackContainer> m_opStack = new ArrayDeque<MatlabParser.ExpressionStackContainer>();
        Deque<ExpressionStackContainer> m_opStack = new ArrayDeque<ExpressionStackContainer>();

        private static class HiddenOpStackContainer {

@@ -355,3 +339,4 @@ final public class MatlabParser implements MatlabExpressionWithSubExpression {
        }
        Deque<HiddenOpStackContainer> m_hiddenOpStack = new ArrayDeque<HiddenOpStackContainer>();
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -99,7 +99,7 @@ final public class HierarchicalGridSMTLIBGenerator implements HierarchcialGridCh
        if (m_currentlyRunning) {
            outputCells();
        }
        m_parentCellsCVC3Code.addFirst(new MatlabParser(m_variableDefinitions, cell.contents()).getRootExpression().getCheckerOutput(m_CVC3Generator, m_booleanType));
        m_parentCellsCVC3Code.addFirst(MatlabParser.parseMatlabCode(m_variableDefinitions, cell.contents()).getCheckerOutput(m_CVC3Generator, m_booleanType));
        m_currentlyRunning = true;
    }

@@ -119,7 +119,7 @@ final public class HierarchicalGridSMTLIBGenerator implements HierarchcialGridCh
    @Override
    public void handleLeafCell(Cell cell) {
        // First get the appropriate CVC3 code from matlab.
        String newCellCode = new MatlabParser(m_variableDefinitions, cell.contents()).getRootExpression().getCheckerOutput(m_CVC3Generator, m_booleanType);
        String newCellCode = MatlabParser.parseMatlabCode(m_variableDefinitions, cell.contents()).getCheckerOutput(m_CVC3Generator, m_booleanType);

        // Next form the disjoint queries and add to the list.
        for(String otherCellCode : m_currentCompleteQueries) {
+6 −6
Original line number Diff line number Diff line
@@ -69,8 +69,8 @@ public class GenericGeneratorTest {
    public void RealTest() {
        if (RealExpectedOutput != null) {
            Map<String, Variable> vars = VariableParser.parseVariables("x");
            MatlabParser parser = new MatlabParser(vars, query);
            assertEquals(RealExpectedOutput, parser.getRootExpression().getCheckerOutput(generator, outputType));
            MatlabExpression expr = MatlabParser.parseMatlabCode(vars, query);
            assertEquals(RealExpectedOutput, expr.getCheckerOutput(generator, outputType));
        }
    }

@@ -78,8 +78,8 @@ public class GenericGeneratorTest {
    public void SignedFixedPointTest() {
        if (SignedFixedPointExpectedOutput != null) {
            Map<String, Variable> vars = VariableParser.parseVariables("x:fixedt(1, 8, 2)");
            MatlabParser parser = new MatlabParser(vars, query);
            assertEquals(SignedFixedPointExpectedOutput, parser.getRootExpression().getCheckerOutput(generator, outputType));
            MatlabExpression expr = MatlabParser.parseMatlabCode(vars, query);
            assertEquals(SignedFixedPointExpectedOutput, expr.getCheckerOutput(generator, outputType));
        }
    }

@@ -87,8 +87,8 @@ public class GenericGeneratorTest {
    public void UnsignedFixedPointTest() {
        if (UnsignedFixedPointExpectedOutput != null) {
            Map<String, Variable> vars = VariableParser.parseVariables("x:fixedt(0, 8, 2)");
            MatlabParser parser = new MatlabParser(vars, query);
            assertEquals(UnsignedFixedPointExpectedOutput, parser.getRootExpression().getCheckerOutput(generator, outputType));
            MatlabExpression expr = MatlabParser.parseMatlabCode(vars, query);
            assertEquals(UnsignedFixedPointExpectedOutput, expr.getCheckerOutput(generator, outputType));
        }
    }
}
+2 −4
Original line number Diff line number Diff line
@@ -73,14 +73,12 @@ public class MatlabParserOperatorParseTest {
    public void doTest() {
        if (bop != null) {
            // Binary op to test ...
            MatlabParser parser = new MatlabParser(null, "1" + inputSymbol + "2\n");
            MatlabExpressionBinaryOperation exp = (MatlabExpressionBinaryOperation)parser.getRootExpression();
            MatlabExpressionBinaryOperation exp = (MatlabExpressionBinaryOperation)MatlabParser.parseMatlabCode(null, "1" + inputSymbol + "2\n");

            assertThat(exp.toString(), is("(1 " + bop.toString() + " 2)"));
        } else {
            // Unary op to test ...
            MatlabParser parser = new MatlabParser(null, inputSymbol + "3\n");
            MatlabExpressionUnaryOperation exp = (MatlabExpressionUnaryOperation)parser.getRootExpression();
            MatlabExpressionUnaryOperation exp = (MatlabExpressionUnaryOperation)MatlabParser.parseMatlabCode(null, inputSymbol + "3\n");

            assertThat(exp.toString(), is("( " + uop.toString() + " 3)"));
        }
Loading