diff --git a/ANNOTATION.md b/ANNOTATION.md index ed81127e..cc90098d 100644 --- a/ANNOTATION.md +++ b/ANNOTATION.md @@ -102,6 +102,7 @@ | /self::\*\[@InternalType='Module'\]//\*\[@InternalType='Try'\]/\*\[@InternalType='Try\.orelse'\] | IfElse | | /self::\*\[@InternalType='Module'\]//\*\[@InternalType='TryExcept'\] | TryCatch, Statement | | /self::\*\[@InternalType='Module'\]//\*\[@InternalType='ExceptHandler'\] | TryCatch, Statement | +| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='ExceptHandler\.name'\] | SimpleIdentifier | | /self::\*\[@InternalType='Module'\]//\*\[@InternalType='TryFinally'\] | TryFinally, Statement | | /self::\*\[@InternalType='Module'\]//\*\[@InternalType='Raise'\] | Throw, Statement | | /self::\*\[@InternalType='Module'\]//\*\[@InternalType='With'\] | BlockScope, Statement | diff --git a/driver/normalizer/annotation.go b/driver/normalizer/annotation.go index 6cf71f11..932bd0d7 100644 --- a/driver/normalizer/annotation.go +++ b/driver/normalizer/annotation.go @@ -214,6 +214,7 @@ var AnnotationRules = On(Any).Self( ), On(HasInternalType(pyast.TryExcept)).Roles(TryCatch, Statement), // py2 On(HasInternalType(pyast.ExceptHandler)).Roles(TryCatch, Statement), // py3 + On(HasInternalType("ExceptHandler.name")).Roles(SimpleIdentifier), On(HasInternalType(pyast.TryFinally)).Roles(TryFinally, Statement), On(HasInternalType(pyast.Raise)).Roles(Throw, Statement), // FIXME: review, add path for the body and items childs diff --git a/driver/normalizer/parser.go b/driver/normalizer/parser.go index 177d2c4c..defa662b 100644 --- a/driver/normalizer/parser.go +++ b/driver/normalizer/parser.go @@ -66,13 +66,14 @@ var ToNoder = &native.ObjectToNoder{ "FunctionDef": {"body": true, "decorator_list": true}, "Lambda": {"body": true}, "arguments": {"defaults": true}, - "Try": {"body": true, "orelse": true, "finalbody": true, "handlers": true}, + "Try": {"body": true, "orelse": true, "finalbody": true}, "Raise": {"args": true}, "ClassDef": {"body": true, "bases": true, "decorator_list": true, "keywords": true}, }, PromotedPropertyStrings: map[string]map[string]bool{ - "alias": {"asname": true}, - "ImportFrom": {"module": true}, + "alias": {"asname": true}, + "ImportFrom": {"module": true}, + "ExceptHandler": {"name": true}, }, // FIXME: test[ast_type=Compare].comparators is a list?? (should be "right") } diff --git a/tests/except.py.uast b/tests/except.py.uast index deb5fefe..4592fd69 100644 --- a/tests/except.py.uast +++ b/tests/except.py.uast @@ -236,32 +236,45 @@ Module { . . . . . . } . . . . . } . . . . } -. . . . 2: Try.handlers { -. . . . . Roles: TryCatch +. . . . 2: ExceptHandler { +. . . . . Roles: TryCatch,Statement +. . . . . StartPosition: { +. . . . . . Offset: 69 +. . . . . . Line: 4 +. . . . . . Col: 25 +. . . . . } +. . . . . EndPosition: { +. . . . . . Offset: 104 +. . . . . . Line: 5 +. . . . . . Col: 33 +. . . . . } . . . . . Properties: { -. . . . . . promotedPropertyList: true +. . . . . . ExceptHandler.name: e +. . . . . . internalRole: handlers . . . . . } . . . . . Children: { -. . . . . . 0: ExceptHandler { -. . . . . . . Roles: TryCatch,Statement -. . . . . . . TOKEN "e" +. . . . . . 0: Expr { +. . . . . . . Roles: Expression . . . . . . . StartPosition: { -. . . . . . . . Offset: 69 -. . . . . . . . Line: 4 -. . . . . . . . Col: 25 +. . . . . . . . Offset: 76 +. . . . . . . . Line: 5 +. . . . . . . . Col: 5 . . . . . . . } . . . . . . . EndPosition: { . . . . . . . . Offset: 104 . . . . . . . . Line: 5 . . . . . . . . Col: 33 . . . . . . . } +. . . . . . . Properties: { +. . . . . . . . internalRole: body +. . . . . . . } . . . . . . . Children: { -. . . . . . . . 0: Expr { -. . . . . . . . . Roles: Expression +. . . . . . . . 0: Call { +. . . . . . . . . Roles: Call,Expression . . . . . . . . . StartPosition: { -. . . . . . . . . . Offset: 76 +. . . . . . . . . . Offset: 77 . . . . . . . . . . Line: 5 -. . . . . . . . . . Col: 5 +. . . . . . . . . . Col: 6 . . . . . . . . . } . . . . . . . . . EndPosition: { . . . . . . . . . . Offset: 104 @@ -269,15 +282,16 @@ Module { . . . . . . . . . . Col: 33 . . . . . . . . . } . . . . . . . . . Properties: { -. . . . . . . . . . internalRole: body +. . . . . . . . . . internalRole: value . . . . . . . . . } . . . . . . . . . Children: { -. . . . . . . . . . 0: Call { -. . . . . . . . . . . Roles: Call,Expression +. . . . . . . . . . 0: StringLiteral { +. . . . . . . . . . . Roles: StringLiteral,Expression,CallPositionalArgument +. . . . . . . . . . . TOKEN "someexception catched" . . . . . . . . . . . StartPosition: { -. . . . . . . . . . . . Offset: 77 +. . . . . . . . . . . . Offset: 82 . . . . . . . . . . . . Line: 5 -. . . . . . . . . . . . Col: 6 +. . . . . . . . . . . . Col: 11 . . . . . . . . . . . } . . . . . . . . . . . EndPosition: { . . . . . . . . . . . . Offset: 104 @@ -285,75 +299,82 @@ Module { . . . . . . . . . . . . Col: 33 . . . . . . . . . . . } . . . . . . . . . . . Properties: { -. . . . . . . . . . . . internalRole: value +. . . . . . . . . . . . internalRole: args . . . . . . . . . . . } -. . . . . . . . . . . Children: { -. . . . . . . . . . . . 0: StringLiteral { -. . . . . . . . . . . . . Roles: StringLiteral,Expression,CallPositionalArgument -. . . . . . . . . . . . . TOKEN "someexception catched" -. . . . . . . . . . . . . StartPosition: { -. . . . . . . . . . . . . . Offset: 82 -. . . . . . . . . . . . . . Line: 5 -. . . . . . . . . . . . . . Col: 11 -. . . . . . . . . . . . . } -. . . . . . . . . . . . . EndPosition: { -. . . . . . . . . . . . . . Offset: 104 -. . . . . . . . . . . . . . Line: 5 -. . . . . . . . . . . . . . Col: 33 -. . . . . . . . . . . . . } -. . . . . . . . . . . . . Properties: { -. . . . . . . . . . . . . . internalRole: args -. . . . . . . . . . . . . } -. . . . . . . . . . . . } -. . . . . . . . . . . . 1: Name { -. . . . . . . . . . . . . Roles: CallCallee,SimpleIdentifier,Expression -. . . . . . . . . . . . . TOKEN "print" -. . . . . . . . . . . . . StartPosition: { -. . . . . . . . . . . . . . Offset: 76 -. . . . . . . . . . . . . . Line: 5 -. . . . . . . . . . . . . . Col: 5 -. . . . . . . . . . . . . } -. . . . . . . . . . . . . EndPosition: { -. . . . . . . . . . . . . . Offset: 80 -. . . . . . . . . . . . . . Line: 5 -. . . . . . . . . . . . . . Col: 9 -. . . . . . . . . . . . . } -. . . . . . . . . . . . . Properties: { -. . . . . . . . . . . . . . ctx: Load -. . . . . . . . . . . . . . internalRole: func -. . . . . . . . . . . . . } -. . . . . . . . . . . . } +. . . . . . . . . . } +. . . . . . . . . . 1: Name { +. . . . . . . . . . . Roles: CallCallee,SimpleIdentifier,Expression +. . . . . . . . . . . TOKEN "print" +. . . . . . . . . . . StartPosition: { +. . . . . . . . . . . . Offset: 76 +. . . . . . . . . . . . Line: 5 +. . . . . . . . . . . . Col: 5 +. . . . . . . . . . . } +. . . . . . . . . . . EndPosition: { +. . . . . . . . . . . . Offset: 80 +. . . . . . . . . . . . Line: 5 +. . . . . . . . . . . . Col: 9 +. . . . . . . . . . . } +. . . . . . . . . . . Properties: { +. . . . . . . . . . . . ctx: Load +. . . . . . . . . . . . internalRole: func . . . . . . . . . . . } . . . . . . . . . . } . . . . . . . . . } . . . . . . . . } -. . . . . . . . 1: Name { -. . . . . . . . . Roles: SimpleIdentifier,Expression -. . . . . . . . . TOKEN "SomeException" -. . . . . . . . . StartPosition: { -. . . . . . . . . . Offset: 52 -. . . . . . . . . . Line: 4 -. . . . . . . . . . Col: 8 -. . . . . . . . . } -. . . . . . . . . EndPosition: { -. . . . . . . . . . Offset: 64 -. . . . . . . . . . Line: 4 -. . . . . . . . . . Col: 20 -. . . . . . . . . } -. . . . . . . . . Properties: { -. . . . . . . . . . ctx: Load -. . . . . . . . . . internalRole: type -. . . . . . . . . } -. . . . . . . . } . . . . . . . } . . . . . . } -. . . . . . 1: ExceptHandler { -. . . . . . . Roles: TryCatch,Statement -. . . . . . . TOKEN "" +. . . . . . 1: Name { +. . . . . . . Roles: SimpleIdentifier,Expression +. . . . . . . TOKEN "SomeException" +. . . . . . . StartPosition: { +. . . . . . . . Offset: 52 +. . . . . . . . Line: 4 +. . . . . . . . Col: 8 +. . . . . . . } +. . . . . . . EndPosition: { +. . . . . . . . Offset: 64 +. . . . . . . . Line: 4 +. . . . . . . . Col: 20 +. . . . . . . } +. . . . . . . Properties: { +. . . . . . . . ctx: Load +. . . . . . . . internalRole: type +. . . . . . . } +. . . . . . } +. . . . . . 2: ExceptHandler.name { +. . . . . . . Roles: SimpleIdentifier +. . . . . . . TOKEN "e" +. . . . . . . Properties: { +. . . . . . . . promotedPropertyString: true +. . . . . . . } +. . . . . . } +. . . . . } +. . . . } +. . . . 3: ExceptHandler { +. . . . . Roles: TryCatch,Statement +. . . . . TOKEN "" +. . . . . StartPosition: { +. . . . . . Offset: 107 +. . . . . . Line: 6 +. . . . . . Col: 1 +. . . . . } +. . . . . EndPosition: { +. . . . . . Offset: 131 +. . . . . . Line: 7 +. . . . . . Col: 17 +. . . . . } +. . . . . Properties: { +. . . . . . internalRole: handlers +. . . . . . type: +. . . . . } +. . . . . Children: { +. . . . . . 0: Expr { +. . . . . . . Roles: Expression . . . . . . . StartPosition: { -. . . . . . . . Offset: 107 -. . . . . . . . Line: 6 -. . . . . . . . Col: 1 +. . . . . . . . Offset: 119 +. . . . . . . . Line: 7 +. . . . . . . . Col: 5 . . . . . . . } . . . . . . . EndPosition: { . . . . . . . . Offset: 131 @@ -361,15 +382,15 @@ Module { . . . . . . . . Col: 17 . . . . . . . } . . . . . . . Properties: { -. . . . . . . . type: +. . . . . . . . internalRole: body . . . . . . . } . . . . . . . Children: { -. . . . . . . . 0: Expr { -. . . . . . . . . Roles: Expression +. . . . . . . . 0: Call { +. . . . . . . . . Roles: Call,Expression . . . . . . . . . StartPosition: { -. . . . . . . . . . Offset: 119 +. . . . . . . . . . Offset: 120 . . . . . . . . . . Line: 7 -. . . . . . . . . . Col: 5 +. . . . . . . . . . Col: 6 . . . . . . . . . } . . . . . . . . . EndPosition: { . . . . . . . . . . Offset: 131 @@ -377,15 +398,16 @@ Module { . . . . . . . . . . Col: 17 . . . . . . . . . } . . . . . . . . . Properties: { -. . . . . . . . . . internalRole: body +. . . . . . . . . . internalRole: value . . . . . . . . . } . . . . . . . . . Children: { -. . . . . . . . . . 0: Call { -. . . . . . . . . . . Roles: Call,Expression +. . . . . . . . . . 0: StringLiteral { +. . . . . . . . . . . Roles: StringLiteral,Expression,CallPositionalArgument +. . . . . . . . . . . TOKEN "ayyyy" . . . . . . . . . . . StartPosition: { -. . . . . . . . . . . . Offset: 120 +. . . . . . . . . . . . Offset: 125 . . . . . . . . . . . . Line: 7 -. . . . . . . . . . . . Col: 6 +. . . . . . . . . . . . Col: 11 . . . . . . . . . . . } . . . . . . . . . . . EndPosition: { . . . . . . . . . . . . Offset: 131 @@ -393,44 +415,25 @@ Module { . . . . . . . . . . . . Col: 17 . . . . . . . . . . . } . . . . . . . . . . . Properties: { -. . . . . . . . . . . . internalRole: value +. . . . . . . . . . . . internalRole: args +. . . . . . . . . . . } +. . . . . . . . . . } +. . . . . . . . . . 1: Name { +. . . . . . . . . . . Roles: CallCallee,SimpleIdentifier,Expression +. . . . . . . . . . . TOKEN "print" +. . . . . . . . . . . StartPosition: { +. . . . . . . . . . . . Offset: 119 +. . . . . . . . . . . . Line: 7 +. . . . . . . . . . . . Col: 5 . . . . . . . . . . . } -. . . . . . . . . . . Children: { -. . . . . . . . . . . . 0: StringLiteral { -. . . . . . . . . . . . . Roles: StringLiteral,Expression,CallPositionalArgument -. . . . . . . . . . . . . TOKEN "ayyyy" -. . . . . . . . . . . . . StartPosition: { -. . . . . . . . . . . . . . Offset: 125 -. . . . . . . . . . . . . . Line: 7 -. . . . . . . . . . . . . . Col: 11 -. . . . . . . . . . . . . } -. . . . . . . . . . . . . EndPosition: { -. . . . . . . . . . . . . . Offset: 131 -. . . . . . . . . . . . . . Line: 7 -. . . . . . . . . . . . . . Col: 17 -. . . . . . . . . . . . . } -. . . . . . . . . . . . . Properties: { -. . . . . . . . . . . . . . internalRole: args -. . . . . . . . . . . . . } -. . . . . . . . . . . . } -. . . . . . . . . . . . 1: Name { -. . . . . . . . . . . . . Roles: CallCallee,SimpleIdentifier,Expression -. . . . . . . . . . . . . TOKEN "print" -. . . . . . . . . . . . . StartPosition: { -. . . . . . . . . . . . . . Offset: 119 -. . . . . . . . . . . . . . Line: 7 -. . . . . . . . . . . . . . Col: 5 -. . . . . . . . . . . . . } -. . . . . . . . . . . . . EndPosition: { -. . . . . . . . . . . . . . Offset: 123 -. . . . . . . . . . . . . . Line: 7 -. . . . . . . . . . . . . . Col: 9 -. . . . . . . . . . . . . } -. . . . . . . . . . . . . Properties: { -. . . . . . . . . . . . . . ctx: Load -. . . . . . . . . . . . . . internalRole: func -. . . . . . . . . . . . . } -. . . . . . . . . . . . } +. . . . . . . . . . . EndPosition: { +. . . . . . . . . . . . Offset: 123 +. . . . . . . . . . . . Line: 7 +. . . . . . . . . . . . Col: 9 +. . . . . . . . . . . } +. . . . . . . . . . . Properties: { +. . . . . . . . . . . . ctx: Load +. . . . . . . . . . . . internalRole: func . . . . . . . . . . . } . . . . . . . . . . } . . . . . . . . . } diff --git a/tests/import.py.uast b/tests/import.py.uast index cf795b58..d3158a94 100644 --- a/tests/import.py.uast +++ b/tests/import.py.uast @@ -117,9 +117,9 @@ Module { . . . . Col: 19 . . . } . . . Properties: { +. . . . ImportFrom.module: os . . . . internalRole: body . . . . level: 0 -. . . . module: os . . . } . . . Children: { . . . . 0: alias { @@ -162,9 +162,9 @@ Module { . . . . Col: 32 . . . } . . . Properties: { +. . . . ImportFrom.module: os.path . . . . internalRole: body . . . . level: 0 -. . . . module: os.path . . . } . . . Children: { . . . . 0: alias { diff --git a/tests/issue62.py.uast b/tests/issue62.py.uast index c036c668..85643fda 100644 --- a/tests/issue62.py.uast +++ b/tests/issue62.py.uast @@ -27,9 +27,9 @@ Module { . . . . Col: 19 . . . } . . . Properties: { +. . . . ImportFrom.module: os . . . . internalRole: body . . . . level: 0 -. . . . module: os . . . } . . . Children: { . . . . 0: alias { @@ -125,7 +125,7 @@ Module { . . . . . . Col: 12 . . . . . } . . . . . Properties: { -. . . . . . asname: np +. . . . . . alias.asname: np . . . . . . internalRole: names . . . . . } . . . . . Children: { diff --git a/tests/issue62_b.py.uast b/tests/issue62_b.py.uast index 79897959..846f2ae9 100644 --- a/tests/issue62_b.py.uast +++ b/tests/issue62_b.py.uast @@ -27,9 +27,9 @@ Module { . . . . Col: 31 . . . } . . . Properties: { +. . . . ImportFrom.module: collections . . . . internalRole: body . . . . level: 0 -. . . . module: collections . . . } . . . Children: { . . . . 0: alias { @@ -72,9 +72,9 @@ Module { . . . . Col: 50 . . . } . . . Properties: { +. . . . ImportFrom.module: ast2vec.bblfsh_roles . . . . internalRole: body . . . . level: 0 -. . . . module: ast2vec.bblfsh_roles . . . } . . . Children: { . . . . 0: alias { @@ -148,9 +148,9 @@ Module { . . . . Col: 40 . . . } . . . Properties: { +. . . . ImportFrom.module: ast2vec.repo2.base . . . . internalRole: body . . . . level: 0 -. . . . module: ast2vec.repo2.base . . . } . . . Children: { . . . . 0: alias {