Scala将表达式列表和另一个表达式作为一个列表传递给构造函数

时间:2017-05-24 21:38:21

标签: scala list parser-combinators

我目前正在撰写Parser JavaTokenParsersScala Grammar list = "[" [ { expr "," } expr ] "]" class ExpParser extends JavaTokenParsers { def expr: Parser[Expression] = int | list | bool | id [...] private def list: Parser[Liste] = "[" ~> repsep(expr, ",") ~ expr <~ "]" ^^ { case el ~ e => Liste(List(el, e)) } // error [..] } object ParseProgram extends ExpParser { def parse(s: String): ParseResult[Expression] = { parseAll(expr, s) } }

}
sealed trait Expression
[...]
case class Liste(l: List[Expression]) extends Expression
[...]

我的解析器类

List

我的案例类:

Liste Constructor

我尝试创建一个新的list并将其传递给Expressions,但我收到以下错误消息:

  

类型不匹配,预期:列表[表达式],实际:列表[对象]

那么我怎样才能将repsep(expr, ",")的{​​{1}} expression传递给~ expr以及list Liste Constructor bool removeMultiples( int multiple ) { bool success; auto it = std::remove_if(v.begin(), v.end(), [multiple](int i){return i%multiple == 0;}); if ( ( success = it != v.end() ) ) v.erase( it, v.end() ); return success; } react-native run-android * What went wrong: Execution failed for task ':app:preDexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1 package com.wosyldelivery; import android.app.Application; import android.util.Log; import com.facebook.react.ReactApplication; import com.microsoft.codepush.react.CodePush; import com.oblador.vectoricons.VectorIconsPackage; import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.react.shell.MainReactPackage; import java.util.Arrays; import java.util.List; public class MainApplication extends Application implements ReactApplication { private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { @Override public boolean getUseDeveloperSupport() { return BuildConfig.DEBUG; } @Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new VectorIconsPackage() ); } @Override protected String getJSBundleFile() { return CodePush.getJSBundleFile(); } }; @Override public ReactNativeHost getReactNativeHost() { return mReactNativeHost; } }

1 个答案:

答案 0 :(得分:2)

repsep(expr, ",")为您提供Parser[List[Expression]]并且还不够吗?

private def list: Parser[Liste] = "[" ~> repsep(expr, ",") <~ "]"  ^^ {
  case el => Liste(el)
}

你的原始解析意味着有一个像[True,FalseTrue]这样的列表,这就是你想要的吗?