简体   繁体   English

如何从 parseAction 获取 pyparsing 解析器的值

[英]How to get values of a pyparsing parser from parseAction

I have this pyparsing variable in a class:我在 class 中有这个 pyparsing 变量:

def takeval(type, name, value):
     #do stuff with args
    
self.variable = pp.Optional(self.let_ | self.const_ | self.var_).set_results_name('vartype') ^ self.varname ^ self.set_ ^ self.object
#I want to set the function

self.variable = self.variable.setParseAction(takeval(vartype, varname, object))

How would I get the values of vartype, varname and object from a parse action?如何从解析操作中获取 vartype、varname 和 object 的值?

  • I want vartype to be the value of one of: self.let_, self.const_ or self.var_我希望 vartype 是以下值之一:self.let_、self.const_ 或 self.var_
  • I want varname to be the value of self.varname我希望 varname 是 self.varname 的值
  • I wan object to be the value of self.object我希望 object 成为 self.object 的值

Example of goal output:目标 output 示例:

I parse the string: 'const hi = "hello"'我解析字符串: 'const hi = "hello"'

I want 'const', 'hi', 'hello' to be sent to the target function.我希望将'const', 'hi', 'hello'发送到目标 function。

Any help appreciated任何帮助表示赞赏

I have even three versions for you.我什至为你准备了三个版本。

  • Version 1 needs for each of you parser actions a separate call parser action.对于每个解析器操作,版本 1 需要一个单独的调用解析器操作。
  • Verison 2 needs only one such function.版本 2 只需要一个这样的 function。
  • Version 3 needs nothing of such but the caller code is not so nice any more.版本 3 不需要这样的东西,但调用者代码不再那么好。

Normally I use the result names a lot.通常我会经常使用结果名称。 But for simplification I did not do this in this example.但是为了简单起见,我在这个例子中没有这样做。

from pyparsing import *

class Parser:
    
  def takeval(self, type, name, value):
    print("Type is " + type)
    print("Name is " + name)
    print("Value is "+ value)
  
  # For version 1
  def call_takeval(self, s, loc, toks):
    self.takeval(toks[0], toks[1], toks[2])
  
  # For version 2
  def as_parse_action(self, parse_action):
    def call_parse_action(s, loc, toks):
      parse_action(toks[0], toks[1], toks[2])
    return call_parse_action
  
  
  def __init__(self):
  
    self.let_ = Literal("let")
    self.const_ = Literal("const")
    self.var_ = Literal("var")
    self.varname = Word(alphas)
    self.set_ = Literal("=")
    self.obj = Literal('"').suppress() + Word(alphas) + Literal('"').suppress()
    
    self.variable_version_1 = Optional(self.let_ | self.const_ | self.var_).setResultsName('vartype') \
        + self.varname.setResultsName('varname') + self.set_.suppress() + self.obj.setResultsName('varobj')
    
    self.variable_version_2 = Optional(self.let_ | self.const_ | self.var_).setResultsName('vartype') \
        + self.varname.setResultsName('varname') + self.set_.suppress() + self.obj.setResultsName('varobj')
    
    self.variable_version_3 = Optional(self.let_ | self.const_ | self.var_).setResultsName('vartype') \
        + self.varname.setResultsName('varname') + self.set_.suppress() + self.obj.setResultsName('varobj')

    self.variable_version_1 = self.variable_version_1.setParseAction(self.call_takeval)
    
    self.variable_version_2 = self.variable_version_2.setParseAction(self.as_parse_action(self.takeval))
    
    self.variable_version_3 = self.variable_version_2.setParseAction( \
        lambda s, loc, toks : self.takeval(toks[0], toks[1], toks[2]))
    
    
    
  def parse(self, text):
    print("Version 1:")
    self.variable_version_1.parseString(text)
    print("Version 2:")
    self.variable_version_2.parseString(text)
    print("Version 3:")
    self.variable_version_3.parseString(text)



test_string = 'const hi = "hello"'
parser = Parser()
parser.parse(test_string)

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

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