1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 10:46:02 +02:00

Implementation of IASTNode.getSyntax(), bug 250251.

This commit is contained in:
Markus Schorn 2008-12-22 14:37:59 +00:00
parent edc37c6d03
commit 0f4e9a3759
5 changed files with 78 additions and 0 deletions

View file

@ -5650,6 +5650,58 @@ public class AST2Tests extends AST2BaseTest {
} catch (ExpansionOverlapsBoundaryException e) {}
}
// #define IF if
// #define IF_P if (
// #define IF_P_T if (1
// #define SEMI_IF ; if
// #define IF_COND if (1)
// void test() {
public void testSyntax_Bug250251() throws Exception {
String code= getAboveComment();
IASTTranslationUnit tu= parseAndCheckBindings(code + "if (1) {};}");
IASTFunctionDefinition f= getDeclaration(tu, 0);
IASTIfStatement x = getStatement(f, 0);
IToken syntax= x.getSyntax();
checkToken(syntax, "if", 0); syntax= syntax.getNext();
checkToken(syntax, "(", 3); syntax= syntax.getNext();
checkToken(syntax, "1", 4); syntax= syntax.getNext();
checkToken(syntax, ")", 5); syntax= syntax.getNext();
checkToken(syntax, "{", 7); syntax= syntax.getNext();
checkToken(syntax, "}", 8); syntax= syntax.getNext();
assertNull(syntax);
tu= parseAndCheckBindings(code + "if( 1) {}}");
f= getDeclaration(tu, 0); x= getStatement(f, 0);
syntax= x.getSyntax();
checkToken(syntax, "if", 0); syntax= syntax.getNext();
checkToken(syntax, "(", 2); syntax= syntax.getNext();
checkToken(syntax, "1", 5); syntax= syntax.getNext();
checkToken(syntax, ")", 6); syntax= syntax.getNext();
checkToken(syntax, "{", 8); syntax= syntax.getNext();
checkToken(syntax, "}", 9); syntax= syntax.getNext();
assertNull(syntax);
tu= parseAndCheckBindings(code + "IF(1) {}}");
f= getDeclaration(tu, 0); x= getStatement(f, 0);
syntax= x.getSyntax();
checkToken(syntax, "IF", 0); syntax= syntax.getNext();
checkToken(syntax, "(", 2); syntax= syntax.getNext();
checkToken(syntax, "1", 3); syntax= syntax.getNext();
checkToken(syntax, ")", 4); syntax= syntax.getNext();
checkToken(syntax, "{", 6); syntax= syntax.getNext();
checkToken(syntax, "}", 7); syntax= syntax.getNext();
assertNull(syntax);
tu= parseAndCheckBindings(code + "SEMI_IF (1) {}}");
f= getDeclaration(tu, 0); x= getStatement(f, 1);
try {
syntax= x.getSyntax();
fail();
} catch (ExpansionOverlapsBoundaryException e) {}
}
private void checkToken(IToken token, String image, int offset) {
assertEquals(image, token.getImage());
assertEquals(offset, token.getOffset());

View file

@ -185,6 +185,19 @@ public interface IASTNode {
*/
public IToken getTrailingSyntax() throws ExpansionOverlapsBoundaryException, UnsupportedOperationException;
/**
* Returns the tokens that make up this node. The tokens are obtained from the lexer,
* no preprocessing is performed.
* The offsets of the tokens are relative to the file-offset of the beginning of this node.
* <p> For examples see {@link #getLeadingSyntax()}.
* @return a chain of tokens or <code>null</code>, if there are none.
* @throws ExpansionOverlapsBoundaryException if one of the boundaries of the node is
* overlapped by a macro-expansion.
* @throws UnsupportedOperationException if invoked on preprocessor nodes, or nodes that are not
* part of a translation unit.
* @since 5.1
*/
public IToken getSyntax() throws ExpansionOverlapsBoundaryException;
/**
* Returns true if this node is frozen, false otherwise.

View file

@ -222,6 +222,10 @@ public abstract class ASTNode implements IASTNode {
return false;
}
public IToken getSyntax() throws ExpansionOverlapsBoundaryException {
return getSyntax(offset, offset+length, 0);
}
public IToken getLeadingSyntax() throws ExpansionOverlapsBoundaryException {
int left= getBoundary(-1);
return getSyntax(left, offset, -1);

View file

@ -84,6 +84,10 @@ public class ASTLiteralNode implements IASTNode {
public void setPropertyInParent(ASTNodeProperty property) {
}
public IToken getSyntax() {
throw new UnsupportedOperationException();
}
public IToken getLeadingSyntax() {
throw new UnsupportedOperationException();
}

View file

@ -195,6 +195,11 @@ public class PDOMASTAdapter {
return this;
}
public IToken getSyntax() throws ExpansionOverlapsBoundaryException,
UnsupportedOperationException {
return fDelegate.getSyntax();
}
public IToken getLeadingSyntax() throws ExpansionOverlapsBoundaryException,
UnsupportedOperationException {
return fDelegate.getLeadingSyntax();