Skip to content

Commit

Permalink
Move the parser combinators to a separate library
Browse files Browse the repository at this point in the history
  • Loading branch information
kyouko-taiga committed Jul 25, 2022
1 parent 0ba19b4 commit 9871271
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 510 deletions.
69 changes: 38 additions & 31 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
{
"object": {
"pins": [
{
"package": "LLVM",
"repositoryURL": "https://github.com/kyouko-taiga/LLVMSwift.git",
"state": {
"branch": "master",
"revision": "58d87e0def9be831da9f9d3ad96bdccfa59982fa",
"version": null
}
},
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser.git",
"state": {
"branch": null,
"revision": "6b2aa2748a7881eebb9f84fb10c01293e15b52ca",
"version": "0.5.0"
}
},
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections.git",
"state": {
"branch": null,
"revision": "48254824bb4248676bf7ce56014ff57b142b77eb",
"version": "1.0.2"
}
"pins" : [
{
"identity" : "durian",
"kind" : "remoteSourceControl",
"location" : "https://github.com/val-lang/Durian.git",
"state" : {
"revision" : "63da2be9ba8b92e6a1608ad1a9a609241b1f55cb",
"version" : "1.0.0"
}
]
},
"version": 1
},
{
"identity" : "llvmswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/kyouko-taiga/LLVMSwift.git",
"state" : {
"branch" : "master",
"revision" : "58d87e0def9be831da9f9d3ad96bdccfa59982fa"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "6b2aa2748a7881eebb9f84fb10c01293e15b52ca",
"version" : "0.5.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "48254824bb4248676bf7ce56014ff57b142b77eb",
"version" : "1.0.2"
}
}
],
"version" : 2
}
10 changes: 6 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ let package = Package(
.package(
url: "https://github.com/kyouko-taiga/LLVMSwift.git",
branch: "master"),
.package(
url: "https://github.com/val-lang/Durian.git",
from: "1.0.0"),
],

targets: [
// The compiler's executable target.
.executableTarget(
name: "CLI",
dependencies: [
"Compiler", "Library",
"Compiler",
"Library",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]),

Expand All @@ -38,15 +42,13 @@ let package = Package(
name: "Compiler",
dependencies: [
"Utils",
"ParserCombinators",
.product(name: "Collections", package: "swift-collections"),
.product(name: "LLVM", package: "LLVMSwift"),
.product(name: "Durian", package: "Durian"),
]),

.target(name: "Utils"),

.target(name: "ParserCombinators"),

// Test targets.
.testTarget(
name: "ValTests",
Expand Down
72 changes: 36 additions & 36 deletions Sources/Compiler/Parse/Parser.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ParserCombinators
import Durian
import Utils

/// # Notes:
Expand Down Expand Up @@ -64,12 +64,12 @@ public enum Parser {

// MARK: Declarations

private static func anyDecl<Base: ParserCombinator>(
private static func anyDecl<Base: Combinator>(
_ base: Base
) -> AnyParserCombinator<ParserContext, AnyDeclID>
) -> AnyCombinator<ParserContext, AnyDeclID>
where Base.Context == ParserContext, Base.Element: DeclID
{
AnyParserCombinator(parse: { (context) in
AnyCombinator(parse: { (context) in
try base.parse(&context).map(AnyDeclID.init(_:))
})
}
Expand Down Expand Up @@ -109,8 +109,8 @@ public enum Parser {
})
)

static let namespaceDecl: RecursiveCombinator<ParserContext, NodeID<NamespaceDecl>> = (
RecursiveCombinator(_namespaceDecl.parse(_:))
static let namespaceDecl: Recursive<ParserContext, NodeID<NamespaceDecl>> = (
Recursive(_namespaceDecl.parse(_:))
)

private static let _namespaceDecl = (
Expand Down Expand Up @@ -175,8 +175,8 @@ public enum Parser {
})
)

static let productTypeDecl: RecursiveCombinator<ParserContext, NodeID<ProductTypeDecl>> = (
RecursiveCombinator(_productTypeDecl.parse(_:))
static let productTypeDecl: Recursive<ParserContext, NodeID<ProductTypeDecl>> = (
Recursive(_productTypeDecl.parse(_:))
)

private static let _productTypeDecl = (
Expand Down Expand Up @@ -812,7 +812,7 @@ public enum Parser {

/// Applies `decl` after having parsed its modifiers. If `decl` fails without committing,
/// returns a non-committing failure if no modifier was parsed.
private static func declModifiers<Decl: ParserCombinator>(
private static func declModifiers<Decl: Combinator>(
and decl: Decl
) throws -> Apply<
ParserContext, (
Expand Down Expand Up @@ -907,18 +907,18 @@ public enum Parser {

// MARK: Value expressions

private static func anyExpr<Base: ParserCombinator>(
private static func anyExpr<Base: Combinator>(
_ base: Base
) -> AnyParserCombinator<ParserContext, AnyExprID>
) -> AnyCombinator<ParserContext, AnyExprID>
where Base.Context == ParserContext, Base.Element: ExprID
{
AnyParserCombinator(parse: { (context) in
AnyCombinator(parse: { (context) in
try base.parse(&context).map(AnyExprID.init(_:))
})
}

static let expr: RecursiveCombinator<ParserContext, AnyExprID> = (
RecursiveCombinator(infixExpr.parse(_:))
static let expr: Recursive<ParserContext, AnyExprID> = (
Recursive(infixExpr.parse(_:))
)

static let infixExpr = (
Expand Down Expand Up @@ -1410,8 +1410,8 @@ public enum Parser {
.map({ (context, id) -> MatchCase.Body in .block(id) })
)

static let conditionalExpr: RecursiveCombinator<ParserContext, NodeID<CondExpr>> = (
RecursiveCombinator(_conditionalExpr.parse(_:))
static let conditionalExpr: Recursive<ParserContext, NodeID<CondExpr>> = (
Recursive(_conditionalExpr.parse(_:))
)

private static let _conditionalExpr = (
Expand Down Expand Up @@ -1517,18 +1517,18 @@ public enum Parser {

// MARK: Patterns

private static func anyPattern<Base: ParserCombinator>(
private static func anyPattern<Base: Combinator>(
_ base: Base
) -> AnyParserCombinator<ParserContext, AnyPatternID>
) -> AnyCombinator<ParserContext, AnyPatternID>
where Base.Context == ParserContext, Base.Element: PatternID
{
AnyParserCombinator(parse: { (context) in
AnyCombinator(parse: { (context) in
try base.parse(&context).map(AnyPatternID.init(_:))
})
}

static let pattern: RecursiveCombinator<ParserContext, AnyPatternID> = (
RecursiveCombinator(_pattern.parse(_:))
static let pattern: Recursive<ParserContext, AnyPatternID> = (
Recursive(_pattern.parse(_:))
)

private static let _pattern = (
Expand Down Expand Up @@ -1649,18 +1649,18 @@ public enum Parser {

// MARK: Statements

private static func anyStmt<Base: ParserCombinator>(
private static func anyStmt<Base: Combinator>(
_ base: Base
) -> AnyParserCombinator<ParserContext, AnyStmtID>
) -> AnyCombinator<ParserContext, AnyStmtID>
where Base.Context == ParserContext, Base.Element: StmtID
{
AnyParserCombinator(parse: { (context) in
AnyCombinator(parse: { (context) in
try base.parse(&context).map(AnyStmtID.init(_:))
})
}

static let stmt: RecursiveCombinator<ParserContext, AnyStmtID> = (
RecursiveCombinator(_stmt.parse(_:))
static let stmt: Recursive<ParserContext, AnyStmtID> = (
Recursive(_stmt.parse(_:))
)

static let _stmt = (
Expand Down Expand Up @@ -1862,18 +1862,18 @@ public enum Parser {

// MARK: Type expressions

private static func anyTypeExpr<Base: ParserCombinator>(
private static func anyTypeExpr<Base: Combinator>(
_ base: Base
) -> AnyParserCombinator<ParserContext, AnyTypeExprID>
) -> AnyCombinator<ParserContext, AnyTypeExprID>
where Base.Context == ParserContext, Base.Element: TypeExprID
{
AnyParserCombinator(parse: { (context) in
AnyCombinator(parse: { (context) in
try base.parse(&context).map(AnyTypeExprID.init(_:))
})
}

static let typeExpr: RecursiveCombinator<ParserContext, AnyTypeExprID> = (
RecursiveCombinator(unionTypeExpr.parse(_:))
static let typeExpr: Recursive<ParserContext, AnyTypeExprID> = (
Recursive(unionTypeExpr.parse(_:))
)

// static let storedProjectionTypeExpr = ?
Expand Down Expand Up @@ -2388,7 +2388,7 @@ public enum Parser {
extension Parser {

/// A combinator that parses tokens with a specific kind.
struct TakeKind: ParserCombinator {
struct TakeKind: Combinator {

typealias Context = ParserContext

Expand All @@ -2404,7 +2404,7 @@ extension Parser {
}

/// A combinator that parses contextual keywords.
struct ContextualKeyword<T: RawRepresentable>: ParserCombinator where T.RawValue == String {
struct ContextualKeyword<T: RawRepresentable>: Combinator where T.RawValue == String {

typealias Context = ParserContext

Expand Down Expand Up @@ -2449,7 +2449,7 @@ extension Parser {

/// Creates a combinator that sets the specified flags before applying `base`, and restores them
/// to their previous state afterward.
static func settingFlags<Base: ParserCombinator>(
static func settingFlags<Base: Combinator>(
_ newFlags: ParserContext.Flags,
apply base: Base
) -> Apply<ParserContext, Base.Element>
Expand All @@ -2464,7 +2464,7 @@ extension Parser {
}

/// Creates a combinator that applies `base` only if its input is not preceeded by whitespaces.
static func withoutLeadingWhitespace<Base: ParserCombinator>(
static func withoutLeadingWhitespace<Base: Combinator>(
_ base: Base
) -> Apply<ParserContext, Base.Element>
where Base.Context == ParserContext
Expand All @@ -2473,7 +2473,7 @@ extension Parser {
}

/// Creates a combinator that applies `base` only if its input is not preceeded by newlines.
static func onSameLine<Base: ParserCombinator>(
static func onSameLine<Base: Combinator>(
_ base: Base
) -> Apply<ParserContext, Base.Element>
where Base.Context == ParserContext
Expand Down
2 changes: 1 addition & 1 deletion Sources/Compiler/Parse/ParserContext.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import DequeModule
import ParserCombinators
import Durian

/// A type representing the context of the parser.
struct ParserContext {
Expand Down
22 changes: 0 additions & 22 deletions Sources/ParserCombinators/AnyParserCombinator.swift

This file was deleted.

16 changes: 0 additions & 16 deletions Sources/ParserCombinators/Apply.swift

This file was deleted.

Loading

0 comments on commit 9871271

Please sign in to comment.