Search code examples
parsingrebol

In Rebol PARSE, how to check for begin of input?


I need a rule, let call it AT-BEGIN, that matches the begin of input.

Maybe it exists, or how to implement it?

EXAMPLES I WANT TO WORK:

  1. parse "x" [AT-BEGIN "x"] => match
  2. parse "{some-other-chars}x" [to "x" AT-BEGIN "x"] => no-match

MOTIVATION:

I'm try parsing some markdown-like patterns, where an '*' starts emphasis, but only if after a space, or at beginning of text: [space | at-begin] emphasis

Using @hostilefork solution I could write: [to "*" pos: [if (head? pos) | (pos: back pos) :pos space] skip ...]


Solution

  • There's no such rule. But if there were, you'd have to define if you specifically want to know if it's at the beginning of a series...or at the beginning of where you were asked to start the parse from. e.g. should this succeed or fail?

    parse (next "cab") [to "a" begin skip "b"]
    

    It's not at the beginning of the series but the parse position has not moved. Does that count as the beginning?

    If you want a test just for the beginning of the series:

    [to "a" pos: if (head? pos) ...]
    

    You'd have to capture the position at the beginning or otherwise know it to see if the parse position had advanced at all:

    [start: to "a" pos: if (pos = start) ...]