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);
}
}
}
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));
}
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);
}
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));
}
}
}
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);
}
Aggregations