giuliolunati
giuliolunati

Reputation: 766

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 ...]

Upvotes: 2

Views: 148

Answers (2)

joing
joing

Reputation: 139

Maybe not exactly what you want, but finding the first match can be done by the following.

You have to search for the pattern of complement to "b" followed by "a".

>> text-to-searh: "jjjj ball  adran"
>> b*: complement charset "b"
>> parse/all text-to-search [ any [ b* #"a" | "a" hit:  to end | skip ] ]
>> probe hit
"ll  adran"

Needs some modification to also match the case that first char is "a". hit will be positioned at the character following the "a".

Upvotes: 0

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) ...]

Upvotes: 1

Related Questions