Skip to content

Source plumbing, source rendering #66

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 4 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/_MatchingEngine/Regex/AST/AST.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ private struct ASTStorage {

extension AST {
// :-(
//
// Existential-based programming is highly prone to silent
// errors, but it does enable us to avoid having to switch
// over `self` _everywhere_ we want to do anything.
var _associatedValue: _ASTNode {
switch self {
case let .alternation(v): return v
Expand Down
24 changes: 15 additions & 9 deletions Sources/_MatchingEngine/Regex/AST/ASTBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ AST.

*/

// TODO: Sink this file into _StringProcessing and make it all
// internal. For now, this lets us incrementally add source
// ranges...

public let _fakeLoc = "".startIndex
public let _fakeRange = _fakeLoc ..< _fakeLoc
public func _fake<T: Hashable>(_ t: T) -> AST.Loc<T> {
Expand Down Expand Up @@ -138,21 +142,23 @@ public func quantRange(
}

public func charClass(
_ members: CustomCharacterClass.Member...,
_ members: AST.CustomCharacterClass.Member...,
inverted: Bool = false
) -> AST {
let cc = CustomCharacterClass(
inverted ? .inverted : .normal, members, _fakeRange
)
let cc = CustomCC(
_fake(inverted ? .inverted : .normal),
members,
_fakeRange)
return .customCharacterClass(cc)
}
public func charClass(
_ members: CustomCharacterClass.Member...,
_ members: AST.CustomCharacterClass.Member...,
inverted: Bool = false
) -> CustomCharacterClass.Member {
let cc = CustomCharacterClass(
inverted ? .inverted : .normal, members, _fakeRange
)
) -> AST.CustomCharacterClass.Member {
let cc = CustomCC(
_fake(inverted ? .inverted : .normal),
members,
_fakeRange)
return .custom(cc)
}
public func posixSet(
Expand Down
98 changes: 83 additions & 15 deletions Sources/_MatchingEngine/Regex/AST/ASTProtocols.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
/*

Common protocols for AST nodes and values. These allow us
to do more capabilities-based programming, currently
implemented on top of existentials.

*/



// MARK: - AST parent/child

protocol _ASTNode: _ASTPrintable {
var sourceRange: SourceRange { get }
}
extension _ASTNode {
var startLoc: SourceLoc { sourceRange.lowerBound }
var endLoc: SourceLoc { sourceRange.upperBound }
}

protocol _ASTParent: _ASTNode {
var children: [AST] { get }
}

extension AST.Concatenation: _ASTParent {}
extension AST.Alternation: _ASTParent {}

extension AST.Group: _ASTParent {
var children: [AST] { [child] }
}
extension AST.Quantification: _ASTParent {
var children: [AST] { [child] }
}


// MARK: - Printing

/// AST entities can be pretty-printed or dumped
Expand Down Expand Up @@ -53,23 +88,56 @@ extension AST: _ASTPrintable {
}
}

// MARK: - AST parent/child
// MARK: - Rendering

protocol _ASTNode: _ASTPrintable {
var sourceRange: SourceRange { get }
}
// Useful for testing, debugging, etc.
//
// TODO: Prettier rendering, probably inverted
extension AST {

protocol _ASTParent: _ASTNode {
var children: [AST] { get }
}
func _postOrder() -> Array<AST> {
var nodes = Array<AST>()
_postOrder(into: &nodes)
return nodes
}
func _postOrder(into array: inout Array<AST>) {
children?.forEach { $0._postOrder(into: &array) }
array.append(self)
}

extension AST.Concatenation: _ASTParent {}
extension AST.Alternation: _ASTParent {}
// We render from top-to-bottom, coalescing siblings
public func _render(in input: String) -> [String] {
let base = String(repeating: " ", count: input.count)
var lines = [base]

extension AST.Group: _ASTParent {
var children: [AST] { [child] }
}
extension AST.Quantification: _ASTParent {
var children: [AST] { [child] }
}
let nodes = _postOrder().filter(\.sourceRange.isReal)

nodes.forEach { node in
let sr = node.sourceRange
let count = input[sr].count
for idx in lines.indices {
if lines[idx][sr].all(\.isWhitespace) {
node._renderRange(count: count, into: &lines[idx])
return
}
}
var nextLine = base
node._renderRange(count: count, into: &nextLine)
lines.append(nextLine)
}

return lines.first!.all(\.isWhitespace) ? [] : lines
}

// Produce a textually "rendered" rane
//
// NOTE: `input` must be the string from which a
// source range was derived.
func _renderRange(
count: Int, into output: inout String
) {
guard count > 0 else { return }
let repl = String(repeating: "-", count: count-1) + "^"
output.replaceSubrange(sourceRange, with: repl)
}
}
2 changes: 1 addition & 1 deletion Sources/_MatchingEngine/Regex/AST/Atom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ extension Atom {

extension Atom {
var sourceRange: SourceRange {
// TODO: Does this mean we need to make Atom a struct?
// FIXME: source location tracking
_fakeRange
}
}
Expand Down
95 changes: 50 additions & 45 deletions Sources/_MatchingEngine/Regex/AST/CustomCharClass.swift
Original file line number Diff line number Diff line change
@@ -1,56 +1,61 @@

// TODO: source ranges? ASTValue? etc?
public struct CustomCharacterClass: Hashable {
public var start: Start
public var members: [Member]

public let sourceRange: SourceRange


public init(
_ start: Start,
_ members: [Member],
_ sr: SourceRange
) {
self.start = start
self.members = members
self.sourceRange = sr
}

public enum Member: Hashable {
/// A nested custom character class `[[ab][cd]]`
case custom(CustomCharacterClass)

/// A character range `a-z`
case range(Atom, Atom)

/// A single character or escape
case atom(Atom)

/// A binary operator applied to sets of members `abc&&def`
case setOperation([Member], SetOp, [Member])
}
public enum SetOp: String, Hashable {
case subtraction = "--"
case intersection = "&&"
case symmetricDifference = "~~"
}
public enum Start: Hashable {
/// `[`
case normal

/// `[^`
case inverted
extension AST {
public struct CustomCharacterClass: Hashable {
public var start: Loc<Start>
public var members: [Member]

public let sourceRange: SourceRange

public init(
_ start: Loc<Start>,
_ members: [Member],
_ sr: SourceRange
) {
self.start = start
self.members = members
self.sourceRange = sr
}

// FIXME: track source ranges
public enum Member: Hashable {
/// A nested custom character class `[[ab][cd]]`
case custom(CustomCharacterClass)

/// A character range `a-z`
case range(Atom, Atom)

/// A single character or escape
case atom(Atom)

/// A binary operator applied to sets of members `abc&&def`
case setOperation([Member], Loc<SetOp>, [Member])
}
public enum SetOp: String, Hashable {
case subtraction = "--"
case intersection = "&&"
case symmetricDifference = "~~"
}
public enum Start: Hashable {
/// `[`
case normal

/// `[^`
case inverted
}
}
}

extension CustomCharacterClass {
public var isInverted: Bool { start == .inverted }
/// `AST.CustomCharacterClass.Start` is a mouthful
internal typealias CustomCC = AST.CustomCharacterClass

extension CustomCC {
public var isInverted: Bool { start.value == .inverted }
}

extension CustomCharacterClass: _ASTNode {
extension CustomCC: _ASTNode {
public var _dumpBase: String {
// FIXME: print out members...
"customCharacterClass"
}
}

11 changes: 8 additions & 3 deletions Sources/_MatchingEngine/Regex/Parse/LexicalAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,16 @@ extension Source {

switch (lowerOpt, closedRange, upperOpt) {
case let (l?, nil, nil):
// FIXME: source location tracking
return .exactly(_fake(l))
case let (l?, true, nil):
// FIXME: source location tracking
return .nOrMore(_fake(l))
case let (nil, closed?, u?):
// FIXME: source location tracking
return .upToN(_fake(closed ? u : u-1))
case let (l?, closed?, u?):
// FIXME: source location tracking
return .range(
_fake(l) ... _fake(closed ? u : u-1))
case let (nil, nil, u) where u != nil:
Expand Down Expand Up @@ -327,6 +331,7 @@ extension Source {
while src.tryEat(" ") {
didSomething = true
}
// FIXME: source location tracking
return didSomething ? AST.Trivia(_fakeRange) : nil
}
}
Expand Down Expand Up @@ -390,7 +395,7 @@ extension Source {
}

mutating func lexCustomCCStart(
) throws -> Value<CustomCharacterClass.Start>? {
) throws -> Value<CustomCC.Start>? {
try recordLoc { src in
// POSIX named sets are atoms.
guard !src.starts(with: "[:") else { return nil }
Expand All @@ -406,7 +411,7 @@ extension Source {
///
/// CustomCCBinOp -> '--' | '~~' | '&&'
///
mutating func lexCustomCCBinOp() throws -> Value<CustomCharacterClass.SetOp>? {
mutating func lexCustomCCBinOp() throws -> Value<CustomCC.SetOp>? {
try recordLoc { src in
// TODO: Perhaps a syntax options check (!PCRE)
// TODO: Better AST types here
Expand All @@ -417,7 +422,7 @@ extension Source {
}

// Check to see if we can lex a binary operator.
func peekCCBinOp() -> CustomCharacterClass.SetOp? {
func peekCCBinOp() -> CustomCC.SetOp? {
if starts(with: "--") { return .subtraction }
if starts(with: "~~") { return .symmetricDifference }
if starts(with: "&&") { return .intersection }
Expand Down
Loading