Python 如何使用Pyparsing解析嵌套函数调用?

Python 如何使用Pyparsing解析嵌套函数调用?,python,nested,grammar,pyparsing,function-calls,Python,Nested,Grammar,Pyparsing,Function Calls,我正在开发一个css解析器,但不知道如何解析像alpha(rgb(1,2,3),0.5)这样的嵌套函数调用 这是我的密码: # -*- coding: utf-8 -*- from pyparsing import * #, Word, alphas, OneOrMore, countedArray, And, srange, hexnums, Combine, cStyleComment from string import punctuation from app.utils.fil

我正在开发一个css解析器,但不知道如何解析像alpha(rgb(1,2,3),0.5)这样的嵌套函数调用

这是我的密码:

# -*- coding: utf-8 -*-


from pyparsing import * #, Word, alphas, OneOrMore, countedArray, And, srange, hexnums,  Combine, cStyleComment

from string import punctuation
from app.utils.file_manager import loadCSSFile, writeFile
from pyparsing import OneOrMore, Optional, Word, quotedString, delimitedList,  Suppress
from __builtin__ import len

# divide tudo por espaços, tabulações e novas linhas
words = ZeroOrMore(cStyleComment | Word(printables))

digit = '0123456789'; underscore = '_'; hyphen =  '-'
hexDigit = 'abcdefABCDEF' + digit
letter = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
az_AZ_underscore = underscore + letter;  
az_AZ_09_underscore = az_AZ_underscore + digit; 
az_AZ_hyphen = hyphen + letter;
az_AZ_09_hiphen = az_AZ_hyphen + digit;
LPAR = Suppress('('); RPAR = Suppress(')')

# identifiers
identifier = Combine(Word(az_AZ_underscore) + Optional(Word(az_AZ_09_underscore)))
# identifiers
identifier_reserved = Combine(Word(az_AZ_hyphen) + Optional(Word(az_AZ_09_hiphen)))

# numbers
hexadecimal = Word(hexnums, min=1)
integer = Word(digit, min=1)
decimal = Combine('.' + integer | integer + Optional('.' + integer))


# value values
color_hex = Combine('#' +  hexadecimal ) 
at_identifier =  Combine('@' + identifier)
arg = at_identifier | color_hex | decimal | quotedString 




function_call = identifier + LPAR + Optional(delimitedList(arg)) + RPAR

value =  Group(color_hex | at_identifier | function_call)

print(value.parseString('a(b())'))
我想做一些类似于arg=at|u identifier | color|u hex | decimal | quotedString | function|u调用的事情,但这是不可能的,因为变量是function|u调用尚未声明


如何使用Pyparsing解析嵌套函数调用?

您真的非常接近。要定义这样的递归语法,需要向前声明嵌套表达式

正如您已经看到的,这是:

arg = at_identifier | color_hex | decimal | quotedString
function_call = identifier + LPAR + Optional(delimitedList(arg)) + RPAR
仅解析带有本身不是函数调用的参数的函数调用

要递归定义此表达式,请首先使用Forward()定义一个空占位符表达式:

我们还不知道这个表达式将包含什么,但我们知道它将是一个有效的参数类型。既然现在已经声明,我们可以使用它:

arg = at_identifier | color_hex | decimal | quotedString | function_call

现在我们已经定义了arg,我们可以定义函数调用的内容。我们必须使用一个操作符来修改函数调用,而不是重新定义它,而不是使用普通的Python赋值。Pyparsing允许您使用,谢谢!它工作得很好!你是pyparsing的作者吗?再次感谢这个伟大的工具!嗨,保罗-我发现很难从PyCon 2006找到你的演讲。这里提到了它,但是链接已经死了:-我最终通过访问archive.org找到了它。他们有以前托管在那里的文件,但要么JS脚本有缺陷,要么归档过程中出现了一些问题,使得在浏览器中很难查看(整个页面经常闪烁)。将存档保存为PDF格式使我能够以可用的形式进行演示。我只是觉得你可能想修复这个死链接,这样其他人就不会像我一样有那么多麻烦了。(PyCon 2006回收/更新用于08年的TX不一致性)-鼠标悬停在右下角以浏览幻灯片
arg = at_identifier | color_hex | decimal | quotedString | function_call
function_call <<= identifier + LPAR + Optional(delimitedList(arg)) + RPAR
function_call <<= Group(identifier + LPAR + Group(Optional(delimitedList(arg))) + RPAR)