gastownhall/beads · error

unexpected character %q in expression

Error message

unexpected character %q in expression

What it means

After handling digits, operators, parentheses, and range/variable syntax, tokenize() reaches the default branch for any character it does not recognize and rejects the whole expression. This is the catch-all lexical error for illegal characters in range expressions.

Source

Thrown at internal/formula/range.go:191

					}
					tokens = append(tokens, token{tokNumber, val})
					i = j
					continue
				}
			}
			tokens = append(tokens, token{tokMinus, 0})
		case '*':
			tokens = append(tokens, token{tokMul, 0})
		case '/':
			tokens = append(tokens, token{tokDiv, 0})
		case '^':
			tokens = append(tokens, token{tokPow, 0})
		case '(':
			tokens = append(tokens, token{tokLParen, 0})
		case ')':
			tokens = append(tokens, token{tokRParen, 0})
		default:
			return nil, fmt.Errorf("unexpected character %q in expression", ch)
		}
		i++
	}

	tokens = append(tokens, token{tokEOF, 0})
	return tokens, nil
}

// Parser state
type exprParser struct {
	tokens []token
	pos    int
}

func (p *exprParser) current() token {
	if p.pos >= len(p.tokens) {
		return token{tokEOF, 0}
	}

View on GitHub (pinned to 71377f2769)

Solutions

  1. Remove or replace the offending character reported by %q
  2. Use only supported tokens: digits, '.', '+', '-', '*', '/', '^', '(', ')', and recognized variable syntax
  3. Run ValidateRange first to fail fast with the same message

Example fix

// before
range 1..10 & 3 { ... }
// after
range 1..10 { ... }
Defensive patterns

Strategy: validation

Validate before calling

allowed := regexp.MustCompile(`^[0-9+\-*/^().\s{}]*$`)
if !allowed.MatchString(expr) {
    // reject before calling EvaluateExpr/ValidateRange
}

Try / catch

tokens, err := tokenize(expr)
if err != nil && strings.Contains(err.Error(), "unexpected character") {
    // strip or escape the offending character and retry once
}

Prevention

When it happens

Trigger: EvaluateExpr or ValidateRange with an expression containing a character outside the supported token set — e.g. 'range 1..10 &' or '1..10 %3' or a stray comma/letter where an operand is expected.

Common situations: Shell metacharacters leaking into config, typos ('1..10!' instead of '1..10'), unsupported operators like '%' or '&', or locale/whitespace oddities like non-ASCII digits.

Related errors


AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30). Data as JSON: /api/errors/0ab4d54806d06ede. Report an issue: GitHub.