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

Put in work so far. Now capable of dealing with default types.


git-svn-id: https://groke.mcmaster.ca/svn/grad/colin/branches/TableTool_javization@9610 57e6efec-57d4-0310-aeb1-a6c144bb1a8b
parent 306612a2
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -6,8 +6,13 @@ package ca.mcmaster.cas.matlab2smt;

/**
 *
 * @author Matthew Dawson
 * @author matthew
 */
public class VariableParser {
public class BooleanVariableType implements VariableType {

    @Override
    public String getCVC3Definition(String variableName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
+27 −0
Original line number Diff line number Diff line
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.mcmaster.cas.matlab2smt;

/**
 *
 * @author matthew
 */
public final class RealVariableType implements VariableType {

    @Override
    public String getCVC3Definition(String variableName) {
        return "REAL";
    }
    
    @Override
    public boolean equals(Object other) {
        return other instanceof RealVariableType;
    }

    @Override
    public int hashCode() {
        return 0;
    }
}
+27 −0
Original line number Diff line number Diff line
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.mcmaster.cas.matlab2smt;

/**
 *
 * @author matthew
 */
public class Variable {
    protected Variable(String name, VariableType type) {
        m_name = name;
        m_type = type;
    }
    
    public String name() {
        return m_name;
    }
    
    public VariableType type() {
        return m_type;
    }
    
    private String m_name;
    private VariableType m_type;
}
+7 −1
Original line number Diff line number Diff line
grammar VariableParser;

varlist: var (',' var)*; //Build list of variables
var:    ID;
var:    ID vartype;
vartype: ':' type_info
       | defaulttype;
defaulttype: ;
type_info: 'bool'
         | 'real'
         | 'fixed' ;

ID: [a-zA-Z][a-zA-Z0-9]*; //Variable identifier.  Start with letter, then alpha numerical allowed.
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
+71 −0
Original line number Diff line number Diff line
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.mcmaster.cas.matlab2smt;

import ca.mcmaster.cas.matlab2smt.VariableParserParser.DefaulttypeContext;
import ca.mcmaster.cas.matlab2smt.VariableParserParser.VarContext;
import java.util.HashMap;
import java.util.Map;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

/**
 *
 * @author Matthew Dawson
 */
public class VariableParser {
    public VariableParser(String vars) {
        parseStringToVariables(vars);
    }
    
    public Variable getVar(String name) {
        return m_variables.get(name);
    }
    
    private void parseStringToVariables(String variables) {
        // create a CharStream that reads from standard input
        ANTLRInputStream input = new ANTLRInputStream(variables);

        // create a lexer that feeds off of input CharStream
        VariableParserLexer lexer = new VariableParserLexer(input);

        // create a buffer of tokens pulled from the lexer
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        // create a parser that feeds off the tokens buffer
        VariableParserParser parser = new VariableParserParser(tokens);

        ParseTree tree = parser.varlist();
        ParseTreeWalker walker = new ParseTreeWalker();
        VariableParserListener listener = new VariablesParserToVaribles();
        walker.walk(listener, tree);
    }
    
    private Map<String, Variable>  m_variables = new HashMap<String, Variable>();
    
    private class VariablesParserToVaribles extends VariableParserBaseListener {
        private String currentName;
        private VariableType currentType;

        @Override
        public void enterVar(VarContext ctx) {
            currentName = ctx.ID().getText();
        }

        @Override
        public void enterDefaulttype(DefaulttypeContext ctx) {
            currentType = new RealVariableType();
        }
        
        @Override
        public void exitVar(VarContext ctx) {
            m_variables.put(currentName, new Variable(currentName, currentType));
            currentName = null;
            currentType = null;
        }
    }
}
Loading