使用Scala解析器快速优化提取内部重复标识符的方法

时间:2013-09-03 16:22:34

标签: parsing scala

我在Scala中定义了这些语法规则:

def root = rep(block)
def block = ("block" ~ blockName ~ "{" ~ definition ~ "}")
def blockName = ident
def definition = creation ~ destruction ~ upgrades
def creation = "creation" ~ "{" ~ duration ~ cost ~ "}"
def destruction = "destruction" ~ "{" ~ duration ~ "}"
def upgrades = "upgrades" ~ "{" ~ rep(ident ~ "{" ~ duration ~ cost ~ "}") ~ "}"
def duration = "duration" ~ "=" ~ wholeNumber
def cost = "cost" ~ "=" ~ repsep(resource, ",")
def resource = (resourceType ~ "*" ~ wholeNumber)  
def resourceType = "wood" | "metal" | "food"

我的输入字符串是

"""
block citytown {
  creation {
    duration = 100
    cost = food * 10, wood * 10
  }
  destruction {
    duration = 100
  }

  upgrades {
    level1 {
      duration = 100
      cost = wood * 200  , food * 10
    }
    level2 {
      duration = 100
      cost = food * 30 , food * 10
    }
  }

}"""

我需要一种快速且优化的方法来提取每个块中的成本和持续时间的标识符和值。我尝试使用^^运算符,但似乎对重复的内部值没有用

0 个答案:

没有答案