use of de.be4.classicalb.core.parser.node.Start in project probparsers by bendisposto.
the class StructureTest method testIdentifierTick.
@Test
public void testIdentifierTick() throws Exception {
final Start root = parseInput("machine Mac variables x' y end", false);
final AMachineParseUnit parseUnit = (AMachineParseUnit) root.getPParseUnit();
final LinkedList<PVariable> variables = parseUnit.getVariables();
assertEquals(2, variables.size());
assertEquals("x'", ((AVariable) variables.get(0)).getName().getText());
assertEquals("y", ((AVariable) variables.get(1)).getName().getText());
}
use of de.be4.classicalb.core.parser.node.Start in project probparsers by bendisposto.
the class EventBParser method parse.
/**
* Parses the input string.
*
* @param input
* the {@link String} to be parsed
* @param debugOutput
* output debug messages on standard out?
* @return the root node of the AST
* @throws BException
* The {@link BException} class stores the actual exception as
* delegate and forwards all method calls to it. So it is save
* for tools to just use this exception if they want to extract
* an error message. If the tools needs to extract additional
* information, such as a sourcecode position or involved tokens
* respectively nodes, it needs to retrieve the delegate
* exception. The {@link BException} class offers a
* {@link BException#getCause()} method for this, which returns
* the delegate exception.
* <p>
* Internal exceptions:
* <ul>
* <li> {@link EventBLexerException}: If any error occurs in the
* generated or customized lexer a {@link LexerException} is
* thrown. Usually the lexer classes just throw a
* {@link LexerException}. But this class unfortunately does not
* contain any explicit information about the sourcecode
* position where the error occured. Using aspect-oriented
* programming we intercept the throwing of these exceptions to
* replace them by our own exception. In our own exception we
* provide the sourcecode position of the last characters that
* were read from the input.</li>
* <li> {@link EventBParseException}: This exception is thrown in
* two situations. On the one hand if the parser throws a
* {@link ParserException} we convert it into a
* {@link EventBParseException}. On the other hand it can be
* thrown if any error is found during the AST transformations
* after the parser has finished. We try to provide a token if a
* single token is involved in the error. Otherwise a
* {@link SourcecodeRange} is provided, which can be used to
* retrieve detailed position information from the
* {@link SourcePositions} (s. {@link #getSourcePositions()}).</li>
* <li> {@link CheckException}: If any problem occurs while
* performing semantic checks, a {@link CheckException} is
* thrown. We provide one or more nodes that are involved in the
* problem. For example, if we find dublicate machine clauses,
* we will list all occurances in the exception.</li>
* </ul>
*/
public Start parse(final String input, final boolean debugOutput) throws BException {
final Reader reader = new StringReader(input);
try {
/*
* Main parser
*/
final EventBLexer lexer = new EventBLexer(new PushbackReader(reader, 99));
lexer.setDebugOutput(debugOutput);
Parser parser = new Parser(lexer);
final Start rootNode = parser.parse();
final List<IToken> tokenList = ((ITokenListContainer) lexer).getTokenList();
/*
* Retrieving sourcecode positions which were found by ParserAspect
*/
final Map<PositionedNode, SourcecodeRange> positions = ((IParser) parser).getMapping();
sourcePositions = new SourcePositions(tokenList, positions);
parser = null;
return rootNode;
} catch (final LexerException e) {
/*
* Actually it's supposed to be a EventBLexerException because the
* aspect 'LexerAspect' replaces any LexerException to provide
* sourcecode position information in the BLexerException.
*/
throw new BException(e);
} catch (final ParserException e) {
throw new BException(createEventBParseException(e));
} catch (final EventBParseException e) {
throw new BException(e);
} catch (final IOException e) {
// shouldn't happen and if, we cannot handle it
throw new BException(e);
}
}
use of de.be4.classicalb.core.parser.node.Start in project probparsers by bendisposto.
the class AbstractTest method parseInput.
protected Start parseInput(final String input, final boolean debugOutput) throws BException {
if (debugOutput) {
System.out.println();
System.out.println();
}
final EventBParser parser = new EventBParser();
final Start rootNode = parser.parse(input, debugOutput);
return rootNode;
}
use of de.be4.classicalb.core.parser.node.Start in project probparsers by bendisposto.
the class CommentTest method testCommentVariables1.
@Test
public void testCommentVariables1() throws Exception {
final Start rootNode = parseInput("machine CommentVariables1 variables\n" + "varA" + "//varA comment\n" + " varB\n" + "varC\n" + "/*varC\ncomment*/" + "end", false);
final AMachineParseUnit parseUnit = (AMachineParseUnit) rootNode.getPParseUnit();
final LinkedList<PVariable> variables = parseUnit.getVariables();
assertEquals(3, variables.size());
AVariable variable = (AVariable) variables.get(0);
assertEquals("varA", variable.getName().getText());
assertEquals("varA comment", variable.getComments().get(0).getText());
variable = (AVariable) variables.get(1);
assertEquals("varB", variable.getName().getText());
assertEquals(0, variable.getComments().size());
variable = (AVariable) variables.get(2);
assertEquals("varC", variable.getName().getText());
final LinkedList<TComment> comments = variable.getComments();
assertNotNull(comments);
assertEquals(1, comments.size());
final StringTokenizer tokenizer = new StringTokenizer(comments.get(0).getText(), "\n\r");
assertEquals(2, tokenizer.countTokens());
assertEquals("varC", tokenizer.nextToken());
assertEquals("comment", tokenizer.nextToken());
}
use of de.be4.classicalb.core.parser.node.Start in project probparsers by bendisposto.
the class CommentTest method testCommentPredicates1.
@Test
public void testCommentPredicates1() throws Exception {
final Start rootNode = parseInput("machine CommentPredicates1 invariants\n @inv1 asdf //MyComment\nend", false);
final AMachineParseUnit parseUnit = (AMachineParseUnit) rootNode.getPParseUnit();
final LinkedList<PInvariant> invariants = parseUnit.getInvariants();
final AInvariant invariant = (AInvariant) invariants.get(0);
// correct comment content?
assertEquals("MyComment", invariant.getComments().get(0).getText());
}
Aggregations