Parsekit:如何匹配单个引号字符? [英] Parsekit: how to match individual quote characters?

查看:49
本文介绍了Parsekit:如何匹配单个引号字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone上使用解析器Parsekit时.是否可以在双引号中包含?特殊BNF包含哪些内容?(是否可以在定义的语法中转义序列?)

When using the parser Parsekit for the iPhone. Is it possible to include against a double quote? And things which are part of the special BNF? (Is it possible to escape sequences in a defined grammer?)

@start = doublequote+;
doublequote= '"'

推荐答案

ParseKit的开发人员在这里.

Developer of ParseKit here.

默认情况下,您可以使用内置的 QuotedString 解析器(将匹配 QuotedString 令牌)轻松地将其与带引号的字符串进行匹配:

By default you can match against quoted strings easily using the built-in QuotedString parser (which will match QuotedString tokens):

@start = quotes;
quotes = QuotedString+;

与输入类似的

,例如:"foo"'bar'"baz"

作为三个带引号的字符串:"foo" 'bar'"baz"

as three quoted strings: "foo", 'bar', "baz"

因此,这表明默认情况下,当遇到"'时,ParseKit令牌生成器( PKTokenizer 类)会生成 QuotedString 令牌..

So this demonstrates that by default the ParseKit tokenizer (the PKTokenizer class) produces QuotedString tokens when encountering a " or '.

有关默认令牌生成器行为的更多详细信息,请阅读 ParseKit令牌生成文档.

For more details on default tokenizer behavior, read the ParseKit tokenization documentation.

但是,如果您希望将引号字符("')识别为独立的符号,而不是指出其开头或结尾带引号的字符串,您必须先更改令牌生成器的行为.

However, if you want quote chars (", ') to be recognized as standalone symbols rather than indicating the start or end of a quoted string, you must alter the tokenizer behavior first.

在代码中,您可以通过调用 PKTokenizer 对象上的方法来更改令牌生成器的行为.

In code, you would alter tokenizer behavior by calling methods on your PKTokenizer object.

在语法中,您可以使用 tokenizer指令来更改令牌生成器的行为.

In grammars, you alter tokenizer behavior with tokenizer directives.

Tokenizer指令是放在语法顶部的特殊规则,它们以 @ 字符开头.在这种情况下,您想更改令牌生成器将哪些字符识别为独立的符号令牌.具体来说,您要使用 @symbolState 标记器指令添加两个字符作为符号.

Tokenizer directives are special rules placed at the top of your grammar which start with a @ character. In this case, you want to change which characters are recognized as standalone symbol tokens by the tokenizer. Specifically, you want to add two chars as symbols with the @symbolState tokenizer directive.

您可以在语法中将其更改为:

You can do that in your grammar by changing it to:

@symbolState = '"' "'"; // a tokenizer directive stating ' and " should be recognized as standalone symbol tokens
                        // (by default they are start- and end-markers for quoted string tokens)

@start = stuff;
stuff = (Word | Symbol)+;

鉴于与上述相同的输入,您将匹配单独的引号符号和单词:" foo " ' bar '" baz "

Given the same input as above, you would match separate quote symbols and words: ", foo, ", ', bar, ', ", baz, "

这篇关于Parsekit:如何匹配单个引号字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆