Skip to content

Commit

Permalink
added regex matching for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Dec 22, 2017
1 parent 37fc182 commit 9fd9491
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 4 deletions.
7 changes: 7 additions & 0 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"html/template"
"reflect"
"regexp"

"github.com/gobuffalo/plush/ast"

Expand Down Expand Up @@ -379,6 +380,12 @@ func (c *compiler) stringsOperator(l string, r interface{}, op string) (interfac
return l <= rr, nil
case "==":
return l == rr, nil
case "~=":
x, err := regexp.Compile(rr)
if err != nil {
return nil, errors.WithStack(errors.Errorf("couldn't compile regex %s", rr))
}
return x.MatchString(l), nil
}
return nil, errors.WithStack(errors.Errorf("unknown operator for string %s", op))
}
Expand Down
8 changes: 8 additions & 0 deletions if_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,11 @@ func Test_Render_If_Else_True(t *testing.T) {
r.NoError(err)
r.Equal("<p>hi</p>", s)
}

func Test_Render_If_Matches(t *testing.T) {
r := require.New(t)
input := `<p><%= if ("foo" ~= "bar") { return "hi"} else { return "bye"} %></p>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("<p>bye</p>", s)
}
7 changes: 7 additions & 0 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ func (l *Lexer) nextInsideToken() token.Token {
break
}
tok = newToken(token.LT, l.ch)
case '~':
if l.peekChar() == '=' {
l.readChar()
tok = token.Token{Type: token.MATCHES, Literal: "~="}
break
}
tok = newToken(token.MATCHES, l.ch)
case '>':
if l.peekChar() == '=' {
l.readChar()
Expand Down
1 change: 1 addition & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func newParser(l *lexer.Lexer) *parser {
p.registerInfix(token.ASTERISK, p.parseInfixExpression)
p.registerInfix(token.EQ, p.parseInfixExpression)
p.registerInfix(token.NOT_EQ, p.parseInfixExpression)
p.registerInfix(token.MATCHES, p.parseInfixExpression)
p.registerInfix(token.LT, p.parseInfixExpression)
p.registerInfix(token.GT, p.parseInfixExpression)
p.registerInfix(token.LTEQ, p.parseInfixExpression)
Expand Down
4 changes: 4 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ func Test_OperatorPrecedence(t *testing.T) {
"add(a * b[2], b[1], 2 * [1, 2][1])",
"add((a * (b[2])), (b[1]), (2 * ([1, 2][1])))",
},
{
"foo ~= bar",
"(foo ~= bar)",
},
}

for _, tt := range tests {
Expand Down
1 change: 1 addition & 0 deletions parser/precedences.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
var precedences = map[token.Type]int{
token.EQ: EQUALS,
token.NOT_EQ: EQUALS,
token.MATCHES: EQUALS,
token.LT: LESSGREATER,
token.LTEQ: LESSGREATER,
token.GT: LESSGREATER,
Expand Down
9 changes: 5 additions & 4 deletions token/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ const (
GT = ">"
GTEQ = ">="

EQ = "=="
NOT_EQ = "!="
AND = "&&"
OR = "||"
EQ = "=="
NOT_EQ = "!="
AND = "&&"
OR = "||"
MATCHES = "~="

// Delimiters

Expand Down

0 comments on commit 9fd9491

Please sign in to comment.