Last active
January 27, 2022 14:03
-
-
Save GeekAndDad/8d849208a3dd0ad08c4d2bb363a468e0 to your computer and use it in GitHub Desktop.
Playground to test parsing. Is there simpler way to do this?
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
import Parsing | |
/// simple test. | |
/// First char must exist and must be a char from a to z | |
/// Followed by zero or more characters from a to z or 0 to 9 | |
/// Must end with a space or be the end of input | |
/// | |
let validFirstChars = CharacterSet(charactersIn: "a"..."z") | |
let validFollowingChars = CharacterSet(charactersIn: "0"..."9").union(validFirstChars) | |
let test = Parse { | |
Prefix(minLength: 1, maxLength: 1) { $0.unicodeScalars.isEmpty ? | |
false : | |
validFirstChars.contains($0.unicodeScalars.first!) } | |
Prefix { validFollowingChars.contains($0.unicodeScalars.first!) } | |
OneOf { " " ; "" } | |
}.map { $0.appending($1) } | |
print("\nstart test\n") | |
print( "should be 'a123': ", test.parse("a123")! ) | |
print( "should be 'a123': ", test.parse("a123 ")! ) | |
print( "should be 'a': ", test.parse("a")! ) | |
print( "should be 'a': ", test.parse("a ")! ) | |
print( "should be nil: " , test.parse("0") ) | |
/* Output should be: | |
start test | |
should be 'a123': a123 | |
should be 'a123': a123 | |
should be 'a': a | |
should be 'a': a | |
should be nil: nil | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment