Search in sources :

Example 11 with Type

use of de.be4.classicalb.core.parser.IDefinitions.Type in project probparsers by bendisposto.

the class CliBParser method runPRepl.

private static void runPRepl(final ParsingBehaviour behaviour) throws IOException, FileNotFoundException {
    PrintStream out;
    ServerSocket serverSocket = new ServerSocket(0, 50, InetAddress.getLoopbackAddress());
    // write port number as prolog term
    System.out.println(serverSocket.getLocalPort() + ".");
    socket = serverSocket.accept();
    socketOutputStream = socket.getOutputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), encoding));
    String line = "";
    MockedDefinitions context = new MockedDefinitions();
    boolean terminate = false;
    while (!terminate) {
        line = in.readLine();
        EPreplCommands command;
        String theFormula;
        if (line == null) {
            // the prob instance has been terminated. exit gracefully
            command = EPreplCommands.halt;
        } else {
            command = EPreplCommands.valueOf(line);
        }
        switch(command) {
            case version:
                print(CliBParser.getBuildRevision() + System.lineSeparator());
                break;
            case definition:
                String name = in.readLine();
                String type = in.readLine();
                String parameterCount = in.readLine();
                context.addMockedDefinition(name, type, parameterCount);
                break;
            case machine:
                String filename = in.readLine();
                String outFile = in.readLine();
                out = new PrintStream(outFile, encoding);
                final File bfile = new File(filename);
                int returnValue;
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                PrintStream ps = new PrintStream(baos);
                try {
                    final String fileName = bfile.getName();
                    final String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
                    if (extension.equals("rmch")) {
                        returnValue = RulesProject.parseProject(bfile, behaviour, out, ps);
                    } else {
                        final BParser parser = new BParser(bfile.getAbsolutePath());
                        returnValue = parser.fullParsing(bfile, behaviour, out, ps);
                    }
                    context = new MockedDefinitions();
                } catch (Exception e) {
                    e.printStackTrace();
                    returnValue = -4;
                } finally {
                    if (true) {
                        out.close();
                    }
                }
                if (returnValue == 0) {
                    print("exit(" + returnValue + ")." + System.lineSeparator());
                } else {
                    String output = baos.toString().replace(System.lineSeparator(), " ").trim();
                    print(output + System.lineSeparator());
                }
                break;
            case formula:
                theFormula = "#FORMULA\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case expression:
                theFormula = "#EXPRESSION\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case predicate:
                theFormula = "#PREDICATE\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case substitution:
                theFormula = "#SUBSTITUTION\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case extendedformula:
                theFormula = "#FORMULA\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case extendedexpression:
                theFormula = "#EXPRESSION\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case extendedpredicate:
                theFormula = "#PREDICATE\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case extendedsubstitution:
                theFormula = "#SUBSTITUTION\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case ltl:
                String extension = in.readLine();
                final ProBParserBase extParser = LtlConsoleParser.getExtensionParser(extension);
                final TemporalLogicParser<?> parser = new LtlParser(extParser);
                parseTemporalFormula(in, parser);
                break;
            case ctl:
                String extension2 = in.readLine();
                final ProBParserBase extParser2 = LtlConsoleParser.getExtensionParser(extension2);
                final TemporalLogicParser<?> parser2 = new CtlParser(extParser2);
                parseTemporalFormula(in, parser2);
                break;
            case halt:
                socket.close();
                serverSocket.close();
                terminate = true;
                break;
            default:
                throw new UnsupportedOperationException("Unsupported Command " + line);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) LtlParser(de.be4.ltl.core.parser.LtlParser) ServerSocket(java.net.ServerSocket) BParser(de.be4.classicalb.core.parser.BParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LtlParseException(de.be4.ltl.core.parser.LtlParseException) LexerException(de.be4.classicalb.core.parser.lexer.LexerException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MockedDefinitions(de.be4.classicalb.core.parser.MockedDefinitions) BufferedReader(java.io.BufferedReader) CtlParser(de.be4.ltl.core.parser.CtlParser) File(java.io.File) ProBParserBase(de.prob.parserbase.ProBParserBase)

Example 12 with Type

use of de.be4.classicalb.core.parser.IDefinitions.Type in project probparsers by bendisposto.

the class UnitPragmaTest method testLexer.

@Test
public void testLexer() throws Exception {
    String input = "MACHINE UnitPragmaExpressions1 VARIABLES   lala, /*@ unit \"10**1 * m**1\" */ xx,   /*@ unit \"10**1 * m**1\" */ yy,   /*@ unit \"10**2 * m**2\" */ zz,   test  INVARIANT  /*@ label \"lol\" */  lala = \"trololo\" &xx:NAT &   yy:NAT &   zz:NAT &   test:NAT INITIALISATION xx,yy,zz,test:=1,2,3,4 OPERATIONS   multiply = zz := xx*yy;   add      = xx := yy+1;   sub      = xx := yy-1;   type     = test := yy END";
    BLexer lex = new BLexer(new PushbackReader(new StringReader(input), 500));
    Token t;
    while (!((t = lex.next()) instanceof EOF)) {
        System.out.print(t.getClass().getSimpleName() + "(" + t.getText() + ")");
        System.out.print(" ");
    }
    BParser p = new BParser();
    Start ast = p.parse(input, false);
    ASTPrinter pr = new ASTPrinter();
    ast.apply(pr);
    System.out.println(printAST(ast));
}
Also used : BLexer(de.be4.classicalb.core.parser.BLexer) Start(de.be4.classicalb.core.parser.node.Start) ASTPrinter(de.be4.classicalb.core.parser.visualisation.ASTPrinter) StringReader(java.io.StringReader) Token(de.be4.classicalb.core.parser.node.Token) BParser(de.be4.classicalb.core.parser.BParser) Helpers.getTreeAsString(util.Helpers.getTreeAsString) EOF(de.be4.classicalb.core.parser.node.EOF) PushbackReader(java.io.PushbackReader) Test(org.junit.Test)

Example 13 with Type

use of de.be4.classicalb.core.parser.IDefinitions.Type in project probparsers by bendisposto.

the class ASTBuilder method addPrintSubDefinitionToIdefinitions.

public static void addPrintSubDefinitionToIdefinitions(IDefinitions definitions) {
    if (definitions.containsDefinition(PRINT)) {
        return;
    }
    /*-
		 * PRINT(x) == skip; 
		 * EXTERNAL_SUBSTITUTION_PRINT(T) == T; /* declare as external for any type T
		 */
    ASubstitutionDefinitionDefinition printDef = new ASubstitutionDefinitionDefinition();
    printDef.setName(new TDefLiteralSubstitution(PRINT));
    printDef.setParameters(createIdentifierList("value"));
    printDef.setRhs(new ASkipSubstitution());
    definitions.addDefinition(printDef, IDefinitions.Type.Substitution);
    AExpressionDefinitionDefinition forceDefType = new AExpressionDefinitionDefinition();
    forceDefType.setName(new TIdentifierLiteral("EXTERNAL_SUBSTITUTION_" + PRINT));
    forceDefType.setParameters(createIdentifierList("T"));
    forceDefType.setRhs(createIdentifier("T"));
    definitions.addDefinition(forceDefType, IDefinitions.Type.Expression);
}
Also used : TDefLiteralSubstitution(de.be4.classicalb.core.parser.node.TDefLiteralSubstitution) ASubstitutionDefinitionDefinition(de.be4.classicalb.core.parser.node.ASubstitutionDefinitionDefinition) ASkipSubstitution(de.be4.classicalb.core.parser.node.ASkipSubstitution) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral)

Example 14 with Type

use of de.be4.classicalb.core.parser.IDefinitions.Type in project probparsers by bendisposto.

the class OpSubstitutions method caseAIdentifierExpression.

@Override
public void caseAIdentifierExpression(final AIdentifierExpression node) {
    final String identifierString = Utils.getTIdentifierListAsString(node.getIdentifier());
    final Integer number = scopedVariables.get(identifierString);
    final Type type = definitions.getType(identifierString);
    if (number == null && type != Type.NoDefinition) {
        if (type == Type.Expression || type == Type.ExprOrSubst) {
            /*
				 * getFirst() is enough cause definitions cannot have composed
				 * identifiers
				 */
            replaceWithDefExpression(node, node.getIdentifier().getFirst(), null);
            if (type == Type.ExprOrSubst) {
                // type is determined now => set to Expression
                definitions.setDefinitionType(identifierString, Type.Expression);
            }
        } else {
            // finding some other type here is an error!
            throw new VisitorException(new CheckException("Expecting expression here but found definition with type '" + type + "'", node));
        }
    }
}
Also used : Type(de.be4.classicalb.core.parser.IDefinitions.Type) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException)

Example 15 with Type

use of de.be4.classicalb.core.parser.IDefinitions.Type in project probparsers by bendisposto.

the class CreateFreetypeTest method createAdd.

private AOperation createAdd(String name, String param, PExpression type, String cons) {
    final AMemberPredicate pre = new AMemberPredicate(createIdentifier(param), type);
    final ASetExtensionExpression newVal = new ASetExtensionExpression(Arrays.<PExpression>asList(new AFunctionExpression(createIdentifier(cons), createIdentifiers(param))));
    final PSubstitution subst = new APreconditionSubstitution(pre, createAssignment(VAR_NAME, new AUnionExpression(createIdentifier(VAR_NAME), newVal)));
    return new AOperation(EMPTY_EXPRS, createIdLits(name), createIdentifiers(param), subst);
}
Also used : AUnionExpression(de.be4.classicalb.core.parser.node.AUnionExpression) ASetExtensionExpression(de.be4.classicalb.core.parser.node.ASetExtensionExpression) APreconditionSubstitution(de.be4.classicalb.core.parser.node.APreconditionSubstitution) AOperation(de.be4.classicalb.core.parser.node.AOperation) PSubstitution(de.be4.classicalb.core.parser.node.PSubstitution) AMemberPredicate(de.be4.classicalb.core.parser.node.AMemberPredicate) AFunctionExpression(de.be4.classicalb.core.parser.node.AFunctionExpression)

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)8 Type (de.be4.classicalb.core.parser.IDefinitions.Type)6 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)5 Test (org.junit.Test)5 Helpers.getTreeAsString (util.Helpers.getTreeAsString)5 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)3 LinkedList (java.util.LinkedList)3 BParser (de.be4.classicalb.core.parser.BParser)2 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)2 AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)2 AFunctionExpression (de.be4.classicalb.core.parser.node.AFunctionExpression)2 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)2 PDefinition (de.be4.classicalb.core.parser.node.PDefinition)2 TDefLiteralSubstitution (de.be4.classicalb.core.parser.node.TDefLiteralSubstitution)2 Token (de.be4.classicalb.core.preparser.node.Token)2 File (java.io.File)2 BLexer (de.be4.classicalb.core.parser.BLexer)1 MockedDefinitions (de.be4.classicalb.core.parser.MockedDefinitions)1 BException (de.be4.classicalb.core.parser.exceptions.BException)1 BParseException (de.be4.classicalb.core.parser.exceptions.BParseException)1