use of de.be4.classicalb.core.parser.BLexer in project probparsers by bendisposto.
the class PreParser method determineDependencies.
private Map<String, Set<String>> determineDependencies(Set<String> definitionNames, Map<Token, Token> definitions) throws PreParseException {
HashMap<String, Set<String>> dependencies = new HashMap<>();
for (Entry<Token, Token> entry : definitions.entrySet()) {
Token nameToken = entry.getKey();
Token rhsToken = entry.getValue();
// The FORMULA_PREFIX is needed to switch the lexer state from
// section to normal. Note, that we do not parse the right hand side
// of the definition here. Hence FORMULA_PREFIX has no further
// meaning and substitutions can also be handled by the lexer.
final Reader reader = new StringReader(BParser.FORMULA_PREFIX + "\n" + rhsToken.getText());
final BLexer lexer = new BLexer(new PushbackReader(reader, BLexer.PUSHBACK_BUFFER_SIZE), new DefinitionTypes());
lexer.setParseOptions(parseOptions);
Set<String> set = new HashSet<>();
de.be4.classicalb.core.parser.node.Token next = null;
try {
next = lexer.next();
while (!(next instanceof EOF)) {
if (next instanceof TIdentifierLiteral) {
TIdentifierLiteral id = (TIdentifierLiteral) next;
String name = id.getText();
if (definitionNames.contains(name)) {
set.add(name);
}
}
next = lexer.next();
}
} catch (IOException e) {
} catch (BLexerException e) {
de.be4.classicalb.core.parser.node.Token errorToken = e.getLastToken();
final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(nameToken, rhsToken, errorToken, e.getMessage());
throw new PreParseException(newMessage);
} catch (de.be4.classicalb.core.parser.lexer.LexerException e) {
final String newMessage = determineNewErrorMessageWithCorrectedPositionInformationsWithoutToken(nameToken, rhsToken, e.getMessage());
throw new PreParseException(newMessage);
}
dependencies.put(nameToken.getText(), set);
}
return dependencies;
}
use of de.be4.classicalb.core.parser.BLexer 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