1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-15 13:05:22 +02:00

moved utility classes into core util package

This commit is contained in:
Mike Kucera 2008-03-13 14:36:47 +00:00
parent 08bfbd1229
commit 6cb825d13f
29 changed files with 2035 additions and 2156 deletions

View file

@ -8,7 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.dom.lrparser.util;
package org.eclipse.cdt.core.parser.util;
import java.io.PrintStream;
@ -50,84 +50,105 @@ import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
/**
* A utility that prints an AST to the console, useful for debugging purposes.
*
*
* @author Mike Kucera
*/
@SuppressWarnings({"restriction","nls"})
class ASTPrinter {
@SuppressWarnings("nls")
public class ASTPrinter {
private static boolean PRINT_PARENT_PROPERTIES = false;
private static boolean RESOLVE_BINDINGS = false;
/**
* Prints the AST to the given PrintStream.
*
* @return Always returns false, boolean return type allows this method
* to be called from a conditional breakpoint during debugging.
*/
public static void printAST(IASTTranslationUnit root, PrintStream stream) {
PrintStream out = stream == null ? System.out : stream;
public static boolean print(IASTNode root, PrintStream out) {
if(root == null) {
out.println("null");
return;
return false;
}
PrintVisitor visitor = new PrintVisitor(out);
IASTPreprocessorStatement[] preStats = root.getAllPreprocessorStatements();
if(preStats != null) {
for(int i = 0; i < preStats.length; i++) {
print(out, 0, preStats[i]);
if(root instanceof IASTTranslationUnit) {
IASTPreprocessorStatement[] preStats = ((IASTTranslationUnit)root).getAllPreprocessorStatements();
if(preStats != null) {
for(IASTPreprocessorStatement stat : preStats)
print(out, 0, stat);
}
}
root.accept(visitor);
root.accept(new PrintVisitor(out));
IASTProblem[] problems = root.getPreprocessorProblems();
if(problems != null) {
for(int i = 0; i < problems.length; i++) {
print(out, 0, problems[i]);
}
}
IASTComment[] comments = root.getComments();
if(comments != null) {
for(int i = 0; i < comments.length; i++) {
print(out, 0, comments[i]);
if(root instanceof IASTTranslationUnit) {
IASTProblem[] problems = ((IASTTranslationUnit)root).getPreprocessorProblems();
if(problems != null) {
for(IASTProblem problem : problems)
print(out, 0, problem);
}
IASTComment[] comments = ((IASTTranslationUnit)root).getComments();
if(comments != null) {
for(IASTComment comment : comments)
print(out, 0, comment);
}
}
return false;
}
/**
* Prints the AST to stdout.
*
* @return Always returns false, boolean return type allows this method
* to be called from a conditional breakpoint during debugging.
*/
public static void printAST(IASTTranslationUnit root) {
printAST(root, null);
public static boolean print(IASTNode root) {
return print(root, System.out);
}
public static void printProblems(IASTTranslationUnit root, PrintStream stream) {
PrintStream out = stream == null ? System.out : stream;
/**
* Prints problem nodes in the AST to the given printstream.
*
* @return Always returns false, boolean return type allows this method
* to be called from a conditional breakpoint during debugging.
*/
public static boolean printProblems(IASTNode root, PrintStream out) {
if(root == null) {
out.println("null");
return;
return false;
}
ProblemVisitor visitor = new ProblemVisitor(out);
root.accept(visitor);
root.accept(new ProblemVisitor(out));
IASTProblem[] problems = root.getPreprocessorProblems();
if(problems != null) {
for(int i = 0; i < problems.length; i++) {
print(out, 0, problems[i]);
if(root instanceof IASTTranslationUnit) {
IASTProblem[] problems = ((IASTTranslationUnit)root).getPreprocessorProblems();
if(problems != null) {
for(IASTProblem problem : problems) {
print(out, 0, problem);
}
}
}
return false;
}
public static void printProblems(IASTTranslationUnit root) {
printProblems(root, System.out);
/**
* Prints problem nodes in the AST to stdout.
*
* @return Always returns false, boolean return type allows this method
* to be called from a conditional breakpoint during debugging.
*/
public static boolean printProblems(IASTNode root) {
return printProblems(root, System.out);
}
private static void print(PrintStream out, int indentLevel, Object n) {
for(int i = 0; i < indentLevel; i++)
out.print(" ");
@ -142,7 +163,7 @@ class ASTPrinter {
if(n instanceof ASTNode) {
ASTNode node = (ASTNode) n;
out.print(" (" + node.getOffset() + "," + node.getLength() + ") "); //$NON-NLS-2$ //$NON-NLS-3$
out.print(" (" + node.getOffset() + "," + node.getLength() + ") ");
if(node.getParent() == null && !(node instanceof IASTTranslationUnit)) {
out.print("PARENT IS NULL ");
}

View file

@ -19,12 +19,31 @@ import java.util.TreeMap;
/**
* A facade for a Map that allows char[] slices to be used as keys.
* Provides functionality similar to a Map, with the feature that char arrays
* and sections of char arrays (known as slices) may be used as keys.
*
* This class is useful because small pieces of an existing large char[] buffer
* can be directly used as map keys. This avoids the need to create many String
* objects as would normally be needed as keys in a standard java.util.Map.
* Thus performance is improved in the CDT core.
*
* Most methods are overloaded with two versions, one that uses a
* section of a char[] as the key (a slice), and one that uses
* the entire char[] as the key.
*
* This class is intended as a replacement for CharArrayObjectMap.
*
* ex:
* char[] key = "one two three".toCharArray();
* map.put(key, 4, 3, new Integer(99));
* map.get(key, 4, 3); // returns 99
* map.get("two".toCharArray()); // returns 99
*
* @see ICharArrayMap for API docs
* @author Mike Kucera
*
* @param <V>
*/
public final class CharArrayMap<V> implements ICharArrayMap<V> {
public final class CharArrayMap<V> {
/**
* Wrapper class used as keys in the map. The purpose
@ -82,10 +101,11 @@ public final class CharArrayMap<V> implements ICharArrayMap<V> {
return result;
}
@Override
@SuppressWarnings("nls")
@Override
public String toString() {
String slice = new String(buffer, start, length);
return "'" + slice + "'@(" + start + "," + length + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
return "'" + slice + "'@(" + start + "," + length + ")";
}
@ -152,60 +172,103 @@ public final class CharArrayMap<V> implements ICharArrayMap<V> {
map = new HashMap<Key,V>(initialCapacity);
}
/**
* Creates a new mapping in this map, uses the given array slice as the key.
* If the map previously contained a mapping for this key, the old value is replaced.
* @throws NullPointerException if chars is null
* @throws IndexOutOfBoundsException if the boundaries specified by start and length are out of range
*/
public void put(char[] chars, int start, int length, V value) {
checkBoundaries(chars, start, length);
map.put(new Key(chars, start, length), value);
}
/**
* Creates a new mapping in this map, uses all of the given array as the key.
* If the map previously contained a mapping for this key, the old value is replaced.
* @throws NullPointerException if chars is null
*/
public void put(char[] chars, V value) {
map.put(new Key(chars), value);
}
/**
* Returns the value to which the specified array slice is mapped in this map,
* or null if the map contains no mapping for this key.
* @throws NullPointerException if chars is null
* @throws IndexOutOfBoundsException if the boundaries specified by start and length are out of range
*/
public V get(char[] chars, int start, int length) {
checkBoundaries(chars, start, length);
return map.get(new Key(chars, start, length));
}
/**
* Returns the value to which the specified array is mapped in this map,
* or null if the map contains no mapping for this key.
* @throws NullPointerException if chars is null
*/
public V get(char[] chars) {
return map.get(new Key(chars));
}
/**
* Removes the mapping for the given array slice if present.
* Returns the value object that corresponded to the key
* or null if the key was not in the map.
* @throws NullPointerException if chars is null
* @throws IndexOutOfBoundsException if the boundaries specified by start and length are out of range
*/
public V remove(char[] chars, int start, int length) {
checkBoundaries(chars, start, length);
return map.remove(new Key(chars, start, length));
}
/**
* Removes the mapping for the given array if present.
* Returns the value object that corresponded to the key
* or null if the key was not in the map.
* @throws NullPointerException if chars is null
*/
public V remove(char[] chars) {
return map.remove(new Key(chars));
}
/**
* Returns true if the given key has a value associated with it in the map.
* @throws NullPointerException if chars is null
* @throws IndexOutOfBoundsException if the boundaries specified by start and length are out of range
*/
public boolean containsKey(char[] chars, int start, int length) {
checkBoundaries(chars, start, length);
return map.containsKey(new Key(chars, start, length));
}
/**
* Returns true if the given key has a value associated with it in the map.
* @throws NullPointerException if chars is null
*/
public boolean containsKey(char[] chars) {
return map.containsKey(new Key(chars));
}
/**
* Returns true if the given value is contained in the map.
*/
public boolean containsValue(V value) {
return map.containsValue(value);
}
/**
* Use this in a foreach loop.
*/
public Collection<V> values() {
return map.values();
}
/**
* Returns the keys stored in the map.
*/
public Collection<char[]> keys() {
Set<Key> keys= map.keySet();
ArrayList<char[]> r= new ArrayList<char[]>(keys.size());
@ -215,17 +278,23 @@ public final class CharArrayMap<V> implements ICharArrayMap<V> {
return r;
}
/**
* Removes all mappings from the map.
*/
public void clear() {
map.clear();
}
/**
* Returns the number of mappings.
*/
public int size() {
return map.size();
}
/**
* Returns true if the map is empty.
*/
public boolean isEmpty() {
return map.isEmpty();
}

View file

@ -8,14 +8,12 @@
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.dom.lrparser.util;
package org.eclipse.cdt.core.parser.util;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import lpg.lpgjavaruntime.IToken;
/**
* Useful utility methods for dealing with Collections.
@ -56,6 +54,10 @@ public final class CollectionUtils {
/**
* Allows a foreach loop to iterate backwards over a list
* from the end to the start.
*
* eg)
* for(Object o : reverseIterable(list)) { ... }
*
* @throws NullPointerException if list is null
*/
public static <T> Iterable<T> reverseIterable(final List<T> list) {
@ -69,9 +71,9 @@ public final class CollectionUtils {
*
* This is useful for using an iterator in a foreach loop directly.
*
* ex)
* eg)
*
* foreach(Object o : iterable(list.listIterator())) {
* for(Object o : iterable(list.listIterator())) {
* // do something
* }
*
@ -89,22 +91,6 @@ public final class CollectionUtils {
}
/**
* Allows simple pattern match testing of lists of tokens.
*
* @throws NullPointerException if source or pattern is null
*/
public static boolean matchTokens(List<IToken> source, Integer ... pattern) {
if(source.size() != pattern.length) // throws NPE if either parameter is null
return false;
for(int i = 0, n = pattern.length; i < n; i++) {
if(source.get(i).getKind() != pattern[i].intValue())
return false;
}
return true;
}
/**
* Finds the first object in the heterogeneous list that is an instance of
@ -116,12 +102,12 @@ public final class CollectionUtils {
* @throws UnsupportedOperationException if the list's Iterator does not support the remove() method
*/
@SuppressWarnings("unchecked")
public static <T> T findFirstAndRemove(List<Object> list, Class<T> clazz) {
for(Iterator<Object> iter = list.iterator(); iter.hasNext();) {
public static <T> T findFirstAndRemove(List<?> list, Class<T> clazz) {
for(Iterator<?> iter = list.iterator(); iter.hasNext();) {
Object o = iter.next();
if(clazz.isInstance(o)) {
iter.remove();
return (T) o;
return (T) o; // safe
}
}
return null;

View file

@ -8,7 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.dom.lrparser.util;
package org.eclipse.cdt.core.parser.util;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
@ -58,19 +58,4 @@ public class DebugUtil {
System.out.println(message);
}
/**
* Prints a textual representation of the AST to stdout.
*/
public static void printAST(IASTTranslationUnit tu) {
ASTPrinter.printAST(tu);
}
/**
* Throws an AssertionError if any of the bindings in
* the given AST cannot be resolved.
*/
public static void assertBindings(IASTTranslationUnit tu) throws AssertionError {
tu.accept(BindingCheckVisitor.VISITOR);
}
}

View file

@ -1,120 +0,0 @@
package org.eclipse.cdt.core.parser.util;
import java.util.Collection;
/**
* Provides an interface similar to Map, with the feature that char arrays
* and sections of char arrays (known as slices) may be used as keys.
*
* This interface is useful because small pieces of an existing large char[] buffer
* can be directly used as map keys. This avoids the need to create many String
* objects as would normally be needed as keys in a standard java.util.Map.
* Thus performance is improved in the CDT core.
*
* Most methods are overloaded with two versions, one that uses a
* section of a char[] as the key (a slice), and one that uses
* the entire char[] as the key.
*
* ex:
* char[] key = "one two three".toCharArray();
* map.put(key, 4, 3, new Integer(99));
* map.get(key, 4, 3); // returns 99
* map.get("two".toCharArray()); // returns 99
*
* @author Mike Kucera
*
* @param <V>
*/
public interface ICharArrayMap<V> {
/**
* Creates a new mapping in this map, uses the given array slice as the key.
* If the map previously contained a mapping for this key, the old value is replaced.
* @throws NullPointerException if chars is null
* @throws IndexOutOfBoundsException if the boundaries specified by start and length are out of range
*/
void put(char[] chars, int start, int length, V value);
/**
* Creates a new mapping in this map, uses all of the given array as the key.
* If the map previously contained a mapping for this key, the old value is replaced.
* @throws NullPointerException if chars is null
*/
void put(char[] chars, V value);
/**
* Returns the value to which the specified array slice is mapped in this map,
* or null if the map contains no mapping for this key.
* @throws NullPointerException if chars is null
* @throws IndexOutOfBoundsException if the boundaries specified by start and length are out of range
*/
V get(char[] chars, int start, int length);
/**
* Returns the value to which the specified array is mapped in this map,
* or null if the map contains no mapping for this key.
* @throws NullPointerException if chars is null
*/
V get(char[] chars);
/**
* Removes the mapping for the given array slice if present.
* Returns the value object that corresponded to the key
* or null if the key was not in the map.
* @throws NullPointerException if chars is null
* @throws IndexOutOfBoundsException if the boundaries specified by start and length are out of range
*/
V remove(char[] chars, int start, int length);
/**
* Removes the mapping for the given array if present.
* Returns the value object that corresponded to the key
* or null if the key was not in the map.
* @throws NullPointerException if chars is null
*/
V remove(char[] chars);
/**
* Returns true if the given key has a value associated with it in the map.
* @throws NullPointerException if chars is null
* @throws IndexOutOfBoundsException if the boundaries specified by start and length are out of range
*/
boolean containsKey(char[] chars, int start, int length);
/**
* Returns true if the given key has a value associated with it in the map.
* @throws NullPointerException if chars is null
*/
boolean containsKey(char[] chars);
/**
* Returns true if the given value is contained in the map.
*/
boolean containsValue(V value);
/**
* Use this in a foreach loop.
*/
Collection<V> values();
/**
* Returns the keys stored in the map.
*/
Collection<char[]> keys();
/**
* Removes all mappings from the map.
*/
void clear();
/**
* Returns the number of mappings.
*/
int size();
/**
* Returns true if the map is empty.
*/
boolean isEmpty();
}

View file

@ -10,7 +10,7 @@
* Markus Schorn (Wind River Systems)
* Bryan Wilkinson (QNX) - https://bugs.eclipse.org/bugs/show_bug.cgi?id=151207
* Ed Swartz (Nokia)
* Mike Kucera (IBM) - bug #206952
* Mike Kucera (IBM)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;
@ -139,7 +139,9 @@ import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.core.parser.ITokenDuple;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.util.ASTPrinter;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.DebugUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser;
@ -162,11 +164,7 @@ import org.eclipse.cdt.internal.core.parser.token.TokenFactory;
*/
public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
private static final String CONST_CAST = "const_cast"; //$NON-NLS-1$
private static final String REINTERPRET_CAST = "reinterpret_cast"; //$NON-NLS-1$
private static final String STATIC_CAST = "static_cast"; //$NON-NLS-1$
private static final String DYNAMIC_CAST = "dynamic_cast"; //$NON-NLS-1$
private static final int DEFAULT_CATCH_HANDLER_LIST_SIZE = 4;
@ -216,69 +214,65 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
/**
* Consumes template parameters.
* Identifies the first and last tokens that make up the template parameter list.
* Used as part of parsing an idExpression().
*
* @param previousLast
* Previous "last" token (returned if nothing was consumed)
* @return Last consumed token, or <code>previousLast</code> if nothing
* was consumed
* @throws BacktrackException
* request a backtrack
* @param previousLast Previous "last" token (returned if nothing was consumed)
* @return Last consumed token, or <code>previousLast</code> if nothing was consumed
* @throws BacktrackException request a backtrack
*/
protected IToken consumeTemplateParameters(IToken previousLast)
throws EndOfFileException, BacktrackException {
int startingOffset = previousLast == null ? LA(1).getOffset()
: previousLast.getOffset();
IToken last = previousLast;
protected IToken consumeTemplateParameters(IToken previousLast) throws EndOfFileException, BacktrackException {
int startingOffset = previousLast == null ? LA(1).getOffset() : previousLast.getOffset();
IToken last = previousLast; // if there are no parameters then previousLast gets returned
if (LT(1) == IToken.tLT) {
last = consume();
// until we get all the names sorted out
ScopeStack scopes = new ScopeStack();
// used to match brackets, parens and angle brackets
// capable of recognizing cases like T<(a>b)> correctly
ScopeStack scopes = new ScopeStack();
scopes.push(IToken.tLT);
while (scopes.size() > 0) {
while(scopes.size() > 0) {
int top;
last = consume();
switch (last.getType()) {
case IToken.tGT:
if (scopes.peek() == IToken.tLT) {
scopes.pop();
}
break;
case IToken.tRBRACKET:
do {
top = scopes.pop();
} while (scopes.size() > 0
&& (top == IToken.tGT || top == IToken.tLT));
if (top != IToken.tLBRACKET)
throwBacktrack(startingOffset, last.getEndOffset()
- startingOffset);
break;
case IToken.tRPAREN:
do {
top = scopes.pop();
} while (scopes.size() > 0
&& (top == IToken.tGT || top == IToken.tLT));
if (top != IToken.tLPAREN)
throwBacktrack(startingOffset, last.getEndOffset()
- startingOffset);
break;
case IToken.tLT:
case IToken.tLBRACKET:
case IToken.tLPAREN:
scopes.push(last.getType());
break;
switch(last.getType()) {
case IToken.tGT: // '>'
if(scopes.peek() == IToken.tLT) {
scopes.pop();
}
break;
case IToken.tRBRACKET: // ']'
do {
top = scopes.pop();
} while (scopes.size() > 0 && top == IToken.tLT);
if(top != IToken.tLBRACKET)
throwBacktrack(startingOffset, last.getEndOffset() - startingOffset);
break;
case IToken.tRPAREN: // ')'
do {
top = scopes.pop();
} while (scopes.size() > 0 && top == IToken.tLT);
if(top != IToken.tLPAREN)
throwBacktrack(startingOffset, last.getEndOffset() - startingOffset);
break;
case IToken.tLT: // '<'
case IToken.tLBRACKET: // '['
case IToken.tLPAREN: // '('
scopes.push(last.getType());
break;
}
}
}
return last;
}
protected List<IASTNode> templateArgumentList() throws EndOfFileException,
BacktrackException {
protected List<IASTNode> templateArgumentList() throws EndOfFileException, BacktrackException {
IToken start = LA(1);
int startingOffset = start.getOffset();
int endOffset = 0;
@ -334,11 +328,11 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
/**
* Parse a name. name : ("::")? name2 ("::" name2)* name2 : IDENTIFER :
* template-id
* Parse a name.
* name ::= ("::")? name2 ("::" name2)*
* name2 ::= IDENTIFER | template-id
*
* @throws BacktrackException
* request a backtrack
* @throws BacktrackException request a backtrack
*/
protected ITokenDuple name() throws BacktrackException, EndOfFileException {
@ -420,14 +414,8 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
/**
* @param last
* @param argumentList
* @return
* @throws EndOfFileException
* @throws BacktrackException
*/
protected IToken consumeTemplateArguments(IToken last,
TemplateParameterManager argumentList) throws EndOfFileException, BacktrackException {
protected IToken consumeTemplateArguments(IToken last, TemplateParameterManager argumentList) throws EndOfFileException, BacktrackException {
if (LT(1) == IToken.tLT) {
IToken secondMark = mark();
consume();
@ -456,11 +444,10 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
return last;
}
protected IASTName operatorId(IToken originalToken,
TemplateParameterManager templateArgs) throws BacktrackException,
EndOfFileException {
protected IASTName operatorId(IToken originalToken, TemplateParameterManager templateArgs) throws BacktrackException, EndOfFileException {
// we know this is an operator
IToken operatorToken = consume();
IToken toSend = null;
IASTTypeId typeId = null;
@ -978,8 +965,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
return null;
}
if (LT(1) != IToken.tEOC)
declarator = declarator(SimpleDeclarationStrategy.TRY_FUNCTION,
forNewExpression);
declarator = declarator(SimpleDeclarationStrategy.TRY_FUNCTION, forNewExpression);
} catch (BacktrackException bt) {
return null;
/*
@ -1327,8 +1313,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @throws BacktrackException
*/
@Override
protected IASTExpression unaryExpression() throws EndOfFileException,
BacktrackException {
protected IASTExpression unaryExpression() throws EndOfFileException, BacktrackException {
switch (LT(1)) {
case IToken.tSTAR:
return unaryOperatorCastExpression(IASTUnaryExpression.op_star);// IASTExpression.Kind.UNARY_STAR_CASTEXPRESSION);
@ -1380,8 +1365,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @param expression
* @throws BacktrackException
*/
protected IASTExpression postfixExpression() throws EndOfFileException,
BacktrackException {
protected IASTExpression postfixExpression() throws EndOfFileException, BacktrackException {
IASTExpression firstExpression = null;
boolean isTemplate = false;
@ -1796,8 +1780,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
return new CPPASTIdExpression();
}
protected IASTName idExpression() throws EndOfFileException,
BacktrackException {
protected IASTName idExpression() throws EndOfFileException, BacktrackException {
IASTName name = null;
try {
name = createName(name());
@ -1818,8 +1801,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
name = operatorId(start, null);
else {
backup(mark);
throwBacktrack(start.getOffset(), (end != null ? end.getEndOffset() : start.getEndOffset())
- start.getOffset());
throwBacktrack(start.getOffset(), end == null ? start.getLength() : end.getEndOffset());
}
} else if (LT(1) == IToken.t_operator)
name = operatorId(null, null);
@ -1828,8 +1810,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
protected IASTExpression specialCastExpression(int kind)
throws EndOfFileException, BacktrackException {
protected IASTExpression specialCastExpression(int kind) throws EndOfFileException, BacktrackException {
int startingOffset = LA(1).getOffset();
IToken op = consume();
consume(IToken.tLT);
@ -1844,18 +1825,26 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
result.setTypeId(typeID);
result.setOperand(lhs);
if (op.toString().equals(DYNAMIC_CAST)) {
result.setOperator(ICPPASTCastExpression.op_dynamic_cast);
} else if (op.toString().equals(STATIC_CAST)) {
result.setOperator(ICPPASTCastExpression.op_static_cast);
} else if (op.toString().equals(REINTERPRET_CAST)) {
result.setOperator(ICPPASTCastExpression.op_reinterpret_cast);
} else if (op.toString().equals(CONST_CAST)) {
result.setOperator(ICPPASTCastExpression.op_const_cast);
} else {
result.setOperator(IASTCastExpression.op_cast);
int operator;
switch(op.getType()) {
case IToken.t_dynamic_cast:
operator = ICPPASTCastExpression.op_dynamic_cast;
break;
case IToken.t_static_cast:
operator = ICPPASTCastExpression.op_static_cast;
break;
case IToken.t_reinterpret_cast:
operator = ICPPASTCastExpression.op_reinterpret_cast;
break;
case IToken.t_const_cast:
operator = ICPPASTCastExpression.op_const_cast;
break;
default:
operator = IASTCastExpression.op_cast;
break;
}
result.setOperator(operator);
return result;
}
@ -2352,8 +2341,8 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
}
protected IASTDeclaration simpleDeclarationStrategyUnion()
throws EndOfFileException, BacktrackException {
protected IASTDeclaration simpleDeclarationStrategyUnion() throws EndOfFileException, BacktrackException {
IToken simpleDeclarationMark = mark();
IASTDeclaration d1 = null, d2 = null;
IToken after = null;
@ -2376,8 +2365,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
return d1;
if (d1 instanceof IASTSimpleDeclaration) {
IASTSimpleDeclaration sd = (IASTSimpleDeclaration) d1;
if( sd.getDeclSpecifier() instanceof ICPPASTDeclSpecifier &&
((ICPPASTDeclSpecifier)sd.getDeclSpecifier()).isFriend() )
if( sd.getDeclSpecifier() instanceof ICPPASTDeclSpecifier && ((ICPPASTDeclSpecifier)sd.getDeclSpecifier()).isFriend() )
return d1;
if (sd.getDeclarators().length != 1)
return d1;
@ -2423,7 +2411,6 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
result.addDeclaration(d1);
result.addDeclaration(d2);
return result;
}
protected IASTAmbiguousDeclaration createAmbiguousDeclaration() {
@ -2442,9 +2429,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @throws BacktrackException
* request a backtrack
*/
protected IASTDeclaration namespaceDefinitionOrAlias()
throws BacktrackException, EndOfFileException {
protected IASTDeclaration namespaceDefinitionOrAlias() throws BacktrackException, EndOfFileException {
IToken first = consume();
int last = first.getEndOffset();
IASTName name = null;
@ -4215,8 +4200,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
consume(IToken.tELLIPSIS);
isEllipsis = true;
} else {
decl = simpleDeclaration(
SimpleDeclarationStrategy.TRY_VARIABLE, true);
decl = simpleDeclaration(SimpleDeclarationStrategy.TRY_VARIABLE, true);
}
if (LT(1) != IToken.tEOC)
consume(IToken.tRPAREN);

View file

@ -105,7 +105,6 @@ $Globals
/.
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99ASTNodeFactory;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99BuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99TypedefTrackerParserAction;
./
$End

View file

@ -55,7 +55,6 @@ $Globals
import org.eclipse.cdt.core.dom.ast.*;
import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
./
$End

View file

@ -139,7 +139,6 @@ $Globals
import org.eclipse.cdt.core.dom.lrparser.action.cpp.CPPBuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
./
$End
@ -1344,7 +1343,7 @@ type_specifier_seq
abstract_declarator
::= direct_abstract_declarator
::=? direct_abstract_declarator
| <openscope-ast> ptr_operator_seq
/. $Build consumeDeclaratorWithPointer(false); $EndBuild ./
| <openscope-ast> ptr_operator_seq direct_abstract_declarator
@ -1352,8 +1351,10 @@ abstract_declarator
direct_abstract_declarator
::= basic_direct_abstract_declarator
| array_direct_abstract_declarator
::=? basic_direct_abstract_declarator
direct_abstract_declarator
::= array_direct_abstract_declarator
| function_direct_abstract_declarator
@ -1773,7 +1774,7 @@ handler
exception_declaration
::= type_specifier_seq <openscope-ast> declarator
/. $Build consumeDeclarationSimple(true); $EndBuild ./
| type_specifier_seq <openscope-ast> abstract_declarator
| type_specifier_seq <openscope-ast> abstract_declarator -- TODO might need to be abstract_declarator_without_function, might be too lenient, what exactly can you catch?
/. $Build consumeDeclarationSimple(true); $EndBuild ./
| type_specifier_seq
/. $Build consumeDeclarationSimple(false); $EndBuild ./

View file

@ -16,7 +16,6 @@ import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.dom.parser.IScannerExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.c.GCCScannerExtensionConfiguration;
import org.eclipse.cdt.core.index.IIndex;
@ -28,6 +27,8 @@ import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.util.ASTPrinter;
import org.eclipse.cdt.core.parser.util.DebugUtil;
import org.eclipse.cdt.internal.core.dom.parser.c.CASTTranslationUnit;
import org.eclipse.cdt.internal.core.parser.scanner.CPreprocessor;
import org.eclipse.cdt.internal.core.pdom.dom.IPDOMLinkageFactory;
@ -110,7 +111,7 @@ public abstract class BaseExtensibleLanguage extends AbstractLanguage implements
System.out.println("Parsing");
System.out.println("Options: " + options);
System.out.println("GPP AST:");
DebugUtil.printAST(gtu);
ASTPrinter.print(gtu);
System.out.println();
}
@ -131,7 +132,7 @@ public abstract class BaseExtensibleLanguage extends AbstractLanguage implements
if(DEBUG_PRINT_AST) {
System.out.println("Base Extensible Language AST:");
DebugUtil.printAST(tu);
ASTPrinter.print(tu);
}
return tu;
@ -158,7 +159,7 @@ public abstract class BaseExtensibleLanguage extends AbstractLanguage implements
System.out.println();
System.out.println("********************************************************");
System.out.println("GPP AST:");
DebugUtil.printAST(cn.getTranslationUnit());
ASTPrinter.print(cn.getTranslationUnit());
System.out.println();
}
@ -179,7 +180,7 @@ public abstract class BaseExtensibleLanguage extends AbstractLanguage implements
if(DEBUG_PRINT_AST) {
System.out.println("Base Extensible Language AST:");
DebugUtil.printAST(tu);
ASTPrinter.print(tu);
System.out.println();
System.out.println("Completion Node: " + completionNode);
}

View file

@ -57,7 +57,6 @@ import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTProblemExpression;
import org.eclipse.cdt.core.dom.ast.IASTProblemHolder;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
@ -69,10 +68,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import static org.eclipse.cdt.core.dom.lrparser.util.CollectionUtils.matchTokens;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.internal.core.dom.lrparser.c99.C99Parsersym;
import org.eclipse.cdt.internal.core.dom.lrparser.c99.C99SizeofExpressionParser;
import org.eclipse.cdt.core.parser.util.DebugUtil;
import org.eclipse.cdt.internal.core.dom.lrparser.cpp.CPPParsersym;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
@ -280,6 +276,23 @@ public abstract class BuildASTParserAction {
}
/**
* Allows simple pattern match testing of lists of tokens.
*
* @throws NullPointerException if source or pattern is null
*/
public static boolean matchTokens(List<IToken> source, Integer ... pattern) {
if(source.size() != pattern.length) // throws NPE if either parameter is null
return false;
for(int i = 0, n = pattern.length; i < n; i++) {
if(source.get(i).getKind() != pattern[i].intValue())
return false;
}
return true;
}
/*************************************************************************************************************
* Start of actions.
************************************************************************************************************/

View file

@ -11,7 +11,7 @@
package org.eclipse.cdt.core.dom.lrparser.action;
import static org.eclipse.cdt.core.dom.lrparser.util.CollectionUtils.reverseIterable;
import static org.eclipse.cdt.core.parser.util.CollectionUtils.reverseIterable;
import java.util.LinkedList;
import java.util.List;
@ -169,7 +169,7 @@ public class ScopedStack<T> {
printScope(topScope);
System.out.println(separator);
for(LinkedList<T> list : reverseIterable(scopeStack)) {
for(List<T> list : reverseIterable(scopeStack)) {
printScope(list);
}

View file

@ -77,8 +77,8 @@ import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.action.BuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.TokenMap;
import org.eclipse.cdt.core.dom.lrparser.util.CollectionUtils;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.parser.util.CollectionUtils;
import org.eclipse.cdt.core.parser.util.DebugUtil;
import org.eclipse.cdt.internal.core.dom.lrparser.c99.C99ExpressionStatementParser;
import org.eclipse.cdt.internal.core.dom.lrparser.c99.C99NoCastExpressionParser;
import org.eclipse.cdt.internal.core.dom.lrparser.c99.C99Parsersym;

View file

@ -13,7 +13,7 @@ package org.eclipse.cdt.core.dom.lrparser.action.c99;
import static org.eclipse.cdt.core.dom.lrparser.action.c99.CNamespace.GOTO_LABEL;
import static org.eclipse.cdt.core.dom.lrparser.action.c99.CNamespace.IDENTIFIER;
import static org.eclipse.cdt.core.dom.lrparser.action.c99.CNamespace.STRUCT_TAG;
import static org.eclipse.cdt.core.dom.lrparser.util.CollectionUtils.reverseIterable;
import static org.eclipse.cdt.core.parser.util.CollectionUtils.reverseIterable;
import java.util.LinkedList;
import java.util.List;
@ -36,7 +36,7 @@ import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.action.ScopedStack;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.parser.util.DebugUtil;
import org.eclipse.cdt.internal.core.dom.lrparser.c99.C99Parsersym;
import org.eclipse.cdt.internal.core.dom.lrparser.c99.bindings.C99ArrayType;
import org.eclipse.cdt.internal.core.dom.lrparser.c99.bindings.C99BasicType;

View file

@ -15,7 +15,7 @@ import java.util.LinkedList;
import lpg.lpgjavaruntime.IToken;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.parser.util.DebugUtil;
/**
* A simple set of trial and undo actions that just keep track
* of typedef names. This information is then fed back to the parser

View file

@ -10,7 +10,7 @@
*******************************************************************************/
package org.eclipse.cdt.core.dom.lrparser.action.cpp;
import static org.eclipse.cdt.core.dom.lrparser.util.CollectionUtils.*;
import static org.eclipse.cdt.core.parser.util.CollectionUtils.*;
import static org.eclipse.cdt.internal.core.dom.lrparser.cpp.CPPParsersym.*;
@ -87,8 +87,8 @@ import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.LPGTokenAdapter;
import org.eclipse.cdt.core.dom.lrparser.action.BuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.util.CollectionUtils;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.parser.util.CollectionUtils;
import org.eclipse.cdt.core.parser.util.DebugUtil;
import org.eclipse.cdt.internal.core.dom.lrparser.c99.C99ExpressionStatementParser;
import org.eclipse.cdt.internal.core.dom.lrparser.c99.C99Parsersym;
import org.eclipse.cdt.internal.core.dom.lrparser.cpp.CPPExpressionStatementParser;

View file

@ -1,49 +0,0 @@
/*******************************************************************************
* Copyright (c) 2006, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.dom.lrparser.util;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.c.CASTVisitor;
/**
* An AST visitor that asserts that all bindings have been resolved.
*
* @author Mike Kucera
*/
class BindingCheckVisitor extends CASTVisitor {
public static ASTVisitor VISITOR = new BindingCheckVisitor();
private BindingCheckVisitor() {
shouldVisitNames = true;
shouldVisitDeclarations = true;
shouldVisitInitializers = true;
shouldVisitParameterDeclarations = true;
shouldVisitDeclarators = true;
shouldVisitDeclSpecifiers = true;
shouldVisitExpressions = true;
shouldVisitStatements = true;
shouldVisitTypeIds = true;
shouldVisitEnumerators = true;
shouldVisitTranslationUnit = true;
shouldVisitProblems = false;
}
@Override
public int visit(IASTName name) {
if(name.getBinding() == null)
throw new AssertionError("Binding did not get pre-resolved: '" + name + "'"); //$NON-NLS-1$ //$NON-NLS-2$
return PROCESS_CONTINUE;
}
}

View file

@ -19,14 +19,12 @@ import java.util.*;
import org.eclipse.cdt.core.dom.ast.*;
import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.TokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99ASTNodeFactory;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99BuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99TypedefTrackerParserAction;
public class C99ExpressionStatementParser extends PrsStream implements RuleAction , IParserActionTokenProvider, IParser
{

View file

@ -19,14 +19,12 @@ import java.util.*;
import org.eclipse.cdt.core.dom.ast.*;
import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.TokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99ASTNodeFactory;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99BuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99TypedefTrackerParserAction;
public class C99NoCastExpressionParser extends PrsStream implements RuleAction , IParserActionTokenProvider, IParser
{

View file

@ -19,14 +19,12 @@ import java.util.*;
import org.eclipse.cdt.core.dom.ast.*;
import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.TokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99ASTNodeFactory;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99BuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99TypedefTrackerParserAction;
public class C99Parser extends PrsStream implements RuleAction , IParserActionTokenProvider, IParser
{

View file

@ -19,14 +19,12 @@ import java.util.*;
import org.eclipse.cdt.core.dom.ast.*;
import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.TokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99ASTNodeFactory;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99BuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99TypedefTrackerParserAction;
public class C99SizeofExpressionParser extends PrsStream implements RuleAction , IParserActionTokenProvider, IParser
{

View file

@ -23,7 +23,6 @@ import org.eclipse.cdt.core.dom.lrparser.action.cpp.CPPASTNodeFactory;
import org.eclipse.cdt.core.dom.lrparser.action.cpp.CPPBuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.TokenMap;

View file

@ -23,7 +23,6 @@ import org.eclipse.cdt.core.dom.lrparser.action.cpp.CPPASTNodeFactory;
import org.eclipse.cdt.core.dom.lrparser.action.cpp.CPPBuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.TokenMap;

View file

@ -23,7 +23,6 @@ import org.eclipse.cdt.core.dom.lrparser.action.cpp.CPPASTNodeFactory;
import org.eclipse.cdt.core.dom.lrparser.action.cpp.CPPBuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.TokenMap;

View file

@ -23,7 +23,6 @@ import org.eclipse.cdt.core.dom.lrparser.action.cpp.CPPASTNodeFactory;
import org.eclipse.cdt.core.dom.lrparser.action.cpp.CPPBuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.IParser;
import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.TokenMap;