diff options
-rw-r--r-- | src/engine/SCons/Tool/JavaCommon.py | 10 | ||||
-rw-r--r-- | src/engine/SCons/Tool/JavaCommonTests.py | 46 |
2 files changed, 51 insertions, 5 deletions
diff --git a/src/engine/SCons/Tool/JavaCommon.py b/src/engine/SCons/Tool/JavaCommon.py index 3348bc0..6b69c73 100644 --- a/src/engine/SCons/Tool/JavaCommon.py +++ b/src/engine/SCons/Tool/JavaCommon.py @@ -42,12 +42,12 @@ if java_parsing: # 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: @@ -95,7 +95,7 @@ if java_parsing: def parseToken(self, token): if token[:2] == '//': - pass # ignore comment + return IgnoreState('\n', self) elif token == '/*': return IgnoreState('*/', self) elif token == '{': @@ -163,7 +163,9 @@ if java_parsing: # 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 diff --git a/src/engine/SCons/Tool/JavaCommonTests.py b/src/engine/SCons/Tool/JavaCommonTests.py index 96bd31d..1b5421c 100644 --- a/src/engine/SCons/Tool/JavaCommonTests.py +++ b/src/engine/SCons/Tool/JavaCommonTests.py @@ -23,13 +23,14 @@ __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("""\ @@ -123,6 +124,49 @@ class Private { ] 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 ] |