在PetitParser2中,我如何匹配一组封闭的标记,比如月份名称?例如(伪代码) [ :word | MonthNames anySatisfy: [ :mn | mn beginsWith: word ] ] asParser.
PPPredicateSequenceParser似乎是一种可能性,但似乎你必须事先知道字符串的大小。我想我可以这样做:
| monthRules |
monthRules := Array streamContents: [ :unamused: |
MonthNames collect: [ :e |
s nextPut: e asString asPParser.
s nextPut: (e first: 3) asPParser ] ].
^ PP2ChoiceNode withAll: monthRules但我想知道有没有什么东西是直接的
发布于 2019-11-29 15:58:42
另一种更笨拙、效率更低的选择是使用自定义块:
[ :context |
| position names |
names := #('January' 'February' 'March' 'April').
position := context position.
names do: [ :name |
(context next: name size) = name ifTrue: [
^ name
] ifFalse: [
context position: position
]
].
^ PP2Failure new
] asPParser parse: 'April'不过,我不建议这样做,因为PP2对块一无所知,也不能应用任何优化。
发布于 2019-11-26 16:43:07
我建议对集合中的每个元素使用解析器:
monthsParser := 'January' asPParser /
'February' asPParser /
'March' asPParser.
monthsParser parse: 'January'或者,从集合创建一个选择解析器:
names := #('January' 'February' 'March' 'April').
monthsParser := PP2ChoiceNode withAll: (names collect: [ :l |
l asPParser ]).
monthsParser parse: 'January'PP2的“优化”应该很快就能选择正确的替代方案。
https://stackoverflow.com/questions/59036943
复制相似问题