Permalink
Browse files

java: Added signature parser. Test desc/sign parsers in the all test.

For #2
  • Loading branch information...
1 parent 64172a2 commit 2e42897577550a38e578d327f568831bf5a98406 @quarnster committed Mar 19, 2013
Showing with 109 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 build/build.go
  3. +29 −0 java/class_all_test.go
  4. +34 −0 java/signatures/signatures.peg
  5. +44 −0 java/signatures/signatures_test.go
View
@@ -1,4 +1,5 @@
build/*
!build/build.go
java/descriptors/descriptors.go
+java/signatures/signatures.go
clang/parser/parser.go
View
@@ -95,6 +95,7 @@ func main() {
} else if len(b) != 0 {
fmt.Println(string(b))
}
+ buildPeg("../java/signatures/signatures.peg", "TypeSignature,TypeArgument,Entry,FormalTypeParameter")
buildPeg("../java/descriptors/descriptors.peg", "Entry,ComponentType,FieldType,ObjectType")
buildPeg("../clang/parser/parser.peg", "Spacing,Pattern,Completion,Completions,ReturnType,Arguments,Argument,UnhandledStuff,KeyedStuff,Type,TemplateName,Function")
tests := []string{"test"}
@@ -2,6 +2,9 @@ package java
import (
"bytes"
+ "github.com/quarnster/completion/java/descriptors"
+ "github.com/quarnster/completion/java/signatures"
+ "github.com/quarnster/completion/util"
"runtime"
"sync"
"testing"
@@ -12,6 +15,30 @@ func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
+func testparse(c *Class, members []member_info, t *testing.T) {
+ for i := range members {
+ var p descriptors.DESCRIPTORS
+ desc := c.Constant_pool.Lut(members[i].Descriptor_index).String()
+ if !p.Parse(desc) || p.RootNode().Range.End != len(desc) {
+ t.Errorf("Failed to parse descriptor: %s\n%s\n%s", p.Error(), desc, p.RootNode())
+ }
+ var p2 signatures.SIGNATURES
+ for _, attr := range members[i].Attributes {
+ if c.Constant_pool.Lut(attr.Attribute_name_index).String() == "Signature" {
+ br := util.BinaryReader{bytes.NewReader(attr.Info), util.BigEndian}
+ if i16, err := br.Uint16(); err != nil {
+ t.Error(err)
+ } else {
+ sign := c.Constant_pool.Lut(u2(i16)).String()
+ if !p2.Parse(sign) || p2.RootNode().Range.End != len(sign) {
+ t.Errorf("Failed to parse signature: %s\n%s\n%s", p.Error(), desc, p.RootNode())
+ }
+ }
+ }
+ }
+ }
+}
+
func TestLoadAllClasses(t *testing.T) {
classpath, err := DefaultClasspath()
if err != nil {
@@ -32,6 +59,8 @@ func TestLoadAllClasses(t *testing.T) {
outChan <- err
} else {
t.Log("class", c.Constant_pool.Lut(c.This_class))
+ testparse(c, c.RawFields, t)
+ testparse(c, c.RawMethods, t)
}
}
wg.Done()
@@ -0,0 +1,34 @@
+Entry <- ClassSignature / MethodTypeSignature / FieldTypeSignature
+
+# http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3.4
+Identifier <- (![.;[/<>:] .)+
+
+ClassSignature <- FormalTypeParameters? SuperclassSignature SuperinterfaceSignature*
+
+FormalTypeParameters <- '<' FormalTypeParameter+ '>'
+FormalTypeParameter <- Identifier ClassBound InterfaceBound*
+ClassBound <- ':' FieldTypeSignature?
+InterfaceBound <- ':' FieldTypeSignature
+SuperclassSignature <- ClassTypeSignature
+SuperinterfaceSignature <- ClassTypeSignature
+
+FieldTypeSignature <- ClassTypeSignature / ArrayTypeSignature / TypeVariableSignature
+
+ClassTypeSignature <- 'L' PackageSpecifier? SimpleClassTypeSignature ClassTypeSignatureSuffix* ';'
+PackageSpecifier <- Identifier '/' PackageSpecifier*
+SimpleClassTypeSignature <- Identifier TypeArguments?
+ClassTypeSignatureSuffix <- '.' SimpleClassTypeSignature
+TypeVariableSignature <- 'T' Identifier ';'
+TypeArguments <- '<' TypeArgument+ '>'
+TypeArgument <- (WildcardIndicator? FieldTypeSignature) / '*'
+WildcardIndicator <- '+' / '-'
+ArrayTypeSignature <- '[' TypeSignature
+TypeSignature <- FieldTypeSignature / BaseType
+
+
+MethodTypeSignature <- FormalTypeParameters? '(' TypeSignature* ')' ReturnType ThrowsSignature*
+ReturnType <- TypeSignature / VoidDescriptor
+ThrowsSignature <- '^' (ClassTypeSignature / TypeVariableSignature)
+
+VoidDescriptor <- 'V'
+BaseType <- 'B' / 'C' / 'D' / 'F' / 'I' / 'J' / 'S' / 'Z'
@@ -0,0 +1,44 @@
+package signatures
+
+import (
+ "testing"
+)
+
+func TestSignatures(t *testing.T) {
+ tests := []string{
+ "<E:Ljava/lang/Object;>Ljava/util/AbstractList<TE;>;Ljava/util/List<TE;>;Ljava/util/RandomAccess;Ljava/lang/Cloneable;Ljava/io/Serializable;",
+ "(Ljava/util/Collection<+TE;>;)V",
+ "()Ljava/util/Enumeration<TE;>;",
+ "(I)TE;",
+ "()TE;",
+ "()TE;",
+ "(TE;I)V",
+ "(TE;I)V",
+ "(TE;)V",
+ "<T:Ljava/lang/Object;>([TT;)[TT;",
+ "(I)TE;",
+ "(I)TE;",
+ "(ITE;)TE;",
+ "(TE;)Z",
+ "(ITE;)V",
+ "(I)TE;",
+ "(Ljava/util/Collection<*>;)Z",
+ "(Ljava/util/Collection<+TE;>;)Z",
+ "(Ljava/util/Collection<*>;)Z",
+ "(Ljava/util/Collection<*>;)Z",
+ "(ILjava/util/Collection<+TE;>;)Z",
+ "(II)Ljava/util/List<TE;>;",
+ "(I)Ljava/util/ListIterator<TE;>;",
+ "()Ljava/util/ListIterator<TE;>;",
+ "()Ljava/util/Iterator<TE;>;",
+ "Ljava/lang/Object;Ljava/io/Serializable;Ljava/lang/Comparable<Ljava/lang/String;>;Ljava/lang/CharSequence;",
+ "Ljava/util/Comparator<Ljava/lang/String;>;"}
+ for i := range tests {
+ var p SIGNATURES
+ t.Log(tests[i], p.Parse(tests[i]))
+ t.Log(p.RootNode().String())
+ if p.RootNode().Range.End != len(tests[i]) {
+ t.Error(p.Error())
+ }
+ }
+}

0 comments on commit 2e42897

Please sign in to comment.