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 2003-06-24 John Camelon
Updates for ISourceElementRequestor - elaborated types & enumerations. Updates for ISourceElementRequestor - elaborated types & enumerations.

View file

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

View file

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

View file

@ -14,14 +14,11 @@ package org.eclipse.cdt.core.parser.tests;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.List; import java.util.List;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.eclipse.cdt.core.parser.IParser; 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.ParserFactory;
import org.eclipse.cdt.core.parser.ParserMode; import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.internal.core.dom.ClassSpecifier; 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.NamespaceDefinition;
import org.eclipse.cdt.internal.core.dom.SimpleDeclaration; import org.eclipse.cdt.internal.core.dom.SimpleDeclaration;
import org.eclipse.cdt.internal.core.dom.TemplateDeclaration; import org.eclipse.cdt.internal.core.dom.TemplateDeclaration;
import org.eclipse.cdt.internal.core.parser.Parser;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
/** /**
@ -53,47 +49,12 @@ public class LineNumberTest extends TestCase {
fileIn = new FileInputStream(fileName); 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 public void testDOMLineNos() throws Exception
{ {
DOMBuilder domBuilder = new DOMBuilder(); DOMBuilder domBuilder = new DOMBuilder();
IParser parser = ParserFactory.createParser( ParserFactory.createScanner( new InputStreamReader( fileIn ), null, null, null, ParserMode.QUICK_PARSE ), domBuilder, ParserMode.QUICK_PARSE ); 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"); if( ! parser.parse() ) fail( "Parse of file failed");
List macros = domBuilder.getTranslationUnit().getMacros(); 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 ) 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 ); p.setRequestor( rq );
return p; return p;
} }

View file

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

View file

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

View file

@ -66,12 +66,10 @@ public class CModelBuilder {
this.newElements = new HashMap(); this.newElements = new HashMap();
} }
public Map parse(boolean requiresLineNumbers) throws Exception { public Map parse() throws Exception {
// Note - if a CModel client wishes to have a CModel with valid line numbers
// DOMFactory.createDOMBuilder should be given the parameter 'true'
DOMBuilder domBuilder = new DOMBuilder(); 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); 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 ) if( translationUnit.getCProject() != null )
{ {
IProject currentProject = translationUnit.getCProject().getProject(); IProject currentProject = translationUnit.getCProject().getProject();

View file

@ -206,13 +206,13 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
} }
return sourceManipulationInfo; return sourceManipulationInfo;
} }
protected Map parse(InputStream in, boolean requiresLineNumbers) { protected Map parse(InputStream in) {
try { try {
removeChildren(); removeChildren();
if (CCorePlugin.getDefault().useNewParser()) { if (CCorePlugin.getDefault().useNewParser()) {
// new parser // new parser
CModelBuilder modelBuilder = new CModelBuilder(this); CModelBuilder modelBuilder = new CModelBuilder(this);
return (modelBuilder.parse(requiresLineNumbers)); return (modelBuilder.parse());
} else { } else {
// cdt 1.0 parser // cdt 1.0 parser
@ -483,7 +483,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
String buf =this.getBuffer().getContents(); String buf =this.getBuffer().getContents();
if (buf != null) { if (buf != null) {
StringBufferInputStream in = new StringBufferInputStream (buf); StringBufferInputStream in = new StringBufferInputStream (buf);
return (parse (in, requireLineNumbers)); return (parse (in));
} }
return null; 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 2003-06-24 John Camelon
Updates for ISourceElementRequestor - elaborated types & enumerations. 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); 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. * 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() throws ScannerException, Parser.EndOfFile;
public IToken nextToken( boolean next ) 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 setCppNature( boolean value );
public void mapLineNumbers( boolean value );
public void setMode(ParserMode mode); public void setMode(ParserMode mode);
public void setCallback(IParserCallback c); public void setCallback(IParserCallback c);

View file

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

View file

@ -25,5 +25,7 @@ public interface IASTClassSpecifier extends IASTTypeSpecifier, IASTScope, IASTOf
public Iterator getBaseClauses(); public Iterator getBaseClauses();
public AccessVisibility getCurrentVisibilityMode(); 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 LinkedList undoStack = new LinkedList();
private Set inclusions = new HashSet(); private Set inclusions = new HashSet();
private Set defines = new HashSet(); private Set defines = new HashSet();
private OffsetToLineMapping offsetLineMap = new OffsetToLineMapping();
/** /**
* @return * @return
@ -157,21 +156,4 @@ public class ContextStack {
return topContext; 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.Collections;
import java.util.List; import java.util.List;
import org.eclipse.cdt.core.parser.ast.IASTExpression;
/** /**
* @author jcamelon * @author jcamelon
* *
*/ */
public class Declarator public class Declarator implements IParameterCollection
{ {
private final DeclarationWrapper owner1; private final DeclarationWrapper owner1;
private final Declarator owner2; private final Declarator owner2;
private String name; private String name;
private List ptrOps = new ArrayList(); private IASTExpression initialValueExpression;
private List ptrOps = new ArrayList();
private List parameters = new ArrayList();
private int nameStartOffset, nameEndOffset; private int nameStartOffset, nameEndOffset;
@ -114,4 +118,32 @@ public class Declarator
{ {
ptrOps.add( ptrOp ); 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; package org.eclipse.cdt.internal.core.parser;
import java.util.List;
/** /**
* @author jcamelon * @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 interface IParameterCollection
public abstract int getLineNo(int offset); {
} 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 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. * @param containerObject The IParserCallback object representing the parameterDeclarationClause owning the parm.
* @throws Backtrack request a backtrack * @throws Backtrack request a backtrack
*/ */
protected void parameterDeclaration(Object containerObject) protected void parameterDeclaration(Object containerObject, IParameterCollection collection)
throws Backtrack throws Backtrack
{ {
IToken current = LA(1); IToken current = LA(1);
@ -1178,6 +1178,10 @@ public class Parser implements IParser
} }
if (current == LA(1)) if (current == LA(1))
throw backtrack; throw backtrack;
if ( collection != null )
collection.addParameter( sdw );
try try
{ {
callback.parameterDeclarationEnd(parameterDecl); callback.parameterDeclarationEnd(parameterDecl);
@ -1914,6 +1918,7 @@ public class Parser implements IParser
catch (Exception e) catch (Exception e)
{ {
} }
//TODO add in expression information here
} }
catch (Backtrack b) catch (Backtrack b)
{ {
@ -1928,6 +1933,7 @@ public class Parser implements IParser
} }
if (LT(1) == IToken.tLBRACE) if (LT(1) == IToken.tLBRACE)
{ {
//TODO do this for real
// for now, just consume to matching brace // for now, just consume to matching brace
consume(); consume();
int depth = 1; int depth = 1;
@ -2134,7 +2140,7 @@ public class Parser implements IParser
default : default :
if (seenParameter) if (seenParameter)
throw backtrack; throw backtrack;
parameterDeclaration(clause); parameterDeclaration(clause, d);
seenParameter = true; seenParameter = true;
} }
} }
@ -2187,8 +2193,7 @@ public class Parser implements IParser
done = true; done = true;
break; break;
case IToken.tIDENTIFIER : case IToken.tIDENTIFIER :
//TODO this is not exactly right - should be type-id rather than just a name typeId();
name();
try try
{ {
callback 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 // There were C++-specific clauses after const/volatile modifier
// Then it is a marker for the method // Then it is a marker for the method
@ -2488,14 +2493,7 @@ public class Parser implements IParser
try try
{ {
callback.pointerOperatorType(ptrOp, consume(IToken.tAMPER)); callback.pointerOperatorType(ptrOp, consume(IToken.tAMPER));
callback.pointerOperatorEnd(ptrOp);
}
catch (Exception e)
{
}
try
{
callback.pointerOperatorEnd(ptrOp);
} }
catch (Exception e) catch (Exception e)
{ {
@ -4289,20 +4287,7 @@ public class Parser implements IParser
if (scanner != null) if (scanner != null)
scanner.setCppNature(b); 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) /* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.IParser#getLastErrorOffset() * @see org.eclipse.cdt.internal.core.parser.IParser#getLastErrorOffset()
*/ */
@ -4310,6 +4295,7 @@ public class Parser implements IParser
{ {
return firstErrorOffset; return firstErrorOffset;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.IParser#setRequestor(org.eclipse.cdt.core.parser.ISourceElementRequestor) * @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') } else if (c == '\n')
{ {
contextStack.newLine();
c = getChar(false); c = getChar(false);
if( c == '\n')
contextStack.newLine();
} else // '\' is not the last character on the line } else // '\' is not the last character on the line
{ {
ungetChar(c); ungetChar(c);
c = '\\'; c = '\\';
} }
} }
else if( c == '\n' )
contextStack.newLine();
} }
else if( c == '\n' )
contextStack.newLine();
return c; return c;
} }
private void ungetChar(int c) throws ScannerException{ private void ungetChar(int c) throws ScannerException{
contextStack.getCurrentContext().pushUndo(c); contextStack.getCurrentContext().pushUndo(c);
if( c == '\n' ) contextStack.recantNewline();
contextStack.undoRollback( lastContext, requestor ); contextStack.undoRollback( lastContext, requestor );
} }
@ -2186,14 +2179,7 @@ public class Scanner implements IScanner {
return "0"; return "0";
} }
/**
* @return
*/
public int getLineNumberForOffset(int offset) throws NoSuchMethodException {
if( this.mapLineNumbers )
return contextStack.mapOffsetToLineNumber(offset);
throw new NoSuchMethodException();
}
private boolean cppNature = true; private boolean cppNature = true;
/* (non-Javadoc) /* (non-Javadoc)
@ -2203,14 +2189,6 @@ public class Scanner implements IScanner {
cppNature = value; 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) /* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.IScanner#setRequestor(org.eclipse.cdt.core.parser.ISourceElementRequestor) * @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; 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,152 +9,186 @@
* IBM Rational Software - Initial API and implementation * IBM Rational Software - Initial API and implementation
***********************************************************************/ ***********************************************************************/
package org.eclipse.cdt.internal.core.parser.ast.quick; package org.eclipse.cdt.internal.core.parser.ast.quick;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Stack;
import org.eclipse.cdt.core.parser.ast.AccessVisibility; import org.eclipse.cdt.core.parser.ast.AccessVisibility;
import org.eclipse.cdt.core.parser.ast.ClassKind; import org.eclipse.cdt.core.parser.ast.ClassKind;
import org.eclipse.cdt.core.parser.ast.ClassNameType; import org.eclipse.cdt.core.parser.ast.ClassNameType;
import org.eclipse.cdt.core.parser.ast.IASTBaseSpecifier; 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.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.IASTScope;
import org.eclipse.cdt.core.parser.ast.IASTTemplateDeclaration; import org.eclipse.cdt.core.parser.ast.IASTTemplateDeclaration;
import org.eclipse.cdt.internal.core.parser.ast.NamedOffsets; import org.eclipse.cdt.internal.core.parser.ast.NamedOffsets;
/** /**
* @author jcamelon * @author jcamelon
* *
*/ */
public class ASTClassSpecifier extends ASTDeclaration implements IASTQClassSpecifier, IASTQScope { public class ASTClassSpecifier
extends ASTDeclaration
public ASTClassSpecifier(IASTScope scope, implements IASTQClassSpecifier, IASTQScope
String name, {
ClassKind kind, public ASTClassSpecifier(
ClassNameType type, IASTScope scope,
AccessVisibility access, String name,
IASTTemplateDeclaration ownerTemplateDeclaration ) ClassKind kind,
{ ClassNameType type,
super(scope); AccessVisibility access,
classNameType = type; IASTTemplateDeclaration ownerTemplateDeclaration)
classKind = kind; {
this.access = access; super(scope);
this.name = name; classNameType = type;
templateOwner = ownerTemplateDeclaration; classKind = kind;
} this.access = access;
this.name = name;
private IASTTemplateDeclaration templateOwner = null; templateOwner = ownerTemplateDeclaration;
private final String name;
private List declarations = new ArrayList(); Stack names = new Stack();
private List baseClauses = new ArrayList(); IASTScope parent = getOwnerScope();
private AccessVisibility access; while (parent != null)
private NamedOffsets offsets = new NamedOffsets(); {
private final ClassNameType classNameType; if (parent instanceof IASTNamespaceDefinition
private final ClassKind classKind; || parent instanceof IASTClassSpecifier)
/* (non-Javadoc) {
* @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getClassNameType() names.push(((IASTOffsetableNamedElement)parent).getName());
*/ parent = ((IASTDeclaration)parent).getOwnerScope();
public ClassNameType getClassNameType() { }
return classNameType; break;
} }
if (names.size() != 0)
/* (non-Javadoc) {
* @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getClassKind() qualifiedNames = new String[names.size()];
*/ int counter = 0;
public ClassKind getClassKind() { while (!names.empty())
return classKind; qualifiedNames[counter++] = (String)names.pop();
} }
else
/* (non-Javadoc) qualifiedNames = null;
* @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getBaseClauses() }
*/ private final String[] qualifiedNames;
public Iterator getBaseClauses() { private IASTTemplateDeclaration templateOwner = null;
return baseClauses.iterator(); private final String name;
} private List declarations = new ArrayList();
private List baseClauses = new ArrayList();
/* (non-Javadoc) private AccessVisibility access;
* @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getCurrentVisiblity() private NamedOffsets offsets = new NamedOffsets();
*/ private final ClassNameType classNameType;
public AccessVisibility getCurrentVisibilityMode() { private final ClassKind classKind;
return access; /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getClassNameType()
*/
/* (non-Javadoc) public ClassNameType getClassNameType()
* @see org.eclipse.cdt.core.parser.ast.IASTScope#getDeclarations() {
*/ return classNameType;
public Iterator getDeclarations() { }
return declarations.iterator(); /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getClassKind()
*/
/* (non-Javadoc) public ClassKind getClassKind()
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement#getName() {
*/ return classKind;
public String getName() { }
return name; /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getBaseClauses()
*/
/* (non-Javadoc) public Iterator getBaseClauses()
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement#getElementNameOffset() {
*/ return baseClauses.iterator();
public int getElementNameOffset() { }
return offsets.getElementNameOffset(); /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTClassSpecifier#getCurrentVisiblity()
*/
/* (non-Javadoc) public AccessVisibility getCurrentVisibilityMode()
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement#setNameOffset(int) {
*/ return access;
public void setNameOffset(int o) { }
offsets.setNameOffset(o); /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTScope#getDeclarations()
*/
/* (non-Javadoc) public Iterator getDeclarations()
* @see org.eclipse.cdt.core.parser.ast.IASTTemplatedDeclaration#getOwnerTemplateDeclaration() {
*/ return declarations.iterator();
public IASTTemplateDeclaration getOwnerTemplateDeclaration() { }
return templateOwner; /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement#getName()
*/
/* (non-Javadoc) public String getName()
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#setStartingOffset(int) {
*/ return name;
public void setStartingOffset(int o) { }
offsets.setStartingOffset(o); /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement#getElementNameOffset()
*/
/* (non-Javadoc) public int getElementNameOffset()
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#setEndingOffset(int) {
*/ return offsets.getElementNameOffset();
public void setEndingOffset(int o) { }
offsets.setEndingOffset(o); /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement#setNameOffset(int)
*/
/* (non-Javadoc) public void setNameOffset(int o)
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#getElementStartingOffset() {
*/ offsets.setNameOffset(o);
public int getElementStartingOffset() { }
return offsets.getElementStartingOffset(); /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTTemplatedDeclaration#getOwnerTemplateDeclaration()
*/
/* (non-Javadoc) public IASTTemplateDeclaration getOwnerTemplateDeclaration()
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#getElementEndingOffset() {
*/ return templateOwner;
public int getElementEndingOffset() { }
return offsets.getElementEndingOffset(); /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#setStartingOffset(int)
*/
/* (non-Javadoc) public void setStartingOffset(int o)
* @see org.eclipse.cdt.internal.core.parser.ast.quick.IASTQScope#addDeclaration(org.eclipse.cdt.core.parser.ast.IASTDeclaration) {
*/ offsets.setStartingOffset(o);
public void addDeclaration(IASTDeclaration declaration) { }
declarations.add( declaration ); /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#setEndingOffset(int)
*/
/* (non-Javadoc) public void setEndingOffset(int o)
* @see org.eclipse.cdt.internal.core.parser.ast.quick.IASTQClassSpecifier#addBaseClass(org.eclipse.cdt.core.parser.ast.IASTBaseSpecifier) {
*/ offsets.setEndingOffset(o);
public void addBaseClass(IASTBaseSpecifier baseSpecifier) { }
baseClauses.add( baseSpecifier ); /* (non-Javadoc)
} * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#getElementStartingOffset()
*/
public int getElementStartingOffset()
{
return offsets.getElementStartingOffset();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#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)
{
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)
{
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 2003-06-24 John Camelon
Updates for ISourceElementRequestor - elaborated types & enumerations. Updates for ISourceElementRequestor - elaborated types & enumerations.

View file

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

View file

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

View file

@ -53,47 +53,12 @@ public class LineNumberTest extends TestCase {
fileIn = new FileInputStream(fileName); 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 public void testDOMLineNos() throws Exception
{ {
DOMBuilder domBuilder = new DOMBuilder(); DOMBuilder domBuilder = new DOMBuilder();
IParser parser = ParserFactory.createParser( ParserFactory.createScanner( new InputStreamReader( fileIn ), null, null, null, ParserMode.QUICK_PARSE ), domBuilder, ParserMode.QUICK_PARSE ); 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"); if( ! parser.parse() ) fail( "Parse of file failed");
List macros = domBuilder.getTranslationUnit().getMacros(); List macros = domBuilder.getTranslationUnit().getMacros();

View file

@ -22,6 +22,7 @@ import java.util.StringTokenizer;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
import junit.framework.Test; import junit.framework.Test;
import org.eclipse.cdt.core.parser.ILineOffsetReconciler;
import org.eclipse.cdt.core.parser.IParser; import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.ParserFactory; import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserMode; 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; String output = null;
int lineNumber = -1; int lineNumber = -1;
try { try {
lineNumber = parser.getLineNumberForOffset(parser.getLastErrorOffset()); lineNumber = mapping.getLineNumberForOffset(parser.getLastErrorOffset());
} catch (Exception ex) {} } catch (Exception ex) {}
if (e instanceof AssertionFailedError) { if (e instanceof AssertionFailedError) {
@ -178,7 +179,7 @@ public class TortureTest extends FractionalAutomatedTest {
thread.stop(); thread.stop();
reportHang(testCode, filePath); reportHang(testCode, filePath);
} else if (thread.result != null) { } else if (thread.result != null) {
reportException(thread.result, filePath, thread.parser); reportException(thread.result, filePath, thread.parser, thread.mapping );
} }
} else { } else {
// gcc probably didn't expect this test to pass. // gcc probably didn't expect this test to pass.
@ -198,15 +199,16 @@ public class TortureTest extends FractionalAutomatedTest {
public Throwable result = null; public Throwable result = null;
public IParser parser = null; public IParser parser = null;
public boolean quickParse = true; public boolean quickParse = true;
public ILineOffsetReconciler mapping = null;
public void run(){ public void run(){
try { try {
DOMBuilder domBuilder = new DOMBuilder(); DOMBuilder domBuilder = new DOMBuilder();
IParser parser = ParserFactory.createParser( IParser parser = ParserFactory.createParser(
ParserFactory.createScanner( new StringReader( code ), null, null, null, ParserMode.QUICK_PARSE ), nullCallback, ParserMode.QUICK_PARSE); 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.setCppNature(cppNature);
parser.mapLineNumbers(true);
assertTrue(parser.parse()); 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 2003-06-24 Thomas Fletcher
- Proposals will now include additional help information with them - Proposals will now include additional help information with them

View file

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