-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
130 additions
and
52 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
71 changes: 63 additions & 8 deletions
71
src/main/scala/io/septimalmind/baboon/parser/BaboonParser.scala
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,31 +1,86 @@ | ||
package io.septimalmind.baboon.parser | ||
|
||
import distage.Id | ||
import fastparse.Parsed | ||
import io.septimalmind.baboon.parser.model.issues.BaboonIssue | ||
import io.septimalmind.baboon.parser.model.{FSPath, RawDomain} | ||
import izumi.fundamentals.collections.nonempty.NEList | ||
import io.septimalmind.baboon.parser.model.{ | ||
FSPath, | ||
RawDomain, | ||
RawInclude, | ||
RawTLDef | ||
} | ||
import izumi.fundamentals.collections.nonempty.{NEList, NEString} | ||
import izumi.functional.IzEither.* | ||
import izumi.fundamentals.platform.files.IzFiles | ||
|
||
import java.nio.file.Path | ||
|
||
trait BaboonParser { | ||
def parse( | ||
input: BaboonParser.Input | ||
): Either[NEList[BaboonIssue.ParserIssue], RawDomain] | ||
def parse(input: BaboonParser.Input): Either[NEList[BaboonIssue], RawDomain] | ||
} | ||
|
||
object BaboonParser { | ||
case class Input(path: FSPath, content: String) | ||
|
||
class BaboonParserImpl() extends BaboonParser { | ||
class BaboonParserImpl(inputs: Seq[Path] @Id("inputs")) extends BaboonParser { | ||
def parse( | ||
input: BaboonParser.Input | ||
): Either[NEList[BaboonIssue.ParserIssue], RawDomain] = { | ||
): Either[NEList[BaboonIssue], RawDomain] = { | ||
val context = ParserContext(input.path, input.content) | ||
|
||
fastparse.parse(context.content, context.defModel.model(_)) match { | ||
case Parsed.Success(value, _) => | ||
Right(value) | ||
for { | ||
included <- processIncludes(value.members.includes) | ||
} yield { | ||
value.copy( | ||
members = value.members.copy( | ||
includes = Seq.empty, | ||
defs = included ++ value.members.defs | ||
) | ||
) | ||
} | ||
|
||
case failure: Parsed.Failure => | ||
Left(NEList(BaboonIssue.ParserFailed(failure))) | ||
} | ||
} | ||
|
||
def processIncludes( | ||
includes: Seq[RawInclude] | ||
): Either[NEList[BaboonIssue], Seq[RawTLDef]] = { | ||
if (includes.nonEmpty) { | ||
includes.map { inc => | ||
val inclusion = inputs | ||
.map(_.resolve(inc.value).toFile) | ||
.find(f => f.exists() && f.isFile) | ||
inclusion match { | ||
case Some(incFile) => | ||
val content = IzFiles.readString(incFile) | ||
val context = ParserContext( | ||
FSPath.parse(NEString.unsafeFrom(incFile.getCanonicalPath)), | ||
content | ||
) | ||
fastparse.parse(context.content, context.defModel.content(_)) match { | ||
case Parsed.Success(value, _) => | ||
for { | ||
sub <- processIncludes(value.includes) | ||
} yield { | ||
sub ++ value.defs | ||
} | ||
case failure: Parsed.Failure => | ||
Left(NEList(BaboonIssue.ParserFailed(failure))) | ||
|
||
} | ||
|
||
case None => | ||
Left(NEList(BaboonIssue.IncludeNotFound(inc.value))) | ||
} | ||
}.biFlatten | ||
} else { | ||
Right(Seq.empty) | ||
} | ||
} | ||
} | ||
|
||
} |
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
4 changes: 1 addition & 3 deletions
4
src/main/scala/io/septimalmind/baboon/parser/model/RawDomain.scala
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,7 +1,5 @@ | ||
package io.septimalmind.baboon.parser.model | ||
|
||
|
||
|
||
case class RawDomain(header: RawHeader, | ||
version: RawVersion, | ||
members: Seq[RawTLDef]) | ||
members: RawContent) |
4 changes: 4 additions & 0 deletions
4
src/main/scala/io/septimalmind/baboon/parser/model/RawVersion.scala
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,3 +1,7 @@ | ||
package io.septimalmind.baboon.parser.model | ||
|
||
case class RawVersion(meta: RawNodeMeta, value: String) | ||
|
||
case class RawInclude(meta: RawNodeMeta, value: String) | ||
|
||
case class RawContent(includes: Seq[RawInclude], defs: Seq[RawTLDef]) |
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,16 @@ | ||
root data Maps { | ||
f1: map[i08, str] | ||
} | ||
|
||
enum T1_E1 { | ||
A | ||
C | ||
} | ||
|
||
root data T1_E1_RET { | ||
f: T1_E1 | ||
} | ||
|
||
data T1_D2 { | ||
f: str | ||
} |
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