Skip to content

Commit

Permalink
feat(struct): make basic support for go lang
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 23, 2021
1 parent c116b5e commit c55638b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
29 changes: 29 additions & 0 deletions _fixtures/ctags/go_tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/
!_TAG_OUTPUT_FILESEP slash /slash or backslash/
!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
!_TAG_PATTERN_LENGTH_LIMIT 96 /0 for no limit/
!_TAG_PROC_CWD /Users/fdhuang/consultant/devops/coco/ //
!_TAG_PROGRAM_AUTHOR Universal Ctags Team //
!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/
!_TAG_PROGRAM_URL https://ctags.io/ /official site/
!_TAG_PROGRAM_VERSION 5.9.0 /d532b5c/
InitDatastore pkg/datastore.go /^func InitDatastore() {$/;" func line:37 language:Go package:pkg
access pkg/datastore.go /^ name, access, datatype string$/;" member line:19 language:Go struct:pkg.memberinfo_st typeref:typename:string
access pkg/datastore.go /^ name, access, returntype string$/;" member line:23 language:Go struct:pkg.methodinfo_st typeref:typename:string
classinfo_st pkg/datastore.go /^type classinfo_st struct {$/;" struct line:26 language:Go package:pkg
classmap pkg/datastore.go /^var classmap map[string]classinfo_st$/;" var line:34 language:Go package:pkg typeref:typename:map[string]classinfo_st
datatype pkg/datastore.go /^ name, access, datatype string$/;" member line:19 language:Go struct:pkg.memberinfo_st typeref:typename:string
id pkg/datastore.go /^ id int$/;" member line:28 language:Go struct:pkg.classinfo_st typeref:typename:int
idcounter pkg/datastore.go /^var idcounter int = 1$/;" var line:35 language:Go package:pkg typeref:typename:int
memberinfo_st pkg/datastore.go /^type memberinfo_st struct {$/;" struct line:18 language:Go package:pkg
members pkg/datastore.go /^ members []memberinfo_st$/;" member line:30 language:Go struct:pkg.classinfo_st typeref:typename:[]memberinfo_st
methodinfo_st pkg/datastore.go /^type methodinfo_st struct {$/;" struct line:22 language:Go package:pkg
methods pkg/datastore.go /^ methods []methodinfo_st$/;" member line:31 language:Go struct:pkg.classinfo_st typeref:typename:[]methodinfo_st
name pkg/datastore.go /^ name string$/;" member line:27 language:Go struct:pkg.classinfo_st typeref:typename:string
name pkg/datastore.go /^ name, access, datatype string$/;" member line:19 language:Go struct:pkg.memberinfo_st typeref:typename:string
name pkg/datastore.go /^ name, access, returntype string$/;" member line:23 language:Go struct:pkg.methodinfo_st typeref:typename:string
parents pkg/datastore.go /^ parents []string$/;" member line:29 language:Go struct:pkg.classinfo_st typeref:typename:[]string
pkg pkg/datastore.go /^package pkg$/;" package line:16 language:Go
returntype pkg/datastore.go /^ name, access, returntype string$/;" member line:23 language:Go struct:pkg.methodinfo_st typeref:typename:string
39 changes: 39 additions & 0 deletions _fixtures/ctags/source/datastore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// tags2uml
// Copyright 2014 ruben2020 https://github.com/ruben2020/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pkg

type memberinfo_st struct {
name, access, datatype string
}

type methodinfo_st struct {
name, access, returntype string
}

type classinfo_st struct {
name string
id int
parents []string
members []memberinfo_st
methods []methodinfo_st
}

var classmap map[string]classinfo_st
var idcounter int = 1

func InitDatastore() {
classmap = make(map[string]classinfo_st)
}
25 changes: 21 additions & 4 deletions plugins/coco_struct_analysis/src/ctags_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ lazy_static! {
.unwrap();
static ref RE_ACCESS: Regex = Regex::new(r"access:(?P<access>[A-Za-z0-9_]+)").unwrap();
static ref RE_LANGUAGE: Regex = Regex::new(r"language:(?P<language>[A-Za-z0-9_\#]+)").unwrap();

static ref RE_TYPE: Regex =
Regex::new(r"/\^([ ]*)(?P<datatype>[A-Za-z0-9_.]+)([^A-Za-z0-9_]+)(.*)\$/").unwrap();
static ref RUST_TYPE: Regex = Regex::new(
r"/\^([ ]*)([A-Za-z0-9_.]+)(\t|\s)([A-Za-z0-9_.]+)\s?:(\t|\s)?(?P<datatype>[A-Za-z0-9_.<>]+)"
)
.unwrap();
r"/\^([ ]*)([A-Za-z0-9_.]+)(\t|\s)([A-Za-z0-9_.]+)\s*:(\t|\s)*(?P<datatype>[A-Za-z0-9_.<>]+)"
).unwrap();
static ref GO_TYPE: Regex =
Regex::new(r"/\^([\s]*)([A-Za-z0-9_.]+)(\s|\t)*(?P<datatype>[A-Za-z0-9_.<>\[\]]+)").unwrap();

static ref TYPE_KEYWORDS: [&'static str; 18] = [
"private",
"public",
Expand Down Expand Up @@ -175,6 +178,11 @@ impl CtagsParser {
data_type = (&capts["datatype"]).to_string();
}
}
"Go" => {
if let Some(capts) = GO_TYPE.captures(line) {
data_type = (&capts["datatype"]).to_string();
}
}
_ => {}
}

Expand Down Expand Up @@ -335,14 +343,23 @@ name src/coco_struct.rs /^ pub name: String,$/;\" field line:22 language:Rust
let parser = CtagsParser::parse(dir);
let classes = parser.classes();

println!("{:?}", classes);
assert_eq!(3, classes.len());
assert_eq!("ClassInfo", classes[0].name);
assert_eq!(7, classes[0].members.len());
assert_eq!("file", classes[0].members[0].name);
assert_eq!("parents", classes[0].members[6].name);
}

#[test]
pub fn should_parse_golang_file() {
let dir = tags_dir().join("go_tags");
let parser = CtagsParser::parse(dir);
let classes = parser.classes();

println!("{:?}", classes);
assert_eq!(3, classes.len());
}

#[test]
pub fn should_parse_cpp_file() {
let dir = tags_dir().join("cpp_tags");
Expand Down
2 changes: 1 addition & 1 deletion plugins/coco_struct_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ mod tests {
let mut code = String::new();
file.read_to_string(&mut code).unwrap();
let classes: Vec<ClassInfo> = serde_json::from_str(&code).unwrap();
assert_eq!(6, classes.len());
assert_eq!(9, classes.len());
}
}

0 comments on commit c55638b

Please sign in to comment.