From a520a41f74a553aea12385d034244c27825ecfe9 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Wed, 18 Jun 2008 17:13:44 +0200 Subject: MLKReadtable: Add support for changing syntax types and setting and unsetting constituent traits. --- MLKReadtable.h | 38 ++++++++++++++++++++++++++++++-- MLKReadtable.m | 68 ++++++++++++++++++++++++++++++++-------------------------- 2 files changed, 73 insertions(+), 33 deletions(-) diff --git a/MLKReadtable.h b/MLKReadtable.h index f1fe16d..00da691 100644 --- a/MLKReadtable.h +++ b/MLKReadtable.h @@ -32,6 +32,32 @@ enum MLKReadtableCase MLKReadtableCase_PRESERVE }; +enum MLKSyntaxType +{ + CONSTITUENT = 0, + WHITESPACE = 1, + TERMINATING_MACRO = 2, + NONTERMINATING_MACRO = 3, + SINGLE_ESCAPE = 4, + MULTI_ESCAPE = 5 +}; + +enum MLKConstituentTrait +{ + ALPHABETIC = 1, + INVALID = 2, + PACKAGE_MARKER = 4, + ALPHA_DIGIT = 8, + EXPONENT_MARKER = 16, + NUMBER_MARKER = 32, + RATIO_MARKER = 64, + DECIMAL_POINT = 128, + MINUS_SIGN = 256, + PLUS_SIGN = 512, + SIGN = 1024, + DOT = 2048 +}; + @interface MLKReadtable : MLKLispValue { @@ -77,7 +103,15 @@ enum MLKReadtableCase -(MLKFuncallable *) macroFunctionForCharacter:(unichar)ch; -(unichar) charWithReadtableCase:(unichar)ch; -// For internal use. -(int) characterConstituentTraits:(unichar)ch; --(int) characterSyntaxType:(unichar)ch; +-(BOOL) character:(unichar)ch + hasTrait:(enum MLKConstituentTrait)trait; +-(enum MLKSyntaxType) characterSyntaxType:(unichar)ch; + +-(void) setSyntaxType:(enum MLKSyntaxType)type + forCharacter:(unichar)ch; +-(void) setConstituentTrait:(enum MLKConstituentTrait)trait + forCharacter:(unichar)ch; +-(void) unsetConstituentTrait:(enum MLKConstituentTrait)trait + forCharacter:(unichar)ch; @end diff --git a/MLKReadtable.m b/MLKReadtable.m index c94e7ea..8d56d72 100644 --- a/MLKReadtable.m +++ b/MLKReadtable.m @@ -23,34 +23,6 @@ #import -enum MLKSyntaxType -{ - CONSTITUENT = 0, - WHITESPACE = 1, - TERMINATING_MACRO = 2, - NONTERMINATING_MACRO = 3, - SINGLE_ESCAPE = 4, - MULTI_ESCAPE = 5 -}; - - -enum MLKConstituentTrait -{ - ALPHABETIC = 1, - INVALID = 2, - PACKAGE_MARKER = 4, - ALPHA_DIGIT = 8, - EXPONENT_MARKER = 16, - NUMBER_MARKER = 32, - RATIO_MARKER = 64, - DECIMAL_POINT = 128, - MINUS_SIGN = 256, - PLUS_SIGN = 512, - SIGN = 1024, - DOT = 2048 -}; - - @implementation MLKReadtable -(MLKReadtable *) initSuper { @@ -89,7 +61,7 @@ enum MLKConstituentTrait [self isTerminatingMacroCharacter:ch]); } --(int) characterSyntaxType:(unichar)ch +-(enum MLKSyntaxType) characterSyntaxType:(unichar)ch { NSNumber *type = [_traits objectForKey:[NSNumber numberWithLong:ch]]; if (!type) @@ -157,12 +129,21 @@ DEFINE_SYNTAX_PREDICATE(isConstituentCharacter:, CONSTITUENT) return [traits intValue]; } +-(BOOL) character:(unichar)ch + hasTrait:(enum MLKConstituentTrait)trait +{ + NSNumber *traits = [_traits objectForKey:[NSNumber numberWithLong:ch]]; + if (!traits) + return (trait == ALPHABETIC); + else + return [traits intValue] & trait; +} + #define DEFINE_TRAIT_PREDICATE(SELECTOR, TRAIT) \ -(BOOL) SELECTOR (unichar)ch \ { \ - return ([self characterConstituentTraits:ch] \ - & TRAIT); \ + return ([self character:ch hasTrait:TRAIT]); \ } DEFINE_TRAIT_PREDICATE(isInvalid:, INVALID) @@ -179,6 +160,31 @@ DEFINE_TRAIT_PREDICATE(isSign:, SIGN) DEFINE_TRAIT_PREDICATE(isDot:, DOT) +-(void) setSyntaxType:(enum MLKSyntaxType)type + forCharacter:(unichar)ch +{ + [_syntaxTable setObject:[NSNumber numberWithInt:type] + forKey:[NSNumber numberWithLong:ch]]; +} + +-(void) setConstituentTrait:(enum MLKConstituentTrait)trait + forCharacter:(unichar)ch +{ + int traits = [self characterConstituentTraits:ch]; + traits = traits | trait; + [_traits setObject:[NSNumber numberWithInt:traits] + forKey:[NSNumber numberWithLong:ch]]; +} + +-(void) unsetConstituentTrait:(enum MLKConstituentTrait)trait + forCharacter:(unichar)ch +{ + int traits = [self characterConstituentTraits:ch]; + traits = traits & ~trait; + [_traits setObject:[NSNumber numberWithInt:traits] + forKey:[NSNumber numberWithLong:ch]]; +} + -(BOOL) isDecimalDigit:(unichar)ch { return [self isDigit:ch inBase:10]; -- cgit v1.2.3