The R Project SVN R-packages

Rev

Rev 6231 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5705 urbaneks 1
%{
2
 
3
/*
4
 *  R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"
5
 *  
6
 *  R.app Copyright notes:
7
 *                     Copyright (C) 2004-5  The R Foundation
8
 *                     written by Stefano M. Iacus and Simon Urbanek
9
 *
10
 *                  
11
 *  R Copyright notes:
12
 *                     Copyright (C) 1995-1996   Robert Gentleman and Ross Ihaka
13
 *                     Copyright (C) 1998-2001   The R Development Core Team
14
 *                     Copyright (C) 2002-2004   The R Foundation
15
 *
16
 *  This program is free software; you can redistribute it and/or modify
17
 *  it under the terms of the GNU General Public License as published by
18
 *  the Free Software Foundation; either version 2 of the License, or
19
 *  (at your option) any later version.
20
 *
21
 *  This program is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  A copy of the GNU General Public License is available via WWW at
27
 *  http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
28
 *  writing to the Free Software Foundation, Inc., 59 Temple Place,
29
 *  Suite 330, Boston, MA  02111-1307  USA.
30
 *
31
 *  RScriptEditorTokens.l
32
 *
33
 *  Created by Hans-J. Bibiko on 15/02/2011.
34
 *
35
 *  Flex parser for syntax highlighting R code.
36
 *
37
 */
38
 
39
#import "RScriptEditorTokens.h"
40
 
41
size_t utf8strlen(const char * _s);
42
size_t yyuoffset, yyuleng;
43
 
44
//keep track of the current utf-8 character (not byte) offset and token length
45
#define YY_USER_ACTION { yyuoffset += yyuleng; yyuleng = utf8strlen(yytext); }
6432 urbaneks 46
//ignore the output of unmatched characters
47
#define ECHO {}
5705 urbaneks 48
%}
49
%option noyywrap
50
%option nounput
51
%option case-sensitive
52
 
53
s			[ \t\n\r]+
5741 urbaneks 54
numeric		((0(x|X)[0-9a-fA-F]*)|([+-]?(([0-9]+\.[0-9]*)|([0-9]*\.[0-9]+)|([0-9]+))(e[+-]?[0-9]+)?(i|L)?))
5705 urbaneks 55
alpha		[a-zA-Z_\.À-゚]
56
ops			"+"|"-"|"*"|"/"|^|>|>=|<|<=|==|!=|!|&|\||\|\||~|$|:|@
57
fun			[ \t\n\r]*\(
58
decl		=|<-|<<-|->|-->
59
word		[a-zA-Z_\.0-9À-゚@]
60
variable	[a-zA-ZÀ-゚\.][a-zA-Z_\.0-9À-゚]*
61
nonword		[^a-zA-Z_0-9À-゚#\n\t\r\.]
62
keyword		(next|in|N(ULL|aN|A(_(c(haracter_|omplex_)|integer_|real_))?)|T(RUE)?|Inf|else|repeat|F(ALSE)?|break)
63
functions	(if|f(or|unction)|while)
64
 
65
%x equation
66
%x varequation
67
%%
68
 
69
 
70
\"([^"\\]|\\(.|[\n\r]))*\"?			{ return RPT_DOUBLE_QUOTED_TEXT;   }      /* double quoted strings          */
71
'([^'\\]|\\(.|[\n\r]))*'?			{ return RPT_SINGLE_QUOTED_TEXT;   }      /* single quoted strings          */
72
`[^`]*`?							{ return RPT_BACKTICK_QUOTED_TEXT; }      /* backtick quoted string         */
73
 
74
 
75
#[^\n\r]*(\n|\r)?			{ return RPT_COMMENT; }								/* # Comments                     */
76
 
77
{keyword}					{ return RPT_RESERVED_WORD; }						/* all R reserved keywords        */
78
{functions}/{fun}			{ return RPT_RESERVED_WORD; }						/* all R reserved functions       */
79
 
80
{variable}/{fun}			{ return RPT_OTHER; }								/* non-reserved functions         */
81
 
6432 urbaneks 82
{variable}/{decl}			{ BEGIN(varequation); return RPT_VARIABLE; }		/* R variables before operator    */
83
<varequation>{decl}			{ BEGIN(INITIAL); return RPT_OTHER; }       		
84
 
5705 urbaneks 85
{variable}/{ops}			{ BEGIN(varequation); return RPT_VARIABLE; }		/* R variables before operator    */
86
<varequation>{ops}			{ BEGIN(INITIAL); return RPT_OTHER; }       		
87
{variable}					{ return RPT_VARIABLE; }							/* R variables */
88
 
89
{numeric}/{ops}				{ BEGIN(equation); return RPT_NUMERIC; }			/* numeric before operator        */
90
<equation>{ops}				{ BEGIN(INITIAL); return RPT_OTHER; }				/* set operator after a numeric   */
5741 urbaneks 91
{numeric}					{ return RPT_NUMERIC; }								/* single numeric value           */
5705 urbaneks 92
{numeric}/{alpha}			{ return RPT_WORD; }								/* catch numeric followed by char */
93
 
5713 urbaneks 94
{ops}						{ return RPT_OPERATOR; }							/* all operators                  */
95
{decl}						{ return RPT_DECLARATION; }							/* all declarations               */
6231 bibiko 96
{s}+						{  }												/* ignore spaces                  */
5705 urbaneks 97
 
98
{word}+						{ return RPT_WORD; }								/* return any word                */
99
 
100
{nonword}					{ return RPT_OTHER; }								/* return anything else           */
101
 
102
 
103
 
104
<<EOF>>   						{
105
	BEGIN(INITIAL);   /* make sure we return to initial state when finished! */
106
	yy_delete_buffer(YY_CURRENT_BUFFER);
107
	return 0;
108
}
109
%%
110
 
111
#define ONEMASK ((size_t)(-1) / 0xFF)
112
// adapted from http://www.daemonology.net/blog/2008-06-05-faster-utf8-strlen.html
113
size_t utf8strlen(const char * _s)
114
{
115
	const char * s;
116
	size_t count = 0;
117
	size_t u;
118
	unsigned char b;
119
 
120
	/* Handle any initial misaligned bytes. */
121
	for (s = _s; (uintptr_t)(s) & (sizeof(size_t) - 1); s++) {
122
		b = *s;
123
 
124
		/* Exit if we hit a zero byte. */
125
		if (b == '\0')
126
			goto done;
127
 
128
		/* Is this byte NOT the first byte of a character? */
129
		count += (b >> 7) & ((~b) >> 6);
130
	}
131
 
132
	/* Handle complete blocks. */
133
	for (; ; s += sizeof(size_t)) {
134
		/* Prefetch 256 bytes ahead. */
135
		__builtin_prefetch(&s[256], 0, 0);
136
 
137
		/* Grab 4 or 8 bytes of UTF-8 data. */
138
		u = *(size_t *)(s);
139
 
140
		/* Exit the loop if there are any zero bytes. */
141
		if ((u - ONEMASK) & (~u) & (ONEMASK * 0x80))
142
			break;
143
 
144
		/* Count bytes which are NOT the first byte of a character. */
145
		u = ((u & (ONEMASK * 0x80)) >> 7) & ((~u) >> 6);
146
		count += (u * ONEMASK) >> ((sizeof(size_t) - 1) * 8);
147
	}
148
 
149
	/* Take care of any left-over bytes. */
150
	for (; ; s++) {
151
		b = *s;
152
 
153
		/* Exit if we hit a zero byte. */
154
		if (b == '\0')
155
			break;
156
 
157
		/* Is this byte NOT the first byte of a character? */
158
		count += (b >> 7) & ((~b) >> 6);
159
	}
160
 
161
done:
162
	return ((s - _s) - count);
163
}
164
 
165
/* un-optimized keywords:
166
break
167
else
168
F
169
FALSE
170
in
171
Inf
172
NA
173
NaN
174
NA_character_
175
NA_complex_
176
NA_integer_
177
NA_real_
178
next
179
NULL
180
repeat
181
T
182
TRUE
183
*/
184
 
185
/* un-optimized function keywords:
186
for
187
function
188
if
189
while
190
*/