不区分大小写的令牌匹配 [英] Case insensitive token matching

查看:109
本文介绍了不区分大小写的令牌匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将语法设置为不区分大小写.

Is it possible to set the grammar to match case insensitively.

例如规则:

checkName = 'CHECK' Word;

将匹配check name以及CHECK name

推荐答案

此处是 PEGKit 的创建者.

目前唯一的方法是使用语义谓词来回旋:

The only way to do this currently is to use a Semantic Predicate in a round-about sort of way:

checkName = { MATCHES_IGNORE_CASE(LS(1), @"check") }? Word Word;

一些解释:

  • 语义谓词是直接从ANTLR提升的功能.语义谓词部分是{ ... }?.这些可以放在语法规则中的任何位置.它们应包含单个表达式或以return语句结尾的一系列语句,该语句的结果为布尔值.这个只包含一个表达式.如果表达式的计算结果为 false ,则当前规则(在这种情况下为checkName)的匹配将失败. true 值将允许进行匹配.

  • Semantic Predicates are a feature lifted directly from ANTLR. The Semantic Predicate part is the { ... }?. These can be placed anywhere in your grammar rules. They should contain either a single expression or a series of statements ending in a return statement which evaluates to a boolean value. This one contains a single expression. If the expression evaluates to false, matching of the current rule (checkName in this case) will fail. A true value will allow matching to proceed.

MATCHES_IGNORE_CASE(str, regexPattern)是我为在谓词和操作中用于进行正则表达式匹配而定义的便捷宏.它有一个区分大小写的朋友:MATCHES(str, regexPattern).第二个参数是NSString*正则表达式模式.含义应该很明显.

MATCHES_IGNORE_CASE(str, regexPattern) is a convenience macro I've defined for your use in Predicates and Actions to do regex matches. It has a case-sensitive friend: MATCHES(str, regexPattern). The second argument is an NSString* regex pattern. Meaning should be obvious.

LS(num)是供您在谓词/操作中使用的另一个便利宏.这意味着获取一个 Lookahead字符串,并且该参数指定了要向前看多远.因此,LS(1)意味着1要提前.换句话说,获取解析器将要尝试匹配的第一个即将到来的令牌的字符串值".

LS(num) is another convenience macro for your use in Predicates/Actions. It means fetch a Lookahead String and the argument specifies how far to lookahead. So LS(1) means lookahead by 1. In other words, "fetch the string value of the first upcoming token the parser is about to try to match".

请注意,我仍然在结尾处匹配Word 两次.第一个Word是匹配'check'所必需的(即使在谓词中已经过 testing 了,但未被匹配和消耗).第二个Word用于您的name或其他任何内容.

Notice that I'm still matching Word twice at the end there. The first Word is necessary for matching 'check' (even though it was already tested in the predicate, it was not matched and consumed). The second Word is for your name or whatever.

希望有帮助.

这篇关于不区分大小写的令牌匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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