cayleygraph/cayley · error

invalid syntax

Error message

invalid syntax

What it means

The sexp session's Parse validates input by attempting to parse it as an s-expression. If the parentheses are balanced (not more input required) but the input still parses to zero expressions, the session reports 'invalid syntax', meaning the input is complete but not a valid query expression.

Source

Thrown at query/sexp/session.go:72

		}
		if x == ')' {
			parenDepth--
			if parenDepth < 0 {
				min := 0
				if (i - 10) > min {
					min = i - 10
				}
				return fmt.Errorf("too many close parentheses at char %d: %s", i, input[min:i])
			}
		}
	}
	if parenDepth > 0 {
		return query.ErrParseMore
	}
	if len(ParseString(input)) > 0 {
		return nil
	}
	return errors.New("invalid syntax")
}

func (s *Session) Execute(ctx context.Context, input string, opt query.Options) (query.Iterator, error) {
	switch opt.Collation {
	case query.Raw, query.REPL:
	default:
		return nil, &query.ErrUnsupportedCollation{Collation: opt.Collation}
	}
	it := BuildIteratorTreeForQuery(ctx, s.qs, input).Iterate()
	if err := it.Err(); err != nil {
		return nil, err
	}
	if opt.Limit > 0 {
		it = iterator.NewLimitNext(it, int64(opt.Limit))
	}
	return &results{
		s:   s,
		col: opt.Collation,

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Fix the s-expression syntax (balance parentheses, remove stray tokens) and resubmit.
  2. Validate the query shape with ParseString locally before calling Parse.
  3. Use a supported higher-level query language (Gizmo/GQL) if s-expression syntax is unfamiliar.

Example fix

// before
_, err := ses.Parse(ctx, "g.V))")
// after
_, err := ses.Parse(ctx, "(g.V)")
Defensive patterns

Strategy: validation

Validate before calling

if len(query.ParseString(input)) == 0 && strings.Count(input, "(") == strings.Count(input, ")") {
    return errors.New("input is not a valid sexp query")
}

Type guard

func isValidSexp(input string) bool {
    return len(query.ParseString(input)) > 0 || strings.Count(input, "(") > strings.Count(input, ")")
}

Try / catch

_, err := ses.Parse(ctx, input)
if err != nil && err != query.ErrParseMore {
    return fmt.Errorf("invalid sexp query %q: %w", input, err)
}

Prevention

When it happens

Trigger: Calling sexp.Session.Parse with complete but malformed input, e.g. an unmatched ')' ('g.V)'), an empty string, or only whitespace/comments that yield no parsed expressions.

Common situations: Typos in hand-written s-expression queries; sending garbage lines to a Cayley REPL; clients pre-validating queries before Execute.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of cayleygraph/cayley@81dcd7d73e (2026-09-06). Data as JSON: /api/errors/5f5b116189ebf6d0. Report an issue: GitHub.