Skip to content

Commit

Permalink
Adding support for comments with "//" (#3)
Browse files Browse the repository at this point in the history
* support comments

* adding changelog

* correct tests

* comparison with starrit0

* merge branch with starrit0
  • Loading branch information
antoinegelloz committed May 25, 2020
1 parent 8e09f6e commit 366d3d9
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 14 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
v1.10.0 [2020-05-22]
-------------------

#### New features
- Adding support for inline comments with '//'


v1.9.0 [2020-05-12]
-------------------

#### New features
Adding support for all bitwise operators
Replacing all operators by constants for potential future changes
- Adding support for all bitwise operators
- Replacing all operators by constants for potential future changes
14 changes: 6 additions & 8 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,18 @@ const (
OpEndsWith = "endsWith"
OpMatches = "matches"
OpRange = ".."
OpBitwiseAnd = "&"
OpBitwiseOr = "|"
OpBitwiseXor = "^"
OpBitwiseLShift = "<<"
OpBitwiseRShift = ">>"

// Unary Nodes Operators
OpPositive = "+"
OpNegative = "-"
OpNotSymbol = "!"
OpNotVerbose = "not"

// Bitwise Operators
OpBitwiseNot = "~"
OpBitwiseAnd = "&"
OpBitwiseOr = "|"
OpBitwiseXor = "^"
OpBitwiseLShift = "<<"
OpBitwiseRShift = ">>"
OpBitwiseNot = "~"

SingleOperators = "+-/%#,?:~^"
DoubleFirstOperators = "&|=*<>!"
Expand Down
48 changes: 45 additions & 3 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package expr_test
import (
"encoding/json"
"fmt"
"github.com/metronlab/expr/ast"
"github.com/metronlab/expr/constants"
"github.com/metronlab/expr/file"
"reflect"
"strings"
"testing"
"time"

"github.com/metronlab/expr/ast"
"github.com/metronlab/expr/constants"
"github.com/metronlab/expr/file"

"github.com/metronlab/expr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -996,6 +997,23 @@ func TestExpr(t *testing.T) {
"27-2" + constants.OpBitwiseRShift + "(+4-3)+-2",
24,
},
// Comments
{
` // 1 + 1
9 + 5 // 1 + 1
// 1 + 1
//another comment`,
14,
},
{
` 1 + // 1 + 1
9 + 5 // 1 + 1`,
15,
},
{
` 1 + 1 //1 + 2//`,
2,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -1026,6 +1044,30 @@ func TestExpr(t *testing.T) {
}
}

func TestComments_errors(t *testing.T) {
_, err := expr.Compile(
`# 2 + 2`,
)
assert.Error(t, err)
_, err = expr.Compile(
` // 2 + 2
// 1 + 1`,
)
assert.Error(t, err)
_, err = expr.Compile(
"//",
)
assert.Error(t, err)
_, err = expr.Compile(
"//1 + 2 comment",
)
assert.Error(t, err)
_, err = expr.Compile(
"//1 + 2 \n//comment\n //3+4",
)
assert.Error(t, err)
}

func TestExpr_eval_with_env(t *testing.T) {
_, err := expr.Eval("true", expr.Env(map[string]interface{}{}))
assert.Error(t, err)
Expand Down
10 changes: 10 additions & 0 deletions parser/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ func (l *lexer) word() string {
return l.input[l.start:l.end]
}

func (l *lexer) skipComment(r rune) stateFn {
for l.next() != eof && l.loc.Column != 0 {
}
if r == eof {
l.emitEOF()
return nil
}
return root
}

func (l *lexer) ignore() {
l.start = l.end
l.startLoc = l.loc
Expand Down
5 changes: 4 additions & 1 deletion parser/lexer/state.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package lexer

import (
"github.com/metronlab/expr/constants"
"strings"

"github.com/metronlab/expr/constants"
)

type stateFn func(*lexer) stateFn
Expand All @@ -12,6 +13,8 @@ func root(l *lexer) stateFn {
case r == eof:
l.emitEOF()
return nil
case IsComment(r, l):
return l.skipComment(r)
case IsSpace(r):
l.ignore()
return root
Expand Down
7 changes: 7 additions & 0 deletions parser/lexer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import (
"unicode/utf8"
)

func IsComment(r rune, l *lexer) bool {
if l.end >= len(l.input) {
return false
}
return l.input[l.end-1] == '/' && l.input[l.end] == '/'
}

func IsSpace(r rune) bool {
return unicode.IsSpace(r)
}
Expand Down

0 comments on commit 366d3d9

Please sign in to comment.