Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » PHP Development Tools (PDT) » getLineNumber
getLineNumber [message #1059833] Tue, 21 May 2013 15:30 Go to next message
Laleh m is currently offline Laleh m
Messages: 6
Registered: May 2013
Junior Member
Hello,

I am using the org.eclipse.php.internal.core for parsing PHP code. Here is a small test php file to pars:


<?php 

$name = 'Elijah'; 
$yearBorn = 1975; 
$currentYear = 2005;
$age = $currentYear - $yearBorn;
?> 



To get the line number I do as following


public class FactExtractorVisitor implements Visitor { 

...

public boolean visit(Program node) {
	// TODO Auto-generated method stub
	this.root=node;
	return true;
	}
...
	
public boolean visit(Variable node) {
    // TODO Auto-generated method stub
    System.out.println(node.toString());
    System.out.println(this.root.getLineNumber(node.getStart()));
    return true;
}



}



the printout is like:

<Variable start='8' length='5' isDollared='true'>
<Identifier start='9' length='4' name='name'/>
</Variable>
1
name

<Variable start='27' length='9' isDollared='true'>
<Identifier start='28' length='8' name='yearBorn'/>
</Variable>
1

<Variable start='46' length='12' isDollared='true'>
<Identifier start='47' length='11' name='currentYear'/>
</Variable>
1

<Variable start='67' length='4' isDollared='true'>
<Identifier start='68' length='3' name='age'/>
</Variable>
1

why it returns 1 for the line number. What is the correct way to retrieve line number?

Thanks
Re: getLineNumber [message #1060084 is a reply to message #1059833] Wed, 22 May 2013 23:13 Go to previous messageGo to next message
Toshihiro Izumi is currently offline Toshihiro Izumi
Messages: 342
Registered: July 2009
Location: Japan
Senior Member
As you can see org.eclipse.php.internal.core.ast.nodes.Program.getLineNumber(int), the method needs org.eclipse.php.internal.core.ast.nodes.Program.lineEndTable and it is set by org.eclipse.php.internal.core.ast.nodes.Program.setLineEndTable(int[]) which is called from org.eclipse.php.internal.ui.editor.validation.PhpReconcilingStrategy.reconcile(ISourceModule, boolean) and org.eclipse.php.internal.ui.viewsupport.SelectionListenerWithASTManager.PartListenerGroup.calculateASTandInform(ISourceModule, ITextSelection, IProgressMonitor).

In short, Program.getLineNumber() is available on the php editor and the AST within it. It may not be available in your visitor implementation.
>public boolean visit(Program node) {
node must be the AST in the editor.

For example,
PHPStructuredEditor phpStructuredEditor = // get the instance somehow
IModelElement element = phpStructuredEditor.getModelElement();
if (element instanceof ISourceModule) {
    try {
        program = SharedASTProvider.getAST((ISourceModule) element, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
        ...
    } catch (ModelException | IOException e) {
    }
}

I don't recommend this way because it may return unexpected(incorrect) value in a certain case.

Other way is using org.eclipse.jface.text.Document.
For example,
public boolean visit(Variable node) {
    // TODO Auto-generated method stub
    System.out.println(node.toString());
    Document document = new Document(node.getProgramRoot().getSourceModule().getSource());
    System.out.println(document.getLineOfOffset(node.getStart()) + 1);
    return true;
}

Re: getLineNumber [message #1060267 is a reply to message #1060084] Thu, 23 May 2013 13:59 Go to previous messageGo to next message
Laleh m is currently offline Laleh m
Messages: 6
Registered: May 2013
Junior Member
Thank you very much for replying,

When I use the org.eclipse.jface.text.Document in "org.eclipse.text.jar" I get the following exception. I did add the "org.eclipse.text.jar"to the classpath that is under the runtime tab of the MANIFEST.MF.

When running on command line I specify this jar through -cp.

I do'not have any compile errors and when clinking on type Document in my visitor class, the declaration of class Document is opened so I don't know why I get this exception:


!SESSION 2013-05-23 12:53:14.842 -----------------------------------------------
eclipse.buildId=M20130204-1200
java.version=1.7.0_10-ea
java.vendor=Oracle Corporation
BootLoader constants: OS=macosx, ARCH=x86_64, WS=cocoa, NL=en_US
Framework arguments: -application phpparser.parser.Runner TestPHPExamples
Command-line arguments: -application phpparser.parser.Runner TestPHPExamples

!ENTRY org.eclipse.osgi 4 0 2013-05-23 12:53:16.527
!MESSAGE Application error
!STACK 1
java.lang.NoClassDefFoundError: org/eclipse/jface/text/Document
at phpparser.parser.visitors.FactExtractorVisitor.visit(FactExtractorVisitor.java:1033)
at org.eclipse.php.internal.core.ast.nodes.Variable.accept0(Variable.java:112)
at org.eclipse.php.internal.core.ast.nodes.ASTNode.accept(ASTNode.java:290)
at org.eclipse.php.internal.core.ast.nodes.GlobalStatement.childrenAccept(GlobalStatement.java:80)
at org.eclipse.php.internal.core.ast.nodes.GlobalStatement.accept0(GlobalStatement.java:73)
at org.eclipse.php.internal.core.ast.nodes.ASTNode.accept(ASTNode.java:290)
at org.eclipse.php.internal.core.ast.nodes.Program.childrenAccept(Program.java:195)
at org.eclipse.php.internal.core.ast.nodes.Program.accept0(Program.java:188)
at org.eclipse.php.internal.core.ast.nodes.ASTNode.accept(ASTNode.java:290)
at phpparser.parser.Parser.parseFile(Parser.java:101)
at phpparser.parser.Parser.parseScriptProject(Parser.java:75)
at phpparser.parser.Parser.parseProject(Parser.java:32)
at phpparser.parser.Runner.run(Runner.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.eclipse.equinox.internal.app.EclipseAppContainer.callMethodWithException(EclipseAppContainer.java:587)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:198)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jface.text.Document
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 31 more


Any suggestion?
Re: getLineNumber [message #1060471 is a reply to message #1060267] Fri, 24 May 2013 20:24 Go to previous messageGo to next message
Toshihiro Izumi is currently offline Toshihiro Izumi
Messages: 342
Registered: July 2009
Location: Japan
Senior Member
>I did add the "org.eclipse.text.jar"to the classpath that is under the runtime tab of the MANIFEST.MF.
Why.
Add 'org.eclipse.jface.text' to 'Dependencies'.
Re: getLineNumber [message #1060675 is a reply to message #1060471] Mon, 27 May 2013 12:11 Go to previous message
Laleh m is currently offline Laleh m
Messages: 6
Registered: May 2013
Junior Member
THANKS A LOT Razz
Previous Topic:Linking eclipse simulator and FORTRAN language
Next Topic:Projects missing on Samba share on Eclipse launch
Goto Forum:
  


Current Time: Wed Jul 23 22:34:20 EDT 2014

Powered by FUDForum. Page generated in 0.09134 seconds