diff options
Diffstat (limited to 'tikzit/src/common')
-rw-r--r-- | tikzit/src/common/Edge.h | 14 | ||||
-rw-r--r-- | tikzit/src/common/Edge.m | 23 | ||||
-rw-r--r-- | tikzit/src/common/EdgeStyle.m | 8 | ||||
-rw-r--r-- | tikzit/src/common/Graph.m | 23 | ||||
-rw-r--r-- | tikzit/src/common/Preambles.h | 2 | ||||
-rw-r--r-- | tikzit/src/common/Preambles.m | 16 | ||||
-rw-r--r-- | tikzit/src/common/TikzGraphAssembler.h | 5 | ||||
-rw-r--r-- | tikzit/src/common/TikzGraphAssembler.m | 69 | ||||
-rw-r--r-- | tikzit/src/common/test/parser.m | 11 | ||||
-rw-r--r-- | tikzit/src/common/test/test.m | 2 | ||||
-rw-r--r-- | tikzit/src/common/tikzlexer.lm | 61 | ||||
-rw-r--r-- | tikzit/src/common/tikzparser.ym | 42 |
12 files changed, 216 insertions, 60 deletions
diff --git a/tikzit/src/common/Edge.h b/tikzit/src/common/Edge.h index 607fcc6..cc636dd 100644 --- a/tikzit/src/common/Edge.h +++ b/tikzit/src/common/Edge.h @@ -58,6 +58,8 @@ typedef enum { float weight; EdgeStyle *style; GraphElementData *data; + NSString *sourceAnchor; + NSString *targetAnchor; // When set to YES, lazily create the edge node, and keep it around when set // to NO (at least until saved/loaded). @@ -115,6 +117,18 @@ typedef enum { @property (retain) Node *edgeNode; /*! + @property sourceAnchor + @brief The source node anchor point, as in north or center. + */ +@property (copy) NSString *sourceAnchor; + +/*! + @property targetAnchor + @brief The target node anchor point, as in north or center. + */ +@property (copy) NSString *targetAnchor; + +/*! @property hasEdgeNode @brief A read/write property. When set to true, a new edge node is actually constructed. */ diff --git a/tikzit/src/common/Edge.m b/tikzit/src/common/Edge.m index 2514fc1..4ecb9bf 100644 --- a/tikzit/src/common/Edge.m +++ b/tikzit/src/common/Edge.m @@ -39,6 +39,8 @@ source = nil; target = nil; edgeNode = nil; + sourceAnchor = @""; + targetAnchor = @""; return self; } @@ -389,6 +391,25 @@ [self didChangeValueForKey:@"hasEdgeNode"]; } +@synthesize sourceAnchor; +@synthesize targetAnchor; + +- (void)setSourceAnchor:(NSString *)_sourceAnchor{ + if(_sourceAnchor != nil){ + sourceAnchor = _sourceAnchor; + }else{ + sourceAnchor = @""; + } +} + +- (void)setTargetAnchor:(NSString *)_targetAnchor{ + if(_targetAnchor != nil){ + targetAnchor = _targetAnchor; + }else{ + targetAnchor = @""; + } +} + @synthesize data; - (void) insertObject:(GraphElementProperty*)gep inDataAtIndex:(NSUInteger)index { @@ -587,6 +608,8 @@ [source release]; [target release]; [data release]; + [sourceAnchor release]; + [targetAnchor release]; [super dealloc]; } diff --git a/tikzit/src/common/EdgeStyle.m b/tikzit/src/common/EdgeStyle.m index 88b1f54..7a638c9 100644 --- a/tikzit/src/common/EdgeStyle.m +++ b/tikzit/src/common/EdgeStyle.m @@ -169,9 +169,11 @@ else if (headStyle == AH_Latex) [buf appendString:@"latex"]; - [buf appendString:@",draw="]; - [buf appendString:colorName]; - + if(colorName != nil){ + [buf appendString:@",draw="]; + [buf appendString:colorName]; + } + if (decorationStyle != ED_None) { [buf appendString:@",postaction={decorate},decoration={markings,mark="]; if (decorationStyle == ED_Arrow) diff --git a/tikzit/src/common/Graph.m b/tikzit/src/common/Graph.m index 7f7345c..767f307 100644 --- a/tikzit/src/common/Graph.m +++ b/tikzit/src/common/Graph.m @@ -722,14 +722,33 @@ } NSString *edata = [[e data] stringList]; + + NSString *srcAnchor; + NSString *tgtAnchor; + + if ([[e source] style] == nil) { + srcAnchor = @".center"; + } else if ([[e sourceAnchor] isEqual:@""]) { + srcAnchor = @""; + } else { + srcAnchor = [NSString stringWithFormat:@".%@", [e sourceAnchor]]; + } + + if ([[e target] style] == nil) { + tgtAnchor = @".center"; + } else if ([[e targetAnchor] isEqual:@""]) { + tgtAnchor = @""; + } else { + tgtAnchor = [NSString stringWithFormat:@".%@", [e targetAnchor]]; + } [code appendFormat:@"\t\t\\draw%@ (%@%@) to %@(%@%@);\n", ([edata isEqual:@""]) ? @"" : [NSString stringWithFormat:@" %@", edata], [[e source] name], - ([[e source] style] == nil) ? @".center" : @"", + srcAnchor, nodeStr, ([e source] == [e target]) ? @"" : [[e target] name], - ([e source] != [e target] && [[e target] style] == nil) ? @".center" : @"" + tgtAnchor ]; } diff --git a/tikzit/src/common/Preambles.h b/tikzit/src/common/Preambles.h index d507ad9..95081c4 100644 --- a/tikzit/src/common/Preambles.h +++ b/tikzit/src/common/Preambles.h @@ -29,6 +29,7 @@ NSMutableDictionary *preambleDict; NSString *selectedPreambleName; NSArray *styles; + NSArray *edges; StyleManager *styleManager; } @@ -40,6 +41,7 @@ + (Preambles*)preambles; - (id)init; - (void)setStyles:(NSArray*)sty; +- (void)setEdges:(NSArray*)edg; - (NSString*)preambleForName:(NSString*)name; - (BOOL)setPreamble:(NSString*)content forName:(NSString*)name; diff --git a/tikzit/src/common/Preambles.m b/tikzit/src/common/Preambles.m index af3bbc4..5343127 100644 --- a/tikzit/src/common/Preambles.m +++ b/tikzit/src/common/Preambles.m @@ -64,6 +64,7 @@ static NSString *POSTAMBLE = preambleDict = [[NSMutableDictionary alloc] initWithCapacity:1]; [preambleDict setObject:[self defaultPreamble] forKey:@"custom"]; styles = nil; + edges = nil; styleManager = nil; return self; } @@ -96,6 +97,12 @@ static NSString *POSTAMBLE = styles = sty; } +- (void)setEdges:(NSArray*)edg { + [edg retain]; + [edges release]; + edges = edg; +} + - (NSString*)styleDefinitions { if (styleManager != nil) { [self setStyles:[styleManager nodeStyles]]; @@ -120,11 +127,16 @@ static NSString *POSTAMBLE = [stroke hexName], [stroke redFloat], [stroke greenFloat], [stroke blueFloat]]; } } + + if (styleManager != nil) { + [self setEdges:[styleManager edgeStyles]]; + } + [buf appendString:@"\n"]; - for (EdgeStyle *st in [styleManager edgeStyles]) { + for (EdgeStyle *st in edges) { [buf appendFormat:@"%@\n", [st tikz]]; ColorRGB *color = [st colorRGB]; - if ([color name] == nil && ![colors containsObject:color]) { + if (color != nil && [color name] == nil && ![colors containsObject:color]) { [colors addObject:color]; [colbuf appendFormat:@"\\definecolor{%@}{rgb}{%.3f,%.3f,%.3f}\n", [color hexName], [color redFloat], [color greenFloat], [color blueFloat]]; diff --git a/tikzit/src/common/TikzGraphAssembler.h b/tikzit/src/common/TikzGraphAssembler.h index 1b006dd..a0c8a0d 100644 --- a/tikzit/src/common/TikzGraphAssembler.h +++ b/tikzit/src/common/TikzGraphAssembler.h @@ -41,11 +41,14 @@ - (BOOL)parseTikz:(NSString*)tikz; - (BOOL)parseTikz:(NSString*)tikz forGraph:(Graph*)gr; +- (BOOL)testTikz:(NSString*)tikz; + - (void)prepareNode; - (void)finishNode; - (void)prepareEdge; -- (void)setEdgeSource:(NSString*)src target:(NSString*)targ; +- (void)setEdgeSource:(NSString*)edge anchor:(NSString*)anch; +- (void)setEdgeTarget:(NSString*)edge anchor:(NSString*)anch; - (void)finishEdge; - (void)invalidate; diff --git a/tikzit/src/common/TikzGraphAssembler.m b/tikzit/src/common/TikzGraphAssembler.m index 5e60b05..b07ee58 100644 --- a/tikzit/src/common/TikzGraphAssembler.m +++ b/tikzit/src/common/TikzGraphAssembler.m @@ -34,12 +34,20 @@ extern int yylex_destroy(void); static NSLock *parseLock = nil; static id currentAssembler = nil; +extern int yylineno; +extern int yyleng; +int lineno; +int tokenpos; +extern char *yystr; +char linebuff[500]; + + void yyerror(const char *str) { - NSLog(@"Parse error: %s", str); + NSLog(@"Parse error on line %i: %s\n%s\n%@\n", lineno, str, linebuff, [[@"" stringByPaddingToLength:(tokenpos-yyleng) withString: @" " startingAtIndex:0] stringByAppendingString:[@"" stringByPaddingToLength:yyleng withString: @"^" startingAtIndex:0]]); if (currentAssembler != nil) { - NSError *error = [NSError - errorWithMessage:[NSString stringWithCString:str] - code:TZ_ERR_PARSE]; + NSError *error = [NSError errorWithDomain:@"net.sourceforge.tikzit" + code:TZ_ERR_PARSE + userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithCString:str] forKey: NSLocalizedDescriptionKey]]; [currentAssembler invalidateWithError:error]; } } @@ -81,6 +89,10 @@ int yywrap() { - (BOOL)parseTikz:(NSString*)tikz forGraph:(Graph*)gr { [parseLock lock]; + + lineno = 1; + tokenpos = 0; + linebuff[0] = 0; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; currentAssembler = self; @@ -93,12 +105,14 @@ int yywrap() { // the node map keeps track of the mapping of names to nodes nodeMap = [[NSMutableDictionary alloc] init]; - - // do the parsing - yy_scan_string([tikz UTF8String]); - yyparse(); - yylex_destroy(); - + + // do the parsing if actual input + if([tikz length] > 0){ + yy_scan_string([tikz UTF8String]); + yyparse(); + yylex_destroy(); + } + [nodeMap release]; nodeMap = nil; @@ -110,6 +124,22 @@ int yywrap() { return (graph != nil); } +- (BOOL)testTikz:(NSString *)tikz{ + BOOL r; + + NSString * testTikz = [NSString stringWithFormat: @"{%@}", tikz]; + + yy_scan_string([testTikz UTF8String]); + yylex(); + + r = !(yyleng < [testTikz length]); + + [testTikz autorelease]; + yylex_destroy(); + + return r; +} + - (void)prepareNode { currentNode = [[Node alloc] init]; } @@ -137,14 +167,19 @@ int yywrap() { currentEdge = nil; } -- (void)setEdgeSource:(NSString*)src target:(NSString*)targ { - if (![targ isEqualToString:@""]) { - [currentEdge setSource:[nodeMap objectForKey:src]]; - [currentEdge setTarget:[nodeMap objectForKey:targ]]; +- (void)setEdgeSource:(NSString*)edge anchor:(NSString*)anch { + Node *s = [nodeMap objectForKey:edge]; + [currentEdge setSource:s]; + [currentEdge setSourceAnchor:anch]; +} + +- (void)setEdgeTarget:(NSString*)edge anchor:(NSString*)anch { + if (![edge isEqualToString:@""]) { + [currentEdge setTarget:[nodeMap objectForKey:edge]]; + [currentEdge setTargetAnchor:anch]; } else { - Node *s = [nodeMap objectForKey:src]; - [currentEdge setSource:s]; - [currentEdge setTarget:s]; + [currentEdge setTargetAnchor:anch]; + [currentEdge setTarget:[currentEdge source]]; } } diff --git a/tikzit/src/common/test/parser.m b/tikzit/src/common/test/parser.m index 29dabe7..3346acd 100644 --- a/tikzit/src/common/test/parser.m +++ b/tikzit/src/common/test/parser.m @@ -34,7 +34,7 @@ void testParser() { [TikzGraphAssembler setup]; - NodeStyle *rn = [NodeStyle defaultStyleWithName:@"rn"]; + NodeStyle *rn = [NodeStyle defaultNodeStyleWithName:@"rn"]; NSArray *styles = [NSArray arrayWithObject:rn]; NSString *tikz = @@ -74,9 +74,12 @@ void testParser() { TEST(@"Edge has edge node", [e1 edgeNode]!=nil); TEST(@"Edge node labeled correctly", [[[e1 edgeNode] label] isEqualToString:@"-"]); - NSString *sty = [[[[[e1 edgeNode] data] atoms] objectEnumerator] nextObject]; - TEST(@"Edge node styled correctly", sty!=nil && [sty isEqualToString:@"tick"]); - +// NSString *sty = [[[[[e1 edgeNode] data] atoms] objectEnumerator] nextObject]; +// TEST(@"Edge node styled correctly", sty!=nil && [sty isEqualToString:@"tick"]); + + PUTS(@"Source anchor: %@",[e1 sourceAnchor]); + PUTS(@"Target anchor: %@",[e1 targetAnchor]); + endTestBlock(@"parser"); [pool drain]; diff --git a/tikzit/src/common/test/test.m b/tikzit/src/common/test/test.m index 8437646..9afcd67 100644 --- a/tikzit/src/common/test/test.m +++ b/tikzit/src/common/test/test.m @@ -160,7 +160,7 @@ void runTests(); int main() { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - setColorEnabled (YES); + setColorEnabled (NO); startTests(); runTests(); diff --git a/tikzit/src/common/tikzlexer.lm b/tikzit/src/common/tikzlexer.lm index 9418d83..816d91c 100644 --- a/tikzit/src/common/tikzlexer.lm +++ b/tikzit/src/common/tikzlexer.lm @@ -1,4 +1,6 @@ %option nounput +%option yylineno + %{ // // tikzparser.l @@ -30,48 +32,61 @@ #include "tikzparser.h" #endif +extern char linebuff[500]; +extern int lineno; +extern int tokenpos; %} %% -\n /* ignore end of line */; -[ \t]+ /* ignore whitespace */; -\\begin return LATEXBEGIN; -\\end return LATEXEND; -\{tikzpicture\} return TIKZPICTURE; -\{pgfonlayer\} return PGFONLAYER; -\.center return ANCHORCENTER; -\( return LEFTPARENTHESIS; -\) return RIGHTPARENTHESIS; -\[ return LEFTBRACKET; -\] return RIGHTBRACKET; -; return SEMICOLON; -, return COMMA; -= return EQUALS; -\\draw return DRAW; -to return TO; -\\node return NODE; -\\path return PATH; -node return ALTNODE; -rectangle return RECTANGLE; -at return AT; +%\n /* ignore end of line */; +\n.* { strcpy(linebuff, yytext+1); + lineno++; + tokenpos = 0; + yyless(1); + } +[ ]+ { tokenpos += yyleng; } /* ignore whitespace */; +[\t]+ { tokenpos += 4*yyleng; } /* ignore whitespace */; +\\begin { tokenpos += yyleng; return LATEXBEGIN; } +\\end { tokenpos += yyleng; return LATEXEND; } +\{tikzpicture\} { tokenpos += yyleng; return TIKZPICTURE; } +\{pgfonlayer\} { tokenpos += yyleng; return PGFONLAYER; } +\( { tokenpos += yyleng; return LEFTPARENTHESIS; } +\) { tokenpos += yyleng; return RIGHTPARENTHESIS; } +\[ { tokenpos += yyleng; return LEFTBRACKET; } +\] { tokenpos += yyleng; return RIGHTBRACKET; } +; { tokenpos += yyleng; return SEMICOLON; } +, { tokenpos += yyleng; return COMMA; } +\. { tokenpos += yyleng; return FULLSTOP; } += { tokenpos += yyleng; return EQUALS; } +\\draw { tokenpos += yyleng; return DRAW; } +to { tokenpos += yyleng; return TO; } +\\node { tokenpos += yyleng; return NODE; } +\\path { tokenpos += yyleng; return PATH; } +node { tokenpos += yyleng; return ALTNODE; } +rectangle { tokenpos += yyleng; return RECTANGLE; } +at { tokenpos += yyleng; return AT; } [0-9]+ { + tokenpos += yyleng; yylval.nsstr=[NSString stringWithUTF8String:yytext]; return NATURALNUMBER; } (\-?[0-9]*\.[0-9]+)|(\-?[0-9]+) { + tokenpos += yyleng; yylval.nsstr=[NSString stringWithUTF8String:yytext]; return REALNUMBER; } -\\?[a-zA-Z<>\-\']+ { //' +\\?[a-zA-Z<>\-\'][a-zA-Z<>\-\'0-9]* { //' + tokenpos += yyleng; yylval.nsstr=[NSString stringWithUTF8String:yytext]; return LWORD; } \"[^\"]*\" /* " */ { + tokenpos += yyleng; yylval.nsstr=[NSString stringWithUTF8String:yytext]; return QUOTEDSTRING; } @@ -93,8 +108,10 @@ at return AT; } NSString *s = [buf copy]; + yyleng += 1 + [buf length]; [s autorelease]; yylval.nsstr = s; + tokenpos += yyleng; return DELIMITEDSTRING; } diff --git a/tikzit/src/common/tikzparser.ym b/tikzit/src/common/tikzparser.ym index 57f0600..487cfd4 100644 --- a/tikzit/src/common/tikzparser.ym +++ b/tikzit/src/common/tikzparser.ym @@ -29,6 +29,9 @@ #import "TikzGraphAssembler.h" #import "GraphElementProperty.h" +extern char* yystr; +extern int yylineno; +extern int tokenpos; extern int yylex(void); extern void yyerror(const char *str); @@ -39,17 +42,19 @@ extern void yyerror(const char *str); NSString *nsstr; }; +%error-verbose + %token LATEXBEGIN %token LATEXEND %token TIKZPICTURE %token PGFONLAYER -%token ANCHORCENTER %token LEFTPARENTHESIS %token RIGHTPARENTHESIS %token LEFTBRACKET %token RIGHTBRACKET %token SEMICOLON %token COMMA +%token FULLSTOP %token EQUALS %token DRAW %token TO @@ -65,9 +70,10 @@ extern void yyerror(const char *str); %token DELIMITEDSTRING %type<nsstr> nodename +%type<nsstr> anchor +%type<nsstr> optanchor %type<nsstr> nodeid %type<pt> coords -%type<nsstr> target %type<nsstr> propsym %type<nsstr> propsyms %type<nsstr> val @@ -119,6 +125,8 @@ propsym: nodecmd : NODE { [[TikzGraphAssembler currentAssembler] prepareNode]; }; +nodename: LEFTPARENTHESIS nodeid RIGHTPARENTHESIS { $$ = $<nsstr>2; }; + node: nodecmd optproperties nodename AT coords nodelabel SEMICOLON { @@ -135,8 +143,17 @@ nodelabel: [n setLabel:$<nsstr>1]; } -optanchor: | ANCHORCENTER; -nodename: LEFTPARENTHESIS nodeid optanchor RIGHTPARENTHESIS { $$ = $<nsstr>2; }; +anchor: LWORD { $$ = $<nsstr>1; } | NATURALNUMBER { $$ = $<nsstr>1; }; + +optanchor: { $$ = @""; } | FULLSTOP anchor { $$ = $<nsstr>2; }; + +source: LEFTPARENTHESIS nodeid optanchor RIGHTPARENTHESIS + { + TikzGraphAssembler *a = [TikzGraphAssembler currentAssembler]; + [a setEdgeSource:$<nsstr>2 + anchor:$<nsstr>3]; + }; + nodeid: LWORD { $$ = $<nsstr>1; } | NATURALNUMBER { $$ = $<nsstr>1; }; coords: @@ -147,14 +164,23 @@ coords: edgecmd : DRAW { [[TikzGraphAssembler currentAssembler] prepareEdge]; }; edge: - edgecmd optproperties nodename TO optedgenode target SEMICOLON + edgecmd optproperties source TO optedgenode target SEMICOLON { TikzGraphAssembler *a = [TikzGraphAssembler currentAssembler]; - [a setEdgeSource:$<nsstr>3 - target:$<nsstr>6]; [a finishEdge]; }; -target: nodename { $$=$<nsstr>1; } | selfloop { $$=@""; }; +target: LEFTPARENTHESIS nodeid optanchor RIGHTPARENTHESIS + { + TikzGraphAssembler *a = [TikzGraphAssembler currentAssembler]; + [a setEdgeTarget:$<nsstr>2 + anchor:$<nsstr>3]; + } + | selfloop + { + TikzGraphAssembler *a = [TikzGraphAssembler currentAssembler]; + [a setEdgeTarget:@"" + anchor:@""]; + }; selfloop: LEFTPARENTHESIS RIGHTPARENTHESIS; altnodecmd: ALTNODE { [[TikzGraphAssembler currentAssembler] prepareNode]; }; |