The R Project SVN R-packages

Rev

Rev 5993 | Rev 5998 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5993 Rev 5995
Line 43... Line 43...
43
/**
43
/**
44
 * Include all the extern variables and prototypes required for flex (used for syntax highlighting)
44
 * Include all the extern variables and prototypes required for flex (used for syntax highlighting)
45
 */
45
 */
46
 
46
 
47
#import "RScriptEditorTokens.h"
47
#import "RScriptEditorTokens.h"
-
 
48
#import "RdScriptEditorTokens.h"
48
 
49
 
-
 
50
// R lexer
49
extern NSUInteger yylex();
51
extern NSUInteger yylex();
50
extern NSUInteger yyuoffset, yyuleng;
52
extern NSUInteger yyuoffset, yyuleng;
51
typedef struct yy_buffer_state *YY_BUFFER_STATE;
53
typedef struct yy_buffer_state *YY_BUFFER_STATE;
52
void yy_switch_to_buffer(YY_BUFFER_STATE);
54
void yy_switch_to_buffer(YY_BUFFER_STATE);
53
YY_BUFFER_STATE yy_scan_string (const char *);
55
YY_BUFFER_STATE yy_scan_string (const char *);
54
 
56
 
-
 
57
// Rd lexer
-
 
58
extern NSUInteger rdlex();
-
 
59
typedef struct rd_buffer_state *RD_BUFFER_STATE;
-
 
60
void rd_switch_to_buffer(RD_BUFFER_STATE);
-
 
61
RD_BUFFER_STATE rd_scan_string (const char *);
-
 
62
 
-
 
63
 
55
#pragma mark -
64
#pragma mark -
56
#pragma mark attribute definition 
65
#pragma mark attribute definition 
57
 
66
 
58
#define kAPlinked      @"Linked" // attribute for a via auto-pair inserted char
67
#define kAPlinked      @"Linked" // attribute for a via auto-pair inserted char
59
#define kAPval         @"linked"
68
#define kAPval         @"linked"
Line 620... Line 629...
620
	// mainly for suppressing auto-pairing in 
629
	// mainly for suppressing auto-pairing in 
621
	[theTextStorage removeAttribute:kLEXToken range:textRange];
630
	[theTextStorage removeAttribute:kLEXToken range:textRange];
622
 
631
 
623
	// initialise flex
632
	// initialise flex
624
	yyuoffset = textRange.location; yyuleng = 0;
633
	yyuoffset = textRange.location; yyuleng = 0;
625
	yy_switch_to_buffer(yy_scan_string(NSStringUTF8String([selfstr substringWithRange:textRange])));
-
 
626
 
634
 
-
 
635
	if([[self delegate] isRdDocument]) {
-
 
636
 
-
 
637
			rd_switch_to_buffer(rd_scan_string(NSStringUTF8String([selfstr substringWithRange:textRange])));
-
 
638
 
627
	// now loop through all the tokens
639
			// now loop through all the tokens
628
	while ((token = yylex())) {
640
			while ((token = rdlex())) {
629
// NSLog(@"t %d", token);
641
		// NSLog(@"t %d", token);
630
		switch (token) {
642
				switch (token) {
631
			case RPT_SINGLE_QUOTED_TEXT:
-
 
632
			case RPT_DOUBLE_QUOTED_TEXT:
643
					case RDPT_COMMENT:
633
			    tokenColor = shColorString;
644
					    tokenColor = shColorComment;
634
			    break;
645
					    break;
635
			case RPT_RESERVED_WORD:
646
					case RDPT_SECTION:
636
			    tokenColor = shColorKeyword;
647
					    tokenColor = shColorNumber;
637
			    break;
648
					    break;
638
			case RPT_NUMERIC:
649
					case RDPT_MACRO_ARG:
639
				tokenColor = shColorNumber;
650
					    tokenColor = shColorKeyword;
640
				break;
651
					    break;
641
			case RPT_BACKTICK_QUOTED_TEXT:
652
					case RDPT_MACRO_GEN:
642
			    tokenColor = shColorString;
653
					    tokenColor = shColorIdentifier;
643
			    break;
654
					    break;
644
			case RPT_COMMENT:
655
					default:
645
			    tokenColor = shColorComment;
656
					    tokenColor = shColorNormal;
646
			    break;
657
				}
-
 
658
 
647
			case RPT_VARIABLE:
659
				tokenRange = NSMakeRange(yyuoffset, yyuleng);
-
 
660
 
-
 
661
				// make sure that tokenRange is valid (and therefore within textRange)
-
 
662
				// otherwise a bug in the lex code could cause the the TextView to crash
-
 
663
				// NOTE Disabled for testing purposes for speed it up
648
			    tokenColor = shColorIdentifier;
664
				tokenRange = NSIntersectionRange(tokenRange, textRange);
649
			    break;
665
				if (!tokenRange.length) continue;
-
 
666
 
-
 
667
				// If the current token is marked as SQL keyword, uppercase it if required.
650
			case RPT_WHITESPACE:
668
				tokenEnd = NSMaxRange(tokenRange) - 1;
-
 
669
 
-
 
670
				NSMutableAttributedStringAddAttributeValueRange(theTextStorage, NSForegroundColorAttributeName, tokenColor, tokenRange);
-
 
671
 
-
 
672
				// Add an attribute to be used in the auto-pairing (keyDown:)
-
 
673
				// to disable auto-pairing if caret is inside of any token found by lex.
-
 
674
				// For discussion: maybe change it later (only for quotes not keywords?)
651
			    continue;
675
				if(token < 6)
-
 
676
					NSMutableAttributedStringAddAttributeValueRange(theTextStorage, kLEXToken, kLEXTokenValue, tokenRange);
-
 
677
 
-
 
678
 
-
 
679
				// Add an attribute to be used to distinguish quotes from keywords etc.
652
			    break;
680
				// used e.g. in completion suggestions
653
			default:
681
				else if(token < 4)
654
			    tokenColor = shColorNormal;
682
					NSMutableAttributedStringAddAttributeValueRange(theTextStorage, kQuote, kQuoteValue, tokenRange);
-
 
683
 
655
		}
684
			}
-
 
685
 
-
 
686
	} else {
656
 
687
 
657
		tokenRange = NSMakeRange(yyuoffset, yyuleng);
688
		yy_switch_to_buffer(yy_scan_string(NSStringUTF8String([selfstr substringWithRange:textRange])));
658
 
689
 
659
		// make sure that tokenRange is valid (and therefore within textRange)
690
		// now loop through all the tokens
-
 
691
		while ((token = yylex())) {
-
 
692
	// NSLog(@"t %d", token);
-
 
693
			switch (token) {
-
 
694
				case RPT_SINGLE_QUOTED_TEXT:
-
 
695
				case RPT_DOUBLE_QUOTED_TEXT:
-
 
696
				    tokenColor = shColorString;
-
 
697
				    break;
-
 
698
				case RPT_RESERVED_WORD:
660
		// otherwise a bug in the lex code could cause the the TextView to crash
699
				    tokenColor = shColorKeyword;
-
 
700
				    break;
-
 
701
				case RPT_NUMERIC:
-
 
702
					tokenColor = shColorNumber;
-
 
703
					break;
-
 
704
				case RPT_BACKTICK_QUOTED_TEXT:
-
 
705
				    tokenColor = shColorString;
-
 
706
				    break;
-
 
707
				case RPT_COMMENT:
661
		// NOTE Disabled for testing purposes for speed it up
708
				    tokenColor = shColorComment;
-
 
709
				    break;
-
 
710
				case RPT_VARIABLE:
662
		tokenRange = NSIntersectionRange(tokenRange, textRange);
711
				    tokenColor = shColorIdentifier;
-
 
712
				    break;
-
 
713
				case RPT_WHITESPACE:
-
 
714
				    continue;
-
 
715
				    break;
-
 
716
				default:
663
		if (!tokenRange.length) continue;
717
				    tokenColor = shColorNormal;
-
 
718
			}
664
 
719
 
665
		// If the current token is marked as SQL keyword, uppercase it if required.
-
 
666
		tokenEnd = NSMaxRange(tokenRange) - 1;
720
			tokenRange = NSMakeRange(yyuoffset, yyuleng);
667
 
721
 
-
 
722
			// make sure that tokenRange is valid (and therefore within textRange)
-
 
723
			// otherwise a bug in the lex code could cause the the TextView to crash
-
 
724
			// NOTE Disabled for testing purposes for speed it up
-
 
725
			tokenRange = NSIntersectionRange(tokenRange, textRange);
-
 
726
			if (!tokenRange.length) continue;
-
 
727
 
-
 
728
			// If the current token is marked as SQL keyword, uppercase it if required.
-
 
729
			tokenEnd = NSMaxRange(tokenRange) - 1;
-
 
730
 
668
		NSMutableAttributedStringAddAttributeValueRange(theTextStorage, NSForegroundColorAttributeName, tokenColor, tokenRange);
731
			NSMutableAttributedStringAddAttributeValueRange(theTextStorage, NSForegroundColorAttributeName, tokenColor, tokenRange);
669
		
732
		
670
		// Add an attribute to be used in the auto-pairing (keyDown:)
733
			// Add an attribute to be used in the auto-pairing (keyDown:)
671
		// to disable auto-pairing if caret is inside of any token found by lex.
734
			// to disable auto-pairing if caret is inside of any token found by lex.
672
		// For discussion: maybe change it later (only for quotes not keywords?)
735
			// For discussion: maybe change it later (only for quotes not keywords?)
673
		if(token < 6)
736
			if(token < 6)
674
			NSMutableAttributedStringAddAttributeValueRange(theTextStorage, kLEXToken, kLEXTokenValue, tokenRange);
737
				NSMutableAttributedStringAddAttributeValueRange(theTextStorage, kLEXToken, kLEXTokenValue, tokenRange);
675
		
738
		
676
 
739
 
677
		// Add an attribute to be used to distinguish quotes from keywords etc.
740
			// Add an attribute to be used to distinguish quotes from keywords etc.
678
		// used e.g. in completion suggestions
741
			// used e.g. in completion suggestions
679
		else if(token < 4)
742
			else if(token < 4)
680
			NSMutableAttributedStringAddAttributeValueRange(theTextStorage, kQuote, kQuoteValue, tokenRange);
743
				NSMutableAttributedStringAddAttributeValueRange(theTextStorage, kQuote, kQuoteValue, tokenRange);
681
		
744
		
682
 
745
 
-
 
746
		}
683
	}
747
	}
684
 
748
 
685
	// set current textColor to the color of the caret's position - 1
749
	// set current textColor to the color of the caret's position - 1
686
	// to try to suppress writing in normalColor before syntax highlighting 
750
	// to try to suppress writing in normalColor before syntax highlighting 
687
	NSUInteger ix = [self selectedRange].location;
751
	NSUInteger ix = [self selectedRange].location;