Author: timotei
Date: Tue Jul 26 17:32:47 2011
New Revision: 50430

URL: http://svn.gna.org/viewcvs/wesnoth?rev=50430&view=rev
Log:
eclipse plugin: Add a simple lua parser that parses
the tags from lua code.

Added:
    
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/wml/core/SimpleLuaParser.java
Modified:
    
trunk/utils/umc_dev/org.wesnoth.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java
    
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/preprocessor/PreprocessorUtils.java
    trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/schema/Tag.java
    trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/utils/StringUtils.java
    
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/wml/core/SimpleWMLParser.java

Modified: 
trunk/utils/umc_dev/org.wesnoth.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/umc_dev/org.wesnoth.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java?rev=50430&r1=50429&r2=50430&view=diff
==============================================================================
--- 
trunk/utils/umc_dev/org.wesnoth.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java
 (original)
+++ 
trunk/utils/umc_dev/org.wesnoth.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java
 Tue Jul 26 17:32:47 2011
@@ -384,7 +384,7 @@
                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())

Modified: 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/preprocessor/PreprocessorUtils.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/preprocessor/PreprocessorUtils.java?rev=50430&r1=50429&r2=50430&view=diff
==============================================================================
--- 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/preprocessor/PreprocessorUtils.java
 (original)
+++ 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/preprocessor/PreprocessorUtils.java
 Tue Jul 26 17:32:47 2011
@@ -103,7 +103,7 @@
                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 @@
                                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);

Modified: trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/schema/Tag.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/schema/Tag.java?rev=50430&r1=50429&r2=50430&view=diff
==============================================================================
--- trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/schema/Tag.java (original)
+++ trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/schema/Tag.java Tue Jul 26 
17:32:47 2011
@@ -233,5 +233,4 @@
                        return 0;
                }
        }
-
 }

Modified: trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/utils/StringUtils.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/utils/StringUtils.java?rev=50430&r1=50429&r2=50430&view=diff
==============================================================================
--- trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/utils/StringUtils.java 
(original)
+++ trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/utils/StringUtils.java Tue 
Jul 26 17:32:47 2011
@@ -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 @@
        {
            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;
+    }
 }

Added: 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/wml/core/SimpleLuaParser.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/wml/core/SimpleLuaParser.java?rev=50430&view=auto
==============================================================================
--- 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/wml/core/SimpleLuaParser.java 
(added)
+++ 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/wml/core/SimpleLuaParser.java 
Tue Jul 26 17:32:47 2011
@@ -1,0 +1,75 @@
+/*******************************************************************************
+ * Copyright (c) 2011 by Timotei Dolean <[email protected]>
+ *
+ * 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_;
+    }
+}

Modified: 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/wml/core/SimpleWMLParser.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/wml/core/SimpleWMLParser.java?rev=50430&r1=50429&r2=50430&view=diff
==============================================================================
--- 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/wml/core/SimpleWMLParser.java 
(original)
+++ 
trunk/utils/umc_dev/org.wesnoth/src/org/wesnoth/wml/core/SimpleWMLParser.java 
Tue Jul 26 17:32:47 2011
@@ -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 @@
     protected IFile file_;
     protected ProjectCache projectCache_;
     protected int dependencyIndex_;
+    protected List<Tag> tags_;
 
     /**
      * Creates a new parser for the specified file
@@ -58,6 +63,8 @@
         config_ = Preconditions.checkNotNull( config );
         file_ = file;
         projectCache_ = projCache;
+
+        tags_ = new ArrayList<Tag>();
 
         dependencyIndex_ = ResourceUtils.getDependencyIndex( file );
     }
@@ -115,7 +122,10 @@
                 }
             }
             else if ( object instanceof WMLLuaCode ) {
-
+                SimpleLuaParser luaParser = new SimpleLuaParser(
+                        ( ( WMLLuaCode ) object ).getValue( ) );
+                luaParser.parse( );
+                tags_.addAll( luaParser.getTags( ) );
             }
         }
         //TODO: parse custom events
@@ -201,6 +211,15 @@
     }
 
     /**
+     * Returns the parsed tags
+     * @return A list of tags
+     */
+    public List<Tag> getParsedTags()
+    {
+        return tags_;
+    }
+
+    /**
      * Returns the parsed WMLConfig
      * @return Returns the parsed WMLConfig
      */


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to