1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Create new interface and support for calculating lineNumber/offset mapping.

Updated IASTClassSpecifier for qualified name query.  
Began structuring expressions and declarators in Parser for ISourceElementRequestor.  
Updated other packages to use new interfaces.
Updated automatedtests/torture test to use new line number information.
This commit is contained in:
John Camelon 2003-06-25 22:47:52 +00:00
parent 2fd4306837
commit 6a7293f951
35 changed files with 534 additions and 411 deletions

View file

@ -1,3 +1,10 @@
2003-06-25 John Camelon
Create new interface and support for calculating lineNumber/offset mapping.
Updated IASTClassSpecifier for qualified name query.
Began structuring expressions and declarators in Parser for ISourceElementRequestor.
Updated other packages to use new interfaces.
Updated automatedtests/torture test to use new line number information.
2003-06-24 John Camelon
Updates for ISourceElementRequestor - elaborated types & enumerations.

View file

@ -21,6 +21,7 @@ import java.util.StringTokenizer;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import org.eclipse.cdt.core.parser.ILineOffsetReconciler;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserMode;
@ -47,6 +48,7 @@ public class AutomatedTest extends AutomatedFramework {
File file = null;
IParser parser = null;
ILineOffsetReconciler mapping = null;
try{
file = (File)fileList.removeFirst();
@ -55,7 +57,8 @@ public class AutomatedTest extends AutomatedFramework {
String filePath = file.getCanonicalPath();
parser = ParserFactory.createParser( ParserFactory.createScanner( new InputStreamReader (stream), filePath, null, null, ParserMode.QUICK_PARSE ), nullCallback, ParserMode.QUICK_PARSE);
parser.setCppNature( ((String)natures.get( filePath )).equalsIgnoreCase("cpp") );
parser.mapLineNumbers(true);
mapping = ParserFactory.createLineOffsetReconciler( new InputStreamReader( stream ) );
assertTrue( parser.parse() );
}
@ -64,10 +67,10 @@ public class AutomatedTest extends AutomatedFramework {
String output = null;
if( e instanceof AssertionFailedError ){
output = file.getCanonicalPath() + ": Parse failed on line ";
output += parser.getLineNumberForOffset(parser.getLastErrorOffset()) + "\n";
output += mapping.getLineNumberForOffset(parser.getLastErrorOffset()) + "\n";
} else {
output = file.getCanonicalPath() + ": " + e.getClass().toString();
output += " on line " + parser.getLineNumberForOffset(parser.getLastErrorOffset()) + "\n";
output += " on line " + mapping.getLineNumberForOffset(parser.getLastErrorOffset()) + "\n";
}
if( report != null ){
report.write( output.getBytes() );

View file

@ -240,7 +240,6 @@ public class FractionalAutomatedTest extends AutomatedFramework {
IParser parser = ParserFactory.createParser(
ParserFactory.createScanner( new StringReader( code ), null, null, null, ParserMode.QUICK_PARSE ), nullCallback, ParserMode.QUICK_PARSE);
parser.setCppNature( cppNature );
parser.mapLineNumbers(true);
parser.parse();
} catch ( Exception e ){
result = e.getClass().toString();

View file

@ -14,14 +14,11 @@ package org.eclipse.cdt.core.parser.tests;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.List;
import junit.framework.TestCase;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.internal.core.dom.ClassSpecifier;
@ -31,7 +28,6 @@ import org.eclipse.cdt.internal.core.dom.IOffsetable;
import org.eclipse.cdt.internal.core.dom.NamespaceDefinition;
import org.eclipse.cdt.internal.core.dom.SimpleDeclaration;
import org.eclipse.cdt.internal.core.dom.TemplateDeclaration;
import org.eclipse.cdt.internal.core.parser.Parser;
import org.eclipse.core.runtime.Path;
/**
@ -53,47 +49,12 @@ public class LineNumberTest extends TestCase {
fileIn = new FileInputStream(fileName);
}
public void testLineNos() throws Exception
{
IScanner scanner = ParserFactory.createScanner( new StringReader( "int x = 3;\n foo\nfire\nfoe " ), "string", null, null, null );
scanner.mapLineNumbers(true);
IToken t = scanner.nextToken();
assertEquals( t.getType(), IToken.t_int );
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), 1 );
t = scanner.nextToken();
assertEquals( t.getImage(), "x");
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), 1 );
t = scanner.nextToken();
assertEquals( t.getType(), IToken.tASSIGN );
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), 1 );
t = scanner.nextToken();
assertEquals( t.getImage(), "3" );
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), 1 );
t = scanner.nextToken();
assertEquals( t.getType(), IToken.tSEMI);
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), 1 );
for( int i = 2; i < 5; ++i )
{
t = scanner.nextToken();
assertEquals( t.getType(), IToken.tIDENTIFIER);
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), i );
}
try {
t = scanner.nextToken();
fail( "EOF");
}
catch (Parser.EndOfFile e) {
assertEquals( scanner.getLineNumberForOffset(29), 4 );
}
}
public void testDOMLineNos() throws Exception
{
DOMBuilder domBuilder = new DOMBuilder();
IParser parser = ParserFactory.createParser( ParserFactory.createScanner( new InputStreamReader( fileIn ), null, null, null, ParserMode.QUICK_PARSE ), domBuilder, ParserMode.QUICK_PARSE );
parser.mapLineNumbers(true);
//parser.mapLineNumbers(true);
if( ! parser.parse() ) fail( "Parse of file failed");
List macros = domBuilder.getTranslationUnit().getMacros();

View file

@ -72,7 +72,7 @@ public class PreprocessorTest extends TestCase {
public IPreprocessor setupPreprocessor( String text, List includePaths, Map defns, ISourceElementRequestor rq )
{
IPreprocessor p = ParserFactory.createPreprocesor( new StringReader( text ), "test", defns, includePaths, ParserMode.COMPLETE_PARSE );
IPreprocessor p = ParserFactory.createPreprocessor( new StringReader( text ), "test", defns, includePaths, ParserMode.COMPLETE_PARSE );
p.setRequestor( rq );
return p;
}

View file

@ -22,6 +22,7 @@ import java.util.StringTokenizer;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import org.eclipse.cdt.core.parser.ILineOffsetReconciler;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserMode;
@ -102,12 +103,12 @@ public class TortureTest extends FractionalAutomatedTest {
}
static protected void reportException (Throwable e, String file, IParser parser){
static protected void reportException (Throwable e, String file, IParser parser, ILineOffsetReconciler mapping){
String output = null;
int lineNumber = -1;
try {
lineNumber = parser.getLineNumberForOffset(parser.getLastErrorOffset());
lineNumber = mapping.getLineNumberForOffset(parser.getLastErrorOffset());
} catch (Exception ex) {}
if (e instanceof AssertionFailedError) {
@ -178,7 +179,7 @@ public class TortureTest extends FractionalAutomatedTest {
thread.stop();
reportHang(testCode, filePath);
} else if (thread.result != null) {
reportException(thread.result, filePath, thread.parser);
reportException(thread.result, filePath, thread.parser, thread.mapping);
}
} else {
// gcc probably didn't expect this test to pass.
@ -192,6 +193,7 @@ public class TortureTest extends FractionalAutomatedTest {
static class ParseThread extends Thread {
public ILineOffsetReconciler mapping = null;
public String code;
public boolean cppNature;
public String file;
@ -202,11 +204,11 @@ public class TortureTest extends FractionalAutomatedTest {
public void run(){
try {
DOMBuilder domBuilder = new DOMBuilder();
IParser parser = ParserFactory.createParser(
parser = ParserFactory.createParser(
ParserFactory.createScanner( new StringReader( code ), null, null, null, ParserMode.QUICK_PARSE ), nullCallback, ParserMode.QUICK_PARSE);
parser.setCppNature(cppNature);
parser.mapLineNumbers(true);
mapping = ParserFactory.createLineOffsetReconciler( new StringReader( code ) );
assertTrue(parser.parse());
}

View file

@ -458,6 +458,7 @@ public class DOMBuilder implements IParserCallback, ISourceElementRequestor
public void pointerOperatorEnd(Object ptrOperator) {
PointerOperator ptrOp = (PointerOperator)ptrOperator;
Declarator owner = ptrOp.getOwnerDeclarator();
if( owner != null ) // can be since operator *
owner.addPointerOperator( ptrOp );
}

View file

@ -66,12 +66,10 @@ public class CModelBuilder {
this.newElements = new HashMap();
}
public Map parse(boolean requiresLineNumbers) throws Exception {
// Note - if a CModel client wishes to have a CModel with valid line numbers
// DOMFactory.createDOMBuilder should be given the parameter 'true'
public Map parse() throws Exception {
DOMBuilder domBuilder = new DOMBuilder();
IParser parser = ParserFactory.createParser(ParserFactory.createScanner( new StringReader( translationUnit.getBuffer().getContents() ), null, null, null, ParserMode.QUICK_PARSE ), domBuilder, ParserMode.QUICK_PARSE);
parser.mapLineNumbers(requiresLineNumbers);
if( translationUnit.getCProject() != null )
{
IProject currentProject = translationUnit.getCProject().getProject();

View file

@ -206,13 +206,13 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
}
return sourceManipulationInfo;
}
protected Map parse(InputStream in, boolean requiresLineNumbers) {
protected Map parse(InputStream in) {
try {
removeChildren();
if (CCorePlugin.getDefault().useNewParser()) {
// new parser
CModelBuilder modelBuilder = new CModelBuilder(this);
return (modelBuilder.parse(requiresLineNumbers));
return (modelBuilder.parse());
} else {
// cdt 1.0 parser
@ -483,7 +483,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
String buf =this.getBuffer().getContents();
if (buf != null) {
StringBufferInputStream in = new StringBufferInputStream (buf);
return (parse (in, requireLineNumbers));
return (parse (in));
}
return null;

View file

@ -1,3 +1,9 @@
2003-06-25 John Camelon
Create new interface and support for calculating lineNumber/offset mapping.
Updated IASTClassSpecifier for qualified name query.
Began structuring expressions and declarators in Parser for ISourceElementRequestor.
Updated other packages to use new interfaces.
2003-06-24 John Camelon
Updates for ISourceElementRequestor - elaborated types & enumerations.

View file

@ -0,0 +1,21 @@
/**********************************************************************
* Copyright (c) 2002,2003 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.core.parser;
/**
* @author jcamelon
*
*/
public interface ILineOffsetReconciler
{
public int getLineNumberForOffset( int offset );
public IOffsetDuple getOffsetRangeForLineNumber( int LineNumber );
}

View file

@ -0,0 +1,21 @@
/**********************************************************************
* Copyright (c) 2002,2003 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.core.parser;
/**
* @author jcamelon
*
*/
public interface IOffsetDuple
{
public int getCeilingOffset();
public int getFloorOffset();
}

View file

@ -55,25 +55,6 @@ public interface IParser {
*/
public void setCppNature(boolean b);
/**
* Do we wish to keep track of the lineNumbers/Offset mapping?
*
* By default, the value is false. Setting it to true impacts performance but
* provides that feature.
*
* @param value true for the feature, false for improved performance
*/
public void mapLineNumbers( boolean value );
/**
* Given an character offset into the file, return the lineNumber this offset maps to.
*
* @param offset character offset in the file
* @return lineNumber this offset maps to
* @throws NoSuchMethodException if mapLineNumbers( true ) was not previously called
*/
public int getLineNumberForOffset(int offset) throws NoSuchMethodException;
/**
* If an error was encountered, give us the offset of the token that caused the error.
*

View file

@ -26,9 +26,9 @@ public interface IScanner {
public IToken nextToken() throws ScannerException, Parser.EndOfFile;
public IToken nextToken( boolean next ) throws ScannerException, Parser.EndOfFile;
public int getLineNumberForOffset(int offset) throws NoSuchMethodException;
public void setCppNature( boolean value );
public void mapLineNumbers( boolean value );
public void setMode(ParserMode mode);
public void setCallback(IParserCallback c);

View file

@ -15,6 +15,7 @@ import java.util.List;
import java.util.Map;
import org.eclipse.cdt.core.parser.ast.IASTFactory;
import org.eclipse.cdt.internal.core.parser.LineOffsetReconciler;
import org.eclipse.cdt.internal.core.parser.NullSourceElementRequestor;
import org.eclipse.cdt.internal.core.parser.Parser;
import org.eclipse.cdt.internal.core.parser.Preprocessor;
@ -52,7 +53,7 @@ public class ParserFactory {
return s;
}
public static IPreprocessor createPreprocesor( Reader input, String fileName, Map defns, List inclusions, ParserMode mode )
public static IPreprocessor createPreprocessor( Reader input, String fileName, Map defns, List inclusions, ParserMode mode )
{
ParserMode ourMode = ( (mode == null )? ParserMode.COMPLETE_PARSE : mode );
IPreprocessor s = new Preprocessor( input, fileName, defns );
@ -60,4 +61,10 @@ public class ParserFactory {
s.overwriteIncludePath(inclusions);
return s;
}
public static ILineOffsetReconciler createLineOffsetReconciler( Reader input )
{
return new LineOffsetReconciler( input );
}
}

View file

@ -26,4 +26,6 @@ public interface IASTClassSpecifier extends IASTTypeSpecifier, IASTScope, IASTOf
public AccessVisibility getCurrentVisibilityMode();
public String[] getFullyQualifiedName();
}

View file

@ -0,0 +1,20 @@
/**********************************************************************
* Copyright (c) 2002,2003 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.core.parser.ast;
/**
* @author jcamelon
*
*/
public interface IASTConstantExpression extends IASTExpression
{
public int evaluate();
}

View file

@ -0,0 +1,20 @@
/**********************************************************************
* Copyright (c) 2002,2003 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.core.parser.ast;
/**
* @author jcamelon
*
*/
public interface IASTExpression
{
}

View file

@ -148,7 +148,6 @@ public class ContextStack {
private LinkedList undoStack = new LinkedList();
private Set inclusions = new HashSet();
private Set defines = new HashSet();
private OffsetToLineMapping offsetLineMap = new OffsetToLineMapping();
/**
* @return
@ -157,21 +156,4 @@ public class ContextStack {
return topContext;
}
public int mapOffsetToLineNumber( int offset )
{
return offsetLineMap.getLineNo(offset);
}
public void newLine()
{
if( currentContext == topContext )
offsetLineMap.newLine( topContext.getOffset() );
}
public void recantNewline()
{
if( currentContext == topContext )
offsetLineMap.recantLastNewLine();
}
}

View file

@ -14,16 +14,20 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.cdt.core.parser.ast.IASTExpression;
/**
* @author jcamelon
*
*/
public class Declarator
public class Declarator implements IParameterCollection
{
private final DeclarationWrapper owner1;
private final Declarator owner2;
private String name;
private IASTExpression initialValueExpression;
private List ptrOps = new ArrayList();
private List parameters = new ArrayList();
private int nameStartOffset, nameEndOffset;
@ -114,4 +118,32 @@ public class Declarator
{
ptrOps.add( ptrOp );
}
/**
* @return
*/
public List getParameters()
{
return parameters;
}
public void addParameter( DeclarationWrapper param )
{
parameters.add( param );
}
/**
* @return
*/
public IASTExpression getInitialValueExpression()
{
return initialValueExpression;
}
/**
* @param expression
*/
public void setInitialValueExpression(IASTExpression expression)
{
initialValueExpression = expression;
}
}

View file

@ -10,12 +10,14 @@
***********************************************************************/
package org.eclipse.cdt.internal.core.parser;
import java.util.List;
/**
* @author jcamelon
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public interface IOffsetToLineMapping {
public abstract int getLineNo(int offset);
public interface IParameterCollection
{
public List getParameters();
public void addParameter( DeclarationWrapper param );
}

View file

@ -0,0 +1,99 @@
/**********************************************************************
* Copyright (c) 2002,2003 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.internal.core.parser;
import java.io.IOException;
import java.io.Reader;
import org.eclipse.cdt.core.parser.ILineOffsetReconciler;
import org.eclipse.cdt.core.parser.IOffsetDuple;
/**
* @author jcamelon
*
*/
public class LineOffsetReconciler implements ILineOffsetReconciler
{
private Reader ourReader;
/**
* @param input
*/
public LineOffsetReconciler(Reader input)
{
ourReader = input;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ILineOffsetReconciler#getLineNumberForOffset(int)
*/
public int getLineNumberForOffset(int offset)
{
resetReader();
int lineNumber = 1;
for( int i = 0; i < offset; ++i )
{
int c = getChar();
if( c == '\n' )
++lineNumber;
}
return lineNumber;
}
private int getChar()
{
int c;
try
{
c = ourReader.read();
}
catch (IOException e)
{
throw new Error( "Could not read");
}
return c;
}
private void resetReader()
{
try
{
ourReader.reset();
}
catch (IOException e)
{
throw new Error( "Could not reset Reader" );
}
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ILineOffsetReconciler#getOffsetRangeForLineNumber(int)
*/
public IOffsetDuple getOffsetRangeForLineNumber(int LineNumber)
{
int lineNumber = 1;
int floor= -1, ceiling = -1;
int offset = 0;
while( lineNumber != LineNumber )
{
int c = getChar();
++offset;
if( c == '\n' ) ++lineNumber;
}
floor = offset;
while( lineNumber == LineNumber )
{
int c = getChar();
++offset;
if( c == '\n' ) ++lineNumber;
}
ceiling = offset;
return new OffsetDuple( floor, ceiling );
}
}

View file

@ -0,0 +1,45 @@
/**********************************************************************
* Copyright (c) 2002,2003 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.internal.core.parser;
import org.eclipse.cdt.core.parser.IOffsetDuple;
/**
* @author jcamelon
*
*/
public class OffsetDuple implements IOffsetDuple
{
private final int lineFloor, lineCeiling;
/**
* @param floor
* @param ceiling
*/
public OffsetDuple(int floor, int ceiling)
{
lineFloor = floor;
lineCeiling = ceiling;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.IOffsetDuple#getCeilingOffset()
*/
public int getCeilingOffset()
{
return lineCeiling;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.IOffsetDuple#getFloorOffset()
*/
public int getFloorOffset()
{
return lineFloor;
}
}

View file

@ -1,75 +0,0 @@
/**********************************************************************
* Copyright (c) 2002,2003 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.internal.core.parser;
import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* @author jcamelon
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class OffsetToLineMapping implements IOffsetToLineMapping {
public OffsetToLineMapping()
{
}
public void newLine( int offset )
{
lastOffset = offset;
store.put( new Integer( offset ), new Integer( ++lineCounter ) );
}
public void recantLastNewLine()
{
if( store.remove( new Integer( lastOffset ) ) != null )
{
--lineCounter;
lastOffset = -1;
}
}
public int getLineNo( int offset )
{
Iterator iter = store.keySet().iterator();
int first = -1, second = -1;
if( ! iter.hasNext() ) return 1;
first = ((Integer)iter.next()).intValue();
if( ( offset <= first ) || ! iter.hasNext() )
return ((Integer)store.get( new Integer( first ))).intValue();
while( true )
{
second = ((Integer)iter.next()).intValue();
if( offset > first && offset <= second )
return ((Integer)store.get( new Integer( second ))).intValue();
if( ! iter.hasNext() ) break;
first = second;
}
return lineCounter;
}
public int getCurrentLineNumber()
{
return lineCounter;
}
private int lineCounter = 1;
private int lastOffset = -1;
private SortedMap store = new TreeMap();
}

View file

@ -712,7 +712,7 @@ public class Parser implements IParser
}
else
{
parameterDeclaration(templateParameterList);
parameterDeclaration(templateParameterList, null); // this should be something real
}
}
}
@ -1147,7 +1147,7 @@ public class Parser implements IParser
* @param containerObject The IParserCallback object representing the parameterDeclarationClause owning the parm.
* @throws Backtrack request a backtrack
*/
protected void parameterDeclaration(Object containerObject)
protected void parameterDeclaration(Object containerObject, IParameterCollection collection)
throws Backtrack
{
IToken current = LA(1);
@ -1178,6 +1178,10 @@ public class Parser implements IParser
}
if (current == LA(1))
throw backtrack;
if ( collection != null )
collection.addParameter( sdw );
try
{
callback.parameterDeclarationEnd(parameterDecl);
@ -1914,6 +1918,7 @@ public class Parser implements IParser
catch (Exception e)
{
}
//TODO add in expression information here
}
catch (Backtrack b)
{
@ -1928,6 +1933,7 @@ public class Parser implements IParser
}
if (LT(1) == IToken.tLBRACE)
{
//TODO do this for real
// for now, just consume to matching brace
consume();
int depth = 1;
@ -2134,7 +2140,7 @@ public class Parser implements IParser
default :
if (seenParameter)
throw backtrack;
parameterDeclaration(clause);
parameterDeclaration(clause, d);
seenParameter = true;
}
}
@ -2187,8 +2193,7 @@ public class Parser implements IParser
done = true;
break;
case IToken.tIDENTIFIER :
//TODO this is not exactly right - should be type-id rather than just a name
name();
typeId();
try
{
callback
@ -2226,8 +2231,8 @@ public class Parser implements IParser
{
}
}
if (afterCVModifier != LA(1)
|| LT(1) == IToken.tSEMI)
if ( ( afterCVModifier != LA(1) || LT(1) == IToken.tSEMI ) && cvModifier != null )
{
// There were C++-specific clauses after const/volatile modifier
// Then it is a marker for the method
@ -2488,13 +2493,6 @@ public class Parser implements IParser
try
{
callback.pointerOperatorType(ptrOp, consume(IToken.tAMPER));
}
catch (Exception e)
{
}
try
{
callback.pointerOperatorEnd(ptrOp);
}
catch (Exception e)
@ -4289,20 +4287,7 @@ public class Parser implements IParser
if (scanner != null)
scanner.setCppNature(b);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.IParser#getLineNumberForOffset(int)
*/
public int getLineNumberForOffset(int offset) throws NoSuchMethodException
{
return scanner.getLineNumberForOffset(offset);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.IParser#mapLineNumbers(boolean)
*/
public void mapLineNumbers(boolean value)
{
scanner.mapLineNumbers(value);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.IParser#getLastErrorOffset()
*/
@ -4310,6 +4295,7 @@ public class Parser implements IParser
{
return firstErrorOffset;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.IParser#setRequestor(org.eclipse.cdt.core.parser.ISourceElementRequestor)
*/

View file

@ -425,29 +425,22 @@ public class Scanner implements IScanner {
}
} else if (c == '\n')
{
contextStack.newLine();
c = getChar(false);
if( c == '\n')
contextStack.newLine();
} else // '\' is not the last character on the line
{
ungetChar(c);
c = '\\';
}
}
else if( c == '\n' )
contextStack.newLine();
}
else if( c == '\n' )
contextStack.newLine();
return c;
}
private void ungetChar(int c) throws ScannerException{
contextStack.getCurrentContext().pushUndo(c);
if( c == '\n' ) contextStack.recantNewline();
contextStack.undoRollback( lastContext, requestor );
}
@ -2186,14 +2179,7 @@ public class Scanner implements IScanner {
return "0";
}
/**
* @return
*/
public int getLineNumberForOffset(int offset) throws NoSuchMethodException {
if( this.mapLineNumbers )
return contextStack.mapOffsetToLineNumber(offset);
throw new NoSuchMethodException();
}
private boolean cppNature = true;
/* (non-Javadoc)
@ -2203,14 +2189,6 @@ public class Scanner implements IScanner {
cppNature = value;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.IScanner#mapLineNumbers(boolean)
*/
public void mapLineNumbers(boolean value) {
mapLineNumbers = value;
}
private boolean mapLineNumbers = false;
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.IScanner#setRequestor(org.eclipse.cdt.core.parser.ISourceElementRequestor)

View file

@ -143,4 +143,13 @@ public class ASTClassSpecifier implements IASTFClassSpecifier, IPSTSymbolExtensi
return null;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getFullyQualifiedName()
*/
public String[] getFullyQualifiedName()
{
// TODO Auto-generated method stub
return null;
}
}

View file

@ -9,27 +9,32 @@
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.internal.core.parser.ast.quick;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import org.eclipse.cdt.core.parser.ast.AccessVisibility;
import org.eclipse.cdt.core.parser.ast.ClassKind;
import org.eclipse.cdt.core.parser.ast.ClassNameType;
import org.eclipse.cdt.core.parser.ast.IASTBaseSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTClassSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTNamespaceDefinition;
import org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement;
import org.eclipse.cdt.core.parser.ast.IASTScope;
import org.eclipse.cdt.core.parser.ast.IASTTemplateDeclaration;
import org.eclipse.cdt.internal.core.parser.ast.NamedOffsets;
/**
* @author jcamelon
*
*/
public class ASTClassSpecifier extends ASTDeclaration implements IASTQClassSpecifier, IASTQScope {
public ASTClassSpecifier(IASTScope scope,
public class ASTClassSpecifier
extends ASTDeclaration
implements IASTQClassSpecifier, IASTQScope
{
public ASTClassSpecifier(
IASTScope scope,
String name,
ClassKind kind,
ClassNameType type,
@ -42,8 +47,30 @@ public class ASTClassSpecifier extends ASTDeclaration implements IASTQClassSpeci
this.access = access;
this.name = name;
templateOwner = ownerTemplateDeclaration;
}
Stack names = new Stack();
IASTScope parent = getOwnerScope();
while (parent != null)
{
if (parent instanceof IASTNamespaceDefinition
|| parent instanceof IASTClassSpecifier)
{
names.push(((IASTOffsetableNamedElement)parent).getName());
parent = ((IASTDeclaration)parent).getOwnerScope();
}
break;
}
if (names.size() != 0)
{
qualifiedNames = new String[names.size()];
int counter = 0;
while (!names.empty())
qualifiedNames[counter++] = (String)names.pop();
}
else
qualifiedNames = null;
}
private final String[] qualifiedNames;
private IASTTemplateDeclaration templateOwner = null;
private final String name;
private List declarations = new ArrayList();
@ -55,106 +82,113 @@ public class ASTClassSpecifier extends ASTDeclaration implements IASTQClassSpeci
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getClassNameType()
*/
public ClassNameType getClassNameType() {
public ClassNameType getClassNameType()
{
return classNameType;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getClassKind()
*/
public ClassKind getClassKind() {
public ClassKind getClassKind()
{
return classKind;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getBaseClauses()
*/
public Iterator getBaseClauses() {
public Iterator getBaseClauses()
{
return baseClauses.iterator();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getCurrentVisiblity()
*/
public AccessVisibility getCurrentVisibilityMode() {
public AccessVisibility getCurrentVisibilityMode()
{
return access;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTScope#getDeclarations()
*/
public Iterator getDeclarations() {
public Iterator getDeclarations()
{
return declarations.iterator();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement#getName()
*/
public String getName() {
public String getName()
{
return name;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement#getElementNameOffset()
*/
public int getElementNameOffset() {
public int getElementNameOffset()
{
return offsets.getElementNameOffset();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement#setNameOffset(int)
*/
public void setNameOffset(int o) {
public void setNameOffset(int o)
{
offsets.setNameOffset(o);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTTemplatedDeclaration#getOwnerTemplateDeclaration()
*/
public IASTTemplateDeclaration getOwnerTemplateDeclaration() {
public IASTTemplateDeclaration getOwnerTemplateDeclaration()
{
return templateOwner;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#setStartingOffset(int)
*/
public void setStartingOffset(int o) {
public void setStartingOffset(int o)
{
offsets.setStartingOffset(o);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#setEndingOffset(int)
*/
public void setEndingOffset(int o) {
public void setEndingOffset(int o)
{
offsets.setEndingOffset(o);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#getElementStartingOffset()
*/
public int getElementStartingOffset() {
public int getElementStartingOffset()
{
return offsets.getElementStartingOffset();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#getElementEndingOffset()
*/
public int getElementEndingOffset() {
public int getElementEndingOffset()
{
return offsets.getElementEndingOffset();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.ast.quick.IASTQScope#addDeclaration(org.eclipse.cdt.core.parser.ast.IASTDeclaration)
*/
public void addDeclaration(IASTDeclaration declaration) {
public void addDeclaration(IASTDeclaration declaration)
{
declarations.add(declaration);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.ast.quick.IASTQClassSpecifier#addBaseClass(org.eclipse.cdt.core.parser.ast.IASTBaseSpecifier)
*/
public void addBaseClass(IASTBaseSpecifier baseSpecifier) {
public void addBaseClass(IASTBaseSpecifier baseSpecifier)
{
baseClauses.add(baseSpecifier);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getFullyQualifiedName()
*/
public String[] getFullyQualifiedName()
{
return qualifiedNames;
}
}

View file

@ -1,3 +1,10 @@
2003-06-25 John Camelon
Create new interface and support for calculating lineNumber/offset mapping.
Updated IASTClassSpecifier for qualified name query.
Began structuring expressions and declarators in Parser for ISourceElementRequestor.
Updated other packages to use new interfaces.
Updated automatedtests/torture test to use new line number information.
2003-06-24 John Camelon
Updates for ISourceElementRequestor - elaborated types & enumerations.

View file

@ -21,6 +21,7 @@ import java.util.StringTokenizer;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import org.eclipse.cdt.core.parser.ILineOffsetReconciler;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserMode;
@ -47,6 +48,7 @@ public class AutomatedTest extends AutomatedFramework {
File file = null;
IParser parser = null;
ILineOffsetReconciler mapping = null;
try{
file = (File)fileList.removeFirst();
@ -55,7 +57,8 @@ public class AutomatedTest extends AutomatedFramework {
String filePath = file.getCanonicalPath();
parser = ParserFactory.createParser( ParserFactory.createScanner( new InputStreamReader (stream), filePath, null, null, ParserMode.QUICK_PARSE ), nullCallback, ParserMode.QUICK_PARSE);
parser.setCppNature( ((String)natures.get( filePath )).equalsIgnoreCase("cpp") );
parser.mapLineNumbers(true);
mapping = ParserFactory.createLineOffsetReconciler( new InputStreamReader( stream ) );
assertTrue( parser.parse() );
}
@ -64,10 +67,10 @@ public class AutomatedTest extends AutomatedFramework {
String output = null;
if( e instanceof AssertionFailedError ){
output = file.getCanonicalPath() + ": Parse failed on line ";
output += parser.getLineNumberForOffset(parser.getLastErrorOffset()) + "\n";
output += mapping.getLineNumberForOffset(parser.getLastErrorOffset()) + "\n";
} else {
output = file.getCanonicalPath() + ": " + e.getClass().toString();
output += " on line " + parser.getLineNumberForOffset(parser.getLastErrorOffset()) + "\n";
output += " on line " + mapping.getLineNumberForOffset(parser.getLastErrorOffset()) + "\n";
}
if( report != null ){
report.write( output.getBytes() );

View file

@ -240,7 +240,6 @@ public class FractionalAutomatedTest extends AutomatedFramework {
IParser parser = ParserFactory.createParser(
ParserFactory.createScanner( new StringReader( code ), null, null, null, ParserMode.QUICK_PARSE ), nullCallback, ParserMode.QUICK_PARSE);
parser.setCppNature( cppNature );
parser.mapLineNumbers(true);
parser.parse();
} catch ( Exception e ){
result = e.getClass().toString();

View file

@ -53,47 +53,12 @@ public class LineNumberTest extends TestCase {
fileIn = new FileInputStream(fileName);
}
public void testLineNos() throws Exception
{
IScanner scanner = ParserFactory.createScanner( new StringReader( "int x = 3;\n foo\nfire\nfoe " ), "string", null, null, null );
scanner.mapLineNumbers(true);
IToken t = scanner.nextToken();
assertEquals( t.getType(), IToken.t_int );
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), 1 );
t = scanner.nextToken();
assertEquals( t.getImage(), "x");
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), 1 );
t = scanner.nextToken();
assertEquals( t.getType(), IToken.tASSIGN );
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), 1 );
t = scanner.nextToken();
assertEquals( t.getImage(), "3" );
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), 1 );
t = scanner.nextToken();
assertEquals( t.getType(), IToken.tSEMI);
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), 1 );
for( int i = 2; i < 5; ++i )
{
t = scanner.nextToken();
assertEquals( t.getType(), IToken.tIDENTIFIER);
assertEquals( scanner.getLineNumberForOffset(t.getOffset()), i );
}
try {
t = scanner.nextToken();
fail( "EOF");
}
catch (Parser.EndOfFile e) {
assertEquals( scanner.getLineNumberForOffset(29), 4 );
}
}
public void testDOMLineNos() throws Exception
{
DOMBuilder domBuilder = new DOMBuilder();
IParser parser = ParserFactory.createParser( ParserFactory.createScanner( new InputStreamReader( fileIn ), null, null, null, ParserMode.QUICK_PARSE ), domBuilder, ParserMode.QUICK_PARSE );
parser.mapLineNumbers(true);
//parser.mapLineNumbers(true);
if( ! parser.parse() ) fail( "Parse of file failed");
List macros = domBuilder.getTranslationUnit().getMacros();

View file

@ -22,6 +22,7 @@ import java.util.StringTokenizer;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import org.eclipse.cdt.core.parser.ILineOffsetReconciler;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserMode;
@ -102,12 +103,12 @@ public class TortureTest extends FractionalAutomatedTest {
}
static protected void reportException (Throwable e, String file, IParser parser){
static protected void reportException (Throwable e, String file, IParser parser, ILineOffsetReconciler mapping){
String output = null;
int lineNumber = -1;
try {
lineNumber = parser.getLineNumberForOffset(parser.getLastErrorOffset());
lineNumber = mapping.getLineNumberForOffset(parser.getLastErrorOffset());
} catch (Exception ex) {}
if (e instanceof AssertionFailedError) {
@ -178,7 +179,7 @@ public class TortureTest extends FractionalAutomatedTest {
thread.stop();
reportHang(testCode, filePath);
} else if (thread.result != null) {
reportException(thread.result, filePath, thread.parser);
reportException(thread.result, filePath, thread.parser, thread.mapping );
}
} else {
// gcc probably didn't expect this test to pass.
@ -198,15 +199,16 @@ public class TortureTest extends FractionalAutomatedTest {
public Throwable result = null;
public IParser parser = null;
public boolean quickParse = true;
public ILineOffsetReconciler mapping = null;
public void run(){
try {
DOMBuilder domBuilder = new DOMBuilder();
IParser parser = ParserFactory.createParser(
ParserFactory.createScanner( new StringReader( code ), null, null, null, ParserMode.QUICK_PARSE ), nullCallback, ParserMode.QUICK_PARSE);
mapping = ParserFactory.createLineOffsetReconciler( new StringReader( code ) );
parser.setCppNature(cppNature);
parser.mapLineNumbers(true);
assertTrue(parser.parse());
}

View file

@ -1,3 +1,9 @@
2003-06-25 John Camelon
Create new interface and support for calculating lineNumber/offset mapping.
Updated IASTClassSpecifier for qualified name query.
Began structuring expressions and declarators in Parser for ISourceElementRequestor.
Updated other packages to use new interfaces.
2003-06-24 Thomas Fletcher
- Proposals will now include additional help information with them

View file

@ -51,7 +51,7 @@ public class CFileElementWorkingCopy extends WorkingCopy {
if (doc != null) {
DocumentInputStream dis= new DocumentInputStream(doc);
try {
parse(dis, false); // we do not believe we require line numbers
parse(dis);
} finally {
try { dis.close(); } catch (IOException e) {}
}