source: other-projects/rsyntax-textarea/devel-packages/jflex-1.4.3/examples/java/unicode.flex@ 25584

Last change on this file since 25584 was 25584, checked in by davidb, 12 years ago

Initial cut an a text edit area for GLI that supports color syntax highlighting

File size: 3.5 KB
Line 
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Copyright (C) 1998-2009 Gerwin Klein <[email protected]> *
3 * All rights reserved. *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License. See the file *
7 * COPYRIGHT for more information. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License along *
15 * with this program; if not, write to the Free Software Foundation, Inc., *
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
17 * *
18 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
19
20
21/* §3.3 of the Java Language Specification :
22
23UnicodeInputCharacter:
24
25 UnicodeEscape
26
27 RawInputCharacter
28
29 UnicodeEscape:
30
31 \ UnicodeMarker HexDigit HexDigit HexDigit HexDigit
32
33 UnicodeMarker:
34
35 u
36
37 UnicodeMarker u
38
39 RawInputCharacter:
40
41 any Unicode character
42
43 HexDigit: one of
44
45 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
46
47only an even number of '\' is eligible to start a Unicode escape sequence
48
49*/
50
51import java.io.*;
52
53%%
54
55%public
56%final
57%class UnicodeEscapes
58%extends FilterReader
59
60%int
61%function read
62
63%switch
64%16bit
65
66UnicodeEscape = {UnicodeMarker} {HexDigit} {4}
67UnicodeMarker = "u"+
68HexDigit = [0-9a-fA-F]
69
70%state DIGITS
71
72%init{
73 super(in);
74%init}
75
76%{
77 private boolean even;
78
79 private int value() {
80 int r = 0;
81
82 for (int k = zzMarkedPos-4; k < zzMarkedPos; k++) {
83 int c = zzBuffer[k];
84
85 if (c >= 'a')
86 c-= 'a'-10;
87 else if (c >= 'A')
88 c-= 'A'-10;
89 else
90 c-= '0';
91
92 r <<= 4;
93 r += c;
94 }
95
96 return r;
97 }
98
99 public int read(char cbuf[], int off, int len) throws IOException {
100 if ( !ready() ) return -1;
101
102 len+= off;
103
104 for (int i=off; i<len; i++) {
105 int c = read();
106
107 if (c < 0)
108 return i-off;
109 else
110 cbuf[i] = (char) c;
111 }
112
113 return len-off;
114 }
115
116 public boolean markSupported() {
117 return false;
118 }
119
120 public boolean ready() throws IOException {
121 return !zzAtEOF && (zzCurrentPos < zzEndRead || zzReader.ready());
122 }
123
124%}
125
126%%
127
128<YYINITIAL> {
129 \\ { even = false; return '\\'; }
130 \\ / \\ { even = !even; return '\\'; }
131 \\ / "u" {
132 if (even) {
133 even = false;
134 return '\\';
135 }
136 else
137 yybegin(DIGITS);
138 }
139 .|\n { return zzBuffer[zzStartRead]; }
140
141 <<EOF>> { return -1; }
142}
143
144<DIGITS> {
145 {UnicodeEscape} { yybegin(YYINITIAL); return value(); }
146 .|\n { throw new Error("incorrect Unicode escape"); }
147
148 <<EOF>> { throw new Error("EOF in Unicode escape"); }
149}
Note: See TracBrowser for help on using the repository browser.