Skip to content
Prev Previous commit
Next Next commit
Track quantifier value locations
  • Loading branch information
milseman committed Dec 13, 2021
commit 46f0d9b21d3b8aee4de086b4f87acbe424580022
14 changes: 7 additions & 7 deletions Sources/_MatchingEngine/Regex/AST/Quantification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ extension AST.Quantification.Amount: _ASTPrintable {
case .zeroOrMore: return "*"
case .oneOrMore: return "+"
case .zeroOrOne: return "?"
case let .exactly(n): return "{\(n)}"
case let .nOrMore(n): return "{\(n),}"
case let .upToN(n): return "{,\(n)}"
case let .exactly(n): return "{\(n.value)}"
case let .nOrMore(n): return "{\(n.value),}"
case let .upToN(n): return "{,\(n.value)}"
case let .range(r):
return "{\(r.lowerBound),\(r.upperBound)}"
}
Expand All @@ -57,11 +57,11 @@ extension AST.Quantification.Amount: _ASTPrintable {
case .zeroOrMore: return "zeroOrMore"
case .oneOrMore: return "oneOrMore"
case .zeroOrOne: return "zeroOrOne"
case let .exactly(n): return "exactly<\(n)>"
case let .nOrMore(n): return "nOrMore<\(n)>"
case let .upToN(n): return "uptoN<\(n)>"
case let .exactly(n): return "exactly<\(n.value)>"
case let .nOrMore(n): return "nOrMore<\(n.value)>"
case let .upToN(n): return "uptoN<\(n.value)>"
case let .range(r):
return ".range<\(r.lowerBound)...\(r.upperBound)>"
return ".range<\(r.lowerBound.value)...\(r.upperBound.value)>"
}
}
}
Expand Down
34 changes: 16 additions & 18 deletions Sources/_MatchingEngine/Regex/Parse/LexicalAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ extension Source {
try recordLoc { src in
// TODO: lex positive numbers, more specifically...

let lowerOpt = try src.lexNumber()?.value
let lowerOpt = try src.lexNumber()

// ',' or '...' or '..<' or nothing
let closedRange: Bool?
Expand All @@ -217,22 +217,25 @@ extension Source {
} else {
closedRange = nil
}
let upperOpt = try! src.lexNumber()?.value
// FIXME: wait, why `try!` ?
let upperOpt: Loc<Int>?
if let u = try! src.lexNumber() {
upperOpt = (closedRange == true) ? u : Loc(u.value-1, u.location)
} else {
upperOpt = nil
}

switch (lowerOpt, closedRange, upperOpt) {
case let (l?, nil, nil):
// FIXME: source location tracking
return .exactly(.init(faking: l))
return .exactly(l)
case let (l?, true, nil):
return .nOrMore(l)
case let (nil, _, u?):
return .upToN(u)
case let (l?, _, u?):
// FIXME: source location tracking
return .nOrMore(.init(faking: l))
case let (nil, closed?, u?):
// FIXME: source location tracking
return .upToN(.init(faking: closed ? u : u-1))
case let (l?, closed?, u?):
// FIXME: source location tracking
return .range(
.init(faking: l) ... .init(faking: closed ? u : u-1))
return .range(l ... u)

case let (nil, nil, u) where u != nil:
fatalError("Not possible")
default:
Expand Down Expand Up @@ -329,12 +332,7 @@ extension Source {
mutating func lexNonSemanticWhitespace() throws -> AST.Trivia? {
guard syntax.ignoreWhitespace else { return nil }
let trivia: Loc<String>? = try recordLoc { src in
var didSomething = false
while src.tryEat(" ") {
didSomething = true
}
// FIXME: record the whitespace...
return didSomething ? "" : nil
src.tryEatPrefix { $0 == " " }?.string
}
guard let trivia = trivia else { return nil }
return AST.Trivia(trivia)
Expand Down
3 changes: 3 additions & 0 deletions Sources/_MatchingEngine/Utility/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ extension Array /* for enumerated */ {
}
}

extension Substring {
var string: String { String(self) }
}
5 changes: 5 additions & 0 deletions Tests/RegexTests/DiagnosticTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,10 @@ extension RegexTests {
" -----^ ",
"------------------^"
])

// TODO: Find out best way to test quantifier values

// TODO: Find out way to render value-members of AST, not
// just children
}
}
4 changes: 2 additions & 2 deletions Tests/RegexTests/ParseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func parseTest(
else {
XCTFail("""

Expected: \(expecting)
Found: \(ast)
Expected: \(expecting._dump())
Found: \(ast._dump())
""")
return
}
Expand Down