1 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 ; 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 39 public class NBSLanguage { 40 41 static final String 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 [] {"token", "S"})); 158 l.addRule (rule ("S", new Object [] {"tokenState", "S"})); 159 l.addRule (rule ("S", new Object [] {"grammarRule", "S"})); 160 l.addRule (rule ("S", new Object [] {"command", "S"})); 161 l.addRule (rule ("S", new Object [] {})); 162 163 l.addRule (rule ("tokenState", new Object [] {"state", "tokenState1"})); 164 l.addRule (rule ("tokenState1", new Object [] {COLON, "token"})); 165 l.addRule (rule ("tokenState1", new Object [] {BRACE, "tokenGroup"})); 166 l.addRule (rule ("token", new Object [] {KEYWORD_TOKEN, COLON, IDENTIFIER, COLON, "token2"})); 167 l.addRule (rule ("token2", new Object [] {PARENTHESIS, "regularExpression", PARENTHESIS2, "token3"})); 168 l.addRule (rule ("token2", new Object [] {BRACE, "properties", BRACE2})); 169 l.addRule (rule ("token3", new Object [] {COLON, "state"})); 170 l.addRule (rule ("token3", new Object [] {})); 171 l.addRule (rule ("state", new Object [] {LT, IDENTIFIER, GT})); 172 l.addRule (rule ("tokenGroup", new Object [] {"tokensInGroup", BRACE2})); 173 l.addRule (rule ("tokensInGroup", new Object [] {"token", "tokensInGroup"})); 174 l.addRule (rule ("tokensInGroup", new Object [] {})); 175 176 l.addRule (rule ("regularExpression", new Object [] {"reChoice", "regularExpression1"})); 177 l.addRule (rule ("regularExpression1", new Object [] {OR, "reChoice", "regularExpression1"})); 178 l.addRule (rule ("regularExpression1", new Object [] {})); 179 l.addRule (rule ("reChoice", new Object [] {"rePart", "reChoice1"})); 180 l.addRule (rule ("reChoice1", new Object [] {"rePart", "reChoice1"})); 181 l.addRule (rule ("reChoice1", new Object [] {})); 182 l.addRule (rule ("rePart", new Object [] {STRING, "rePartOperatorOrMinus"})); 183 l.addRule (rule ("rePart", new Object [] {DOT, "rePartOperator"})); 184 l.addRule (rule ("rePart", new Object [] {"reClass", "rePartOperator"})); 185 l.addRule (rule ("rePart", new Object [] {PARENTHESIS, "regularExpression", PARENTHESIS2, "rePartOperator"})); 186 l.addRule (rule ("rePartOperator", new Object [] {})); 187 l.addRule (rule ("rePartOperator", new Object [] {PLUS})); 188 l.addRule (rule ("rePartOperator", new Object [] {QUESTION})); 189 l.addRule (rule ("rePartOperator", new Object [] {MULTIPLY})); 190 l.addRule (rule ("rePartOperatorOrMinus", new Object [] {MINUS, STRING})); 191 l.addRule (rule ("rePartOperatorOrMinus", new Object [] {"rePartOperator"})); 192 l.addRule (rule ("reClass", new Object [] {BRACKET, "reInClassNegation", "reInClass", BRACKET2})); 193 l.addRule (rule ("reInClassNegation", new Object [] {UPP})); 194 l.addRule (rule ("reInClassNegation", new Object [] {})); 195 l.addRule (rule ("reInClass", new Object [] {STRING, "reInClassMinus", "reInClass1"})); 196 l.addRule (rule ("reInClass1", new Object [] {STRING, "reInClassMinus", "reInClass1"})); 197 l.addRule (rule ("reInClass1", new Object [] {})); 198 l.addRule (rule ("reInClassMinus", new Object [] {MINUS, STRING})); 199 l.addRule (rule ("reInClassMinus", new Object [] {})); 200 201 l.addRule (rule ("grammarRule", new Object [] {IDENTIFIER, EQUAL, "grRightSide", SEMICOLON})); 202 l.addRule (rule ("grRightSide", new Object [] {"grChoice", "grRightSide1"})); 203 l.addRule (rule ("grRightSide1", new Object [] {OR, "grChoice", "grRightSide1"})); 204 l.addRule (rule ("grRightSide1", new Object [] {})); 205 l.addRule (rule ("grChoice", new Object [] {"grPart", "grChoice"})); 206 l.addRule (rule ("grChoice", new Object [] {})); 207 l.addRule (rule ("grPart", new Object [] {IDENTIFIER, "grOperator"})); 208 l.addRule (rule ("grPart", new Object [] {"tokenDef", "grOperator"})); 209 l.addRule (rule ("grPart", new Object [] {STRING, "grOperator"})); 210 l.addRule (rule ("grPart", new Object [] {BRACKET, "grRightSide", BRACKET2})); 211 l.addRule (rule ("grPart", new Object [] {PARENTHESIS, "grRightSide", PARENTHESIS2, "grOperator"})); 212 l.addRule (rule ("grOperator", new Object [] {PLUS})); 213 l.addRule (rule ("grOperator", new Object [] {MULTIPLY})); 214 l.addRule (rule ("grOperator", new Object [] {QUESTION})); 215 l.addRule (rule ("grOperator", new Object [] {})); 216 l.addRule (rule ("tokenDef", new Object [] {LT, IDENTIFIER, "tokenDef1", GT})); 217 l.addRule (rule ("tokenDef1", new Object [] {COMMA, STRING})); 218 l.addRule (rule ("tokenDef1", new Object [] {})); 219 220 l.addRule (rule ("command", new Object [] {KEYWORD, "command0"})); 221 l.addRule (rule ("command0", new Object [] {COLON, "selector", "command1"})); 222 l.addRule (rule ("command0", new Object [] {"value"})); 223 l.addRule (rule ("command1", new Object [] {COLON, "value"})); 224 l.addRule (rule ("command1", new Object [] {})); 225 l.addRule (rule ("value", new Object [] {"class"})); 226 l.addRule (rule ("value", new Object [] {STRING})); 227 l.addRule (rule ("value", new Object [] {BRACE, "properties", BRACE2})); 228 l.addRule (rule ("value", new Object [] {PARENTHESIS, "regularExpression", PARENTHESIS2})); 229 l.addRule (rule ("properties", new Object [] {"property", "properties"})); 230 l.addRule (rule ("properties", new Object [] {})); 231 l.addRule (rule ("property", new Object [] {IDENTIFIER, COLON, "propertyValue", SEMICOLON})); 232 l.addRule (rule ("propertyValue", new Object [] {STRING})); 233 l.addRule (rule ("propertyValue", new Object [] {"class"})); 234 l.addRule (rule ("propertyValue", new Object [] {PARENTHESIS, "regularExpression", PARENTHESIS2})); 235 l.addRule (rule ("selector", new Object [] {"class"})); 236 l.addRule (rule ("class", new Object [] {IDENTIFIER, "class1"})); 237 l.addRule (rule ("class1", new Object [] {DOT, IDENTIFIER, "class1"})); 238 l.addRule (rule ("class1", new Object [] {})); 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 nt, Object [] right) { 247 return LLSyntaxAnalyser.Rule.create ( 248 nt, 249 Arrays.asList (right) 250 ); 251 } 252 } 253 | Popular Tags |