Skip to content

Commit

Permalink
fix: fix the expression in string parse uncorrectly
Browse files Browse the repository at this point in the history
  • Loading branch information
fefit committed Nov 17, 2021
1 parent 14c0908 commit c6068d3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion fet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCompile(t *testing.T) {
t.Run("Test Smarty mode compile", func(t *testing.T) {
assertOutputToBe(t, "hello.tpl", nil, helloFet)
assertOutputToBe(t, "variable.tpl", nil, helloFet)
assertOutputToBe(t, "strvar.tpl", nil, helloFet)
assertOutputToBe(t, "strvar.tpl", nil, helloFet+helloFet)
assertOutputToBe(t, "keywordvar.tpl", nil, helloFet)
// rewrite foreach
assertOutputToBe(t, "foreach.tpl", map[string][]string{
Expand Down
18 changes: 7 additions & 11 deletions lib/expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,25 +479,16 @@ func (str *StringToken) Add(s rune) (ok bool, isComplete bool, retry bool, err e
if logics["IsInVar"] {
last := str.Variables[len(str.Variables)-1]
last.EndIndex = len(stat.Values)
logics["IsInVar"] = false
} else {
logics["IsInVar"] = true
str.Variables = append(str.Variables, &Indexs{
StartIndex: len(stat.Values) - 1,
})
logics["IsInVar"] = true
}
} else if s == Quote {
isComplete = true
str.IsComplete = true
vars := str.Variables
realVars := []*Indexs{}
if len(vars) > 0 {
for _, pos := range vars {
if pos.EndIndex > pos.StartIndex+1 {
realVars = append(realVars, pos)
}
}
str.Variables = realVars
}
}
return
}
Expand All @@ -506,6 +497,11 @@ func (str *StringToken) Add(s rune) (ok bool, isComplete bool, retry bool, err e
func (str *StringToken) Validate(tokens []AnyToken) (retryToken AnyToken, err error) {
_, prevs := getNoSpaceTokens(tokens, 1)
prev := prevs[0]
stat := str.Stat
if stat.Logics["IsInVar"] {
index := str.Variables[len(str.Variables)-1]
return nil, fmt.Errorf("the variable %s in string is not end correctly.", string(stat.Values[index.StartIndex:]))
}
if prev == nil {
return
}
Expand Down
5 changes: 5 additions & 0 deletions lib/expression/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func TestTokenize(t *testing.T) {
})
}

func TestStringVariable(t *testing.T) {
assertTokenList(t, "\"hello `$name`!`$hello` fet!\"", "StringToken")
assertErrorTokenize(t, "\"hello `$name\"")
}

func TestToAst(t *testing.T) {
t.Run("Test to ast", func(t *testing.T) {
_, err := exp.Parse("!!!!!$a.b != \"1\"")
Expand Down
9 changes: 4 additions & 5 deletions lib/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,26 +214,25 @@ func (gen *Generator) parseRecursive(node *Node, options *GenOptions, parseOptio
for _, pos := range vars {
if pos.StartIndex > i {
text := string(runes[i:pos.StartIndex])
str.WriteString("\"")
str.WriteString(" \"")
str.WriteString(text)
str.WriteString("\" ")
}
express := string(runes[pos.StartIndex+1 : pos.EndIndex-1])
ast, _ := exp.Parse(express)

var inner string
if inner, noDelimit, err = gen.Build(ast, options, parseOptions); err != nil {
return noDelimit, err
}
str.WriteString(inner)
i = pos.EndIndex + 1
i = pos.EndIndex
if i >= total {
break
}
}
if i < total {
if i < total-1 {
str.WriteString(" \"")
str.WriteString(string(runes[i-1 : total]))
str.WriteString(string(runes[i:]))
}
str.WriteString(")")
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/smarty/templates/strvar.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{%$name = "fet"%}
{%$hello = "hello"%}
{%"hello `$name`!"%}
{%"hello `$name`!`$hello` `$name`!"%}

0 comments on commit c6068d3

Please sign in to comment.