Commit 0be5886e authored by Árpád Lovassy's avatar Árpád Lovassy Committed by GitHub
Browse files

Merge pull request #90 from alovassy/master

Bug 496451 - bugfix: CfgParser support for resolving environment vari…
parents 6feff96b b07ac816
......@@ -12,9 +12,7 @@ import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenFactory;
......@@ -25,7 +23,6 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.titan.common.logging.ErrorReporter;
import org.eclipse.titan.common.parsers.SyntacticErrorStorage;
import org.eclipse.titan.common.parsers.TITANMarker;
import org.eclipse.titan.common.parsers.TitanListener;
import org.eclipse.titan.common.parsers.cfg.indices.ComponentSectionHandler;
import org.eclipse.titan.common.parsers.cfg.indices.DefineSectionHandler;
......@@ -44,12 +41,7 @@ import org.eclipse.titan.common.utils.StandardCharsets;
* @author Arpad Lovassy
*/
public final class CfgAnalyzer {
private List<TITANMarker> warnings;
private CfgInterval rootInterval;
private Map<String, CfgDefinitionInformation> definitions;
private List<String> mExecuteElements;
private boolean logFileNameDefined = false;
private List<String> includeFiles;
private TitanListener lexerListener = null;
private TitanListener parserListener = null;
......@@ -64,64 +56,12 @@ public final class CfgAnalyzer {
private IncludeSectionHandler orderedIncludeSectionHandler = null;
private DefineSectionHandler defineSectionHandler = null;
private LoggingSectionHandler loggingSectionHandler = null;
private ParserRuleContext mParseTreeRoot = null;
private CommonTokenStream mTokenStream = null;
private String mLogFileName = null;
private Integer mTcpPort = null;
private String mLocalAddress = null;
private Double mKillTimer = null;
private Integer mNumHcs = null;
private Boolean mUnixDomainSocket = null;
private Map<String, String> mComponents = new HashMap<String, String>();
private Map<String, String[]> mGroups = new HashMap<String, String[]>();
public List<TITANMarker> getWarnings() {
return warnings;
}
/**
* Returns true if the log file name was defined in the configuration file.
* @return true if the log file name was defined in the configuration file.
*/
public boolean isLogFileNameDefined() {
return logFileNameDefined;
}
public String getLogFileName() {
return mLogFileName;
}
public Integer getTcpPort() {
return mTcpPort;
}
public String getLocalAddress() {
return mLocalAddress;
}
public Double getKillTimer() {
return mKillTimer;
}
public Integer getNumHcs() {
return mNumHcs;
}
public Boolean isUnixDomainSocketEnabled() {
return mUnixDomainSocket;
}
/** result of the last parsing */
private CfgParseResult mCfgParseResult;
public Map<String, CfgDefinitionInformation> getDefinitions(){
return definitions;
}
public List<String> getExecuteElements() {
return mExecuteElements;
}
public List<String> getIncludeFilePaths(){
return includeFiles;
public CfgParseResult getCfgParseResult() {
return mCfgParseResult;
}
public CfgInterval getRootInterval(){
......@@ -168,14 +108,6 @@ public final class CfgAnalyzer {
return loggingSectionHandler;
}
public ParserRuleContext getParseTreeRoot() {
return mParseTreeRoot;
}
public CommonTokenStream getTokenStream() {
return mTokenStream;
}
public List<SyntacticErrorStorage> getErrorStorage() {
if (lexerListener != null && parserListener != null) {
lexerListener.addAll(parserListener.getErrorsStored());
......@@ -240,15 +172,14 @@ public final class CfgAnalyzer {
parserListener = new TitanListener();
parser.removeErrorListeners(); // remove ConsoleErrorListener
parser.addErrorListener(parserListener);
mParseTreeRoot = parser.pr_ConfigFile();
final ParserRuleContext parseTreeRoot = parser.pr_ConfigFile();
parser.checkMacroErrors();
mTokenStream = tokens;
warnings = parser.getWarnings();
definitions = parser.getDefinitions();
final CfgParseResult cfgParseResult = parser.getCfgParseResult();
mExecuteElements = cfgParseResult.getExecuteElements();
includeFiles = parser.getIncludeFiles();
mCfgParseResult = parser.getCfgParseResult();
// manually add the result parse tree, and its corresponding token stream,
// because they logically belong to here
mCfgParseResult.setParseTreeRoot( parseTreeRoot );
mCfgParseResult.setTokenStream( tokens );
// fill handlers
moduleParametersHandler = parser.getModuleParametersHandler();
......@@ -263,22 +194,5 @@ public final class CfgAnalyzer {
defineSectionHandler = parser.getDefineSectionHandler();
loggingSectionHandler = parser.getLoggingSectionHandler();
logFileNameDefined = cfgParseResult.isLogFileDefined();
mLogFileName = cfgParseResult.getLogFileName();
mTcpPort = cfgParseResult.getTcpPort();
mLocalAddress = cfgParseResult.getLocalAddress();
mKillTimer = cfgParseResult.getKillTimer();
mNumHcs = cfgParseResult.getNumHcs();
mUnixDomainSocket = cfgParseResult.isUnixDomainSocket();
mGroups = cfgParseResult.getGroups();
mComponents = cfgParseResult.getComponents();
}
public Map<String, String[]> getGroups() {
return mGroups;
}
public Map<String, String> getComponents() {
return mComponents;
}
}
......@@ -13,7 +13,7 @@ import java.util.List;
import java.util.Map;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.ParserRuleContext;
import org.eclipse.titan.common.parsers.TITANMarker;
/**
......@@ -23,7 +23,7 @@ import org.eclipse.titan.common.parsers.TITANMarker;
public class CfgParseResult {
/** the result parse tree, which was built during the parsing */
private ParseTree mParseTreeRoot = null;
private ParserRuleContext mParseTreeRoot = null;
/**
* the result of the tokenizing, done by the lexer.
......@@ -95,11 +95,13 @@ public class CfgParseResult {
private List<String> mExecuteElements = new ArrayList<String>();
public ParseTree getParseTreeRoot() {
private Map<String, CfgDefinitionInformation> mDefinitions = new HashMap<String, CfgDefinitionInformation>();
public ParserRuleContext getParseTreeRoot() {
return mParseTreeRoot;
}
public void setParseTreeRoot(ParseTree aParseTreeRoot) {
public void setParseTreeRoot(ParserRuleContext aParseTreeRoot) {
this.mParseTreeRoot = aParseTreeRoot;
}
......@@ -206,4 +208,12 @@ public class CfgParseResult {
public void setExecuteElements(List<String> aExecuteElements) {
this.mExecuteElements = aExecuteElements;
}
public Map< String, CfgDefinitionInformation > getDefinitions() {
return mDefinitions;
}
public void setDefinitions( Map<String,CfgDefinitionInformation> aDefinitions ) {
mDefinitions = aDefinitions;
}
}
......@@ -43,12 +43,6 @@ import java.util.regex.Pattern;
// pattern for matching typed macro string, for example: ${a, float}
private final static Pattern PATTERN_TYPED_MACRO = Pattern.compile("\\$\\s*\\{\\s*([A-Za-z][A-Za-z0-9_]*)\\s*,\\s*[A-Za-z][A-Za-z0-9_]*\\s*\\}");
private List<TITANMarker> mWarnings = new ArrayList<TITANMarker>();
private Map<String, CfgDefinitionInformation> mDefinitions = new HashMap<String, CfgDefinitionInformation>();
private List<String> mIncludeFiles = new ArrayList<String>();
private IFile mActualFile = null;
private Map<String, String> mEnvVariables;
......@@ -102,6 +96,13 @@ import java.util.regex.Pattern;
}
}
/**
* Macro references, which are collected at parse time.
* These are NOT macro definitions (those are collected in mCfgParseResult.mDefinitions),
* these are macro references in expressions with locations and these are possible syntax errors.
* At parse time we don't know, if a macro name is valid, or not, it can be defined later.
* This list is evaluated after parsing. See checkMacroErrors()
*/
private List<Macro> mMacros = new ArrayList<Macro>();
private void addMacro( final String aMacroName, final Token aMacroToken, final String aErrorMessage ) {
......@@ -109,23 +110,7 @@ import java.util.regex.Pattern;
}
public void reportWarning(TITANMarker marker){
mWarnings.add(marker);
}
public List<TITANMarker> getWarnings(){
return mWarnings;
}
public void setDefinitions( Map<String,CfgDefinitionInformation> aDefs ) {
mDefinitions = aDefs;
}
public Map< String, CfgDefinitionInformation > getDefinitions() {
return mDefinitions;
}
public List<String> getIncludeFiles(){
return mIncludeFiles;
mCfgParseResult.getWarnings().add(marker);
}
public void setActualFile(IFile file) {
......@@ -215,7 +200,7 @@ import java.util.regex.Pattern;
*/
public void reportError( final String aMessage, final Token aStartToken, final Token aEndToken ) {
TITANMarker marker = createMarker( aMessage, aStartToken, aEndToken, IMarker.SEVERITY_ERROR, IMarker.PRIORITY_NORMAL );
mWarnings.add(marker);
mCfgParseResult.getWarnings().add(marker);
}
/**
......@@ -224,8 +209,9 @@ import java.util.regex.Pattern;
* @return macro or environment variable value, or null if there is no such definition
*/
private String getDefinitionValue(String aDefinition){
if ( mDefinitions != null && mDefinitions.containsKey( aDefinition ) ) {
return mDefinitions.get( aDefinition ).getValue();
final Map< String, CfgDefinitionInformation > definitions = mCfgParseResult.getDefinitions();
if ( definitions != null && definitions.containsKey( aDefinition ) ) {
return definitions.get( aDefinition ).getValue();
} else if ( mEnvVariables != null && mEnvVariables.containsKey( aDefinition ) ) {
return mEnvVariables.get( aDefinition );
} else {
......@@ -441,7 +427,7 @@ pr_IncludeSection:
INCLUDE_SECTION
( f = STRING2
{ String fileName = $f.getText().substring( 1, $f.getText().length() - 1 );
mIncludeFiles.add( fileName );
mCfgParseResult.getIncludeFiles().add( fileName );
final TerminalNodeImpl node = new TerminalNodeImpl( $f );
node.parent = $ctx;
//another solution for the same thing
......@@ -455,7 +441,7 @@ pr_OrderedIncludeSection:
ORDERED_INCLUDE_SECTION
( f = STRING4
{ String fileName = $f.getText().substring( 1, $f.getText().length() - 1 );
mIncludeFiles.add( fileName );
mCfgParseResult.getIncludeFiles().add( fileName );
final TerminalNodeImpl node = new TerminalNodeImpl( $f );
node.parent = $ctx;
//another solution for the same thing
......@@ -1168,7 +1154,7 @@ pr_MacroAssignment returns [ DefineSectionHandler.Definition definition ]
{ if(name != null && value != null) {
ArrayList<CfgLocation> locations = new ArrayList<CfgLocation>();
locations.add(new CfgLocation(mActualFile, $col, $col));
mDefinitions.put(name, new CfgDefinitionInformation(value, locations));
mCfgParseResult.getDefinitions().put(name, new CfgDefinitionInformation(value, locations));
}
$definition = new DefineSectionHandler.Definition();
......
......@@ -187,43 +187,41 @@ public final class ConfigFileHandler {
StringBuilder sb = new StringBuilder();
// Creates the String representation of the parsed tree starting from the provided root node.
CfgParseTreePrinter.printResolved( originalASTs, sb, disallowedNodes,
ResolveMode.IN_ROW, definesMap, environmentalVariables );
ResolveMode.IN_ROW, definesMap, environmentalVariables );
return sb;
}
private void parseFile(final Path actualFile, final CfgAnalyzer analyzer, final IFile file) {
analyzer.directParse(file, actualFile.toOSString(), null);
if (analyzer.isLogFileNameDefined()) {
final CfgParseResult cfgParseResult = analyzer.getCfgParseResult();
if (cfgParseResult.isLogFileDefined()) {
logFileNameDefined = true;
mLogFileName = analyzer.getLogFileName();
localAddress = analyzer.getLocalAddress();
mLogFileName = cfgParseResult.getLogFileName();
localAddress = cfgParseResult.getLocalAddress();
}
if (analyzer.getTcpPort() != null) {
tcpPort = analyzer.getTcpPort();
if (cfgParseResult.getTcpPort() != null) {
tcpPort = cfgParseResult.getTcpPort();
}
if (analyzer.getLocalAddress() != null) {
localAddress = analyzer.getLocalAddress();
if (cfgParseResult.getLocalAddress() != null) {
localAddress = cfgParseResult.getLocalAddress();
}
if (analyzer.getKillTimer() != null) {
killTimer = analyzer.getKillTimer();
if (cfgParseResult.getKillTimer() != null) {
killTimer = cfgParseResult.getKillTimer();
}
if (analyzer.getNumHcs() != null) {
numHCs = analyzer.getNumHcs();
if (cfgParseResult.getNumHcs() != null) {
numHCs = cfgParseResult.getNumHcs();
}
if (analyzer.isUnixDomainSocketEnabled() != null) {
unixDomainSocket = analyzer.isUnixDomainSocketEnabled();
if (cfgParseResult.isUnixDomainSocket() != null) {
unixDomainSocket = cfgParseResult.isUnixDomainSocket();
}
final ParseTree rootNode = analyzer.getParseTreeRoot();
final ParseTree rootNode = cfgParseResult.getParseTreeRoot();
if ( rootNode != null ) {
CfgParseResult cfgResult = new CfgParseResult();
cfgResult.setParseTreeRoot(rootNode);
cfgResult.setTokenStream( analyzer.getTokenStream() );
originalASTs.put( actualFile, cfgResult );
originalASTs.put( actualFile, cfgParseResult );
final List<String> includeFiles = analyzer.getIncludeFilePaths();
final List<String> includeFiles = cfgParseResult.getIncludeFiles();
for ( String filename:includeFiles ) {
filename = PathConverter.getAbsolutePath( actualFile.toOSString(), filename );
if ( filename != null ) {
......@@ -231,10 +229,10 @@ public final class ConfigFileHandler {
}
}
definesMap.putAll( analyzer.getDefinitions() );
groups.putAll(analyzer.getGroups());
components.putAll(analyzer.getComponents());
executeElements.addAll( analyzer.getExecuteElements() );
definesMap.putAll( cfgParseResult.getDefinitions() );
groups.putAll( cfgParseResult.getGroups() );
components.putAll( cfgParseResult.getComponents() );
executeElements.addAll( cfgParseResult.getExecuteElements() );
}
}
......
......@@ -38,6 +38,7 @@ import org.eclipse.titan.common.parsers.TITANMarker;
import org.eclipse.titan.common.parsers.cfg.CfgAnalyzer;
import org.eclipse.titan.common.parsers.cfg.CfgDefinitionInformation;
import org.eclipse.titan.common.parsers.cfg.CfgLocation;
import org.eclipse.titan.common.parsers.cfg.CfgParseResult;
import org.eclipse.titan.designer.GeneralConstants;
import org.eclipse.titan.designer.AST.Location;
import org.eclipse.titan.designer.AST.MarkerHandler;
......@@ -379,14 +380,15 @@ public final class ProjectConfigurationParser {
CfgAnalyzer cfgAnalyzer = new CfgAnalyzer();
cfgAnalyzer.parse(file, document == null ? null : document.get());
final CfgParseResult cfgParseResult = cfgAnalyzer.getCfgParseResult();
errorsStored = cfgAnalyzer.getErrorStorage();
warnings = cfgAnalyzer.getWarnings();
warnings = cfgParseResult.getWarnings();
if (editor != null && editor.getDocument() != null) {
ConfigEditor parentEditor = editor.getParentEditor();
if ( errorsStored == null || errorsStored.isEmpty() ) {
parentEditor.setParseTreeRoot(cfgAnalyzer.getParseTreeRoot());
parentEditor.setTokenStream(cfgAnalyzer.getTokenStream());
parentEditor.setParseTreeRoot(cfgParseResult.getParseTreeRoot());
parentEditor.setTokenStream(cfgParseResult.getTokenStream());
parentEditor.refresh(cfgAnalyzer);
parentEditor.setErrorMessage(null);
} else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment