KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > syntax > GrammarSyntaxLexer


1 package org.antlr.works.syntax;
2
3 import org.antlr.works.ate.syntax.generic.ATESyntaxLexer;
4 import org.antlr.works.ate.syntax.misc.ATEToken;
5 import org.antlr.works.syntax.element.ElementToken;
6
7 /*
8
9 [The "BSD licence"]
10 Copyright (c) 2005-2006 Jean Bovet
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
15 are met:
16
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22 3. The name of the author may not be used to endorse or promote products
23 derived from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 */

37
38 public class GrammarSyntaxLexer extends ATESyntaxLexer {
39
40     public static final int TOKEN_REFERENCE = 100;
41     public static final int TOKEN_LABEL = 101;
42     public static final int TOKEN_BLOCK_LABEL = 102;
43     public static final int TOKEN_BLOCK_LIMIT = 103;
44     public static final int TOKEN_REWRITE = 104;
45     public static final int TOKEN_DECL = 105;
46     public static final int TOKEN_OPEN_DOUBLE_ANGLE = 106;
47     public static final int TOKEN_CLOSE_DOUBLE_ANGLE = 107;
48     public static final int TOKEN_INTERNAL_REF = 108; // i.e. { ... $type ... }
49

50     @Override JavaDoc
51     protected ATEToken customMatch() {
52         if(c0 == '@') {
53             return matchID();
54         } else if(c0 == '-' && c1 == '>') {
55             int sp = position;
56             position++;
57             return createNewToken(TOKEN_REWRITE, sp, position);
58         } else if(c0 == '<' && c1 == '<') {
59             int sp = position;
60             position++;
61             return createNewToken(TOKEN_OPEN_DOUBLE_ANGLE, sp, position);
62         } else if(c0 == '>' && c1 == '>') {
63             int sp = position;
64             position++;
65             return createNewToken(TOKEN_CLOSE_DOUBLE_ANGLE, sp, position);
66         } else {
67             return null;
68         }
69     }
70
71     @Override JavaDoc
72     protected ATEToken matchID() {
73         int sp = position;
74         if(c0 == '@') {
75             // This kind of ID can contain ':', for example:
76
// @header::lexer
77
while((isID(c1) || c1 == ':') && nextCharacter()) {
78             }
79         } else {
80             // Match regular ID
81
while(isID(c1) && nextCharacter()) {
82             }
83         }
84
85         return createNewToken(TOKEN_ID, sp);
86     }
87
88     @Override JavaDoc
89     public ATEToken createNewToken(int type, int start, int end,
90                                    int startLineNumber, int endLineNumber,
91                                    int startLineIndex, int endLineIndex)
92     {
93         return new ElementToken(type, start, end, startLineNumber, endLineNumber, startLineIndex, endLineIndex, text);
94     }
95
96
97 }
98
Popular Tags