diff --git a/fet_test.go b/fet_test.go index 0fea4dc..b227bcf 100644 --- a/fet_test.go +++ b/fet_test.go @@ -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{ diff --git a/lib/expression/expression.go b/lib/expression/expression.go index 1bc9d8f..19d91e3 100644 --- a/lib/expression/expression.go +++ b/lib/expression/expression.go @@ -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 } @@ -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 } diff --git a/lib/expression/expression_test.go b/lib/expression/expression_test.go index 89d55fa..e5c9a9c 100644 --- a/lib/expression/expression_test.go +++ b/lib/expression/expression_test.go @@ -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\"") diff --git a/lib/generator/generator.go b/lib/generator/generator.go index 59b1919..9010615 100644 --- a/lib/generator/generator.go +++ b/lib/generator/generator.go @@ -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 { diff --git a/tests/smarty/templates/strvar.tpl b/tests/smarty/templates/strvar.tpl index 46d2437..b533b63 100644 --- a/tests/smarty/templates/strvar.tpl +++ b/tests/smarty/templates/strvar.tpl @@ -1,3 +1,3 @@ {%$name = "fet"%} {%$hello = "hello"%} -{%"hello `$name`!"%} \ No newline at end of file +{%"hello `$name`!`$hello` `$name`!"%} \ No newline at end of file