This repository was archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
added UAST diff algorithm #309
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9866519
added AST diff
230391c
fixed style/PR notes
da7c149
added a skeleton of diff apply function
cd6e161
the diff seems to be appliable
2e69a68
Code cleanup and several bugfixes in the diff library
b329d1b
fixed a bug when creating/deleting "nil" child
32fccbc
basic tests added
b08b00f
style fixes
f61f2da
simplified decisionType instances
eeffeb8
style changes
5012732
added dependency to the makefile
d8d9b25
moved python script to go
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package diff | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "gopkg.in/bblfsh/sdk.v2/uast/nodes" | ||
| ) | ||
|
|
||
| // Apply is a method that takes a tree (nodes.Node) and applies the current changelist to that | ||
| // tree. | ||
| func (changelist Changelist) Apply(root nodes.Node) nodes.Node { | ||
| nodeDict := make(map[ID]nodes.Node) | ||
| nodes.WalkPreOrder(root, func(node nodes.Node) bool { | ||
| nodeDict[nodes.UniqueKey(node)] = node | ||
| return true | ||
| }) | ||
|
|
||
| for _, change := range changelist { | ||
| switch ch := change.(type) { | ||
| case Create: | ||
| // create a node and add to the dictionary | ||
| nodeDict[nodes.UniqueKey(ch.Node)] = ch.Node | ||
|
|
||
| case Attach: | ||
| // get src and chld from the dictionary, attach (modify src) | ||
| parent, ok := nodeDict[ch.Parent] | ||
| if !ok { | ||
| panic("invalid attachment point") | ||
| } | ||
| child, ok := nodeDict[ch.Child] | ||
| if !ok { | ||
| child, ok = ch.Child.(nodes.Value) | ||
| if !ok { | ||
| panic(fmt.Errorf("unknown type of a child: %v (type %T)", ch.Child, ch.Child)) | ||
| } | ||
| } | ||
|
|
||
| switch key := ch.Key.(type) { | ||
|
|
||
| case String: | ||
| parent := parent.(nodes.Object) | ||
| parent[string(key)] = child | ||
|
|
||
| case Int: | ||
| parent := parent.(nodes.Array) | ||
| parent[int(key)] = child | ||
| } | ||
|
|
||
| case Deatach: | ||
| // get the src from the dictionary, deatach (modify src) | ||
| parent := nodeDict[ch.Parent] | ||
|
|
||
| switch key := ch.Key.(type) { | ||
|
|
||
| case String: | ||
| parent := parent.(nodes.Object) | ||
| delete(parent, string(key)) | ||
|
|
||
| case Int: | ||
| panic(fmt.Errorf("cannot deatach from an Array")) | ||
| } | ||
|
|
||
| case Delete: | ||
| panic(fmt.Errorf("delete is not supported in a Changelist")) | ||
|
|
||
| default: | ||
| panic(fmt.Sprintf("unknown change %v of type %T", change, change)) | ||
| } | ||
| } | ||
| return root | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package diff | ||
|
|
||
| import ( | ||
| "gopkg.in/bblfsh/sdk.v2/uast/nodes" | ||
| ) | ||
|
|
||
| // Changelist is a list of changes, a result of tree difference. Applying all changes from a | ||
| // changelist on a source tree will result in it being transformed into the destination tree. | ||
| type Changelist []Change | ||
|
|
||
| // Change is a single operation performed | ||
| type Change interface { | ||
| isChange() | ||
dennwc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TransactionID() uint64 | ||
| } | ||
|
|
||
| type changeBase struct { | ||
| txID uint64 | ||
| } | ||
|
|
||
| func (changeBase) isChange() {} | ||
| func (ch changeBase) TransactionID() uint64 { return ch.txID } | ||
|
|
||
| // ID is a type representing node unique ID that can be compared in O(1) | ||
| type ID nodes.Comparable | ||
|
|
||
| // Key in a node, string for nodes.Object and int for nodes.Array | ||
| type Key interface{ isKey() } | ||
|
|
||
| // String is a wrapped string type for the Key interface. | ||
| type String string | ||
|
|
||
| // Int is a wrapped int type for the Key interface. | ||
| type Int int | ||
|
|
||
| func (Int) isKey() {} | ||
| func (String) isKey() {} | ||
|
|
||
| // four change types | ||
|
|
||
| // Create a node. Each array and object is created separately. | ||
| type Create struct { | ||
| changeBase | ||
| Node nodes.Node | ||
| } | ||
|
|
||
| // Delete a node by ID | ||
| type Delete struct { | ||
| changeBase | ||
| NodeID ID | ||
| } | ||
|
|
||
| // Attach a node as a child of another node with a given key | ||
| type Attach struct { | ||
| changeBase | ||
| Parent ID | ||
| Key Key | ||
| Child ID | ||
| } | ||
|
|
||
| // Deatach a child from a node | ||
| type Deatach struct { | ||
| changeBase | ||
| Parent ID | ||
| Key Key // Currently deatach semantics are only defined for nodes.Object so the Key is | ||
| // practically always a string | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package diff | ||
|
|
||
| import ( | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "gopkg.in/bblfsh/sdk.v2/uast/nodes" | ||
| uastyml "gopkg.in/bblfsh/sdk.v2/uast/yaml" | ||
| ) | ||
|
|
||
| const dataDir = "./testdata" | ||
|
|
||
| func readUAST(t testing.TB, path string) nodes.Node { | ||
| data, err := ioutil.ReadFile(path) | ||
| require.NoError(t, err) | ||
| nd, err := uastyml.Unmarshal(data) | ||
| require.NoError(t, err) | ||
| return nd | ||
| } | ||
|
|
||
| func TestChangelist(t *testing.T) { | ||
| dir, err := os.Open(dataDir) | ||
| require.NoError(t, err) | ||
| defer dir.Close() | ||
| names, err := dir.Readdirnames(-1) | ||
| require.NoError(t, err) | ||
|
|
||
| for _, fname := range names { | ||
| if strings.HasSuffix(fname, "_src.uast") { | ||
| name := fname[:len(fname)-len("_src.uast")] | ||
|
|
||
| t.Run(name, func(t *testing.T) { | ||
| srcName := filepath.Join(dataDir, name+"_src.uast") | ||
| dstName := filepath.Join(dataDir, name+"_dst.uast") | ||
| src := readUAST(t, srcName) | ||
| dst := readUAST(t, dstName) | ||
|
|
||
| changes := Changes(src, dst) | ||
| newsrc := changes.Apply(src) | ||
| require.True(t, nodes.Equal(newsrc, dst)) | ||
| }) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.