简体   繁体   English

使用javascript正则表达式提取python代码中的函数名称

[英]Extract function names in python code using javascript regex

I am using prism.js to highlight python code but for some reasons it is not highlighting function names which are called for example deep_flatten() or flatten([1,2,3]) or flatten(list(map(lambda x: x*2,[1,2,3]))) . 我正在使用prism.js突出显示python代码,但由于某些原因,它没有突出显示被称为deep_flatten()flatten([1,2,3])flatten(list(map(lambda x: x*2,[1,2,3])))

So made the following code to overcome this problem 所以做了如下代码来克服这个问题

[...document.getElementsByClassName('code')].forEach(x => {
      x.innerText = x.innerText.replace(/(\w+)\([^\(\)]*\)/gi,match=>{
        if(match.match(/^\d/)){
             return match
                              }

        else {
              return `<span class="called-function">${match}</span>`
            }

        }
   })

It works fine for the first two ones but fails for the other two ones. 对于前两个,它工作正常,但对于其他两个,则失败。 On doing google search I found that this is called something recursive and can be done only with parsers. 在做谷歌搜索时,我发现这被称为递归,只能用解析器来完成。 On searching for python parsers in javscript I found a lot of them but they are very big and for parsing whole code. 在javscript中搜索python解析器时,我发现了很多,但是它们很大并且可以解析整个代码。

How can I make a parser/regex which extracts all function names and encloses them within span tags. 如何制作解析器/正则表达式,以提取所有函数名称并将其包含在span标记内。

I don't want the specific code just some psuedo-code or algorithm as to how to proceed. 我不希望特定代码只是一些伪代码或算法来进行。

The default re package in std libs can't handle recursive regexes, however seems the regex package can std libs中的默认re包不能处理递归regex表达式,但是似乎regex包可以

/(\w+)\([^\(\)]*\)/gi

can be changed to 可以更改为

/(\w+)(\((?:[^\(\)]|(?2))*\))/gi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM