Search code examples
rebolrebol3

Alternative rules in PARSE with SKIP only did not output expected results


I encounter a snippet of code:

blk: [1 #[none] 2 #[none] 3 #[none]]
probe parse blk [
    any [
        set s integer! (print 'integer) | (print 'none) skip
    ]
]

the output is:

integer
none
integer
none
integer
none
none
true

Note that in front of true there are two nones. While the next code snippet output the expected output:

blk: [1 #[none] 2 #[none] 3 #[none]]
probe parse blk [
    any [
        set s integer! (print 'integer) | and none! (print 'none) skip
    ]
]

output:

integer
none
integer
none
integer
none
true

Why the previous one could not output the same result with the last one?


Solution

  • Your first rule should better be

    probe parse blk [
        any [
            set s integer! (print 'integer) | skip (print 'none) 
        ]
    ]
    

    as in your first rule you print none, if there is no integer and just skip after. This causes, that you print none even when the cursor is at the end. The skip just ends the parsing.

    In your second rule the none! is not true at the end, so the parsing stops. It can be written shorter

    probe parse blk [
        any [
            set s integer! (print 'integer) |  none! (print 'none) 
        ]
    ]
    

    In your second rule the and does not move the cursor forward, so you need the additional skip. Without and the none! eats one item already.