summaryrefslogtreecommitdiff
path: root/tikzit/src/common/TikzGraphAssembler.m
diff options
context:
space:
mode:
authorJohan Paulsson <gonz@users.sourceforge.net>2013-01-27 16:54:57 +0000
committerJohan Paulsson <gonz@users.sourceforge.net>2013-01-27 16:54:57 +0000
commitd35a186932e95b979bb947110a2e7d2cca41fcc9 (patch)
treec01417f00717afd223a9a2052cfaa809e8de655e /tikzit/src/common/TikzGraphAssembler.m
parent46d894569372dea8c3e3f698b361f68d6bbebde6 (diff)
Better parser errors on syntax errors. Will show line number, description and where the error happened.
Test function for delimited string if they will break reprising.
Diffstat (limited to 'tikzit/src/common/TikzGraphAssembler.m')
-rw-r--r--tikzit/src/common/TikzGraphAssembler.m44
1 files changed, 37 insertions, 7 deletions
diff --git a/tikzit/src/common/TikzGraphAssembler.m b/tikzit/src/common/TikzGraphAssembler.m
index 7d8d0e7..050b5fe 100644
--- a/tikzit/src/common/TikzGraphAssembler.m
+++ b/tikzit/src/common/TikzGraphAssembler.m
@@ -34,8 +34,16 @@ extern int yylex_destroy(void);
static NSLock *parseLock = nil;
static id currentAssembler = nil;
+int yylineno;
+int yyleng;
+int lineno;
+int tokenpos;
+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 errorWithDomain:@"net.sourceforge.tikzit"
code:TZ_ERR_PARSE
@@ -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];
}