Skip to content

Better error printing #85

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 1 commit into from
Dec 17, 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
35 changes: 35 additions & 0 deletions Sources/_MatchingEngine/Regex/Parse/Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,41 @@ enum ParseError: Error, Hashable {
case emptyProperty
}

extension ParseError: CustomStringConvertible {
var description: String {
switch self {
case let .numberOverflow(s):
return "number overflow: \(s)"
case let .expectedNumDigits(s, i):
return "expected \(i) digits in '\(s)'"
case let .expectedNumber(s, kind: kind):
let radix: String
if kind == .decimal {
radix = ""
} else {
radix = " of radix \(kind.radix)"
}
return "expected a numbers in '\(s)'\(radix)"
case let .expected(s):
return "expected '\(s)'"
case .unexpectedEndOfInput:
return "unexpected end of input"
case let .misc(s):
return s
case let .expectedASCII(c):
return "expected ASCII for '\(c)'"
case .expectedCustomCharacterClassMembers:
return "expected custom character class members"
case .invalidCharacterClassRangeOperand:
return "invalid character class range"
case let .invalidPOSIXSetName(n):
return "invalid character set name: '\(n)'"
case .emptyProperty:
return "empty property"
}
}
}

// TODO: Fixits, notes, etc.

// TODO: Diagnostics engine, recorder, logger, or similar.
Expand Down
8 changes: 8 additions & 0 deletions Tests/RegexTests/DiagnosticTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,12 @@ extension RegexTests {
// TODO: Find out way to render value-members of AST, not
// just children
}

func testErrors() {
// Note: These don't really "test" anything, but good to
// see our output...
print("\(ParseError.emptyProperty)")
print("\(ParseError.expectedNumber("abc", kind: .decimal))")
print("\(ParseError.expectedNumber("abc", kind: .hex))")
}
}