Wait the light to fall

子解析

焉知非鱼

子解析 #

游标不一定要到达字符串的末尾才算成功。也就是说,它不一定要匹配整个字符串。

subparse 总是返回一个 Match 对象

method subparse(
    $target, 
    :$rule = 'TOP', 
    Capture() :$args = \(),  
    Mu :$actions = Mu, 
    *%opt
)

Grammar #

grammar RepeatChar {
    token start($character) { $character+ }
}

解析 #

say RepeatChar.subparse(
    'bbbabb', 
    :rule('start'), 
    :args(\('b'))
);          # 「bbb」

say RepeatChar.parse(
    'bbbabb', 
    :rule('start'), 
    :args(\('b'))
);          # Nil

say RepeatChar.subparse(
    'bbbabb', 
    :rule('start'), 
    :args(\('a'))
);          # <failed match>

say RepeatChar.subparse(
    'bbbabb', 
    :rule('start'), 
    :args(\('a')), 
    :pos(3)
);          # 「a」