feat: file parser bridges
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package wacc
|
||||
|
||||
import java.io.File
|
||||
import parsley.Parsley
|
||||
import parsley.generic.ErrorBridge
|
||||
import parsley.ap._
|
||||
@@ -22,26 +23,39 @@ object ast {
|
||||
/* ============================ ATOMIC EXPRESSIONS ============================ */
|
||||
|
||||
case class IntLiter(v: Int)(val pos: Position) extends Expr6
|
||||
object IntLiter extends ParserBridgePos1[Int, IntLiter]
|
||||
object IntLiter extends ParserBridgePos1Atom[Int, IntLiter]
|
||||
case class BoolLiter(v: Boolean)(val pos: Position) extends Expr6
|
||||
object BoolLiter extends ParserBridgePos1[Boolean, BoolLiter]
|
||||
object BoolLiter extends ParserBridgePos1Atom[Boolean, BoolLiter]
|
||||
case class CharLiter(v: Char)(val pos: Position) extends Expr6
|
||||
object CharLiter extends ParserBridgePos1[Char, CharLiter]
|
||||
object CharLiter extends ParserBridgePos1Atom[Char, CharLiter]
|
||||
case class StrLiter(v: String)(val pos: Position) extends Expr6
|
||||
object StrLiter extends ParserBridgePos1[String, StrLiter]
|
||||
object StrLiter extends ParserBridgePos1Atom[String, StrLiter]
|
||||
case class PairLiter()(val pos: Position) extends Expr6
|
||||
object PairLiter extends ParserBridgePos0[PairLiter]
|
||||
case class Ident(v: String, var uid: Int = -1)(val pos: Position) extends Expr6 with LValue
|
||||
object Ident extends ParserBridgePos1[String, Ident] {
|
||||
object Ident extends ParserBridgePos1Atom[String, Ident] {
|
||||
def apply(v: String)(pos: Position): Ident = new Ident(v)(pos)
|
||||
}
|
||||
case class ArrayElem(name: Ident, indices: NonEmptyList[Expr])(val pos: Position)
|
||||
extends Expr6
|
||||
with LValue
|
||||
object ArrayElem extends ParserBridgePos1[NonEmptyList[Expr], Ident => ArrayElem] {
|
||||
def apply(a: NonEmptyList[Expr])(pos: Position): Ident => ArrayElem =
|
||||
name => ArrayElem(name, a)(pos)
|
||||
object ArrayElem extends ParserBridgePos2Chain[NonEmptyList[Expr], Ident, ArrayElem] {
|
||||
def apply(indices: NonEmptyList[Expr], name: Ident)(pos: Position): ArrayElem =
|
||||
new ArrayElem(name, indices)(pos)
|
||||
}
|
||||
// object ArrayElem extends ParserBridgePos1[NonEmptyList[Expr], (File => Ident) => ArrayElem] {
|
||||
// def apply(a: NonEmptyList[Expr])(pos: Position): (File => Ident) => ArrayElem =
|
||||
// name => ArrayElem(name(pos.file), a)(pos)
|
||||
// }
|
||||
// object ArrayElem extends ParserSingletonBridgePos[(File => NonEmptyList[Expr]) => (File => Ident) => File => ArrayElem] {
|
||||
// // def apply(indices: NonEmptyList[Expr]): (File => Ident) => File => ArrayElem =
|
||||
// // name => file => new ArrayElem(name(file), )
|
||||
// def apply(indices: Parsley[File => NonEmptyList[Expr]]): Parsley[(File => Ident) => File => ArrayElem] =
|
||||
// // error(ap1(pos.map(con),))
|
||||
|
||||
// override final def con(pos: (Int, Int)): (File => NonEmptyList[Expr]) => => C =
|
||||
// (a, b) => file => this.apply(a(file), b(file))(Position(pos._1, pos._2, file))
|
||||
// }
|
||||
case class Parens(expr: Expr)(val pos: Position) extends Expr6
|
||||
object Parens extends ParserBridgePos1[Expr, Parens]
|
||||
|
||||
@@ -119,8 +133,8 @@ object ast {
|
||||
case class ArrayType(elemType: Type, dimensions: Int)(val pos: Position)
|
||||
extends Type
|
||||
with PairElemType
|
||||
object ArrayType extends ParserBridgePos1[Int, Type => ArrayType] {
|
||||
def apply(a: Int)(pos: Position): Type => ArrayType = elemType => ArrayType(elemType, a)(pos)
|
||||
object ArrayType extends ParserBridgePos2Chain[Int, Type, ArrayType] {
|
||||
def apply(dimensions: Int, elemType: Type)(pos: Position): ArrayType = ArrayType(elemType, dimensions)(pos)
|
||||
}
|
||||
case class PairType(fst: PairElemType, snd: PairElemType)(val pos: Position) extends Type
|
||||
object PairType extends ParserBridgePos2[PairElemType, PairElemType, PairType]
|
||||
@@ -155,15 +169,12 @@ object ast {
|
||||
body: NonEmptyList[Stmt]
|
||||
)(val pos: Position)
|
||||
object FuncDecl
|
||||
extends ParserBridgePos2[
|
||||
List[Param],
|
||||
NonEmptyList[Stmt],
|
||||
((Type, Ident)) => FuncDecl
|
||||
extends ParserBridgePos2Chain[
|
||||
(List[Param], NonEmptyList[Stmt]),
|
||||
((Type, Ident)), FuncDecl
|
||||
] {
|
||||
def apply(params: List[Param], body: NonEmptyList[Stmt])(
|
||||
pos: Position
|
||||
): ((Type, Ident)) => FuncDecl =
|
||||
(returnType, name) => FuncDecl(returnType, name, params, body)(pos)
|
||||
def apply(paramsBody: (List[Param], NonEmptyList[Stmt]), retTyName: (Type, Ident))(pos: Position): FuncDecl =
|
||||
new FuncDecl(retTyName._1, retTyName._2, paramsBody._1, paramsBody._2)(pos)
|
||||
}
|
||||
|
||||
case class Param(paramType: Type, name: Ident)(val pos: Position)
|
||||
@@ -219,7 +230,7 @@ object ast {
|
||||
|
||||
/* ============================ PARSER BRIDGES ============================ */
|
||||
|
||||
case class Position(line: Int, column: Int)
|
||||
case class Position(line: Int, column: Int, file: File)
|
||||
|
||||
trait ParserSingletonBridgePos[+A] extends ErrorBridge {
|
||||
protected def con(pos: (Int, Int)): A
|
||||
@@ -227,38 +238,54 @@ object ast {
|
||||
final def <#(op: Parsley[?]): Parsley[A] = this from op
|
||||
}
|
||||
|
||||
trait ParserBridgePos0[+A] extends ParserSingletonBridgePos[A] {
|
||||
trait ParserBridgePos0[+A] extends ParserSingletonBridgePos[File => A] {
|
||||
def apply()(pos: Position): A
|
||||
|
||||
override final def con(pos: (Int, Int)): A =
|
||||
apply()(Position(pos._1, pos._2))
|
||||
override final def con(pos: (Int, Int)): File => A =
|
||||
file => apply()(Position(pos._1, pos._2, file))
|
||||
}
|
||||
|
||||
trait ParserBridgePos1[-A, +B] extends ParserSingletonBridgePos[A => B] {
|
||||
trait ParserBridgePos1Atom[-A, +B] extends ParserSingletonBridgePos[A => File => B] {
|
||||
def apply(a: A)(pos: Position): B
|
||||
def apply(a: Parsley[A]): Parsley[B] = error(ap1(pos.map(con), a))
|
||||
def apply(a: Parsley[A]): Parsley[File => B] = error(ap1(pos.map(con), a))
|
||||
|
||||
override final def con(pos: (Int, Int)): A => B =
|
||||
this.apply(_)(Position(pos._1, pos._2))
|
||||
override final def con(pos: (Int, Int)): A => File => B =
|
||||
a => file => this.apply(a)(Position(pos._1, pos._2, file))
|
||||
}
|
||||
|
||||
trait ParserBridgePos2[-A, -B, +C] extends ParserSingletonBridgePos[(A, B) => C] {
|
||||
trait ParserBridgePos1[-A, +B] extends ParserSingletonBridgePos[(File => A) => File => B] {
|
||||
def apply(a: A)(pos: Position): B
|
||||
def apply(a: Parsley[File => A]): Parsley[File => B] = error(ap1(pos.map(con), a))
|
||||
|
||||
override final def con(pos: (Int, Int)): (File => A) => File => B =
|
||||
a => file => this.apply(a(file))(Position(pos._1, pos._2, file))
|
||||
}
|
||||
|
||||
trait ParserBridgePos2Chain[-A, -B, +C] extends ParserSingletonBridgePos[(File => A) => (File => B) => File => C] {
|
||||
def apply(a: A, b: B)(pos: Position): C
|
||||
def apply(a: Parsley[A], b: => Parsley[B]): Parsley[C] = error(
|
||||
def apply(a: Parsley[File =>A]): Parsley[(File => B) => File => C] = error(ap1(pos.map(con), a))
|
||||
|
||||
override final def con(pos: (Int, Int)): (File => A) => (File => B) => File => C =
|
||||
a => b => file => this.apply(a(file), b(file))(Position(pos._1, pos._2, file))
|
||||
}
|
||||
|
||||
trait ParserBridgePos2[-A, -B, +C] extends ParserSingletonBridgePos[(File => A, File => B) => File => C] {
|
||||
def apply(a: A, b: B)(pos: Position): C
|
||||
def apply(a: Parsley[File => A], b: => Parsley[File => B]): Parsley[File => C] = error(
|
||||
ap2(pos.map(con), a, b)
|
||||
)
|
||||
|
||||
override final def con(pos: (Int, Int)): (A, B) => C =
|
||||
apply(_, _)(Position(pos._1, pos._2))
|
||||
override final def con(pos: (Int, Int)): (File => A, File => B) => File => C =
|
||||
(a, b) => file => this.apply(a(file), b(file))(Position(pos._1, pos._2, file))
|
||||
}
|
||||
|
||||
trait ParserBridgePos3[-A, -B, -C, +D] extends ParserSingletonBridgePos[(A, B, C) => D] {
|
||||
trait ParserBridgePos3[-A, -B, -C, +D] extends ParserSingletonBridgePos[(File => A, File => B, File => C) => File => D] {
|
||||
def apply(a: A, b: B, c: C)(pos: Position): D
|
||||
def apply(a: Parsley[A], b: => Parsley[B], c: => Parsley[C]): Parsley[D] = error(
|
||||
def apply(a: Parsley[File => A], b: => Parsley[File => B], c: => Parsley[File => C]): Parsley[File => D] = error(
|
||||
ap3(pos.map(con), a, b, c)
|
||||
)
|
||||
|
||||
override final def con(pos: (Int, Int)): (A, B, C) => D =
|
||||
apply(_, _, _)(Position(pos._1, pos._2))
|
||||
override final def con(pos: (Int, Int)): (File => A, File => B, File => C) => File => D =
|
||||
(a, b, c) => file => apply(a(file), b(file), c(file))(Position(pos._1, pos._2, file))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user