Improve how method parameter lists are lexed

This commit is contained in:
Ben Gruver 2014-02-17 14:22:57 -08:00 committed by Connor Tumbleson
parent e1742d8547
commit b66bf0500b
6 changed files with 119 additions and 22 deletions

View File

@ -129,8 +129,10 @@ tokens {
OPEN_BRACE; OPEN_BRACE;
OPEN_PAREN; OPEN_PAREN;
PACKED_SWITCH_DIRECTIVE; PACKED_SWITCH_DIRECTIVE;
PARAM_LIST; PARAM_LIST_END;
PARAM_LIST_OR_ID; PARAM_LIST_START;
PARAM_LIST_OR_ID_END;
PARAM_LIST_OR_ID_START;
PARAMETER_DIRECTIVE; PARAMETER_DIRECTIVE;
POSITIVE_INTEGER_LITERAL; POSITIVE_INTEGER_LITERAL;
PRIMITIVE_TYPE; PRIMITIVE_TYPE;
@ -530,6 +532,9 @@ registers_directive
$statements_and_directives::hasRegistersDirective=true; $statements_and_directives::hasRegistersDirective=true;
}; };
param_list_or_id
: PARAM_LIST_OR_ID_START PRIMITIVE_TYPE+ PARAM_LIST_OR_ID_END;
/*identifiers are much more general than most languages. Any of the below can either be /*identifiers are much more general than most languages. Any of the below can either be
the indicated type OR an identifier, depending on the context*/ the indicated type OR an identifier, depending on the context*/
simple_name simple_name
@ -543,7 +548,7 @@ simple_name
| BOOL_LITERAL -> SIMPLE_NAME[$BOOL_LITERAL] | BOOL_LITERAL -> SIMPLE_NAME[$BOOL_LITERAL]
| NULL_LITERAL -> SIMPLE_NAME[$NULL_LITERAL] | NULL_LITERAL -> SIMPLE_NAME[$NULL_LITERAL]
| REGISTER -> SIMPLE_NAME[$REGISTER] | REGISTER -> SIMPLE_NAME[$REGISTER]
| PARAM_LIST_OR_ID -> SIMPLE_NAME[$PARAM_LIST_OR_ID] | param_list_or_id -> { adaptor.create(SIMPLE_NAME, $param_list_or_id.text) }
| PRIMITIVE_TYPE -> SIMPLE_NAME[$PRIMITIVE_TYPE] | PRIMITIVE_TYPE -> SIMPLE_NAME[$PRIMITIVE_TYPE]
| VOID_TYPE -> SIMPLE_NAME[$VOID_TYPE] | VOID_TYPE -> SIMPLE_NAME[$VOID_TYPE]
| ANNOTATION_VISIBILITY -> SIMPLE_NAME[$ANNOTATION_VISIBILITY] | ANNOTATION_VISIBILITY -> SIMPLE_NAME[$ANNOTATION_VISIBILITY]
@ -582,8 +587,8 @@ method_prototype
-> ^(I_METHOD_PROTOTYPE[$start, "I_METHOD_PROTOTYPE"] ^(I_METHOD_RETURN_TYPE type_descriptor) param_list?); -> ^(I_METHOD_PROTOTYPE[$start, "I_METHOD_PROTOTYPE"] ^(I_METHOD_RETURN_TYPE type_descriptor) param_list?);
param_list param_list
: PARAM_LIST -> { parseParamList((CommonToken)$PARAM_LIST) } : PARAM_LIST_START nonvoid_type_descriptor* PARAM_LIST_END -> nonvoid_type_descriptor*
| PARAM_LIST_OR_ID -> { parseParamList((CommonToken)$PARAM_LIST_OR_ID) } | PARAM_LIST_OR_ID_START PRIMITIVE_TYPE* PARAM_LIST_OR_ID_END -> PRIMITIVE_TYPE*
| nonvoid_type_descriptor*; | nonvoid_type_descriptor*;
type_descriptor type_descriptor

View File

@ -465,10 +465,10 @@ method returns[BuilderMethod ret]
}; };
method_prototype returns[List<String> parameters, String returnType] method_prototype returns[List<String> parameters, String returnType]
: ^(I_METHOD_PROTOTYPE ^(I_METHOD_RETURN_TYPE type_descriptor) field_type_list) : ^(I_METHOD_PROTOTYPE ^(I_METHOD_RETURN_TYPE type_descriptor) method_type_list)
{ {
$returnType = $type_descriptor.type; $returnType = $type_descriptor.type;
$parameters = $field_type_list.types; $parameters = $method_type_list.types;
}; };
method_name_and_prototype returns[String name, List<SmaliMethodParameter> parameters, String returnType] method_name_and_prototype returns[String name, List<SmaliMethodParameter> parameters, String returnType]
@ -488,7 +488,7 @@ method_name_and_prototype returns[String name, List<SmaliMethodParameter> parame
$returnType = $method_prototype.returnType; $returnType = $method_prototype.returnType;
}; };
field_type_list returns[List<String> types] method_type_list returns[List<String> types]
@init @init
{ {
$types = Lists.newArrayList(); $types = Lists.newArrayList();

View File

@ -222,6 +222,8 @@ ArrayDescriptor = "[" + ({PrimitiveType} | {ClassDescriptor})
Type = {PrimitiveType} | {ClassDescriptor} | {ArrayDescriptor} Type = {PrimitiveType} | {ClassDescriptor} | {ArrayDescriptor}
%state PARAM_LIST_OR_ID
%state PARAM_LIST
%state STRING %state STRING
%state CHAR %state CHAR
@ -289,6 +291,20 @@ Type = {PrimitiveType} | {ClassDescriptor} | {ArrayDescriptor}
' { beginStringOrChar(CHAR); sb.append('\''); } ' { beginStringOrChar(CHAR); sb.append('\''); }
} }
<PARAM_LIST_OR_ID> {
{PrimitiveType} { return newToken(PRIMITIVE_TYPE); }
[^] { yypushback(1); yybegin(YYINITIAL); return newToken(PARAM_LIST_OR_ID_END); }
<<EOF>> { yybegin(YYINITIAL); return newToken(PARAM_LIST_OR_ID_END); }
}
<PARAM_LIST> {
{PrimitiveType} { return newToken(PRIMITIVE_TYPE); }
{ClassDescriptor} { return newToken(CLASS_DESCRIPTOR); }
{ArrayDescriptor} { return newToken(ARRAY_DESCRIPTOR); }
[^] { yypushback(1); yybegin(YYINITIAL); return newToken(PARAM_LIST_END); }
<<EOF>> { yybegin(YYINITIAL); return newToken(PARAM_LIST_END); }
}
<STRING> { <STRING> {
"\"" { sb.append('"'); return endStringOrChar(STRING_LITERAL); } "\"" { sb.append('"'); return endStringOrChar(STRING_LITERAL); }
@ -587,8 +603,19 @@ Type = {PrimitiveType} | {ClassDescriptor} | {ArrayDescriptor}
V { return newToken(VOID_TYPE); } V { return newToken(VOID_TYPE); }
{ClassDescriptor} { return newToken(CLASS_DESCRIPTOR); } {ClassDescriptor} { return newToken(CLASS_DESCRIPTOR); }
{ArrayDescriptor} { return newToken(ARRAY_DESCRIPTOR); } {ArrayDescriptor} { return newToken(ARRAY_DESCRIPTOR); }
{PrimitiveType} {PrimitiveType}+ { return newToken(PARAM_LIST_OR_ID); }
{Type} {Type}+ { return newToken(PARAM_LIST); } {PrimitiveType} {PrimitiveType}+ {
yypushback(yylength());
yybegin(PARAM_LIST_OR_ID);
return newToken(PARAM_LIST_OR_ID_START);
}
{Type} {Type}+ {
yypushback(yylength());
yybegin(PARAM_LIST);
return newToken(PARAM_LIST_START);
}
{SimpleName} { return newToken(SIMPLE_NAME); } {SimpleName} { return newToken(SIMPLE_NAME); }
"<" {SimpleName} ">" { return newToken(MEMBER_NAME); } "<" {SimpleName} ">" { return newToken(MEMBER_NAME); }
} }

View File

@ -552,7 +552,13 @@ METHOD_DIRECTIVE(".method")
ACCESS_SPEC("public") ACCESS_SPEC("public")
SIMPLE_NAME("setCallForwardingOption") SIMPLE_NAME("setCallForwardingOption")
OPEN_PAREN("(") OPEN_PAREN("(")
PARAM_LIST("IILjava/lang/String;ILandroid/os/Message;") PARAM_LIST_START("")
PRIMITIVE_TYPE("I")
PRIMITIVE_TYPE("I")
CLASS_DESCRIPTOR("Ljava/lang/String;")
PRIMITIVE_TYPE("I")
CLASS_DESCRIPTOR("Landroid/os/Message;")
PARAM_LIST_END("")
CLOSE_PAREN(")") CLOSE_PAREN(")")
VOID_TYPE("V") VOID_TYPE("V")
REGISTERS_DIRECTIVE(".registers") REGISTERS_DIRECTIVE(".registers")
@ -697,7 +703,12 @@ CLASS_DESCRIPTOR("Lcom/android/internal/telephony/gsm/GSMPhone$MyHandler;")
ARROW("->") ARROW("->")
SIMPLE_NAME("obtainMessage") SIMPLE_NAME("obtainMessage")
OPEN_PAREN("(") OPEN_PAREN("(")
PARAM_LIST("IIILjava/lang/Object;") PARAM_LIST_START("")
PRIMITIVE_TYPE("I")
PRIMITIVE_TYPE("I")
PRIMITIVE_TYPE("I")
CLASS_DESCRIPTOR("Ljava/lang/Object;")
PARAM_LIST_END("")
CLOSE_PAREN(")") CLOSE_PAREN(")")
CLASS_DESCRIPTOR("Landroid/os/Message;") CLASS_DESCRIPTOR("Landroid/os/Message;")
INSTRUCTION_FORMAT11x("move-result-object") INSTRUCTION_FORMAT11x("move-result-object")
@ -749,7 +760,14 @@ CLASS_DESCRIPTOR("Lcom/android/internal/telephony/CommandsInterface;")
ARROW("->") ARROW("->")
SIMPLE_NAME("setCallForward") SIMPLE_NAME("setCallForward")
OPEN_PAREN("(") OPEN_PAREN("(")
PARAM_LIST("IIILjava/lang/String;ILandroid/os/Message;") PARAM_LIST_START("")
PRIMITIVE_TYPE("I")
PRIMITIVE_TYPE("I")
PRIMITIVE_TYPE("I")
CLASS_DESCRIPTOR("Ljava/lang/String;")
PRIMITIVE_TYPE("I")
CLASS_DESCRIPTOR("Landroid/os/Message;")
PARAM_LIST_END("")
CLOSE_PAREN(")") CLOSE_PAREN(")")
VOID_TYPE("V") VOID_TYPE("V")
LINE_DIRECTIVE(".line") LINE_DIRECTIVE(".line")

View File

@ -47,4 +47,6 @@ L[Ljava/lang/String;
[java/lang/String; [java/lang/String;
[; [;
<linit> <linit>
III

View File

@ -24,13 +24,52 @@ ARRAY_DESCRIPTOR("[D")
ARRAY_DESCRIPTOR("[Ljava/lang/String;") ARRAY_DESCRIPTOR("[Ljava/lang/String;")
ARRAY_DESCRIPTOR("[LI/I/I;") ARRAY_DESCRIPTOR("[LI/I/I;")
PARAM_LIST_OR_ID("IIIII") PARAM_LIST_OR_ID_START("")
PARAM_LIST_OR_ID("ZBSCIJFD") PRIMITIVE_TYPE("I")
PARAM_LIST("ILa;[La;[I") PRIMITIVE_TYPE("I")
PARAM_LIST("Ljava/lang/String;Ljava/lang/String;") PRIMITIVE_TYPE("I")
PARAM_LIST("[I[I[I") PRIMITIVE_TYPE("I")
PARAM_LIST("[I[Z") PRIMITIVE_TYPE("I")
PARAM_LIST("[I[Ljava/lang/String;") PARAM_LIST_OR_ID_END("")
PARAM_LIST_OR_ID_START("")
PRIMITIVE_TYPE("Z")
PRIMITIVE_TYPE("B")
PRIMITIVE_TYPE("S")
PRIMITIVE_TYPE("C")
PRIMITIVE_TYPE("I")
PRIMITIVE_TYPE("J")
PRIMITIVE_TYPE("F")
PRIMITIVE_TYPE("D")
PARAM_LIST_OR_ID_END("")
PARAM_LIST_START("")
PRIMITIVE_TYPE("I")
CLASS_DESCRIPTOR("La;")
ARRAY_DESCRIPTOR("[La;")
ARRAY_DESCRIPTOR("[I")
PARAM_LIST_END("")
PARAM_LIST_START("")
CLASS_DESCRIPTOR("Ljava/lang/String;")
CLASS_DESCRIPTOR("Ljava/lang/String;")
PARAM_LIST_END("")
PARAM_LIST_START("")
ARRAY_DESCRIPTOR("[I")
ARRAY_DESCRIPTOR("[I")
ARRAY_DESCRIPTOR("[I")
PARAM_LIST_END("")
PARAM_LIST_START("")
ARRAY_DESCRIPTOR("[I")
ARRAY_DESCRIPTOR("[Z")
PARAM_LIST_END("")
PARAM_LIST_START("")
ARRAY_DESCRIPTOR("[I")
ARRAY_DESCRIPTOR("[Ljava/lang/String;")
PARAM_LIST_END("")
MEMBER_NAME("<init>") MEMBER_NAME("<init>")
MEMBER_NAME("<clinit>") MEMBER_NAME("<clinit>")
@ -47,4 +86,10 @@ INVALID_TOKEN("[") VOID_TYPE("V")
INVALID_TOKEN("[") SIMPLE_NAME("java") INVALID_TOKEN("/") SIMPLE_NAME("lang") INVALID_TOKEN("/") SIMPLE_NAME("String") INVALID_TOKEN(";") INVALID_TOKEN("[") SIMPLE_NAME("java") INVALID_TOKEN("/") SIMPLE_NAME("lang") INVALID_TOKEN("/") SIMPLE_NAME("String") INVALID_TOKEN(";")
INVALID_TOKEN("[") INVALID_TOKEN(";") INVALID_TOKEN("[") INVALID_TOKEN(";")
MEMBER_NAME("<linit>") MEMBER_NAME("<linit>")
PARAM_LIST_OR_ID_START("")
PRIMITIVE_TYPE("I")
PRIMITIVE_TYPE("I")
PRIMITIVE_TYPE("I")
PARAM_LIST_OR_ID_END("")