From a188d21e41c07b7a25bb15be3597dc497cdef40b Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 16 Apr 2020 16:18:53 +0930 Subject: [PATCH] Add '<>:#' as valid characters in symbols --- MPEdn/MPEdnParser.m | 22 +++++++++++++++------- MPEdnTests/MPEdnParserTests.m | 4 +++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/MPEdn/MPEdnParser.m b/MPEdn/MPEdnParser.m index 6ad0149..8953fea 100644 --- a/MPEdn/MPEdnParser.m +++ b/MPEdn/MPEdnParser.m @@ -179,14 +179,22 @@ - (void) addTagReader: (id) reader #pragma mark - Tokeniser -static BOOL is_sym_punct (unichar ch) +/// is this a character that may begin a symbol? +static BOOL isStartSymPunct (unichar ch) { // NB: '/' is added to the specified EDN set of punctuation allowed in // symbol names. The check for its valid use will be done by the parser. return (ch >= '!' && ch <= '_' && (ch == '!' || ch == '$' || ch == '%' || ch == '&' || ch == '*' || ch == '+' || ch == '-' || ch == '.' || ch == '=' || ch == '?' || - ch == '_' || ch == '/' || ch == ':' || ch == '#')); + ch == '_' || ch == '/' || ch == ':' || ch == '#' || ch == '<' || + ch == '>')); +} + +/// is this a character that may be in a symbol? +static BOOL isSymPunct (unichar ch) +{ + return isStartSymPunct (ch) || ch == ':' || ch == '#'; } - (unichar) currentEndIdxChar @@ -326,7 +334,7 @@ - (void) nextToken { [self readTagName]; } - } else if (isalpha (ch) || is_sym_punct (ch)) + } else if (isalpha (ch) || isStartSymPunct (ch)) { [self readNameToken]; } else @@ -507,7 +515,7 @@ - (void) readNameToken do { ch = [self advanceEndIdx]; - } while (isalnum (ch) || is_sym_punct (ch)); + } while (isalnum (ch) || isSymPunct (ch)); token = TOKEN_NAME; tokenValue = @@ -521,7 +529,7 @@ - (void) readKeywordToken do { ch = [self advanceEndIdx]; - } while (isalnum (ch) || is_sym_punct (ch)); + } while (isalnum (ch) || isSymPunct (ch)); NSInteger length = endIdx - startIdx - 1; @@ -627,8 +635,8 @@ - (void) readTagName do { ch = [self advanceEndIdx]; - } while (isalnum (ch) || is_sym_punct (ch)); - + } while (isalnum (ch) || isStartSymPunct (ch)); + NSInteger tagLen = endIdx - startIdx - 1; if (tagLen > 0) diff --git a/MPEdnTests/MPEdnParserTests.m b/MPEdnTests/MPEdnParserTests.m index f2e033e..3c61401 100644 --- a/MPEdnTests/MPEdnParserTests.m +++ b/MPEdnTests/MPEdnParserTests.m @@ -121,11 +121,13 @@ - (void) testSymbols MPAssertParseOK (@"+", [MPEdnSymbol symbolWithName: @"+"], @"Symbol"); MPAssertParseOK (@"-", [MPEdnSymbol symbolWithName: @"-"], @"Symbol"); + MPAssertParseOK (@"<", [MPEdnSymbol symbolWithName: @"<"], @"Symbol"); + MPAssertParseOK (@"<:>", [MPEdnSymbol symbolWithName: @"<:>"], @"Symbol"); MPAssertParseOK (@"+a", [MPEdnSymbol symbolWithName: @"+a"], @"Symbol"); MPAssertParseOK (@"?", [MPEdnSymbol symbolWithName: @"?"], @"Symbol"); MPAssertParseOK (@"?a", [MPEdnSymbol symbolWithName: @"?a"], @"Symbol"); MPAssertParseOK (@"a?", [MPEdnSymbol symbolWithName: @"a?"], @"Symbol"); - + MPAssertParseError (@"}", @"Not a symbol"); MPAssertParseError (@"]", @"Not a symbol"); MPAssertParseError (@")", @"Not a symbol");