# This is a really cool parser from Charles Crain
# that finds appropriate class names in Java source.
- # A regular expression that will find, in a java file,
+ # A regular expression that will find, in a java file: newlines;
# any alphanumeric token (keyword, class name, specifier); open or
# close brackets; a single-line comment "//"; the multi-line comment
# begin and end tokens /* and */; single or double quotes; and
# single or double quotes preceeded by a backslash.
- _reToken = re.compile(r'(//[^\r\n]*|\\[\'"]|[\'"\{\}]|[A-Za-z_][\w\.]*|' +
+ _reToken = re.compile(r'(\n|//|\\[\'"]|[\'"\{\}]|[A-Za-z_][\w\.]*|' +
r'/\*|\*/)')
class OuterState:
def parseToken(self, token):
if token[:2] == '//':
- pass # ignore comment
+ return IgnoreState('\n', self)
elif token == '/*':
return IgnoreState('*/', self)
elif token == '{':
# outer_state is always an instance of OuterState
self.outer_state = outer_state
def parseToken(self, token):
- # the only token we get should be the name of the class.
+ # the next non-whitespace token should be the name of the class
+ if token == '\n':
+ return self
self.outer_state.listClasses.append(token)
return self.outer_state
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+import sys
import unittest
import SCons.Tool.JavaCommon
class parse_javaTestCase(unittest.TestCase):
- def test_empty(self):
+ def test_bare_bones(self):
"""Test a bare-bones class"""
pkg_dir, classes = SCons.Tool.JavaCommon.parse_java("""\
]
assert classes == expect, classes
+ def test_comments(self):
+ """Test a class with comments"""
+
+ pkg_dir, classes = SCons.Tool.JavaCommon.parse_java("""\
+package com.sub.foo;
+
+import java.rmi.Naming;
+import java.rmi.RemoteException;
+import java.rmi.RMISecurityManager;
+import java.rmi.server.UnicastRemoteObject;
+
+public class Example1 extends UnicastRemoteObject implements Hello {
+
+ public Example1() throws RemoteException {
+ super();
+ }
+
+ public String sayHello() {
+ return "Hello World!";
+ }
+
+ public static void main(String args[]) {
+ if (System.getSecurityManager() == null) {
+ System.setSecurityManager(new RMISecurityManager());
+ }
+ // a comment
+ try {
+ Example1 obj = new Example1();
+
+ Naming.rebind("//myhost/HelloServer", obj);
+
+ System.out.println("HelloServer bound in registry");
+ } catch (Exception e) {
+ System.out.println("Example1 err: " + e.getMessage());
+ e.printStackTrace();
+ }
+ }
+}
+""")
+
+ assert pkg_dir == 'com/sub/foo', pkg_dir
+ assert classes == ['Example1'], classes
+
if __name__ == "__main__":
suite = unittest.TestSuite()
tclasses = [ parse_javaTestCase ]