diff options
author | Alex Merry <dev@randomguy3.me.uk> | 2013-03-26 12:54:41 +0000 |
---|---|---|
committer | Alex Merry <dev@randomguy3.me.uk> | 2013-03-26 12:54:41 +0000 |
commit | 3bfdc232f22f0fa5be20e0b603a8287626ec0c2f (patch) | |
tree | e54537047ac72238bc4eb2cd467d12e3a48b6939 /tikzit/src/common | |
parent | 0f969b6ced7e9ebcbe4bc4e17867ff32d67f27bb (diff) |
Add edge anchor editing to GTK port
Includes functions for testing if an anchor is valid.
Diffstat (limited to 'tikzit/src/common')
-rw-r--r-- | tikzit/src/common/NSString+Tikz.h | 3 | ||||
-rw-r--r-- | tikzit/src/common/NSString+Tikz.m | 7 | ||||
-rw-r--r-- | tikzit/src/common/TikzGraphAssembler.h | 10 | ||||
-rw-r--r-- | tikzit/src/common/TikzGraphAssembler.m | 39 |
4 files changed, 49 insertions, 10 deletions
diff --git a/tikzit/src/common/NSString+Tikz.h b/tikzit/src/common/NSString+Tikz.h index 639e269..ea6ea40 100644 --- a/tikzit/src/common/NSString+Tikz.h +++ b/tikzit/src/common/NSString+Tikz.h @@ -19,7 +19,8 @@ @interface NSString (Tikz) - (NSString*) tikzEscapedString; - - (BOOL) isValidTikz; + - (BOOL) isValidTikzPropertyNameOrValue; + - (BOOL) isValidAnchor; @end // vi:ft=objc:noet:ts=4:sts=4:sw=4 diff --git a/tikzit/src/common/NSString+Tikz.m b/tikzit/src/common/NSString+Tikz.m index 8b11ad2..520ffd3 100644 --- a/tikzit/src/common/NSString+Tikz.m +++ b/tikzit/src/common/NSString+Tikz.m @@ -16,6 +16,7 @@ */ #import "NSString+Tikz.h" +#import "TikzGraphAssembler.h" @implementation NSString (Tikz) @@ -31,7 +32,7 @@ } } -- (BOOL) isValidTikz { +- (BOOL) isValidTikzPropertyNameOrValue { NSUInteger length = [self length]; unsigned int brace_depth = 0; unsigned int escape = 0; @@ -53,6 +54,10 @@ return !escape && brace_depth == 0; } +- (BOOL) isValidAnchor { + return [TikzGraphAssembler validateTikzEdgeAnchor:self]; +} + @end // vi:ft=objc:noet:ts=4:sts=4:sw=4 diff --git a/tikzit/src/common/TikzGraphAssembler.h b/tikzit/src/common/TikzGraphAssembler.h index c365128..3403969 100644 --- a/tikzit/src/common/TikzGraphAssembler.h +++ b/tikzit/src/common/TikzGraphAssembler.h @@ -100,6 +100,16 @@ */ + (BOOL)validateTikzPropertyNameOrValue:(NSString*)tikz; +/** + * Validate an edge anchor + * + * Checks that the given string will successfully lex if used as an anchor for + * and edge + * @param tikz the string to validate + * @return YES if the string can be used as an edge anchor, NO otherwise + */ ++ (BOOL)validateTikzEdgeAnchor:(NSString*)tikz; + @end // vi:ft=objc:noet:ts=4:sts=4:sw=4 diff --git a/tikzit/src/common/TikzGraphAssembler.m b/tikzit/src/common/TikzGraphAssembler.m index c49298f..6b191a2 100644 --- a/tikzit/src/common/TikzGraphAssembler.m +++ b/tikzit/src/common/TikzGraphAssembler.m @@ -113,21 +113,44 @@ } + (BOOL)validateTikzPropertyNameOrValue:(NSString*)tikz { - BOOL r; - - NSString * testTikz = [NSString stringWithFormat: @"{%@}", tikz]; - + BOOL valid; + + NSString * testTikz = [NSString stringWithFormat: @"{%@}", tikz]; + void *scanner; yylex_init (&scanner); yyset_extra(nil, scanner); yy_scan_string([testTikz UTF8String], scanner); YYSTYPE lval; YYLTYPE lloc; - yylex(&lval, &lloc, scanner); - r = !(yyget_leng(scanner) < [testTikz length]); + int result = yylex(&lval, &lloc, scanner); + valid = (result == DELIMITEDSTRING) && + (yyget_leng(scanner) == [testTikz length]); yylex_destroy(scanner); - - return r; + + return valid; +} + ++ (BOOL)validateTikzEdgeAnchor:(NSString*)tikz { + BOOL valid = YES; + + NSString * testTikz = [NSString stringWithFormat: @"(1.%@)", tikz]; + + void *scanner; + yylex_init (&scanner); + yyset_extra(nil, scanner); + yy_scan_string([testTikz UTF8String], scanner); + YYSTYPE lval; + YYLTYPE lloc; + valid = valid && (yylex(&lval, &lloc, scanner) == LEFTPARENTHESIS); + valid = valid && (yylex(&lval, &lloc, scanner) == REFSTRING); + valid = valid && (yylex(&lval, &lloc, scanner) == FULLSTOP); + valid = valid && (yylex(&lval, &lloc, scanner) == REFSTRING); + valid = valid && (yylex(&lval, &lloc, scanner) == RIGHTPARENTHESIS); + valid = valid && (lloc.last_column == [testTikz length]); + yylex_destroy(scanner); + + return valid; } @end |