<?> Always Succeed Assertion

Documentation for <?> Always Succeed Assertion, assembled from the following types:

class Grammar

From Grammar

(Grammar) <?> Always Succeed Assertion

The <?> is the always succeed assertion. When used as a grammar token, it can be used to trigger an Action class method. In the following grammar we look for Arabic digits and define a succ token with the always succeed assertion.

In the action class, we use calls to the succ method to do set up (in this case, we prepare a new element in @!numbers). In the digit method, we convert an Arabic digit into a Devanagari digit and add it to the last element of @!numbers. Thanks to succ, the last element will always be the number for the currently parsed digit digits.

grammar Digifier {
    rule TOP {
        [ <.succ> <digit>+ ]+
    }
    token succ   { <?> }
    token digit { <[0..9]> }
}
 
class Devanagari {
    has @!numbers;
    method digit ($/{ @!numbers[*-1~= $/.ord.&[+](2358).chr }
    method succ  ($)  { @!numbers.push: ''     }
    method TOP   ($/{ make @!numbers[^(*-1)] }
}
 
say Digifier.parse('255 435 777'actions => Devanagari.new).made;
# OUTPUT: «(२५५ ४३५ ७७७)␤»