From fc8460ea29cf725b8fd7e5c336af5a275898a49b Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 7 Mar 2022 13:45:59 +0100 Subject: [PATCH] Cleanup, Fix Parser Tests --- src/main/scala/hb/dhbw/Parser.scala | 18 ++++++++++++------ src/test/scala/ParserTest.scala | 8 ++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/scala/hb/dhbw/Parser.scala b/src/main/scala/hb/dhbw/Parser.scala index 97ca2a7..7de0c66 100644 --- a/src/main/scala/hb/dhbw/Parser.scala +++ b/src/main/scala/hb/dhbw/Parser.scala @@ -8,7 +8,7 @@ final case class ParserMethod(retType: Option[NType], name: String, params: List final case class NType(name: String, params: List[NType]) object Parser { - val keywords = Set("class", "new", "extends") + val keywords = Set("class", "new", "extends", "return", "this") def kw[_: P](s: String) = s ~~ !(letter | digit | "_" | "'") def letter[_: P] = P( lowercase | uppercase ) @@ -17,12 +17,14 @@ object Parser { def digit[_: P] = P( CharIn("0-9") ) def number[_: P]: P[Int] = P( CharIn("0-9").repX(1).!.map(_.toInt) ) def ident[_: P]: P[String] = - P( (letter | "_") ~~ (letter | digit | "_" | "'").repX ).!.filter(!keywords(_)) + P( (letter) ~~ (letter | digit).repX ).!.filter(!keywords(_)) def fieldVar[_: P]: P[Expr] = P( ".".! ~ ident ).map(ite => FieldVar(null, ite._2) ) def prefixMethodCall[_: P]: P[Expr] = P( "." ~ methodCall) - def methodCall[_: P]: P[MethodCall] =P( ident ~ paramList ).map(ite => MethodCall(null, ite._1, ite._2) ) - def paramList[_: P] : P[List[Expr]]= P("(".! ~ (expr ~ (",".! ~ expr).rep.map(_.toList.map{_._2})).? ~ ")".! ).map(ite => ite._2.map(params => params._1 :: params._2).getOrElse(List.empty)) + def methodCall[_: P]: P[MethodCall] = P( ident ~ paramList ).map(ite => MethodCall(null, ite._1, ite._2) ) + def paramList[_: P] : P[List[Expr]] = + P("(".! ~ (expr ~ (",".! ~ expr).rep.map(_.toList.map{_._2})).? ~ ")".! ) + .map(ite => ite._2.map(params => params._1 :: params._2).getOrElse(List.empty)) def variable[_: P]: P[Expr] = P(ident).map(LocalVar) def expr[_: P]: P[Expr] = P( (variable | constructor)~ (prefixMethodCall | fieldVar).rep.map(_.toList) ) .map(ite => ite._2.foldLeft(ite._1) { (e1 : Expr, e2 : Expr) => @@ -38,10 +40,14 @@ object Parser { .map(ite => ParserClass(ite._1, ite._2.getOrElse(List()),ite._3, ite._4.toList, ite._5.toList)) def field[_: P]: P[(NType, String)] = P(typeParser ~ ident ~ ";") def parameterDef[_ : P]: P[(Option[NType], String)] = P((typeParser.? ~ ident) | ident.map((None, _))) - def method[_: P]: P[ParserMethod] = P(parameterDef ~ (("("~")").map(it => List()) | ("(" ~ parameterDef ~ ("," ~ parameterDef).rep(0) ~ ")").map(ite => (ite._1, ite._2) +: ite._3.toList)) + def method[_: P]: P[ParserMethod] = + P(parameterDef ~ (("("~")").map(it => List()) | ("(" ~ parameterDef ~ ("," ~ parameterDef).rep(0) ~ ")") + .map(ite => (ite._1, ite._2) +: ite._3.toList)) ~ "{" ~ kw("return") ~ expr ~ ";" ~ "}") .map(ite => ParserMethod(ite._1, ite._2, ite._3, ite._4)) - def genericParamList[_: P]: P[List[(NType,NType)]] = P("<" ~ (typeParser ~ kw("extends") ~ typeParser) ~ ("," ~ (typeParser ~ kw("extends") ~ typeParser)).rep(0) ~ ">").map(ite => List((ite._1, ite._2)) ++ ite._3.map(ite3 => (ite3._1, ite3._2))) + def genericParamList[_: P]: P[List[(NType,NType)]] = + P("<" ~ (typeParser ~ kw("extends") ~ typeParser) ~ ("," ~ (typeParser ~ kw("extends") ~ typeParser)).rep(0) ~ ">") + .map(ite => List((ite._1, ite._2)) ++ ite._3.map(ite3 => (ite3._1, ite3._2))) def typeParser[_: P]: P[NType] = P(ident ~ ("<" ~ typeParser ~ ("," ~ typeParser).rep(0) ~ ">").?) .map(ite => NType(ite._1, ite._2.map(ite => List(ite._1) ++ ite._2).getOrElse(List()))) diff --git a/src/test/scala/ParserTest.scala b/src/test/scala/ParserTest.scala index a49372e..9895a9e 100644 --- a/src/test/scala/ParserTest.scala +++ b/src/test/scala/ParserTest.scala @@ -31,12 +31,12 @@ class ParserTest extends FunSuite { } test("Parser.classDefinition"){ - println(fastparse.parse("class Test{\n m(a,b){return a;}}", hb.dhbw.Parser.classDefinition(_))) - assert(fastparse.parse("class Test{m(a,b){return a;}}", hb.dhbw.Parser.classDefinition(_)).isSuccess) + println(fastparse.parse("class Test extends Object{\n m(a,b){return a;}}", hb.dhbw.Parser.classDefinition(_))) + assert(fastparse.parse("class Test extends Object {m(a,b){return a;}}", hb.dhbw.Parser.classDefinition(_)).isSuccess) } test("Parser.classDefinition.generics"){ - println(fastparse.parse("class Test{\n m(a,b){return a;}}", hb.dhbw.Parser.classDefinition(_))) - assert(fastparse.parse("class Test{m(a,b){return a;}}", hb.dhbw.Parser.classDefinition(_)).isSuccess) + println(fastparse.parse("class Test extends Object {\n m(a,b){return a;}}", hb.dhbw.Parser.classDefinition(_))) + assert(fastparse.parse("class Test extends Object {m(a,b){return a;}}", hb.dhbw.Parser.classDefinition(_)).isSuccess) } test("Parser.genericParamList"){