getLineNumber [message #1059833] |
Tue, 21 May 2013 15:30  |
Eclipse User |
|
|
|
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   |
Eclipse User |
|
|
|
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;
}
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03108 seconds