{"action":"created","comment":{"url":"https://api.github.com/repos/japaric/svd2rust/pulls/comments/125775056","pull_request_review_id":48192590,"id":125775056,"diff_hunk":"@@ -136,88 +136,120 @@ pub fn respace(s: &str) -> String {\n     s.split_whitespace().collect::<Vec<_>>().join(\" \")\n }\n \n-pub struct ExpandedRegister<'a> {\n-    pub info: &'a RegisterInfo,\n-    pub name: String,\n-    pub offset: u32,\n-    pub ty: Either<String, Rc<String>>,\n+pub fn register_to_rust(register: &Register) -> Tokens {\n+    match *register {\n+        Register::Single(ref _info) => {\n+            let comment = &format!(\n+                \"0x{:02x} - {}\",\n+                register.address_offset,\n+                respace(&register.description)\n+            )\n+                [..];\n+\n+            let array_index_match = Regex::new(r\"%s\\((?P<i>[ a-zA-Z0-9]*)\\)\").unwrap();\n+            //println!(\"{}\", register.name.as_str());\n+            let rty = Ident::new(array_index_match.replace_all(register.name.as_str(), \"\").to_sanitized_upper_case());\n+            let reg_name = Ident::new(array_index_match.replace_all(register.name.as_str(), \"$i\").to_sanitized_snake_case());\n+\n+            quote! {\n+                #[doc = #comment]\n+                pub #reg_name : #rty,\n+            }\n+        },\n+        Register::Array(ref info, ref array_info) => {\n+            let has_brackets = info.name.contains(\"[%s]\");\n+            \n+            let comment = &format!(\n+                \"0x{:02x} - {} [{}]\",\n+                register.address_offset,\n+                respace(&register.description),\n+                array_info.dim,\n+            )\n+                [..];\n+\n+            let rty = Ident::from({\n+                let name = if has_brackets {\n+                    info.name.replace(\"[%s]\", \"\")\n+                } else {\n+                    info.name.replace(\"%s\", \"\")\n+                };\n+                name.to_sanitized_upper_case().into_owned()\n+            });\n+\n+            let reg_name = Ident::from({\n+                let name = if has_brackets {\n+                    info.name.replace(\"[%s]\", \"\")\n+                } else {\n+                    info.name.replace(\"%s\", \"\")\n+                };\n+                name.to_sanitized_snake_case().into_owned()\n+            });\n+\n+            let length = Ident::from(array_info.dim.to_string());\n+\n+            quote! {\n+                #[doc = #comment]\n+                pub #reg_name : [#rty; #length],\n+            }\n+        },\n+    }\n }\n \n+\n /// Takes a list of \"registers\", some of which may actually be register arrays,\n-/// and turns it into a new *sorted* (by address offset) list of registers where\n-/// the register arrays have been expanded.\n-pub fn expand(registers: &[Register]) -> Vec<ExpandedRegister> {\n+/// and turn in into alist of registers where the register arrays have been expanded.\n+pub fn expand(register: &Register) -> Vec<Register> {\n     let mut out = vec![];\n \n-    for r in registers {\n-        match *r {\n-            Register::Single(ref info) => {\n-                out.push(\n-                    ExpandedRegister {\n-                        info: info,\n-                        name: info.name.to_sanitized_snake_case().into_owned(),\n-                        offset: info.address_offset,\n-                        ty: Either::Left(\n-                            info.name\n-                                .to_sanitized_upper_case()\n-                                .into_owned(),\n-                        ),\n+    match *register {\n+        Register::Single(ref _info) => out.push( register.clone() ),\n+        Register::Array(ref info, ref array_info) => {\n+            let has_brackets = info.name.contains(\"[%s]\");\n+\n+            let indices = array_info\n+                .dim_index\n+                .as_ref()\n+                .map(|v| Cow::from(&**v))\n+                .unwrap_or_else(\n+                    || {\n+                        Cow::from(\n+                            (0..array_info.dim)\n+                                .map(|i| i.to_string())\n+                                .collect::<Vec<_>>(),\n+                        )\n                     },\n-                )\n-            }\n-            Register::Array(ref info, ref array_info) => {\n-                let has_brackets = info.name.contains(\"[%s]\");\n+                );\n \n-                let ty = if has_brackets {\n-                    info.name.replace(\"[%s]\", \"\")\n+            for (idx, i) in indices.iter().zip(0..) {\n+                let name = if has_brackets {\n+                    info.name.replace(\"[%s]\", format!(\"%s({})\", idx).as_str())\n                 } else {\n-                    info.name.replace(\"%s\", \"\")\n+                    info.name.replace(\"%s\", format!(\"%s({})\", idx).as_str())\n                 };\n \n-                let ty = Rc::new(ty.to_sanitized_upper_case().into_owned());\n-\n-                let indices = array_info\n-                    .dim_index\n-                    .as_ref()\n-                    .map(|v| Cow::from(&**v))\n-                    .unwrap_or_else(\n-                        || {\n-                            Cow::from(\n-                                (0..array_info.dim)\n-                                    .map(|i| i.to_string())\n-                                    .collect::<Vec<_>>(),\n-                            )\n-                        },\n-                    );\n-\n-                for (idx, i) in indices.iter().zip(0..) {\n-                    let name = if has_brackets {\n-                        info.name.replace(\"[%s]\", idx)\n-                    } else {\n-                        info.name.replace(\"%s\", idx)\n-                    };\n-\n-                    let offset = info.address_offset +\n-                                 i * array_info.dim_increment;\n-\n-                    out.push(\n-                        ExpandedRegister {\n-                            info: info,\n-                            name: name.to_sanitized_snake_case().into_owned(),\n-                            offset: offset,\n-                            ty: Either::Right(ty.clone()),\n-                        },\n-                    );\n-                }\n+                let offset = info.address_offset +\n+                    i * array_info.dim_increment;\n+\n+                let mut expanded_info = info.clone();\n+                expanded_info.name = name;\n+                expanded_info.address_offset = offset;\n+                \n+                out.push(\n+                    Register::Single(expanded_info)\n+                );\n             }\n         }\n     }\n \n-    out.sort_by_key(|x| x.offset);\n-\n     out\n }\n \n+\n+pub fn sort_by_offset(mut registers: Vec<Register>) -> Vec<Register> {","path":"src/util.rs","position":193,"original_position":193,"commit_id":"73c49898db2107d8c7dfb9416994054fdcc630a1","original_commit_id":"73c49898db2107d8c7dfb9416994054fdcc630a1","user":{"login":"japaric","id":5018213,"avatar_url":"https://avatars0.githubusercontent.com/u/5018213?v=3","gravatar_id":"","url":"https://api.github.com/users/japaric","html_url":"https://github.com/japaric","followers_url":"https://api.github.com/users/japaric/followers","following_url":"https://api.github.com/users/japaric/following{/other_user}","gists_url":"https://api.github.com/users/japaric/gists{/gist_id}","starred_url":"https://api.github.com/users/japaric/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/japaric/subscriptions","organizations_url":"https://api.github.com/users/japaric/orgs","repos_url":"https://api.github.com/users/japaric/repos","events_url":"https://api.github.com/users/japaric/events{/privacy}","received_events_url":"https://api.github.com/users/japaric/received_events","type":"User","site_admin":false},"body":"I don't think this routine is complex enough that should be a function.","created_at":"2017-07-05T22:41:47Z","updated_at":"2017-07-05T23:03:42Z","html_url":"https://github.com/japaric/svd2rust/pull/121#discussion_r125775056","pull_request_url":"https://api.github.com/repos/japaric/svd2rust/pulls/121","_links":{"self":{"href":"https://api.github.com/repos/japaric/svd2rust/pulls/comments/125775056"},"html":{"href":"https://github.com/japaric/svd2rust/pull/121#discussion_r125775056"},"pull_request":{"href":"https://api.github.com/repos/japaric/svd2rust/pulls/121"}}},"pull_request":{"url":"https://api.github.com/repos/japaric/svd2rust/pulls/121","id":129016190,"html_url":"https://github.com/japaric/svd2rust/pull/121","diff_url":"https://github.com/japaric/svd2rust/pull/121.diff","patch_url":"https://github.com/japaric/svd2rust/pull/121.patch","issue_url":"https://api.github.com/repos/japaric/svd2rust/issues/121","number":121,"state":"open","locked":false,"title":"Register array in RegisterBlock","user":{"login":"kjetilkjeka","id":5366742,"avatar_url":"https://avatars0.githubusercontent.com/u/5366742?v=3","gravatar_id":"","url":"https://api.github.com/users/kjetilkjeka","html_url":"https://github.com/kjetilkjeka","followers_url":"https://api.github.com/users/kjetilkjeka/followers","following_url":"https://api.github.com/users/kjetilkjeka/following{/other_user}","gists_url":"https://api.github.com/users/kjetilkjeka/gists{/gist_id}","starred_url":"https://api.github.com/users/kjetilkjeka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kjetilkjeka/subscriptions","organizations_url":"https://api.github.com/users/kjetilkjeka/orgs","repos_url":"https://api.github.com/users/kjetilkjeka/repos","events_url":"https://api.github.com/users/kjetilkjeka/events{/privacy}","received_events_url":"https://api.github.com/users/kjetilkjeka/received_events","type":"User","site_admin":false},"body":"I guess this pull request is dropping a bit out of the sky, but I realized I needed the feature sooner rather than later so decided to just go for it. When it's merged it should fix #114 \r\n\r\n\r\nSome refactoring was done to allow for integration register arrays, the most significant are\r\n- expand function only takes 1 register instead of slice. This means that the function has to be called for every register that should be expanded. This was done so a register can be conditionaly expanded depending on if it's possible to convert to a Rust array or not.\r\n- register_to_rust method was factored out from register_block. Mainly so register block wouldn't grow very large with lots of nested control flow.\r\n- ExpandedRegister was removed, the functions only use Register struct now. This was because if not it would require conversion functions when registers was not expanded. The struct would also need changing as it would only have to support arrays.\r\n- Since the ExpandedRegisters was dropped the expand function now writes ```%s(i)``` (where i is the index) to the names of the expanded registers. This is used later on to resolve name and type.\r\n- regex is a dependecy, this is used to replace ```%s(i)``` with the index. Adding this dependecy might not be desired? and if not I can reimplement it with some logic that iterates over the string.\r\n\r\n\r\nI've tested with [S32K144.svd](https://github.com/kjetilkjeka/s32k144.rs/blob/master/src/S32K144.svd) and it seems to work perfectly with that file.\r\n\r\nAs mentioned in #114 i do hope that the method of using svd array information instead of unrolling arrays after is acceptable.","created_at":"2017-07-05T17:33:02Z","updated_at":"2017-07-05T23:03:42Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b3fdbdbbcb689be83a17393dc8a34fc779a0fe23","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/japaric/svd2rust/pulls/121/commits","review_comments_url":"https://api.github.com/repos/japaric/svd2rust/pulls/121/comments","review_comment_url":"https://api.github.com/repos/japaric/svd2rust/pulls/comments{/number}","comments_url":"https://api.github.com/repos/japaric/svd2rust/issues/121/comments","statuses_url":"https://api.github.com/repos/japaric/svd2rust/statuses/73c49898db2107d8c7dfb9416994054fdcc630a1","head":{"label":"kjetilkjeka:field_array","ref":"field_array","sha":"73c49898db2107d8c7dfb9416994054fdcc630a1","user":{"login":"kjetilkjeka","id":5366742,"avatar_url":"https://avatars0.githubusercontent.com/u/5366742?v=3","gravatar_id":"","url":"https://api.github.com/users/kjetilkjeka","html_url":"https://github.com/kjetilkjeka","followers_url":"https://api.github.com/users/kjetilkjeka/followers","following_url":"https://api.github.com/users/kjetilkjeka/following{/other_user}","gists_url":"https://api.github.com/users/kjetilkjeka/gists{/gist_id}","starred_url":"https://api.github.com/users/kjetilkjeka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kjetilkjeka/subscriptions","organizations_url":"https://api.github.com/users/kjetilkjeka/orgs","repos_url":"https://api.github.com/users/kjetilkjeka/repos","events_url":"https://api.github.com/users/kjetilkjeka/events{/privacy}","received_events_url":"https://api.github.com/users/kjetilkjeka/received_events","type":"User","site_admin":false},"repo":{"id":96207014,"name":"svd2rust","full_name":"kjetilkjeka/svd2rust","owner":{"login":"kjetilkjeka","id":5366742,"avatar_url":"https://avatars0.githubusercontent.com/u/5366742?v=3","gravatar_id":"","url":"https://api.github.com/users/kjetilkjeka","html_url":"https://github.com/kjetilkjeka","followers_url":"https://api.github.com/users/kjetilkjeka/followers","following_url":"https://api.github.com/users/kjetilkjeka/following{/other_user}","gists_url":"https://api.github.com/users/kjetilkjeka/gists{/gist_id}","starred_url":"https://api.github.com/users/kjetilkjeka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kjetilkjeka/subscriptions","organizations_url":"https://api.github.com/users/kjetilkjeka/orgs","repos_url":"https://api.github.com/users/kjetilkjeka/repos","events_url":"https://api.github.com/users/kjetilkjeka/events{/privacy}","received_events_url":"https://api.github.com/users/kjetilkjeka/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/kjetilkjeka/svd2rust","description":"Generate Rust register maps (`struct`s) from SVD files","fork":true,"url":"https://api.github.com/repos/kjetilkjeka/svd2rust","forks_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/forks","keys_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/keys{/key_id}","collaborators_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/teams","hooks_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/hooks","issue_events_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/issues/events{/number}","events_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/events","assignees_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/assignees{/user}","branches_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/branches{/branch}","tags_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/tags","blobs_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/git/refs{/sha}","trees_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/git/trees{/sha}","statuses_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/statuses/{sha}","languages_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/languages","stargazers_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/stargazers","contributors_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/contributors","subscribers_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/subscribers","subscription_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/subscription","commits_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/commits{/sha}","git_commits_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/git/commits{/sha}","comments_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/comments{/number}","issue_comment_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/issues/comments{/number}","contents_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/contents/{+path}","compare_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/compare/{base}...{head}","merges_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/merges","archive_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/downloads","issues_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/issues{/number}","pulls_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/pulls{/number}","milestones_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/milestones{/number}","notifications_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/labels{/name}","releases_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/releases{/id}","deployments_url":"https://api.github.com/repos/kjetilkjeka/svd2rust/deployments","created_at":"2017-07-04T10:38:45Z","updated_at":"2017-07-04T10:38:46Z","pushed_at":"2017-07-05T17:09:28Z","git_url":"git://github.com/kjetilkjeka/svd2rust.git","ssh_url":"git@github.com:kjetilkjeka/svd2rust.git","clone_url":"https://github.com/kjetilkjeka/svd2rust.git","svn_url":"https://github.com/kjetilkjeka/svd2rust","homepage":null,"size":971,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"japaric:master","ref":"master","sha":"5e2da93e941b0557fd7738aca9c30828f80a07f0","user":{"login":"japaric","id":5018213,"avatar_url":"https://avatars0.githubusercontent.com/u/5018213?v=3","gravatar_id":"","url":"https://api.github.com/users/japaric","html_url":"https://github.com/japaric","followers_url":"https://api.github.com/users/japaric/followers","following_url":"https://api.github.com/users/japaric/following{/other_user}","gists_url":"https://api.github.com/users/japaric/gists{/gist_id}","starred_url":"https://api.github.com/users/japaric/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/japaric/subscriptions","organizations_url":"https://api.github.com/users/japaric/orgs","repos_url":"https://api.github.com/users/japaric/repos","events_url":"https://api.github.com/users/japaric/events{/privacy}","received_events_url":"https://api.github.com/users/japaric/received_events","type":"User","site_admin":false},"repo":{"id":70371776,"name":"svd2rust","full_name":"japaric/svd2rust","owner":{"login":"japaric","id":5018213,"avatar_url":"https://avatars0.githubusercontent.com/u/5018213?v=3","gravatar_id":"","url":"https://api.github.com/users/japaric","html_url":"https://github.com/japaric","followers_url":"https://api.github.com/users/japaric/followers","following_url":"https://api.github.com/users/japaric/following{/other_user}","gists_url":"https://api.github.com/users/japaric/gists{/gist_id}","starred_url":"https://api.github.com/users/japaric/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/japaric/subscriptions","organizations_url":"https://api.github.com/users/japaric/orgs","repos_url":"https://api.github.com/users/japaric/repos","events_url":"https://api.github.com/users/japaric/events{/privacy}","received_events_url":"https://api.github.com/users/japaric/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/japaric/svd2rust","description":"Generate Rust register maps (`struct`s) from SVD files","fork":false,"url":"https://api.github.com/repos/japaric/svd2rust","forks_url":"https://api.github.com/repos/japaric/svd2rust/forks","keys_url":"https://api.github.com/repos/japaric/svd2rust/keys{/key_id}","collaborators_url":"https://api.github.com/repos/japaric/svd2rust/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/japaric/svd2rust/teams","hooks_url":"https://api.github.com/repos/japaric/svd2rust/hooks","issue_events_url":"https://api.github.com/repos/japaric/svd2rust/issues/events{/number}","events_url":"https://api.github.com/repos/japaric/svd2rust/events","assignees_url":"https://api.github.com/repos/japaric/svd2rust/assignees{/user}","branches_url":"https://api.github.com/repos/japaric/svd2rust/branches{/branch}","tags_url":"https://api.github.com/repos/japaric/svd2rust/tags","blobs_url":"https://api.github.com/repos/japaric/svd2rust/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/japaric/svd2rust/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/japaric/svd2rust/git/refs{/sha}","trees_url":"https://api.github.com/repos/japaric/svd2rust/git/trees{/sha}","statuses_url":"https://api.github.com/repos/japaric/svd2rust/statuses/{sha}","languages_url":"https://api.github.com/repos/japaric/svd2rust/languages","stargazers_url":"https://api.github.com/repos/japaric/svd2rust/stargazers","contributors_url":"https://api.github.com/repos/japaric/svd2rust/contributors","subscribers_url":"https://api.github.com/repos/japaric/svd2rust/subscribers","subscription_url":"https://api.github.com/repos/japaric/svd2rust/subscription","commits_url":"https://api.github.com/repos/japaric/svd2rust/commits{/sha}","git_commits_url":"https://api.github.com/repos/japaric/svd2rust/git/commits{/sha}","comments_url":"https://api.github.com/repos/japaric/svd2rust/comments{/number}","issue_comment_url":"https://api.github.com/repos/japaric/svd2rust/issues/comments{/number}","contents_url":"https://api.github.com/repos/japaric/svd2rust/contents/{+path}","compare_url":"https://api.github.com/repos/japaric/svd2rust/compare/{base}...{head}","merges_url":"https://api.github.com/repos/japaric/svd2rust/merges","archive_url":"https://api.github.com/repos/japaric/svd2rust/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/japaric/svd2rust/downloads","issues_url":"https://api.github.com/repos/japaric/svd2rust/issues{/number}","pulls_url":"https://api.github.com/repos/japaric/svd2rust/pulls{/number}","milestones_url":"https://api.github.com/repos/japaric/svd2rust/milestones{/number}","notifications_url":"https://api.github.com/repos/japaric/svd2rust/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/japaric/svd2rust/labels{/name}","releases_url":"https://api.github.com/repos/japaric/svd2rust/releases{/id}","deployments_url":"https://api.github.com/repos/japaric/svd2rust/deployments","created_at":"2016-10-09T02:47:52Z","updated_at":"2017-07-05T18:48:48Z","pushed_at":"2017-07-05T17:33:02Z","git_url":"git://github.com/japaric/svd2rust.git","ssh_url":"git@github.com:japaric/svd2rust.git","clone_url":"https://github.com/japaric/svd2rust.git","svn_url":"https://github.com/japaric/svd2rust","homepage":null,"size":967,"stargazers_count":56,"watchers_count":56,"language":"Rust","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":28,"mirror_url":null,"open_issues_count":17,"forks":28,"open_issues":17,"watchers":56,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/japaric/svd2rust/pulls/121"},"html":{"href":"https://github.com/japaric/svd2rust/pull/121"},"issue":{"href":"https://api.github.com/repos/japaric/svd2rust/issues/121"},"comments":{"href":"https://api.github.com/repos/japaric/svd2rust/issues/121/comments"},"review_comments":{"href":"https://api.github.com/repos/japaric/svd2rust/pulls/121/comments"},"review_comment":{"href":"https://api.github.com/repos/japaric/svd2rust/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/japaric/svd2rust/pulls/121/commits"},"statuses":{"href":"https://api.github.com/repos/japaric/svd2rust/statuses/73c49898db2107d8c7dfb9416994054fdcc630a1"}}}}
{"actor":{"display_login":"japaric"}}