여기 내 Perl 6 프로젝트 중 하나 인 액션 클래스 메소드가 설명하는 것을 수행합니다.
거의 크리스토프 게시 된 내용과 동일하지 않습니다,하지만 더 자세하게 기록 (내가 쉽게 이해할 수 있도록 주석의 풍부한 양을 추가 한) :
#| Fallback action method that produces a Hash tree from named captures.
method FALLBACK ($name, $/) {
# Unless an embedded { } block in the grammar already called make()...
unless $/.made.defined {
# If the Match has named captures, produce a hash with one entry
# per capture:
if $/.hash -> %captures {
make hash do for %captures.kv -> $k, $v {
# The key of the hash entry is the capture's name.
$k => $v ~~ Array
# If the capture was repeated by a quantifier, the
# value becomes a list of what each repetition of the
# sub-rule produced:
?? $v.map(*.made).cache
# If the capture wasn't quantified, the value becomes
# what the sub-rule produced:
!! $v.made
}
}
# If the Match has no named captures, produce the string it matched:
else { make ~$/ }
}
}
주 :
- 이것은 위치 캡쳐 (문법 내에
()
으로 만들어진 캡쳐)를 완전히 무시합니다. 해시 트리를 만들기 위해 명명 된 캡쳐 (예 : <foo>
또는 <foo=bar>
) 만 사용됩니다. 당신이 그들과 함께하고 싶은 것에 따라 그것들을 다루기 위해 수정 될 수도 있습니다.다음을 유의하십시오.
$/.hash
은 Map
으로 명명 된 캡처를 제공합니다.
$/.list
은 위치 캡처를 List
으로 제공합니다.
$/.caps
(또는 $/.pairs
)은 name=>submatch
및/또는 index=>submatch
쌍의 시퀀스로 명명 된 캡처와 위치 캡처를 모두 제공합니다.
- 그것은, 또는 방법을 추가하여 당신이 중 하나를 (당신이 결코 의도적으로
make
에 정의되지 않은 값을 원하는 없다고 가정) 문법의 규칙 내부 { make ... }
블록을 추가하여 특정 규칙에 대한 AST 생성을 대체 할 수 있습니다 액션 클래스의 룰의 이름.
'.caps'와'.pairs'의 차이점은 전자는 같은 키에 해당하는 서브 쿼리를 다른 항목으로 나열하는 반면, 후자는 키를 반복하지 않고 배열에 서브 쿼리를 배치한다는 것입니다 필요한 – Christoph