expr
expr copied to clipboard
Dont count newline character as a terminating string literal when parsing
trafficstars
Request
Don't count newline character '\n' as a terminating string literal when parsing. The newline character within a sentence may be intentional and meaningful for other purposes.
With current implementation that treats newline character as a terminating literal for parsing, the following program will panic:
package main
import (
"fmt"
"github.com/antonmedv/expr"
)
type Env struct {
}
func (Env) CountNewLine(s string) int {
newlineCount := 0
for ch := range s {
if ch == '\n' {
newlineCount++
}
}
return newlineCount
}
func main() {
code := "CountNewLine(\"Hello\nHello\")"
program, err := expr.Compile(code, expr.Env(Env{}))
if err != nil {
panic(err)
}
env := Env{}
output, err := expr.Run(program, env)
if err != nil {
panic(err) <-- panic
}
fmt.Println(output)
}