use of de.be4.classicalb.core.parser.IDefinitions in project probparsers by bendisposto.
the class RulesProject method createMainMachine.
private BMachine createMainMachine(List<PDefinition> mainDefinitions) {
BMachine mainMachine = new BMachine(MAIN_MACHINE_NAME);
mainMachine.addIncludesClause(COMPOSITION_MACHINE_NAME);
mainMachine.addPromotesClause(getPromotesList());
mainMachine.addPropertiesPredicates(this.constantStringValues);
IDefinitions idefinitions = new Definitions();
if (mainDefinitions != null) {
for (PDefinition pDefinition : mainDefinitions) {
idefinitions.addDefinition(pDefinition);
}
}
addToStringDefinition(idefinitions);
addSortDefinition(idefinitions);
addFormatToStringDefinition(idefinitions);
addChooseDefinition(idefinitions);
addBooleanPreferenceDefinition(idefinitions, "SET_PREF_ALLOW_LOCAL_OPERATION_CALLS", true);
mainMachine.replaceDefinition(idefinitions);
return mainMachine;
}
use of de.be4.classicalb.core.parser.IDefinitions in project probparsers by bendisposto.
the class RulesTransformation method outARuleFailSubSubstitution.
@Override
public void outARuleFailSubSubstitution(ARuleFailSubSubstitution node) {
addForceDefinition(iDefinitions);
Node newNode = null;
if (!node.getIdentifiers().isEmpty()) {
newNode = createPositinedNode(createCounterExampleSubstitutions(node.getIdentifiers(), node.getWhen(), null, node.getMessage(), node.getErrorType()), node);
} else {
// default value if no value is provided
int errorType = 1;
if (node.getErrorType() != null) {
errorType = Integer.parseInt(node.getErrorType().getText());
}
PSubstitution sub = createCounterExampleSubstitution(errorType, createSetOfPExpression(node.getMessage(), node.getMessage()), false);
if (node.getWhen() != null) {
// there is a when predicate but no parameters
newNode = new AIfSubstitution(node.getWhen(), sub, new ArrayList<PSubstitution>(), null);
} else {
// no parameters and no when predicate
newNode = sub;
}
}
node.replaceBy(newNode);
}
use of de.be4.classicalb.core.parser.IDefinitions in project probparsers by bendisposto.
the class PreParser method evaluateDefinitionFiles.
private void evaluateDefinitionFiles(final List<Token> list) throws PreParseException, BException, BCompoundException {
IDefinitionFileProvider cache = null;
if (contentProvider instanceof IDefinitionFileProvider) {
cache = (IDefinitionFileProvider) contentProvider;
}
for (final Token fileNameToken : list) {
final List<String> newDoneList = new ArrayList<String>(doneDefFiles);
try {
final String fileName = fileNameToken.getText();
if (doneDefFiles.contains(fileName)) {
StringBuilder sb = new StringBuilder();
for (String string : doneDefFiles) {
sb.append(string).append(" -> ");
}
sb.append(fileName);
throw new PreParseException(fileNameToken, "Cyclic references in definition files: " + sb.toString());
}
IDefinitions definitions;
if (cache != null && cache.getDefinitions(fileName) != null) {
definitions = cache.getDefinitions(fileName);
} else {
final String content = contentProvider.getFileContent(directory, fileName);
newDoneList.add(fileName);
final File file = contentProvider.getFile(directory, fileName);
String filePath = fileName;
if (file != null) {
filePath = file.getCanonicalPath();
}
final BParser parser = new BParser(filePath, parseOptions);
parser.setDirectory(directory);
parser.setDoneDefFiles(newDoneList);
parser.setDefinitions(new Definitions(file));
parser.parse(content, debugOutput, contentProvider);
definitions = parser.getDefinitions();
if (cache != null) {
cache.storeDefinition(fileName, definitions);
}
}
defFileDefinitions.addDefinitions(definitions);
definitionTypes.addAll(definitions.getTypes());
} catch (final IOException e) {
throw new PreParseException(fileNameToken, "Definition file cannot be read: " + e.getLocalizedMessage());
} finally {
}
}
}
use of de.be4.classicalb.core.parser.IDefinitions in project probparsers by bendisposto.
the class BParser method eparse.
public Start eparse(String input, IDefinitions context) throws BCompoundException, LexerException, IOException {
final Reader reader = new StringReader(input);
Start ast = null;
List<String> ids = new ArrayList<>();
final DefinitionTypes defTypes = new DefinitionTypes();
defTypes.addAll(context.getTypes());
BLexer bLexer = new BLexer(new PushbackReader(reader, BLexer.PUSHBACK_BUFFER_SIZE), defTypes);
bLexer.setParseOptions(parseOptions);
Token t;
do {
t = bLexer.next();
if (t instanceof TIdentifierLiteral) {
if (!ids.contains(t.getText())) {
ids.add(t.getText());
}
}
} while (!(t instanceof EOF));
Parser p = new Parser(new EBLexer(input, BigInteger.ZERO, ids, defTypes));
boolean ok;
try {
ast = p.parse();
ok = true;
} catch (Exception e) {
handleException(e);
ok = false;
}
BigInteger b = new BigInteger("2");
b = b.pow(ids.size());
b = b.subtract(BigInteger.ONE);
while (!ok && b.compareTo(BigInteger.ZERO) > 0) {
p = new Parser(new EBLexer(input, b, ids, defTypes));
try {
ast = p.parse();
ok = true;
} catch (ParserException e) {
b = b.subtract(BigInteger.ONE);
handleException(e);
}
}
return ast;
}
Aggregations