Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dashes in identfiers #83

Open
TomTheBear opened this issue Jun 12, 2022 · 1 comment
Open

Allow dashes in identfiers #83

TomTheBear opened this issue Jun 12, 2022 · 1 comment

Comments

@TomTheBear
Copy link

TomTheBear commented Jun 12, 2022

I have the case that one of my identifiers contains a -, like type-id. The parser fails because - is not an allowed rune in an identifier.

Example:

func main() {
    vars := map[string]interface{}{"type-id": "4"}
    expr := `type-id == "4"`
    e, err := gval.Full().NewEvaluable(expr)
    if err != nil {
        return
    }
    value, err := e.EvalBool(context.Background(), vars)
    if err != nil {
        return
    }
    fmt.Println(value)
}

I could open a PR, but the fix is so simple: Add the condition (pos > 0 && r == '-') to https://github.com/PaesslerAG/gval/blob/master/parser.go#L33 . The example works, I tested it even for multiple dashes. But I'm new to gval, so I might miss some cases.

@hoani
Copy link

hoani commented May 16, 2024

I'm a bit late to the party, but it looks like we can do this ourselves now:

  lang := gval.NewLanguage(
    gval.Init(func(ctx context.Context, p *gval.Parser) (gval.Evaluable, error) {
      p.SetIsIdentRuneFunc(func(r rune, pos int) bool {
        return unicode.IsLetter(r) || r == '_' || (pos > 0 && unicode.IsDigit(r)) || (pos > 0 && r == '-')
      })
      return p.ParseExpression(ctx)
    }),
    gval.Full()
  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants