Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for Lua #98

Merged
merged 2 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/scanner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
"JavaScript": &JavascriptConfig,
// NOTE: Some JSON files support JS comments (e.g. tsconfig.json)
"JSON": &JavascriptConfig,
"Lua": &LuaConfig,
"Makefile": &MakefileConfig,
"Objective-C": &ObjectiveCConfig,
"Perl": &PerlConfig,
Expand Down Expand Up @@ -111,6 +112,17 @@ var (
},
}

// LuaConfig is a config for Lua.
LuaConfig = Config{
LineCommentStart: []string{"--"},
MultilineCommentStart: "--[[",
MultilineCommentEnd: "--]]",
Strings: [][2]string{
{"\"", "\""},
{"'", "'"}, // character
},
}

// MakefileConfig is a config for Makefiles.
MakefileConfig = ShellConfig

Expand Down
63 changes: 44 additions & 19 deletions internal/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,29 +214,28 @@ func (s *CommentScanner) Scan() bool {
func (s *CommentScanner) processCode(st *stateCode) (state, error) {
for {
// Check for line comment
for _, start := range s.config.LineCommentStart {
eq, err := s.peekEqual(start)
if err != nil {
return st, err
}
if eq {
m, err := s.lineMatch()
if err != nil {
return st, err
}

mm, err := s.multiLineMatch()
if err != nil {
return st, err
}

if len(m) > 0 || len(mm) > 0 {
if len(m) >= len(mm) {
return &stateLineComment{}, nil
}
}

// Check for line comment
if len(s.config.MultilineCommentStart) != 0 {
if eq, err := s.peekEqual(s.config.MultilineCommentStart); err == nil && eq {
// Discard the opening. It will be added by processMultilineComment.
if _, errDiscard := s.reader.Discard(len(s.config.MultilineCommentStart)); errDiscard != nil {
return st, errDiscard
}
return &stateMultilineComment{
line: s.line,
}, nil
} else if err != nil {
return st, err
// Discard the opening. It will be added by processMultilineComment.
if _, errDiscard := s.reader.Discard(len(s.config.MultilineCommentStart)); errDiscard != nil {
return st, errDiscard
}
return &stateMultilineComment{
line: s.line,
}, nil
}

// Check for string
Expand All @@ -263,6 +262,32 @@ func (s *CommentScanner) processCode(st *stateCode) (state, error) {
}
}

func (s *CommentScanner) lineMatch() ([]rune, error) {
// Check for line comment
for _, start := range s.config.LineCommentStart {
eq, err := s.peekEqual(start)
if err != nil {
return nil, err
}
if eq {
return start, nil
}
}
return nil, nil
}

func (s *CommentScanner) multiLineMatch() ([]rune, error) {
// Check for line comment
if len(s.config.MultilineCommentStart) != 0 {
if eq, err := s.peekEqual(s.config.MultilineCommentStart); err == nil && eq {
return s.config.MultilineCommentStart, nil
} else if err != nil {
return nil, err
}
}
return nil, nil
}

// processString processes strings and returns the next state.
func (s *CommentScanner) processString(st *stateString) (state, error) {
for {
Expand Down
202 changes: 162 additions & 40 deletions internal/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var scannerTestCases = []*struct {
line int
}
}{
// Go
{
name: "line_comments.go",
src: `package foo
Expand Down Expand Up @@ -64,33 +65,6 @@ var scannerTestCases = []*struct {
},
},
},
{
name: "line_comments.php",
src: `// file comment

# TODO is a function.
function TODO() {
return // Random comment
}`,
config: PHPConfig,
comments: []struct {
text string
line int
}{
{
text: "// file comment",
line: 1,
},
{
text: "# TODO is a function.",
line: 3,
},
{
text: "// Random comment",
line: 5,
},
},
},
{
name: "comments_in_string.go",
src: `package foo
Expand Down Expand Up @@ -208,35 +182,150 @@ var scannerTestCases = []*struct {
},
},
},

// PHP
{
name: "raw_string.rb",
src: `# file comment
name: "line_comments.php",
src: `// file comment

z = %{
# TODO is a function.
}
function TODO() {
return // Random comment
}`,
config: PHPConfig,
comments: []struct {
text string
line int
}{
{
text: "// file comment",
line: 1,
},
{
text: "# TODO is a function.",
line: 3,
},
{
text: "// Random comment",
line: 5,
},
},
},

def foo()
# Random comment
x = "\"# Random comment x"
y = '\'# Random comment y'
// Lua
{
name: "line_comments.lua",
src: `-- package comment

-- TODO is a function.
function TODO() {
return -- Random comment
}`,
config: LuaConfig,
comments: []struct {
text string
line int
}{
{
text: "-- package comment",
line: 1,
},
{
text: "-- TODO is a function.",
line: 3,
},
{
text: "-- Random comment",
line: 5,
},
},
},
{
name: "comments_in_string.lua",
src: `-- package comment

-- TODO is a function.
func TODO() {
x = "-- Random comment"
y = '-- Random comment'
return x + y
end`,
config: RubyConfig,
}`,
config: LuaConfig,
comments: []struct {
text string
line int
}{
{
text: "# file comment",
text: "-- package comment",
line: 1,
},
{
text: "# Random comment",
line: 8,
text: "-- TODO is a function.",
line: 3,
},
},
},
{
name: "escaped_string.lua",
src: `-- package comment

-- TODO is a function.
func TODO() {
x = "\"-- Random comment"
y = '\'-- Random comment'
return x + y
}`,
config: LuaConfig,
comments: []struct {
text string
line int
}{
{
text: "-- package comment",
line: 1,
},
{
text: "-- TODO is a function.",
line: 3,
},
},
},
{
name: "multi_line.lua",
src: `-- package comment

--[[
TODO is a function.
--]]
func TODO() {
return -- Random comment
}
--[[ extra comment --]]`,
config: LuaConfig,
comments: []struct {
text string
line int
}{
{
text: "-- package comment",
line: 1,
},
{
text: "--[[\n\t\t\tTODO is a function.\n\t\t\t--]]",
line: 3,
},
{
text: "-- Random comment",
line: 7,
},
{
text: "--[[ extra comment --]]",
line: 9,
},
},
},

// Python
{
name: "raw_string.py",
src: `# file comment
Expand Down Expand Up @@ -295,6 +384,39 @@ var scannerTestCases = []*struct {
},
},
},

// Ruby
{
name: "raw_string.rb",
src: `# file comment

z = %{
# TODO is a function.
}

def foo()
# Random comment
x = "\"# Random comment x"
y = '\'# Random comment y'
return x + y
end`,
config: RubyConfig,
comments: []struct {
text string
line int
}{
{
text: "# file comment",
line: 1,
},
{
text: "# Random comment",
line: 8,
},
},
},

// Shell
{
name: "line_comments.sh",
src: `#!/bin/bash
Expand Down