expr icon indicating copy to clipboard operation
expr copied to clipboard

Dont count newline character as a terminating string literal when parsing

Open yguo-wish opened this issue 3 years ago • 0 comments
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)
}

yguo-wish avatar Apr 06 '22 19:04 yguo-wish