@@ -631,6 +631,41 @@ extension AST.Atom {
631631 }
632632}
633633
634+ extension AST . Atom . EscapedBuiltin {
635+ /// If the escape sequence represents a unicode scalar value, returns the
636+ /// value, otherwise `nil`.
637+ public var scalarValue : UnicodeScalar ? {
638+ switch self {
639+ // TODO: Should we separate these into a separate enum? Or move the
640+ // specifics of the scalar to the DSL tree?
641+ case . alarm:
642+ return " \u{7} "
643+ case . backspace:
644+ return " \u{8} "
645+ case . escape:
646+ return " \u{1B} "
647+ case . formfeed:
648+ return " \u{C} "
649+ case . newline:
650+ return " \n "
651+ case . carriageReturn:
652+ return " \r "
653+ case . tab:
654+ return " \t "
655+
656+ case . singleDataUnit, . decimalDigit, . notDecimalDigit,
657+ . horizontalWhitespace, . notHorizontalWhitespace, . notNewline,
658+ . newlineSequence, . whitespace, . notWhitespace, . verticalTab,
659+ . notVerticalTab, . wordCharacter, . notWordCharacter, . graphemeCluster,
660+ . wordBoundary, . notWordBoundary, . startOfSubject,
661+ . endOfSubjectBeforeNewline, . endOfSubject,
662+ . firstMatchingPositionInSubject, . resetStartOfMatch, . trueAnychar,
663+ . textSegment, . notTextSegment:
664+ return nil
665+ }
666+ }
667+ }
668+
634669extension AST . Atom {
635670 /// Retrieve the character value of the atom if it represents a literal
636671 /// character or unicode scalar, nil otherwise.
@@ -642,34 +677,7 @@ extension AST.Atom {
642677 return Character ( s)
643678
644679 case . escaped( let c) :
645- switch c {
646- // TODO: Should we separate these into a separate enum? Or move the
647- // specifics of the scalar to the DSL tree?
648- case . alarm:
649- return " \u{7} "
650- case . backspace:
651- return " \u{8} "
652- case . escape:
653- return " \u{1B} "
654- case . formfeed:
655- return " \u{C} "
656- case . newline:
657- return " \n "
658- case . carriageReturn:
659- return " \r "
660- case . tab:
661- return " \t "
662-
663- case . singleDataUnit, . decimalDigit, . notDecimalDigit,
664- . horizontalWhitespace, . notHorizontalWhitespace, . notNewline,
665- . newlineSequence, . whitespace, . notWhitespace, . verticalTab,
666- . notVerticalTab, . wordCharacter, . notWordCharacter, . graphemeCluster,
667- . wordBoundary, . notWordBoundary, . startOfSubject,
668- . endOfSubjectBeforeNewline, . endOfSubject,
669- . firstMatchingPositionInSubject, . resetStartOfMatch, . trueAnychar,
670- . textSegment, . notTextSegment:
671- return nil
672- }
680+ return c. scalarValue. map ( Character . init)
673681
674682 case . keyboardControl, . keyboardMeta, . keyboardMetaControl:
675683 // TODO: These should have unicode scalar values.
0 commit comments