7

我要解析

'This,is,an,example,text'

就像在 findTokens

'This,is,an,example,text' findTokens: $, 
an OrderedCollection('This' 'is' 'an' 'example' 'text')

但无法弄清楚如何使用 PetitParser、delimitedBy: 和 separatorBy: 并没有帮助我我试过

( #any asParser delimitedBy: $, asParser ) plus flatten parse:  'This,is,an,example,text'

但显然没有奏效

4

4 回答 4

3

您可以delimitedBy:结合使用withoutSeparators

|text parser|

text := 'This,is,an,example,text'.
parser := (#word asParser plus flatten delimitedBy: ($, asParser)) withoutSeparators.

parser parse: text

似乎是最近对 PetitParser 的改进。

于 2012-09-04T09:24:13.187 回答
2

a #delimitedBy: b扩展为a , (b , a) star,因此您的解析器按原样说“给我一个用逗号分隔的字符”。

它不是很可读,但这可以满足您的要求:

((($, asParser not , #any asParser) ==> [:nodes | nodes second])
  plus flatten delimitedBy: $, asParser

第一个子句说“解析任何不是逗号的东西”。所以给'12,24'你得到#('12' $, '24')

于 2011-10-18T09:03:20.313 回答
1

尝试

(#word asParser plus flatten separatedBy: $, asParser) 
     ==> [:nodes| nodes copyWithout: $, ]

我希望我明白你想要什么

于 2011-10-18T10:11:28.977 回答
1

当我想排除某些东西时,我一直使用 PetitParser 这个模式。只需将“我正在寻找的内容”或“我想要排除的内容”(以更容易描述的为准)定义为解析器,然后将其取反,并根据需要进行处理。

s := 'This,is,an,example,text'.
separator := $, asParser ==> [ :n | nil ].
token := separator negate plus flatten.
p := (token separatedBy: separator) ==> [ :nodes |
    nodes copyWithout: nil ].
p parse: s.
于 2011-10-18T14:42:51.207 回答