use of de.be4.classicalb.core.parser.ParsingBehaviour in project probparsers by bendisposto.
the class Helpers method parseFile2.
public static String parseFile2(String filename) throws IOException {
final File machineFile = new File(filename);
final BParser parser = new BParser(machineFile.getAbsolutePath());
Start tree;
OutputStream output = new OutputStream() {
private StringBuilder string = new StringBuilder();
@Override
public void write(int b) throws IOException {
this.string.append((char) b);
}
public String toString() {
return this.string.toString();
}
};
try {
tree = parser.parseFile(machineFile, false);
PrintStream printStream = new PrintStream(output);
BParser.printASTasProlog(printStream, parser, machineFile, tree, new ParsingBehaviour(), parser.getContentProvider());
output.close();
return output.toString();
} catch (BCompoundException e) {
e.printStackTrace();
PrologExceptionPrinter.printException(output, e);
return output.toString();
}
}
use of de.be4.classicalb.core.parser.ParsingBehaviour in project probparsers by bendisposto.
the class CliBParser method main.
public static void main(final String[] args) throws IOException {
// System.out.println("Ready. Press enter");
// System.in.read();
// System.out.println("Starting");
final ConsoleOptions options = createConsoleOptions(args);
if (options.isOptionSet(CLI_SWITCH_VERSION)) {
System.out.println(CliBParser.getBuildRevision());
System.exit(0);
}
final String[] arguments = options.getRemainingOptions();
if (!options.isOptionSet(CLI_SWITCH_PREPL) && arguments.length != 1) {
options.printUsage(System.err);
System.exit(-1);
}
final ParsingBehaviour behaviour = new ParsingBehaviour();
final File f;
PrintStream out;
if (options.isOptionSet(CLI_SWITCH_OUTPUT)) {
final String filename = options.getOptions(CLI_SWITCH_OUTPUT)[0];
f = new File(filename);
try {
out = new PrintStream(filename);
} catch (final FileNotFoundException e) {
if (options.isOptionSet(CLI_SWITCH_PROLOG)) {
PrologExceptionPrinter.printException(System.err, e, f.getAbsolutePath());
} else {
System.err.println("Unable to create file '" + filename + "'");
}
System.exit(-1);
// Unreachable, but needed
return;
}
} else {
out = System.out;
f = null;
}
behaviour.setOut(out);
behaviour.setOutputFile(f);
behaviour.setPrintTime(options.isOptionSet(CLI_SWITCH_TIME));
behaviour.setPrologOutput(options.isOptionSet(CLI_SWITCH_PROLOG));
behaviour.setAddLineNumbers(options.isOptionSet(CLI_SWITCH_PROLOG_LINES));
behaviour.setUseIndention(options.isOptionSet(CLI_SWITCH_INDENTION));
behaviour.setDisplayGraphically(options.isOptionSet(CLI_SWITCH_UI));
behaviour.setPrintAST(options.isOptionSet(CLI_SWITCH_AST));
behaviour.setVerbose(options.isOptionSet(CLI_SWITCH_VERBOSE));
behaviour.setFastPrologOutput(options.isOptionSet(CLI_SWITCH_FASTPROLOG));
behaviour.setMachineNameMustMatchFileName(options.isOptionSet(CLI_SWITCH_NAME_CHECK));
if (options.isOptionSet(CLI_SWITCH_PREPL)) {
runPRepl(behaviour);
} else {
// parsed correctly
if (options.getRemainingOptions().length != 1) {
options.printUsage(System.err);
System.exit(-1);
}
String filename = options.getRemainingOptions()[0];
final File bfile = new File(filename);
int returnValue;
if (options.isOptionSet(CLI_SWITCH_OUTPUT)) {
returnValue = doFileParsing(behaviour, out, System.err, true, bfile);
} else {
returnValue = doFileParsing(behaviour, out, System.err, false, bfile);
}
System.exit(returnValue);
}
}
use of de.be4.classicalb.core.parser.ParsingBehaviour in project probparsers by bendisposto.
the class CliBParser method doFileParsing.
private static int doFileParsing(final ParsingBehaviour behaviour, final PrintStream out, final PrintStream err, final boolean closeStream, final File bfile) {
int returnValue;
try {
final String fileName = bfile.getName();
final String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
if (extension.equals("rmch")) {
returnValue = RulesProject.parseProject(bfile, behaviour, out, err);
} else {
final BParser parser = new BParser(bfile.getAbsolutePath());
returnValue = parser.fullParsing(bfile, behaviour, out, err);
}
} catch (Exception e) {
e.printStackTrace();
returnValue = -4;
} finally {
if (closeStream) {
out.close();
}
}
return returnValue;
}
use of de.be4.classicalb.core.parser.ParsingBehaviour in project probparsers by bendisposto.
the class FilePragmaTest method parseFile.
private static void parseFile(final String filename) throws IOException, BCompoundException {
final int dot = filename.lastIndexOf('.');
if (dot >= 0) {
final File machineFile = new File(filename);
final String probfilename = filename.substring(0, dot) + ".prob";
BParser parser = new BParser(filename);
Start tree = parser.parseFile(machineFile, false);
PrintStream output = new PrintStream(probfilename);
BParser.printASTasProlog(output, parser, machineFile, tree, new ParsingBehaviour(), parser.getContentProvider());
output.close();
} else
throw new IllegalArgumentException("Filename '" + filename + "' has no extension");
}
use of de.be4.classicalb.core.parser.ParsingBehaviour in project probparsers by bendisposto.
the class RulesUtil method getFileAsPrologTerm.
public static String getFileAsPrologTerm(final String file) {
ParsingBehaviour pb = new ParsingBehaviour();
pb.setAddLineNumbers(false);
OutputStream output = new OutputStream() {
private StringBuilder string = new StringBuilder();
@Override
public void write(int b) throws IOException {
this.string.append((char) b);
}
public String toString() {
return this.string.toString();
}
};
RulesProject.parseProject(new File(file), pb, new PrintStream(output), new PrintStream(output));
return output.toString();
}
Aggregations