From fb0c623a3b43586adf82d18e45d28e5491b8a14a Mon Sep 17 00:00:00 2001 From: Hideo Hattori Date: Mon, 15 Jul 2024 15:33:21 +0900 Subject: [PATCH 1/3] single line comment with regex --- language.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/language.go b/language.go index a4410cb..4cfff1d 100644 --- a/language.go +++ b/language.go @@ -25,14 +25,15 @@ type ClocLanguage struct { // Language is a type used to definitions and store statistics for one programming language. type Language struct { - Name string - lineComments []string - multiLines [][]string - Files []string - Code int32 - Comments int32 - Blanks int32 - Total int32 + Name string + lineComments []string + regexLineComments []*regexp.Regexp + multiLines [][]string + Files []string + Code int32 + Comments int32 + Blanks int32 + Total int32 } // Languages is an array representation of Language. @@ -459,6 +460,15 @@ func NewLanguage(name string, lineComments []string, multiLines [][]string) *Lan } } +func (l *Language) WithRegexLineComments(regexLineComments []string) *Language { + var regexLineCommentsCompiled []*regexp.Regexp + for _, r := range regexLineComments { + regexLineCommentsCompiled = append(regexLineCommentsCompiled, regexp.MustCompile(r)) + } + l.regexLineComments = regexLineCommentsCompiled + return l +} + func lang2exts(lang string) (exts string) { var es []string for ext, l := range Exts { From fb8f0f7a154ac2730f8c83723c1da848bc0332a0 Mon Sep 17 00:00:00 2001 From: Hideo Hattori Date: Mon, 15 Jul 2024 15:33:49 +0900 Subject: [PATCH 2/3] add Just lang --- file.go | 34 +++++++++++++++++++++++++--------- language.go | 4 ++++ utils.go | 9 +++++++-- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/file.go b/file.go index cbdf8d0..10be537 100644 --- a/file.go +++ b/file.go @@ -108,17 +108,33 @@ scannerloop: line = trimBOM(line) } - singleloop: - for _, singleComment := range language.lineComments { - if strings.HasPrefix(line, singleComment) { - // check if single comment is a prefix of multi comment - for _, ml := range language.multiLines { - if ml[0] != "" && strings.HasPrefix(line, ml[0]) { - break singleloop + if len(language.regexLineComments) > 0 { + singleloopRegex: + for _, singleCommentRegex := range language.regexLineComments { + if singleCommentRegex.MatchString(line) { + // check if single comment is a prefix of multi comment + for _, ml := range language.multiLines { + if ml[0] != "" && strings.HasPrefix(line, ml[0]) { + break singleloopRegex + } } + onComment(clocFile, opts, len(inComments) > 0, line, lineOrg) + continue scannerloop + } + } + } else { + singleloop: + for _, singleComment := range language.lineComments { + if strings.HasPrefix(line, singleComment) { + // check if single comment is a prefix of multi comment + for _, ml := range language.multiLines { + if ml[0] != "" && strings.HasPrefix(line, ml[0]) { + break singleloop + } + } + onComment(clocFile, opts, len(inComments) > 0, line, lineOrg) + continue scannerloop } - onComment(clocFile, opts, len(inComments) > 0, line, lineOrg) - continue scannerloop } } diff --git a/language.go b/language.go index 4cfff1d..d81a5bb 100644 --- a/language.go +++ b/language.go @@ -212,6 +212,7 @@ var Exts = map[string]string{ "janet": "Janet", "json": "JSON", "jsx": "JSX", + "just": "Just", "kak": "KakouneScript", "kk": "Koka", "kt": "Kotlin", @@ -431,6 +432,8 @@ func getFileType(path string, opts *ClocOptions) (ext string, ok bool) { } switch strings.ToLower(base) { + case "justfile": + return "just", true case "makefile": return "makefile", true case "nukefile": @@ -600,6 +603,7 @@ func NewDefinedLanguages() *DefinedLanguages { "JavaScript": NewLanguage("JavaScript", []string{"//"}, [][]string{{"/*", "*/"}}), "Julia": NewLanguage("Julia", []string{"#"}, [][]string{{"#:=", ":=#"}}), "Jupyter Notebook": NewLanguage("Jupyter Notebook", []string{"#"}, [][]string{{"", ""}}), + "Just": NewLanguage("Just", []string{"#"}, [][]string{{"", ""}}).WithRegexLineComments([]string{`^#[^!].*`}), "JSON": NewLanguage("JSON", []string{}, [][]string{{"", ""}}), "JSX": NewLanguage("JSX", []string{"//"}, [][]string{{"/*", "*/"}}), "KakouneScript": NewLanguage("KakouneScript", []string{"#"}, [][]string{{"", ""}}), diff --git a/utils.go b/utils.go index 46e2150..6872874 100644 --- a/utils.go +++ b/utils.go @@ -146,10 +146,15 @@ func getAllFiles(paths []string, languages *DefinedLanguages, opts *ClocOptions) } if _, ok := result[targetExt]; !ok { - result[targetExt] = NewLanguage( + definedLang := NewLanguage( languages.Langs[targetExt].Name, languages.Langs[targetExt].lineComments, - languages.Langs[targetExt].multiLines) + languages.Langs[targetExt].multiLines, + ) + if len(languages.Langs[targetExt].regexLineComments) > 0 { + definedLang.regexLineComments = languages.Langs[targetExt].regexLineComments + } + result[targetExt] = definedLang } result[targetExt].Files = append(result[targetExt].Files, path) } From f42734b9baa5e7c2675da3053f7614612e66e3a9 Mon Sep 17 00:00:00 2001 From: Hideo Hattori Date: Mon, 15 Jul 2024 15:33:59 +0900 Subject: [PATCH 3/3] add test --- file_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/file_test.go b/file_test.go index 9dd98a3..ed703b3 100644 --- a/file_test.go +++ b/file_test.go @@ -528,3 +528,47 @@ const color = "blue" t.Errorf("invalid logic. lang=%v", clocFile.Lang) } } + +func TestAnalayzeFile4Just(t *testing.T) { + tmpfile, err := os.CreateTemp("", "tmp.go") + if err != nil { + t.Logf("os.CreateTemp() error. err=[%v]", err) + return + } + defer os.Remove(tmpfile.Name()) + + if _, err := tmpfile.Write([]byte(`polyglot: python js perl sh ruby nu + +python: + #!/usr/bin/env python3 + print('Hello from python!') + +js: + #!/usr/bin/env node + console.log('Greetings from JavaScript!') # with comment + +# this is comment +`), + ); err != nil { + t.Fatalf("tmpfile.Write() error. err=[%v]", err) + } + + language := NewLanguage("Just", []string{"#"}, [][]string{{"", ""}}). + WithRegexLineComments([]string{`^#[^!].*`}) + clocOpts := NewClocOptions() + clocFile := AnalyzeFile(tmpfile.Name(), language, clocOpts) + tmpfile.Close() + + if clocFile.Blanks != 3 { + t.Errorf("invalid logic. blanks=%v", clocFile.Blanks) + } + if clocFile.Comments != 1 { + t.Errorf("invalid logic. comments=%v", clocFile.Comments) + } + if clocFile.Code != 7 { + t.Errorf("invalid logic. code=%v", clocFile.Code) + } + if clocFile.Lang != "Just" { + t.Errorf("invalid logic. lang=%v", clocFile.Lang) + } +}