Commit d37a7a8b authored by Kristof Szabados's avatar Kristof Szabados
Browse files

Merge pull request #29 from alovassy/master

LocationAST dependency removed from cfg section handlers and cfg edit…
parents ce3d9b93 44f1fa1f
......@@ -14,8 +14,12 @@ import org.antlr.v4.runtime.tree.ParseTreeVisitor;
*/
public class AddedParseTree implements ParseTree {
/** text of the parse tree node. It doesn't have to be 1 token, it can be any string. */
private String mText;
/** parent rule */
private ParseTree mParent;
public AddedParseTree( final String aText ) {
mText = aText;
}
......@@ -52,7 +56,7 @@ public class AddedParseTree implements ParseTree {
@Override
public ParseTree getParent() {
return null;
return mParent;
}
@Override
......@@ -69,4 +73,9 @@ public class AddedParseTree implements ParseTree {
mText = aText;
}
public void setParent( final ParseTree aParent ) {
mParent = aParent;
}
}
/******************************************************************************
* Copyright (c) 2000-2015 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
******************************************************************************/
package org.eclipse.titan.common.parsers;
public class CommonHiddenStreamToken {
private String mText;
private CommonHiddenStreamToken mHiddenBefore;
private CommonHiddenStreamToken mHiddenAfter;
public CommonHiddenStreamToken(final String aText) {
mText = aText;
}
public String getText() {
return mText;
}
public CommonHiddenStreamToken getHiddenBefore() {
return mHiddenBefore;
}
public CommonHiddenStreamToken getHiddenAfter() {
return mHiddenAfter;
}
}
/******************************************************************************
* Copyright (c) 2000-2015 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
******************************************************************************/
package org.eclipse.titan.common.parsers;
/**
* This interface merely serves for us as a common point between the different LocationAST used by the different parsers. So that we can report
* problems on a common base.
*
* @author Kristof Szabados
* */
public interface ILocationAST {
/**
* @return the offset at the beginning of the represented element.
* */
int getOffset();
/**
* Sets the offset for the beginning of the represented element.
*
* @param offset the offset to set
* */
void setOffset(int offset);
/**
* @return the offset at the end of the represented element.
* */
int getEndOffset();
/**
* Sets the offset for the end of the represented element.
*
* @param endOffset the offset to set
* */
void setEndOffset(int endOffset);
/**
* @return the line in which the beginning of the represented element started
* */
int getLine();
}
/******************************************************************************
* Copyright (c) 2000-2015 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
******************************************************************************/
package org.eclipse.titan.common.parsers;
import java.util.ArrayList;
import java.util.List;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
//TODO: rename to CfgParseTree, inherit from ParserRuleContext
/**
* @author Kristof Szabados
* @author Arpad Lovassy
*/
public class LocationAST {
private TokenStream mTokenStream;
private ParserRuleContext mRule;
private CommonHiddenStreamToken mHiddenAfter;
private CommonHiddenStreamToken mHiddenBefore;
public LocationAST( final String aText ) {
setText( aText );
}
public LocationAST( final ParserRuleContext aRule, TokenStream aTokenStream ) {
setRule( aRule );
mTokenStream = aTokenStream;
}
public LocationAST( final ParserRuleContext aRule ) {
setRule( aRule );
}
public LocationAST( final Token aToken ) {
setToken( aToken );
}
private void setToken( Token aToken ) {
ParserRuleContext rule = new ParserRuleContext();
rule.addChild( aToken );
setRule( rule );
}
private void setRule( ParserRuleContext aRule ) {
mRule = aRule;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("{ ");
if ( mRule != null ) {
sb.append("{ " + mRule.start + ", ");
sb.append("" + mRule.stop + ", ");
sb.append("" + mRule.getChildCount() + ", " );
sb.append(mRule.getText() + "}" );
} else {
sb.append("null");
}
sb.append(", " );
sb.append(mHiddenAfter+", ");
sb.append(mHiddenBefore+"}");
return sb.toString();
}
public String getText() {
String text = mRule != null ? mRule.getText() : null;
return text;
}
public void setText( final String aText ) {
CommonToken token = new CommonToken( 0, aText );
setToken( token );
}
public ParserRuleContext getRule() {
return mRule;
}
public LocationAST getParent() {
final ParserRuleContext parentRule = mRule != null ? mRule.getParent() : null;
return new LocationAST( parentRule, mTokenStream );
}
public LocationAST getFirstChild() {
if ( mRule == null ) {
return null;
}
final ParserRuleContext parent = mRule.getParent();
if ( parent == null || parent.getChildCount() == 0 ) {
return null;
}
final ParseTree firstParseTree = parent.getChild(0);
if ( ! ( firstParseTree instanceof ParserRuleContext ) ) {
return null;
}
final ParserRuleContext firstRule = (ParserRuleContext) firstParseTree;
return new LocationAST( firstRule, mTokenStream );
}
public void setFirstChild(final LocationAST aNode) {
if ( mRule == null ) {
return;
}
if ( mRule.children == null ) {
mRule.children = new ArrayList<ParseTree>();
}
final List<ParseTree> children = mRule.children;
children.set(0, aNode.getRule());
}
public LocationAST getNextSibling() {
final int childIndex = getChildIndex();
if ( childIndex < 0 ) {
// mRule or mRule.getParent() or mRule.getParent().children is null
return null;
}
final ParserRuleContext parent = mRule.getParent();
if ( childIndex + 1 >= parent.getChildCount() ) {
// current node is the last child, there is no next sibling
return null;
}
final ParseTree nextParseTree = parent.getChild( childIndex + 1 );
if ( ! ( nextParseTree instanceof ParserRuleContext ) ) {
return null;
}
final ParserRuleContext nextRule = (ParserRuleContext) nextParseTree;
return new LocationAST( nextRule, mTokenStream );
}
public void setNextSibling(final LocationAST aNode ) {
final int childIndex = getChildIndex();
if ( childIndex < 0 ) {
// mRule or mRule.getParent() or mRule.getParent().children is null
return;
}
final List<ParseTree> children = mRule.getParent().children;
children.set(childIndex + 1, aNode.getRule());
}
public void addChild(final LocationAST aNode) {
if ( mRule == null ) {
return;
}
if ( mRule.children == null ) {
mRule.children = new ArrayList<ParseTree>();
}
final List<ParseTree> children = mRule.children;
children.add(aNode.getRule());
}
public void removeChildren() {
if ( mRule == null ) {
return;
}
final List<ParseTree> children = mRule.children;
if ( children != null ) {
children.clear();
}
}
public CommonHiddenStreamToken getHiddenBefore() {
return mHiddenBefore;
}
public void setHiddenBefore(final CommonHiddenStreamToken aToken) {
mHiddenBefore = aToken;
}
public CommonHiddenStreamToken getHiddenAfter() {
return mHiddenAfter;
}
public void setHiddenAfter(final CommonHiddenStreamToken aToken) {
mHiddenAfter = aToken;
}
/**
* @return the index of the current node in its parent's child list
* -1 if there is no parent (root node)
*/
private int getChildIndex() {
if ( mRule == null ) {
return -1;
}
final ParserRuleContext parent = mRule.getParent();
if ( parent == null ) {
// no parent (root node)
return -1;
}
if ( parent.children == null ) {
// it should not happen, program error:
// parent's children list is not filled
return -1;
}
for ( int i = 0; i < parent.getChildCount(); i++ ) {
final ParseTree child = parent.getChild(i);
if ( this.mRule == child ) {
return i;
}
}
// it should not happen, program error:
// children list is empty, or current node is not listed in the children list of its parent, or wrong parent
return -1;
}
public int getType() {
//TODO: implement
return 0;
}
public TokenStream getTokenStream() {
return mTokenStream;
}
public void setTokenStream(TokenStream mTokenStream) {
this.mTokenStream = mTokenStream;
}
}
......@@ -27,19 +27,6 @@ public class TITANMarker {
this.priority = priority;
}
public TITANMarker(final String message, final ILocationAST start, final ILocationAST end, final int severity, final int priority) {
this.message = message;
if (start != null) {
this.line = start.getLine();
this.offset = start.getOffset();
}
if (end != null) {
this.endOffset = end.getEndOffset();
}
this.severity = severity;
this.priority = priority;
}
public String getMessage() {
return message;
}
......
......@@ -9,7 +9,6 @@ package org.eclipse.titan.common.parsers.cfg;
import org.antlr.v4.runtime.Token;
import org.eclipse.core.resources.IFile;
import org.eclipse.titan.common.parsers.ILocationAST;
/**
* The Location class represents a location in configuration files. It was
......@@ -29,18 +28,10 @@ public final class CfgLocation {
setLocation(location);
}
public CfgLocation(final IFile file) {
this(file, (ILocationAST)null, (ILocationAST)null);
}
public CfgLocation(final IFile file, final int line, final int offset, final int endOffset) {
setLocation(file, line, offset, endOffset);
}
public CfgLocation(final IFile file, final ILocationAST startTok, final ILocationAST endTok) {
setLocation(file, startTok, endTok);
}
/**
* Constructor for ANTLR v4 tokens
* @param aFile the parsed file
......@@ -55,22 +46,6 @@ public final class CfgLocation {
aEndToken.getStopIndex()+1);
}
private final void setLocation(final IFile file, final ILocationAST startTok, final ILocationAST endTok) {
this.file = file;
if (startTok == null) {
line = -1;
offset = -1;
} else {
line = startTok.getLine();
offset = startTok.getOffset();
}
if (endTok == null) {
endOffset = -1;
} else {
endOffset = endTok.getEndOffset();
}
}
private final void setLocation(final CfgLocation location) {
file = location.getFile();
line = location.getLine();
......
......@@ -45,6 +45,7 @@ import java.util.regex.Pattern;
private List<ISection> mSections = new ArrayList<ISection>();
private Map<String, CfgDefinitionInformation> mDefinitions = new HashMap<String, CfgDefinitionInformation>();
private List<String> mIncludeFiles = new ArrayList<String>();
private IFile mActualFile = null;
......@@ -319,18 +320,19 @@ pr_ConfigFile:
pr_Section returns [ ISection section ]:
{ $section = null;
}
( pr_MainControllerSection
| i = pr_IncludeSection { $section = $i.includeSection; includeSectionHandler.setLastSectionRoot( $i.ctx ); }
| pr_OrderedIncludeSection
| pr_ExecuteSection
( mc = pr_MainControllerSection { mcSectionHandler.setLastSectionRoot( $mc.ctx ); }
| i = pr_IncludeSection { $section = $i.includeSection;
includeSectionHandler.setLastSectionRoot( $i.ctx ); }
| oi = pr_OrderedIncludeSection { orderedIncludeSectionHandler.setLastSectionRoot( $oi.ctx ); }
| e = pr_ExecuteSection { executeSectionHandler.setLastSectionRoot( $e.ctx ); }
| d = pr_DefineSection { defineSectionHandler.setLastSectionRoot( $d.ctx ); }
| pr_ExternalCommandsSection
| pr_TestportParametersSection
| pr_GroupsSection
| pr_ModuleParametersSection
| pr_ComponentsSection
| pr_LoggingSection
| pr_ProfilerSection
| ec = pr_ExternalCommandsSection { externalCommandsSectionHandler.setLastSectionRoot( $ec.ctx ); }
| tp = pr_TestportParametersSection { testportParametersHandler.setLastSectionRoot( $tp.ctx ); }
| g = pr_GroupsSection { groupSectionHandler.setLastSectionRoot( $g.ctx ); }
| mp = pr_ModuleParametersSection { moduleParametersHandler.setLastSectionRoot( $mp.ctx ); }
| c = pr_ComponentsSection { componentSectionHandler.setLastSectionRoot( $c.ctx ); }
| l = pr_LoggingSection { loggingSectionHandler.setLastSectionRoot( $l.ctx ); }
| p = pr_ProfilerSection //TODO:{ profilerSectionHandler.setLastSectionRoot( $p.ctx ); }
)
;
......
......@@ -13,12 +13,13 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.titan.common.logging.ErrorReporter;
import org.eclipse.titan.common.parsers.LocationAST;
import org.eclipse.titan.common.path.PathConverter;
/**
......@@ -31,8 +32,8 @@ import org.eclipse.titan.common.path.PathConverter;
public final class ConfigFileHandler {
private static final String ORIGINALLY_FROM = "//This part was originally found in file: ";
private Map<Path, LocationAST> originalASTs = new HashMap<Path, LocationAST>();
private Map<Path, LocationAST> resolvedASTs = new HashMap<Path, LocationAST>();
private Map<Path, ParseTree> originalASTs = new HashMap<Path, ParseTree>();
private Map<Path, ParseTree> resolvedASTs = new HashMap<Path, ParseTree>();
private final Map<String, CfgDefinitionInformation> definesMap = new HashMap<String, CfgDefinitionInformation>();
private final List<Path> processedFiles = new ArrayList<Path>();
private final List<Path> toBeProcessedFiles = new ArrayList<Path>();
......@@ -57,6 +58,9 @@ public final class ConfigFileHandler {
private String mLogFileName = null;
/** the token stream of the last parsing, it is needed to print the hidden tokens */
private TokenStream mTokenStream = null;
public ConfigFileHandler(){
// Do nothing
}
......@@ -183,7 +187,7 @@ public final class ConfigFileHandler {
* Creates the String representation of the parsed tree of all of the parsed files.
* Can be used to create a single configuration file instead of the hierarchy already existing.
*
* @see #print(org.eclipse.titan.common.parsers.LocationAST, StringBuilder)
* @see #print(ParseTree, StringBuilder)
* @param disallowedNodes the list of nodes that should be left out of the process.
* */
public StringBuilder toStringResolved(final List<Integer> disallowedNodes){
......@@ -193,11 +197,11 @@ public final class ConfigFileHandler {
/**
* Creates the String representation of the parsed tree starting from the provided root node.
*
* @see #print(org.eclipse.titan.common.parsers.LocationAST, StringBuilder)
* @see #print(ParseTree, StringBuilder)
* @param asts the root node of the parse tree to start from.
* @param disallowedNodes the list of nodes that should be left out of the process.
* */
private StringBuilder toStringInternal(final Map<Path, LocationAST> asts, final List<Integer> disallowedNodes){
private StringBuilder toStringInternal(final Map<Path, ParseTree> asts, final List<Integer> disallowedNodes){
if(asts == null || asts.isEmpty()){
return new StringBuilder();
}
......@@ -206,7 +210,7 @@ public final class ConfigFileHandler {
final StringBuilder stringbuilder = new StringBuilder();
stringbuilder.setLength(0);
for(final Entry<Path, LocationAST> entry:asts.entrySet()){
for(final Entry<Path, ParseTree> entry:asts.entrySet()){
stringbuilder.append(ORIGINALLY_FROM).
append(entry.getKey().toOSString()).append('\n');
if(entry.getValue() != null){
......@@ -243,7 +247,9 @@ public final class ConfigFileHandler {
unixDomainSocket = analyzer.isUnixDomainSocketEnabled();
}
final LocationAST rootNode = new LocationAST( analyzer.getParseTreeRoot(), analyzer.getTokenStream() );
mTokenStream = analyzer.getTokenStream();
final ParseTree rootNode = analyzer.getParseTreeRoot();
if ( rootNode != null ) {
originalASTs.put( actualFile, rootNode );
......@@ -269,8 +275,8 @@ public final class ConfigFileHandler {
*
* @param root the tree root to start at.
*/
private void print(final LocationAST aRoot, final StringBuilder aSb) {
ConfigTreeNodeUtilities.print( aRoot.getRule(), aRoot.getTokenStream(), aSb, disallowedNodes );
private void print(final ParseTree aRoot, final StringBuilder aSb) {
ConfigTreeNodeUtilities.print( aRoot, mTokenStream, aSb, disallowedNodes );
}
}
......@@ -18,8 +18,6 @@ import org.antlr.v4.runtime.WritableToken;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNodeImpl;
import org.eclipse.titan.common.parsers.AddedParseTree;
import org.eclipse.titan.common.parsers.CommonHiddenStreamToken;
import org.eclipse.titan.common.parsers.LocationAST;
/**
* Basic utility class for configuration file AST operations.
......@@ -36,124 +34,40 @@ public final class ConfigTreeNodeUtilities {
/**
* Returns the string value of the given AST subtree.
*
* @param root
* the root of subtree
* @param aRoot the root of subtree
* @return the String value of the AST