eclipse plugin: Add a simple lua parser that parses the tags from lua code.
This commit is contained in:
parent
b50f805750
commit
71df4a5222
6 changed files with 126 additions and 5 deletions
|
@ -384,7 +384,7 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider
|
|||
if (ruleProposal)
|
||||
proposal.append("["); //$NON-NLS-1$
|
||||
proposal.append(tag.getName());
|
||||
proposal.append("\n"); //$NON-NLS-1$
|
||||
proposal.append("]\n"); //$NON-NLS-1$
|
||||
for(TagKey key : tag.getKeyChildren())
|
||||
{
|
||||
if (key.isRequired())
|
||||
|
|
|
@ -103,7 +103,7 @@ public class PreprocessorUtils
|
|||
if (filesTimeStamps_.containsKey(filePath) &&
|
||||
filesTimeStamps_.get(filePath) >= new File(filePath).lastModified())
|
||||
{
|
||||
Logger.getInstance().log("skipped preprocessing a non-modified file: " + filePath); //$NON-NLS-1$
|
||||
Logger.getInstance().logTool("skipped preprocessing a non-modified file: " + filePath); //$NON-NLS-1$
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -164,7 +164,7 @@ public class PreprocessorUtils
|
|||
arguments.add( definesArg.toString() );
|
||||
}
|
||||
|
||||
Logger.getInstance().log("preprocessing file: " + filePath); //$NON-NLS-1$
|
||||
Logger.getInstance().logTool("preprocessing file: " + filePath); //$NON-NLS-1$
|
||||
ExternalToolInvoker wesnoth = new ExternalToolInvoker(
|
||||
paths.getWesnothExecutablePath( ),
|
||||
arguments);
|
||||
|
|
|
@ -233,5 +233,4 @@ public class Tag
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,8 +8,13 @@
|
|||
*******************************************************************************/
|
||||
package org.wesnoth.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import org.wesnoth.Logger;
|
||||
|
||||
public class StringUtils
|
||||
{
|
||||
|
@ -241,4 +246,27 @@ public class StringUtils
|
|||
{
|
||||
return (target == null || target.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of groups found by the specified regex
|
||||
* in the specified string.
|
||||
* The regex is applied case insensitive
|
||||
* @param regexStr The regex used to extract the groups
|
||||
* @param targetString The string on which to apply the regex
|
||||
* @return The list of strings which matched the regex
|
||||
*/
|
||||
public static List<String> getGroups( String regexStr, String targetString )
|
||||
{
|
||||
List<String> groupList = new ArrayList<String>();
|
||||
try {
|
||||
Pattern regex = Pattern.compile(regexStr, Pattern.CASE_INSENSITIVE);
|
||||
Matcher regexMatcher = regex.matcher(targetString);
|
||||
while (regexMatcher.find()) {
|
||||
groupList.add(regexMatcher.group());
|
||||
}
|
||||
} catch (PatternSyntaxException ex) {
|
||||
Logger.getInstance( ).logException( ex );
|
||||
}
|
||||
return groupList;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2011 by Timotei Dolean <timotei21@gmail.com>
|
||||
*
|
||||
* 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.wesnoth.wml.core;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.wesnoth.Logger;
|
||||
import org.wesnoth.schema.Tag;
|
||||
import org.wesnoth.utils.StringUtils;
|
||||
|
||||
/**
|
||||
* This is a simple Lua parser that returns the found interesting tokens
|
||||
* from lua code
|
||||
*/
|
||||
public class SimpleLuaParser
|
||||
{
|
||||
private List<Tag> tags_;
|
||||
private Reader reader_;
|
||||
|
||||
private String TAG_REGEX = "wml_actions\\..+\\( *cfg *\\)";
|
||||
|
||||
public SimpleLuaParser( String contents )
|
||||
{
|
||||
tags_ = new ArrayList<Tag> ( );
|
||||
reader_ = new StringReader( contents == null ? "" : contents );
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the lua code and gathers the list of tags
|
||||
*/
|
||||
public void parse()
|
||||
{
|
||||
BufferedReader reader = new BufferedReader( reader_ );
|
||||
String line = null;
|
||||
|
||||
try
|
||||
{
|
||||
while( ( line = reader.readLine( ) ) != null ) {
|
||||
List<String> tokens = StringUtils.getGroups( TAG_REGEX, line );
|
||||
|
||||
for ( String token : tokens ) {
|
||||
// parse the tag name
|
||||
String tagName = token.substring(
|
||||
token.indexOf( '.' ) + 1,
|
||||
token.length( ) - 1 );
|
||||
|
||||
tags_.add( new Tag( tagName, '*' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
catch ( IOException e ) {
|
||||
Logger.getInstance( ).logException( e );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parsed tags from the lua code
|
||||
* @return A list with Tags
|
||||
*/
|
||||
public List<Tag> getTags()
|
||||
{
|
||||
return tags_;
|
||||
}
|
||||
}
|
|
@ -8,12 +8,16 @@
|
|||
*******************************************************************************/
|
||||
package org.wesnoth.wml.core;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.emf.common.util.TreeIterator;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
|
||||
import org.wesnoth.Logger;
|
||||
import org.wesnoth.projects.ProjectCache;
|
||||
import org.wesnoth.schema.Tag;
|
||||
import org.wesnoth.utils.ResourceUtils;
|
||||
import org.wesnoth.utils.WMLUtils;
|
||||
import org.wesnoth.wml.WMLKey;
|
||||
|
@ -34,6 +38,7 @@ public class SimpleWMLParser
|
|||
protected IFile file_;
|
||||
protected ProjectCache projectCache_;
|
||||
protected int dependencyIndex_;
|
||||
protected List<Tag> tags_;
|
||||
|
||||
/**
|
||||
* Creates a new parser for the specified file
|
||||
|
@ -59,6 +64,8 @@ public class SimpleWMLParser
|
|||
file_ = file;
|
||||
projectCache_ = projCache;
|
||||
|
||||
tags_ = new ArrayList<Tag>();
|
||||
|
||||
dependencyIndex_ = ResourceUtils.getDependencyIndex( file );
|
||||
}
|
||||
|
||||
|
@ -115,7 +122,10 @@ public class SimpleWMLParser
|
|||
}
|
||||
}
|
||||
else if ( object instanceof WMLLuaCode ) {
|
||||
|
||||
SimpleLuaParser luaParser = new SimpleLuaParser(
|
||||
( ( WMLLuaCode ) object ).getValue( ) );
|
||||
luaParser.parse( );
|
||||
tags_.addAll( luaParser.getTags( ) );
|
||||
}
|
||||
}
|
||||
//TODO: parse custom events
|
||||
|
@ -200,6 +210,15 @@ public class SimpleWMLParser
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parsed tags
|
||||
* @return A list of tags
|
||||
*/
|
||||
public List<Tag> getParsedTags()
|
||||
{
|
||||
return tags_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parsed WMLConfig
|
||||
* @return Returns the parsed WMLConfig
|
||||
|
|
Loading…
Add table
Reference in a new issue