Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Performance improvement: single parser instance (#254)
- Loading branch information
Showing
49 changed files
with
362 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package aqua.parser | ||
|
||
import cats.data.{Validated, ValidatedNec} | ||
import aqua.parser.Ast | ||
import aqua.parser.ParserError | ||
import aqua.parser.LexerError | ||
import aqua.parser.expr.RootExpr | ||
import aqua.parser.head.HeadExpr | ||
import aqua.parser.lift.{FileSpan, LiftParser, Span} | ||
import cats.{Comonad, Eval, ~>} | ||
import cats.parse.LocationMap | ||
import cats.parse.Parser0 as P0 | ||
import cats.Id | ||
import aqua.parser.lift.LiftParser.LiftErrorOps | ||
|
||
|
||
object Parser { | ||
|
||
import Span.spanLiftParser | ||
lazy val spanParser = parserSchema[Span.F]() | ||
import LiftParser.Implicits.idLiftParser | ||
lazy val idParser = parserSchema[Id]() | ||
|
||
def parserSchema[S[_] : LiftParser : Comonad](): P0[ValidatedNec[ParserError[S], Ast[S]]] = | ||
(HeadExpr.ast[S].with1 ~ RootExpr.ast[S]()).map { case (head, bodyMaybe) => | ||
bodyMaybe.map(Ast(head, _)) | ||
} | ||
|
||
def parser[S[_] : LiftParser : Comonad](p: P0[ValidatedNec[ParserError[S], Ast[S]]])(source: String): ValidatedNec[ParserError[S], Ast[S]] = { | ||
p.parseAll(source) match { | ||
case Right(value) => value | ||
case Left(e) => Validated.invalidNec(LexerError(e.wrapErr)) | ||
} | ||
} | ||
|
||
def natParser[S[_] : LiftParser : Comonad, K[_] : Comonad]( | ||
p: P0[ValidatedNec[ParserError[S], Ast[S]]], | ||
nat: S ~> K | ||
)(source: String): ValidatedNec[ParserError[K], Ast[K]] = | ||
parser[S](p)(source).bimap( | ||
e => e.map(_.mapK(nat)), | ||
ast => Ast[K](ast.head.map(_.mapK(nat)), ast.tree.map(_.mapK(nat))) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
package aqua.parser | ||
|
||
import cats.parse.Parser | ||
import cats.~> | ||
|
||
trait ParserError[F[_]] | ||
trait ParserError[F[_]] { | ||
def mapK[K[_]](fk: F ~> K): ParserError[K] | ||
} | ||
|
||
case class LexerError[F[_]](err: F[Parser.Error]) extends ParserError[F] | ||
case class BlockIndentError[F[_]](indent: F[String], message: String) extends ParserError[F] | ||
case class FuncReturnError[F[_]](point: F[Unit], message: String) extends ParserError[F] | ||
case class LexerError[F[_]](err: F[Parser.Error]) extends ParserError[F] { | ||
def mapK[K[_]](fk: F ~> K): LexerError[K] = copy(fk(err)) | ||
} | ||
case class BlockIndentError[F[_]](indent: F[String], message: String) extends ParserError[F] { | ||
def mapK[K[_]](fk: F ~> K): BlockIndentError[K] = | ||
copy(fk(indent)) | ||
} | ||
case class FuncReturnError[F[_]](point: F[Unit], message: String) extends ParserError[F] { | ||
def mapK[K[_]](fk: F ~> K): FuncReturnError[K] = | ||
copy(fk(point)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.