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