-
Notifications
You must be signed in to change notification settings - Fork 50
Some AST construction and DSL refactoring #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,20 +1,23 @@ | ||
public protocol ASTAction { | ||
// TODO: associated types, or just make them produce AST nodes? | ||
// TODO: This might be interesting in the future, but for now | ||
// we make trees | ||
|
||
// MARK: Group | ||
static func capture(_ a: AST, _ sr: SourceRange?) -> AST | ||
|
||
static func nonCapture(_ a: AST, _ sr: SourceRange?) -> AST | ||
|
||
// MARK: Quantification | ||
|
||
static func zeroOrMore( | ||
_ kind: Quantifier.Kind, _ a: AST, _ r: SourceRange? | ||
) -> AST | ||
static func oneOrMore( | ||
_ kind: Quantifier.Kind, _ a: AST, _ r: SourceRange? | ||
) -> AST | ||
static func zeroOrOne( | ||
_ kind: Quantifier.Kind, _ a: AST, _ r: SourceRange? | ||
) -> AST | ||
} | ||
//public protocol ASTAction { | ||
// // TODO: associated types, or just make them produce AST nodes? | ||
// | ||
// // MARK: Group | ||
// static func capture(_ a: AST, _ sr: SourceRange?) -> AST | ||
// | ||
// static func nonCapture(_ a: AST, _ sr: SourceRange?) -> AST | ||
// | ||
// // MARK: Quantification | ||
// | ||
// static func zeroOrMore( | ||
// _ kind: Quantifier.Kind, _ a: AST, _ r: SourceRange? | ||
// ) -> AST | ||
// static func oneOrMore( | ||
// _ kind: Quantifier.Kind, _ a: AST, _ r: SourceRange? | ||
// ) -> AST | ||
// static func zeroOrOne( | ||
// _ kind: Quantifier.Kind, _ a: AST, _ r: SourceRange? | ||
// ) -> AST | ||
//} |
This file contains hidden or 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,156 @@ | ||
/* | ||
|
||
These functions are temporary AST construction helpers. As | ||
the AST gets more and more source-location tracking, we'll | ||
want easier migration paths for our parser tests (which | ||
construct and compare location-less AST nodes) as well as the | ||
result builder DSL (which has a different notion of location). | ||
|
||
Without real namespaces and `using`, attempts at | ||
pseudo-namespaces tie the use site to being nested inside a | ||
type. So for now, these are global, but they will likely be | ||
namespaced in the future if/when clients are weaned off the | ||
AST. | ||
|
||
*/ | ||
|
||
public let _fakeLoc = "".startIndex | ||
public let _fakeRange = _fakeLoc ..< _fakeLoc | ||
|
||
public func alt(_ asts: [AST]) -> AST { | ||
.alternation(asts) | ||
} | ||
public func alt(_ asts: AST...) -> AST { | ||
alt(asts) | ||
} | ||
|
||
public func concat(_ asts: [AST]) -> AST { | ||
.concatenation(asts) | ||
} | ||
public func concat(_ asts: AST...) -> AST { | ||
concat(asts) | ||
} | ||
|
||
public func group( | ||
_ kind: Group.Kind, _ child: AST | ||
) -> AST { | ||
.group(Group(kind, _fakeRange), child) | ||
} | ||
public func capture( | ||
_ child: AST | ||
) -> AST { | ||
group(.capture, child) | ||
} | ||
public func nonCapture( | ||
_ child: AST | ||
) -> AST { | ||
group(.nonCapture, child) | ||
} | ||
public func namedCapture( | ||
_ name: String, | ||
_ child: AST | ||
) -> AST { | ||
group(.namedCapture(name), child) | ||
} | ||
public func nonCaptureReset( | ||
_ child: AST | ||
) -> AST { | ||
group(.nonCaptureReset, child) | ||
} | ||
public func atomicNonCapturing( | ||
_ child: AST | ||
) -> AST { | ||
group(.atomicNonCapturing, child) | ||
} | ||
public func lookahead(_ child: AST) -> AST { | ||
group(.lookahead, child) | ||
} | ||
public func lookbehind(_ child: AST) -> AST { | ||
group(.lookbehind, child) | ||
} | ||
public func negativeLookahead(_ child: AST) -> AST { | ||
group(.negativeLookahead, child) | ||
} | ||
public func negativeLookbehind(_ child: AST) -> AST { | ||
group(.negativeLookbehind, child) | ||
} | ||
|
||
public var any: AST { .atom(.any) } | ||
|
||
public func quant( | ||
_ amount: Quantifier.Amount, | ||
_ kind: Quantifier.Kind = .greedy, | ||
_ child: AST | ||
) -> AST { | ||
.quantification(Quantifier(amount, kind, _fakeRange), child) | ||
} | ||
public func zeroOrMore( | ||
_ kind: Quantifier.Kind = .greedy, | ||
_ child: AST | ||
) -> AST { | ||
quant(.zeroOrMore, kind, child) | ||
} | ||
public func zeroOrOne( | ||
_ kind: Quantifier.Kind = .greedy, | ||
_ child: AST | ||
) -> AST { | ||
quant(.zeroOrOne, kind, child) | ||
} | ||
public func oneOrMore( | ||
_ kind: Quantifier.Kind = .greedy, | ||
_ child: AST | ||
) -> AST { | ||
quant(.oneOrMore, kind, child) | ||
} | ||
public func exactly( | ||
_ kind: Quantifier.Kind = .greedy, | ||
_ i: Int, | ||
child: AST | ||
) -> AST { | ||
quant(.exactly(i), kind, child) | ||
} | ||
public func nOrMore( | ||
_ kind: Quantifier.Kind = .greedy, | ||
_ i: Int, | ||
child: AST | ||
) -> AST { | ||
quant(.nOrMore(i), kind, child) | ||
} | ||
public func upToN( | ||
_ kind: Quantifier.Kind = .greedy, | ||
_ i: Int, | ||
child: AST | ||
) -> AST { | ||
quant(.upToN(i), kind, child) | ||
} | ||
public func quantRange( | ||
_ kind: Quantifier.Kind = .greedy, | ||
_ r: ClosedRange<Int>, | ||
child: AST | ||
) -> AST { | ||
quant(.range(r), kind, child) | ||
} | ||
|
||
public func charClass( | ||
_ members: CustomCharacterClass.Member..., | ||
inverted: Bool = false | ||
) -> AST { | ||
let cc = CustomCharacterClass( | ||
inverted ? .inverted : .normal, members | ||
) | ||
return .customCharacterClass(cc) | ||
} | ||
public func charClass( | ||
_ members: CustomCharacterClass.Member..., | ||
inverted: Bool = false | ||
) -> CustomCharacterClass.Member { | ||
let cc = CustomCharacterClass( | ||
inverted ? .inverted : .normal, members | ||
) | ||
return .custom(cc) | ||
} | ||
public func posixSet( | ||
_ set: Unicode.POSIXCharacterSet, inverted: Bool = false | ||
) -> Atom { | ||
return .named(.init(inverted: inverted, set: set)) | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For namespacing, could you move these top-level symbols to an enum, e.g.
ASTBuilder
, as static methods?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe eventually. I'm inclined not to for now because otherwise you end up either with a bunch of
ASTBuilder.concat(ASTBuilder.atom....)
in unit tests, or every client just reinvents these. Since the contextual type requested is AST and not ASTBuilder, they wouldn't be seen for lookup unless everything was typed as a builder instead of an AST. These don't work well hosted on AST because names can collide with case names.Longer term, I don't think we'll be as enthusiastically creating AST nodes for every need. This is public in the "core" module right now (so visible to _StringProcessing but not its clients). Another option is to make these internal inside _StringProcessing and the test use case can use
@testable
anyways.Also, we'll see if AST even stays as an enum or in its current form. We might need to overhaul it more for options tracking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, these could also be static methods on
AST
itself though. I'm still concerned about polluting the global namespace.I think that this approach works better; we only build location-less ASTs in _StringProcessing module and its tests.