开发者

(Obj-C) What is wrong with this syntax highlighting? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

I am making a IDE for something... The syntax highlighting freezes when I do something like:


int i = abs();
//then push " like so:
int i = abs(");
//when I push anything after that it gives me an error and deletes it:
/*
ERROR:
2011-01-14 11:53:29.554 PROJECT NAME GOES HERE[6767:a0f] HIToolbox: ignoring exception '*** -[NSBigMutableString substringWithRange:]: Range or index out of bounds' that raised inside Carbon event dispatch
(
    0   CoreFoundation                      0x00007fff800b57b4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff83aed0f3 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff800b55d7 +[NSException raise:format:arguments:] + 103
    3   CoreFoundation                      0x00007fff800b5564 +[NSException raise:format:] + 148
    4   Foundation                          0x00007fff87e0ecf2 -[NSString substringWithRange:] + 204
    5   PROJECT NAME GOES HERE              0x00000001000014bb -[ScriptController textStorageDidProcessEditing:] + 741
    6   Foundation                          0x00007fff87deba66 _nsnote_callback + 167
    7   CoreFoundation                      0x00007fff8005d000 __CFXNotificationPost + 1008
    8   CoreFoundation                      0x00007fff80049578 _CFXNotificationPostNotification + 200
    9   Foundation                          0x00007fff87de29ce -[NSNotificationCenter postNotificationName:object:userInfo:] + 101
    10  AppKit                              0x00007fff8577cef6 -[NSTextStorage processEditing] + 137
    11  AppKit                              0x00007fff8577d9e3 -[NSTextStorage endEditing] + 80
    12  AppKit                              0x00007fff857a5b56 -[NSTextView insertText:replacementRange:] + 2745
    13  AppKit                              0x00007fff857a3471 -[NSTextInputContext handleTSMEvent:] + 2204
    14  AppKit                              0x00007fff857a2bc6 _NSTSMEventHandler + 156
    15  HIToolbox                           0x00007fff84d6d9d5 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1002
    16  HIToolbox                           0x00007fff84d6cf28 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 395
    17  HIToolbox                           0x00007fff84d8abeb SendEventToEventTarget + 45
    18  HIToolbox                           0x00007fff84dbc2bb SendTSMEvent + 48
    19  HIToolbox                           0x00007fff84dbbe61 SendUnicodeTextAEToUnicodeDoc + 468
    20  HIToolbox                           0x00007fff84d开发者_Python百科bbbdc TSMKeyEvent + 604
    21  HIToolbox                           0x00007fff84da6724 TSMProcessRawKeyEvent + 1909
    22  AppKit                              0x00007fff857a18bb -[NSTextInputContext handleEvent:] + 620
    23  AppKit                              0x00007fff857a15e5 -[NSView interpretKeyEvents:] + 186
    24  AppKit                              0x00007fff857a13be -[NSTextView keyDown:] + 819
    25  AppKit                              0x00007fff8571306f -[NSWindow sendEvent:] + 8769
    26  AppKit                              0x00007fff85647a86 -[NSApplication sendEvent:] + 4719
    27  AppKit                              0x00007fff855de4da -[NSApplication run] + 474
    28  AppKit                              0x00007fff855d71a8 NSApplicationMain + 364
*/

HERE IS MY SOURCE CODE:


EDIT: TOO LONG: Here is the line that is causing the problem:
// Otherwise a key word was found, lets go colorize it

                // Setup a temporary range to reflect the actual position of the string within the text view
                // by applying the offset of our current scanning location to the location of the key word
                fooRange.location = found.location;
                fooRange.length = found.length;
                fooRange.location += last_location;

                // Work around for an odd bug that only occurs occasionally
                if (![[string substringWithRange: fooRange] isEqual: keyWord]) {
                    fooRange.location++;
                }

and my dictionary:


        [[textview textStorage] setDelegate:self];
    keyWords = [[NSMutableDictionary alloc] init];
    NSColor *blue = [NSColor colorWithCalibratedRed:0 green:0 blue:0.95 alpha:1];
    NSColor *green= [NSColor colorWithCalibratedRed:0 green:0.65 blue:0 alpha:1];
    NSColor *red =  [NSColor colorWithCalibratedRed:0.90 green:0 blue:0 alpha:1];
    NSColor *tBlue =[NSColor blueColor];
    [keyWords setObject:blue forKey:@"abs"];
    [keyWords setObject:blue forKey:@"sin"];
    [keyWords setObject:blue forKey:@"cos"];
    [keyWords setObject:blue forKey:@"pow"];
    [keyWords setObject:blue forKey:@"rand"];
    [keyWords setObject:tBlue forKey:@"int"];
    [keyWords setObject:tBlue forKey:@"float"];
    [keyWords setObject:tBlue forKey:@"double"];
    [keyWords setObject:tBlue forKey:@"BOOL"];
    [keyWords setObject:red forKey:@"debug"];
    [keyWords setObject:red forKey:@"quote-color"];
    [keyWords setObject:@"\"" forKey:@"quote-open"];
    [keyWords setObject:@"\"" forKey:@"quote-close"];
    [keyWords setObject:green forKey:@"comment-color"];


Lacking clues, this is a wild guess.

            fooRange.location = found.location;
            fooRange.length = found.length;
            fooRange.location += last_location;

If the location is within length of the end of the string, that last line is quite effectively going to push the location beyond the end of the string and cause a range exception.


Ok, so you know where the error is occurring, and you have a very clear error message ("Range or index out of bounds").

The next step would be to look at the relevant values (fooRange.location, found.location, fooRange.length, found.length, last_location, and [string length]) and see why the range extends beyond the end of the string. Your logic/arithmetic for computing the range may be incorrect.

You can examine the values either by printing them out (NSLog) or by putting a breakpoint there and running with the debugger.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜