所以我有一个CFG产品:
tipe : "double"
| "float"
| "int"
| "fn" "(" ((tipe ",")* tipe)? ")" ("->" tipe)?
因此,它基本上会创建类似fn(int)
或fn(float) -> int
或fn(int, double....) -> double
的语法
我正在尝试为其创建解析器,到目前为止,我有以下内容:
lazy val tipe : PackratParser[Type] =
"int" ^^^ Int() |
"float" ^^^ Float() |
"double" ^^^ Double() |
最后的生产是我的麻烦。案例类如下:
case class Fnctn(argTypes : Vector[Type], ret : Type)
因此它需要一个类型向量和一个类型本身。向量是指CFG中的((tipe ",")* tipe)?
和ret
至("->" tipe)?
。
在没有-> tipe
的情况下,应默认为Int()
。
因此,在我的解析器实现中,我添加了
("fn" ~> "(" ~> repsep(tipe, ",") <~ ")") ~ ("->" ~> tipe) ^^ {
case x ~ None => Fnctn(x, Int())
case x ~ t => Fnctn(x, t)
}
我认为,如果存在,它将放置正确的一个,否则,它将默认为Int()
。但这给了我一个错误,说found: None, required: Type
所以我尝试使用
... opt("->" ~> tipe)
case x ~ None => Fnctn(x, Int())
case x ~ Some(Int()) => Fnctn(x, Int())
case x ~ Some(Double()) => Fnctn(x, Double())
case x ~ Some(Float()) => Fnctn(x, float())
但是,这只是给我带来了库错误,无法读取的巨型巨型BC,我认为它不像我所做的那样。
我还有其他选择吗?允许的任何帮助