use of de.be4.classicalb.core.preparser.parser.Parser in project probparsers by bendisposto.
the class ParserBugTest method test.
@Test
public void test() throws Exception {
BParser parser = new BParser();
Start ast = parser.parse(s, true);
ast.apply(new ASTPrinter());
assertTrue(true);
}
use of de.be4.classicalb.core.preparser.parser.Parser in project probparsers by bendisposto.
the class RulesProject method printProjectAsPrologTerm.
public void printProjectAsPrologTerm(final IPrologTermOutput pout) {
// parser version
pout.openTerm("parser_version");
pout.printAtom(BParser.getBuildRevision());
pout.closeTerm();
pout.fullstop();
// machine
pout.openTerm("classical_b");
pout.printAtom(MAIN_MACHINE_NAME);
pout.openList();
for (IModel iModel : bModels) {
pout.printAtom(iModel.getPath());
}
pout.closeList();
pout.closeTerm();
pout.fullstop();
NodeIdAssignment tempNodeIdAssignment = parsingBehaviour.isAddLineNumbers() ? this.nodeIdAssignment : new NodeIdAssignment();
for (IModel iModel : bModels) {
iModel.printAsProlog(pout, tempNodeIdAssignment);
}
}
use of de.be4.classicalb.core.preparser.parser.Parser in project probparsers by bendisposto.
the class RulesParseUnit method parse.
public void parse() {
if (this.bCompoundException != null) {
return;
}
try {
bParser = null;
if (machineFile != null) {
bParser = new BParser(machineFile.getPath());
bParser.setDirectory(machineFile.getParentFile());
} else {
bParser = new BParser();
}
ParseOptions parseOptions = new ParseOptions();
parseOptions.setGrammar(RulesGrammar.getInstance());
bParser.setParseOptions(parseOptions);
start = bParser.parse(content, debugOuput, new CachingDefinitionFileProvider());
refFinder = new RulesReferencesFinder(machineFile, start);
refFinder.findReferencedMachines();
this.machineReferences = refFinder.getReferences();
this.machineName = refFinder.getName();
this.rulesMachineChecker = new RulesMachineChecker(machineFile, bParser.getFileName(), machineReferences, start);
rulesMachineChecker.runChecks();
this.operationList.addAll(rulesMachineChecker.getOperations());
} catch (BCompoundException e) {
// store parser exceptions
this.bCompoundException = e;
}
}
use of de.be4.classicalb.core.preparser.parser.Parser 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.preparser.parser.Parser in project probparsers by bendisposto.
the class PreParser method evaluateTypes.
private void evaluateTypes(final List<Token> sortedDefinitionList, final Map<Token, Token> definitions) throws PreParseException {
// use linked list as we rely on pop() and push()
final LinkedList<Token> remainingDefinitions = new LinkedList<>(sortedDefinitionList);
final LinkedList<Token> currentlyUnparseableDefinitions = new LinkedList<Token>();
Set<String> todoDefs = new HashSet<String>();
for (Token token : remainingDefinitions) {
todoDefs.add(token.getText());
}
// use main parser for the rhs of each definition to determine type
// if a definition can not be typed this way, it may be due to another
// definition that is not yet parser (because it appears later in the
// source code)
// in this case, the definition is appended to the list again
// the algorithm terminates if the queue is empty or if no definition
// has been parsed
boolean oneParsed = true;
while (oneParsed) {
oneParsed = false;
while (!remainingDefinitions.isEmpty()) {
final Token definition = remainingDefinitions.pop();
final Token defRhs = definitions.get(definition);
Definitions.Type type = null;
DefinitionType definitionType = determineType(definition, defRhs, todoDefs);
type = definitionType.type;
if (type != null) {
todoDefs.remove(definition.getText());
oneParsed = true;
definitionTypes.addTyping(definition.getText(), type);
// types.addTyping(definition.getText(), type);
} else {
currentlyUnparseableDefinitions.push(definition);
}
}
remainingDefinitions.addAll(currentlyUnparseableDefinitions);
currentlyUnparseableDefinitions.clear();
}
if (!remainingDefinitions.isEmpty()) {
final Token definition = remainingDefinitions.pop();
final Token defRhs = definitions.get(definition);
DefinitionType definitionType = determineType(definition, defRhs, todoDefs);
if (definitionType.errorMessage != null) {
throw new PreParseException(definitionType.errorMessage + " in file: " + modelFileName);
// throw new BParseException(definitionType.errorToken,
// definitionType.errorMessage + " in file: "
// + modelFileName);
} else {
// fall back message
throw new PreParseException(definition, "[" + definition.getLine() + "," + definition.getPos() + "] expecting wellformed expression, predicate or substitution as DEFINITION body (DEFINITION arguments assumed to be expressions)");
}
}
}
Aggregations