python - SCons 中错误的 D 依赖逻辑

标签 python regex import d scons

我发现了 SCons 中 D 源的依赖逻辑中的一个错误。

self.cre regexp import\s+(?:\[a-zA-Z0-9_.\]+)\s*(?:,\s*(?:\[a-zA-Z0-9_.\]+)\s*)*; in SCons.Scanner.D不涵盖诸如...之类的模式

import IMPORT_PATH : SYMBOL;

...仅:

import IMPORT_PATH;

self.cre2 regexp (?:import\s)?\s*([a-zA-Z0-9_.]+)\s*(?:,|;) two lines later 相同.

我认为 self.creself.cre2 正则表达式都需要修复;但我不太明白它们之间有何关系。我的猜测是 self.cre 匹配整个导入语句,而 self.cre2 匹配其中的一部分。我对么?如果是这样,则需要更正 self.cre2 以处理以下情况:

import X, Y, Z;

有人知道如何修复正则表达式以便处理这些情况吗?

我的第一次尝试是改变

p = 'import\s+(?:[a-zA-Z0-9_.]+)\s*(?:,\s*(?:[a-zA-Z0-9_.]+)\s*)*;'

p = 'import\s+(?:[a-zA-Z0-9_.]+)\s*(?:,\s*(?:[a-zA-Z0-9_.]+)(?:\s*:\s*[a-zA-Z0-9_.]+)??\s*)*;'

我尝试过调试这个,但没有成功。

Python:

import re
p = 'import\s+(?:[a-zA-Z0-9_.]+)\s*(?:,\s*(?:[a-zA-Z0-9_.]+)\s*)*;'

re.match(p, "import first;") # match
re.match(p, "import first : f;") # no match

p2 = 'import\s+(?:[a-zA-Z0-9_.]+)\s*(?:,\s*(?:[a-zA-Z0-9_.]+)(?:\s*:\s*[a-zA-Z0-9_.]+)??\s*)*;'

re.match(p2, "import first;") # match
re.match(p2, "import first : f;") # no match but should match
re.match(p2, "import first : f, second : g;") # no match but should match

最佳答案

简短回答

要处理您概述的所有情况,请尝试对 ( self.cre ) 模式进行以下更改:

import\s+(?:[a-zA-Z0-9_.]+)\s*(?:(?:\s+:\s+[a-zA-Z0-9_.]+\s*)?(?:,\s*(?:[a-zA-Z0-9_.]+)(?:\s*:\s*[a-zA-Z0-9_.]+)??\s*)*)*;

Regular expression visualization

Debuggex Demo

深入挖掘

self.cre 与 self.cre2

是的, find_include_names 方法...

def find_include_names(self, node):
    includes = []
    for i in self.cre.findall(node.get_text_contents()):
        includes = includes + self.cre2.findall(i)
    return includes

...确认 self.cre 之间的关系和self.cre2您猜到了:前者匹配整个导入语句,后者匹配(并捕获)其中的模块。 (注意 ( 中的中间 ) ... self.cre2 捕获组与 (?: ... ) 整个其他地方的捕获组self.creself.cre2 。)

self .cre

领取where your Python snippet left off ...

import re

import1 = "import first;"
import2 = "import first : f;"
import3 = "import first : f, second : g;"


p = 'import\s+(?:[a-zA-Z0-9_.]+)\s*(?:,\s*(?:[a-zA-Z0-9_.]+)\s*)*;'

pm1 = re.match(p, import1) # match
if pm1 != None:
    print "p w/ import1 => " + pm1.group(0)

pm2 = re.match(p, import2) # no match
if pm2 != None:
    print "p w/ import2 => " + pm2.group(0)


p2 = 'import\s+(?:[a-zA-Z0-9_.]+)\s*(?:,\s*(?:[a-zA-Z0-9_.]+)(?:\s*:\s*[a-zA-Z0-9_.]+)??\s*)*;'

p2m1 = re.match(p2, import1) # match
if p2m1 != None:
    print "p2 w/ import1 => " + p2m1.group(0)

p2m2 = re.match(p2, import2) # no match but should match
if p2m2 != None:
    print "p2 w/ import2 => " + p2m2.group(0)

p2m3 = re.match(p2, import3) # no match but should match
if p2m3 != None:
    print "p2 w/ import3 => " + p2m3.group(0)

...,我们得到 p 的以下预期输出和p2尝试匹配导入语句:

p w/ import1 => import first;
p2 w/ import1 => import first;

现在考虑p2prime ,其中我制作了 changes to arrive at the pattern I suggested上图:

import re

import1 = "import first;"
import2 = "import first : f;"
import3 = "import first : f, second : g;"
import4 = "import first, second, third;"

p2prime = 'import\s+(?:[a-zA-Z0-9_.]+)\s*(?:(?:\s+:\s+[a-zA-Z0-9_.]+\s*)?(?:,\s*(?:[a-zA-Z0-9_.]+)(?:\s*:\s*[a-zA-Z0-9_.]+)??\s*)*)*;'

p2pm1 = re.match(p2prime, import1) # match
if p2pm1 != None:
    print "p2prime w/ import1 => " + p2pm1.group(0)

p2pm2 = re.match(p2prime, import2) # now a match
if p2pm2 != None:
    print "p2prime w/ import2 => " + p2pm2.group(0)

p2pm3 = re.match(p2prime, import3) # now a match
if p2pm3 != None:
    print "p2prime w/ import3 => " + p2pm3.group(0)

p2pm4 = re.match(p2prime, import4) # now a match
if p2pm4 != None:
    print "p2prime w/ import4 => " + p2pm4.group(0)

使用更新后的模式 ( p2prime ),我们会得到以下所需的输出,以尝试匹配导入语句:

p2prime w/ import1 => import first;
p2prime w/ import2 => import first : f;
p2prime w/ import3 => import first : f, second : g;
p2prime w/ import4 => import first, second, third;

这是一个相当冗长且复杂的模式:因此,如果有机会进一步对其进行微调,我不会感到惊讶;但它可以满足您的需求,并且应该为微调提供坚实的基础。

self.cre2

对于self.cre2 ,类似地尝试以下模式:

(?:import\s)?\s*(?:([a-zA-Z0-9_.]+)(?:\s+:\s+[a-zA-Z0-9_.]+\s*)?)\s*(?:,|;)

Regular expression visualization

Debuggex Demo

但是请记住,自 D's <module> : <symbol> selective imports只是这样 - 选择性的,在选择性导入中捕获模块名称可能不是您最终需要的(例如,与捕获模块和选定的符号名称相比)。正如我对 self.cre 的类似解释我建议,在必要的地方进一步微调应该不难。

关于python - SCons 中错误的 D 依赖逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28647348/

相关文章:

php - 使用正则表达式匹配 MYSQL 中句子中的单词

regex - 如何在ADF数据流中的表达式生成器中编写表达式

image - Magento导入产品API调用模型: product_api: create product 1 product takes 40 seconds

java - 在 netbeans 中创建/访问库

python - 从 Python 执行 PowerShell 脚本的最佳方式是什么

Python argparse 可选破折号整数参数

python - 访问在主机 windows 机器上的 Virtual box ubuntu 上运行的 Web 服务器

python 的 scipy.stats.ranksums 与 R 的 wilcox.test

python - 带连字符的正则表达式单词

__import__ 和 import as 之间的 Python 区别