Skip to content

Commit

Permalink
fix regexp comment
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Stoletov <[email protected]>
  • Loading branch information
irvis committed Feb 3, 2021
1 parent f669df1 commit faa229a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 14 deletions.
25 changes: 17 additions & 8 deletions formatter/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package formatter
import (
"bytes"
"io"
"unicode"

"github.com/timtadh/lexmachine"
"github.com/timtadh/lexmachine/machines"
Expand All @@ -38,13 +39,16 @@ func newScanner(code []byte) (*scanner, error) {
lexer.Add([]byte(`([a-zA-Z_][a-zA-Z0-9_:]*)`), token(nID))
lexer.Add([]byte(`\s*\n\s*\n\s*`), token(nLF))
lexer.Add([]byte("( |\t|\f|\r|\n)+"), skip)
lexer.Add([]byte(`--[^\[\[]( |\S)*`), comment(nComment))
lexer.Add([]byte(`--\[\[([^\]\]])*\]\]`), commentLong(nCommentLong))
lexer.Add([]byte(`::([^::])*::`), token(nLabel))
lexer.Add([]byte(`::[^:]*::`), token(nLabel))

lexer.Add([]byte(`--[^[\n]+[^\n]*`), comment(nComment))
lexer.Add([]byte(`--\[\[[^]]*\]\]`), commentLong(nCommentLong))
lexer.Add([]byte(`--\n`), comment(nComment))
lexer.Add([]byte(`--\r\n`), comment(nComment))

lexer.Add([]byte(`"[^"]*"`), token(nString))
lexer.Add([]byte(`'[^']*'`), token(nString))
lexer.Add([]byte(`\[\[[^\]\]]*\]\]`), token(nString))
lexer.Add([]byte(`\[\[[^]]*\]\]`), token(nString))

if err := lexer.Compile(); err != nil {
return nil, err
Expand Down Expand Up @@ -127,8 +131,13 @@ func token(nodeID int) lexmachine.Action {

func comment(nodeID int) lexmachine.Action {
return func(s *lexmachine.Scanner, m *machines.Match) (interface{}, error) {
b := bytes.TrimLeft(m.Bytes, "--")
b = bytes.TrimSpace(b)
prefix := []byte("-- ")
if !bytes.HasPrefix(m.Bytes, prefix) {
prefix = []byte("--")
}

b := bytes.TrimPrefix(m.Bytes, prefix)
b = bytes.TrimRightFunc(b, unicode.IsSpace)
m.Bytes = b

return s.Token(nodeID, string(b), m), nil
Expand All @@ -137,8 +146,8 @@ func comment(nodeID int) lexmachine.Action {

func commentLong(nodeID int) lexmachine.Action {
return func(s *lexmachine.Scanner, m *machines.Match) (interface{}, error) {
b := bytes.TrimLeft(m.Bytes, "--[[")
b = bytes.TrimRight(b, "]]")
b := bytes.TrimPrefix(m.Bytes, []byte("--[["))
b = bytes.TrimSuffix(b, []byte("]]"))
b = bytes.TrimSpace(b)
m.Bytes = b

Expand Down
8 changes: 7 additions & 1 deletion formatter/statement_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package formatter

import (
"bytes"
"io"
)

Expand Down Expand Up @@ -50,7 +51,12 @@ func (s *commentStatement) GetStatement(prev, cur *element) statement {
}

func (s *commentStatement) Format(c *Config, p printer, w io.Writer) error {
if _, err := w.Write([]byte("-- ")); err != nil {
prefix := []byte("-- ")
if len(s.Element.Token.Lexeme) == 0 || bytes.HasPrefix(s.Element.Token.Lexeme, []byte("---")) {
prefix = []byte("--")
}

if _, err := w.Write(prefix); err != nil {
return err
}

Expand Down
68 changes: 66 additions & 2 deletions formatter/statement_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func TestParseComment(t *testing.T) {
Element: &element{
Token: &lexmachine.Token{
Type: nComment,
Value: "comment2",
Lexeme: []byte("comment2"),
Value: " comment2",
Lexeme: []byte(" comment2"),
TC: 12,
StartLine: 3,
StartColumn: 1,
Expand All @@ -98,6 +98,70 @@ func TestParseComment(t *testing.T) {
},
wantErr: false,
},
{
skip: false,
name: "two comment statement",
args: args{
code: []byte(`
---------------
-- comment
-- comment2
`,
),
},
want: &document{
Body: &body{
Blocks: map[uint64]statement{
0: &commentStatement{
Element: &element{
Token: &lexmachine.Token{
Type: nComment,
Value: "-------------",
Lexeme: []byte("-------------"),
TC: 1,
StartLine: 2,
StartColumn: 1,
EndLine: 2,
EndColumn: 15,
},
},
},
1: &commentStatement{
Element: &element{
Token: &lexmachine.Token{
Type: nComment,
Value: "comment",
Lexeme: []byte("comment"),
TC: 17,
StartLine: 3,
StartColumn: 1,
EndLine: 3,
EndColumn: 10,
},
},
IsNewline: true,
},
2: &commentStatement{
Element: &element{
Token: &lexmachine.Token{
Type: nComment,
Value: " comment2",
Lexeme: []byte(" comment2"),
TC: 28,
StartLine: 4,
StartColumn: 1,
EndLine: 4,
EndColumn: 15,
},
},
IsNewline: true,
},
},
Qty: 3,
},
},
wantErr: false,
},
}
for _, tt := range tests {
if tt.skip == true {
Expand Down
2 changes: 1 addition & 1 deletion tests/default/0005.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ for i, pkg in ipairs(packages) do
end
end

--- Load a local or remote manifest describing a repository.
-- Load a local or remote manifest describing a repository.
-- All functions that use manifest tables assume they were obtained
-- through either this function or load_local_manifest.
-- @param repo_url string: URL or pathname for the repository.
Expand Down
4 changes: 2 additions & 2 deletions tests/default/0007.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bar.say("hello") -- using the module
-- bad
local skt = require("socket")

--- @module foo.bar
-- @module foo.bar
local bar = {}

function bar.say(greeting)
Expand All @@ -99,7 +99,7 @@ local bla = require "bla"
-- good
local bla = require("bla")

--- @module myproject.myclass
-- @module myproject.myclass
local myclass = {}

-- class table
Expand Down

0 comments on commit faa229a

Please sign in to comment.