Skip to content

parsers: add Prefixed for prefix operator chains - #42

Merged
rameel merged 1 commit into
mainfrom
parsers/prefixed
Oct 9, 2026
Merged

rameel merged 1 commit into
mainfrom
parsers/prefixed

Conversation

@rameel

@rameel rameel commented Oct 9, 2026

Copy link
Copy Markdown
Owner

Add Prefixed(op, reduce) for stacked prefix operators such as -, ! or ~ in front of an operand.

using static Ramstack.Parsing.Parser;

var unary = Literal
    .Number<int>()
    .Prefixed(
        OneOf("-~"),
        (value, op) => op == '-' ? -value : ~value);

unary.Parse("5").Value;   // 5
unary.Parse("-~5").Value; // 6, i.e. -(~5)
  • Operators are applied from right to left, so -~5 is -(~5).
  • Without operators the operand is returned as is.

Add Prefixed(op, reduce) to parse zero or more prefix operators followed
by an operand. Apply operators from right to left, so the operator
closest to the operand is reduced first. Stop collecting operators when
op succeeds without consuming input, and discard its result.
@rameel
rameel merged commit 5755de8 into main Oct 9, 2026
2 checks passed
@rameel
rameel deleted the parsers/prefixed branch October 9, 2026 17:05
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

Successfully merging this pull request may close these issues.

1 participant