KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > NBSLanguage


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.languages;
21
22 import org.netbeans.api.languages.ParseException;
23 import org.netbeans.api.languages.ASTToken;
24 import java.util.Arrays JavaDoc;
25 import org.netbeans.modules.languages.Feature;
26 import org.netbeans.modules.languages.Selector;
27 import org.netbeans.modules.languages.parser.LLSyntaxAnalyser;
28 import org.netbeans.api.languages.ParseException;
29 import org.netbeans.modules.languages.parser.Parser;
30 import org.netbeans.modules.languages.parser.Pattern;
31 import org.netbeans.api.languages.ASTToken;
32
33     
34     
35 /**
36  *
37  * @author Jan Jancura
38  */

39 public class NBSLanguage {
40     
41     static final String JavaDoc NBS = "text/x-nbs";
42     static final ASTToken STRING = ASTToken.create (NBS, "string", null, 0);
43     static final ASTToken OPERATOR = ASTToken.create (NBS, "operator", null, 0);
44     static final ASTToken KEYWORD = ASTToken.create (NBS, "keyword", null, 0);
45     static final ASTToken IDENTIFIER = ASTToken.create (NBS, "identifier", null, 0);
46     static final ASTToken COMMENT = ASTToken.create (NBS, "comment", null, 0);
47     static final ASTToken WHITESPACE = ASTToken.create (NBS, "whitespace", null, 0);
48     static final ASTToken BRACE = ASTToken.create (NBS, "operator", "{", 0);
49     static final ASTToken BRACE2 = ASTToken.create (NBS, "operator", "}", 0);
50     static final ASTToken BRACKET = ASTToken.create (NBS, "operator", "[", 0);
51     static final ASTToken BRACKET2 = ASTToken.create (NBS, "operator", "]", 0);
52     static final ASTToken KEYWORD_TOKEN = ASTToken.create (NBS, "keyword", "TOKEN", 0);
53     static final ASTToken COLON = ASTToken.create (NBS, "operator", ":", 0);
54     static final ASTToken SEMICOLON = ASTToken.create (NBS, "operator", ";", 0);
55     static final ASTToken EQUAL = ASTToken.create (NBS, "operator", "=", 0);
56     static final ASTToken MINUS = ASTToken.create (NBS, "operator", "-", 0);
57     static final ASTToken DOLLAR = ASTToken.create (NBS, "operator", "$", 0);
58     static final ASTToken UPP = ASTToken.create (NBS, "operator", "^", 0);
59     static final ASTToken OR = ASTToken.create (NBS, "operator", "|", 0);
60     static final ASTToken COMMA = ASTToken.create (NBS, "operator", ",", 0);
61     static final ASTToken DOT = ASTToken.create (NBS, "operator", ".", 0);
62     static final ASTToken PLUS = ASTToken.create (NBS, "operator", "+", 0);
63     static final ASTToken MULTIPLY = ASTToken.create (NBS, "operator", "*", 0);
64     static final ASTToken QUESTION = ASTToken.create (NBS, "operator", "?", 0);
65     static final ASTToken DOUBLE_QUOTES = ASTToken.create (NBS, "operator", "\"", 0);
66     static final ASTToken LT = ASTToken.create (NBS, "operator", "<", 0);
67     static final ASTToken GT = ASTToken.create (NBS, "operator", ">", 0);
68     static final ASTToken PARENTHESIS = ASTToken.create (NBS, "operator", "(", 0);
69     static final ASTToken PARENTHESIS2 = ASTToken.create (NBS, "operator", ")", 0);
70     
71     private static Parser nbsParser;
72     
73     static Language getNBSLanguage () throws ParseException {
74         Language l = new Language (NBS);
75         l.addToken (
76             Parser.DEFAULT_STATE,
77             "keyword",
78             Pattern.create (
79                 "'ACTION' |" +
80                 "'AST' |" +
81                 "'BRACE' |" +
82                 "'COLOR' |" +
83                 "'COMPLETE' |" +
84                 "'COMPLETION' |" +
85                 "'FOLD' |" +
86                 "'FORMAT' |" +
87                 "'HYPERLINK' |" +
88                 "'IMPORT' |" +
89                 "'INDENT' |" +
90                 "'MARK' | " +
91                 "'NAVIGATOR' |" +
92                 "'PARSE' |" +
93                 "'PROPERTIES' |" +
94                 "'REFORMAT' |" +
95                 "'SKIP' |" +
96                 "'STORE' |" +
97                 "'TOKEN' |" +
98                 "'TOOLTIP'"
99             ),
100             Parser.DEFAULT_STATE,
101             null
102         );
103         l.addToken (
104             Parser.DEFAULT_STATE,
105             "identifier",
106             Pattern.create (
107                 "['a'-'z' 'A'-'Z'] ['a'-'z' 'A'-'Z' '0'-'9' '_']*"
108             ),
109             Parser.DEFAULT_STATE,
110             null
111         );
112         l.addToken (
113             Parser.DEFAULT_STATE,
114             "operator",
115             Pattern.create (
116                 "':' | '*' | '?' | '+' | '-' | '[' | ']' | '<' | " +
117                 "'>' | '^' | '|' | '{' | '}' | '(' | ')' | ',' | " +
118                 "'=' | ';' | '.' | '$'"
119             ),
120             Parser.DEFAULT_STATE,
121             null
122         );
123         l.addToken (
124             Parser.DEFAULT_STATE,
125             "string",
126             Pattern.create (
127                 "'\\\"' ([^'\\\"' '\\\\' '\\r' '\\n'] |" +
128                 "('\\\\' ['r' 'n' 't' '\\\\' '\\\'' '\\\"']) )* '\\\"'"
129             ),
130             Parser.DEFAULT_STATE,
131             null
132         );
133         l.addToken (
134             Parser.DEFAULT_STATE,
135             "string",
136             Pattern.create (
137                 "'\\\'' ([^'\\\'' '\\\\' '\\r' '\\n'] |" +
138                 "('\\\\' ['r' 'n' 't' '\\\\' '\\\'' '\\\"']) )* '\\\''"
139             ),
140             Parser.DEFAULT_STATE,
141             null
142         );
143         l.addToken (
144             Parser.DEFAULT_STATE,
145             "comment",
146             Pattern.create ("'#' [^'\\n' '\\r']* ['\\n' '\\r']+"),
147             Parser.DEFAULT_STATE,
148             null
149         );
150         l.addToken (
151             Parser.DEFAULT_STATE,
152             "whitespace",
153             Pattern.create ("['\\n' '\\r' ' ' '\\t']+"),
154             Parser.DEFAULT_STATE,
155             null
156         );
157         l.addRule (rule ("S", new Object JavaDoc[] {"token", "S"}));
158         l.addRule (rule ("S", new Object JavaDoc[] {"tokenState", "S"}));
159         l.addRule (rule ("S", new Object JavaDoc[] {"grammarRule", "S"}));
160         l.addRule (rule ("S", new Object JavaDoc[] {"command", "S"}));
161         l.addRule (rule ("S", new Object JavaDoc[] {}));
162         
163         l.addRule (rule ("tokenState", new Object JavaDoc[] {"state", "tokenState1"}));
164         l.addRule (rule ("tokenState1", new Object JavaDoc[] {COLON, "token"}));
165         l.addRule (rule ("tokenState1", new Object JavaDoc[] {BRACE, "tokenGroup"}));
166         l.addRule (rule ("token", new Object JavaDoc[] {KEYWORD_TOKEN, COLON, IDENTIFIER, COLON, "token2"}));
167         l.addRule (rule ("token2", new Object JavaDoc[] {PARENTHESIS, "regularExpression", PARENTHESIS2, "token3"}));
168         l.addRule (rule ("token2", new Object JavaDoc[] {BRACE, "properties", BRACE2}));
169         l.addRule (rule ("token3", new Object JavaDoc[] {COLON, "state"}));
170         l.addRule (rule ("token3", new Object JavaDoc[] {}));
171         l.addRule (rule ("state", new Object JavaDoc[] {LT, IDENTIFIER, GT}));
172         l.addRule (rule ("tokenGroup", new Object JavaDoc[] {"tokensInGroup", BRACE2}));
173         l.addRule (rule ("tokensInGroup", new Object JavaDoc[] {"token", "tokensInGroup"}));
174         l.addRule (rule ("tokensInGroup", new Object JavaDoc[] {}));
175         
176         l.addRule (rule ("regularExpression", new Object JavaDoc[] {"reChoice", "regularExpression1"}));
177         l.addRule (rule ("regularExpression1", new Object JavaDoc[] {OR, "reChoice", "regularExpression1"}));
178         l.addRule (rule ("regularExpression1", new Object JavaDoc[] {}));
179         l.addRule (rule ("reChoice", new Object JavaDoc[] {"rePart", "reChoice1"}));
180         l.addRule (rule ("reChoice1", new Object JavaDoc[] {"rePart", "reChoice1"}));
181         l.addRule (rule ("reChoice1", new Object JavaDoc[] {}));
182         l.addRule (rule ("rePart", new Object JavaDoc[] {STRING, "rePartOperatorOrMinus"}));
183         l.addRule (rule ("rePart", new Object JavaDoc[] {DOT, "rePartOperator"}));
184         l.addRule (rule ("rePart", new Object JavaDoc[] {"reClass", "rePartOperator"}));
185         l.addRule (rule ("rePart", new Object JavaDoc[] {PARENTHESIS, "regularExpression", PARENTHESIS2, "rePartOperator"}));
186         l.addRule (rule ("rePartOperator", new Object JavaDoc[] {}));
187         l.addRule (rule ("rePartOperator", new Object JavaDoc[] {PLUS}));
188         l.addRule (rule ("rePartOperator", new Object JavaDoc[] {QUESTION}));
189         l.addRule (rule ("rePartOperator", new Object JavaDoc[] {MULTIPLY}));
190         l.addRule (rule ("rePartOperatorOrMinus", new Object JavaDoc[] {MINUS, STRING}));
191         l.addRule (rule ("rePartOperatorOrMinus", new Object JavaDoc[] {"rePartOperator"}));
192         l.addRule (rule ("reClass", new Object JavaDoc[] {BRACKET, "reInClassNegation", "reInClass", BRACKET2}));
193         l.addRule (rule ("reInClassNegation", new Object JavaDoc[] {UPP}));
194         l.addRule (rule ("reInClassNegation", new Object JavaDoc[] {}));
195         l.addRule (rule ("reInClass", new Object JavaDoc[] {STRING, "reInClassMinus", "reInClass1"}));
196         l.addRule (rule ("reInClass1", new Object JavaDoc[] {STRING, "reInClassMinus", "reInClass1"}));
197         l.addRule (rule ("reInClass1", new Object JavaDoc[] {}));
198         l.addRule (rule ("reInClassMinus", new Object JavaDoc[] {MINUS, STRING}));
199         l.addRule (rule ("reInClassMinus", new Object JavaDoc[] {}));
200         
201         l.addRule (rule ("grammarRule", new Object JavaDoc[] {IDENTIFIER, EQUAL, "grRightSide", SEMICOLON}));
202         l.addRule (rule ("grRightSide", new Object JavaDoc[] {"grChoice", "grRightSide1"}));
203         l.addRule (rule ("grRightSide1", new Object JavaDoc[] {OR, "grChoice", "grRightSide1"}));
204         l.addRule (rule ("grRightSide1", new Object JavaDoc[] {}));
205         l.addRule (rule ("grChoice", new Object JavaDoc[] {"grPart", "grChoice"}));
206         l.addRule (rule ("grChoice", new Object JavaDoc[] {}));
207         l.addRule (rule ("grPart", new Object JavaDoc[] {IDENTIFIER, "grOperator"}));
208         l.addRule (rule ("grPart", new Object JavaDoc[] {"tokenDef", "grOperator"}));
209         l.addRule (rule ("grPart", new Object JavaDoc[] {STRING, "grOperator"}));
210         l.addRule (rule ("grPart", new Object JavaDoc[] {BRACKET, "grRightSide", BRACKET2}));
211         l.addRule (rule ("grPart", new Object JavaDoc[] {PARENTHESIS, "grRightSide", PARENTHESIS2, "grOperator"}));
212         l.addRule (rule ("grOperator", new Object JavaDoc[] {PLUS}));
213         l.addRule (rule ("grOperator", new Object JavaDoc[] {MULTIPLY}));
214         l.addRule (rule ("grOperator", new Object JavaDoc[] {QUESTION}));
215         l.addRule (rule ("grOperator", new Object JavaDoc[] {}));
216         l.addRule (rule ("tokenDef", new Object JavaDoc[] {LT, IDENTIFIER, "tokenDef1", GT}));
217         l.addRule (rule ("tokenDef1", new Object JavaDoc[] {COMMA, STRING}));
218         l.addRule (rule ("tokenDef1", new Object JavaDoc[] {}));
219         
220         l.addRule (rule ("command", new Object JavaDoc[] {KEYWORD, "command0"}));
221         l.addRule (rule ("command0", new Object JavaDoc[] {COLON, "selector", "command1"}));
222         l.addRule (rule ("command0", new Object JavaDoc[] {"value"}));
223         l.addRule (rule ("command1", new Object JavaDoc[] {COLON, "value"}));
224         l.addRule (rule ("command1", new Object JavaDoc[] {}));
225         l.addRule (rule ("value", new Object JavaDoc[] {"class"}));
226         l.addRule (rule ("value", new Object JavaDoc[] {STRING}));
227         l.addRule (rule ("value", new Object JavaDoc[] {BRACE, "properties", BRACE2}));
228         l.addRule (rule ("value", new Object JavaDoc[] {PARENTHESIS, "regularExpression", PARENTHESIS2}));
229         l.addRule (rule ("properties", new Object JavaDoc[] {"property", "properties"}));
230         l.addRule (rule ("properties", new Object JavaDoc[] {}));
231         l.addRule (rule ("property", new Object JavaDoc[] {IDENTIFIER, COLON, "propertyValue", SEMICOLON}));
232         l.addRule (rule ("propertyValue", new Object JavaDoc[] {STRING}));
233         l.addRule (rule ("propertyValue", new Object JavaDoc[] {"class"}));
234         l.addRule (rule ("propertyValue", new Object JavaDoc[] {PARENTHESIS, "regularExpression", PARENTHESIS2}));
235         l.addRule (rule ("selector", new Object JavaDoc[] {"class"}));
236         l.addRule (rule ("class", new Object JavaDoc[] {IDENTIFIER, "class1"}));
237         l.addRule (rule ("class1", new Object JavaDoc[] {DOT, IDENTIFIER, "class1"}));
238         l.addRule (rule ("class1", new Object JavaDoc[] {}));
239
240         l.addFeature (Feature.create ("SKIP", Selector.create ("whitespace")));
241         l.addFeature (Feature.create ("SKIP", Selector.create ("comment")));
242         return l;
243     }
244
245     
246     private static LLSyntaxAnalyser.Rule rule (String JavaDoc nt, Object JavaDoc[] right) {
247         return LLSyntaxAnalyser.Rule.create (
248             nt,
249             Arrays.asList (right)
250         );
251     }
252 }
253
Popular Tags