1 7 8 package org.netbeans.modules.languages.parser; 9 10 import junit.framework.TestCase; 11 import org.netbeans.api.languages.CharInput; 12 import org.netbeans.api.languages.ParseException; 13 import org.netbeans.api.languages.ASTToken; 14 import org.netbeans.modules.languages.parser.TokenInput; 15 import org.netbeans.api.languages.ASTNode; 16 17 import java.util.Arrays ; 18 import java.util.Collections ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import org.netbeans.modules.languages.Language; 22 import org.netbeans.modules.languages.NBSLanguageReader; 23 24 25 29 public class AnalyserTest extends TestCase { 30 31 public AnalyserTest (String testName) { 32 super (testName); 33 } 34 35 private static String mimeType = "text/test"; 36 37 public void test1 () throws ParseException { 38 Language l = new Language (mimeType); 39 l.addRule (LLSyntaxAnalyser.Rule.create ("S", Arrays.asList (new Object [] { 40 ASTToken.create (mimeType, "identifier", null, 0), 41 "S" 42 }))); 43 l.addRule (LLSyntaxAnalyser.Rule.create ("S", Arrays.asList (new Object [] { 44 ASTToken.create (mimeType, "operator", "{", 0), 45 "S", 46 ASTToken.create (mimeType, "operator", "}", 0), 47 "S" 48 }))); 49 l.addRule (LLSyntaxAnalyser.Rule.create ("S", Arrays.asList (new Object [] { 50 }))); 51 LLSyntaxAnalyser a = l.getAnalyser (); 52 TokenInput input = TokenInput.create (new ASTToken[] { 54 ASTToken.create (mimeType, "identifier", "asd", 0), 55 ASTToken.create (mimeType, "identifier", "ss", 0), 56 ASTToken.create (mimeType, "operator", "{", 0), 57 ASTToken.create (mimeType, "identifier", "a", 0), 58 ASTToken.create (mimeType, "operator", "{", 0), 59 ASTToken.create (mimeType, "operator", "}", 0), 60 ASTToken.create (mimeType, "identifier", "asd", 0), 61 ASTToken.create (mimeType, "operator", "}", 0), 62 }); 63 assertNotNull (a.read (input, false)); 64 assert (input.eof ()); 65 } 66 67 public void test2 () throws ParseException { 68 Language l = NBSLanguageReader.readLanguage ( 69 "test", 70 "TOKEN:operator:( '{' | '}' | '.' | ';' | ',' | '(' | ')' )" + 71 "TOKEN:whitespace:( ['\\n' '\\r' ' ' '\\t']+ )" + 72 "TOKEN:keyword:( 'package' | 'class' | 'import' | 'static' | 'synchronized' | 'final' | 'abstract' | 'native' | 'import' | 'extends' | 'implements' | 'public' | 'protected' | 'private' )" + 73 "TOKEN:identifier:( ['a'-'z' 'A'-'Z' '0'-'9' '_' '-' '$' '%']+ )" + 74 "SKIP:whitespace " + 75 "S = packageDeclaration imports classOrInterface otherClasses;" + 76 "packageDeclaration = ;" + 77 "packageDeclaration = <keyword,'package'> dottedName <operator,';'>;" + 78 "dottedName = <identifier> dottedName1;" + 79 "dottedName1 = <operator,'.'> <identifier> dottedName1;" + 80 "dottedName1 = ;" + 81 "imports = <keyword,'import'> dottedName <operator,';'> imports;" + 82 "imports = ;" + 83 "modifiers = <keyword,'public'> modifiers;" + 84 "modifiers = <keyword,'private'> modifiers;" + 85 "modifiers = <keyword,'protected'> modifiers;" + 86 "modifiers = <keyword,'static'> modifiers;" + 87 "modifiers = <keyword,'synchronized'> modifiers;" + 88 "modifiers = <keyword,'final'> modifiers;" + 89 "modifiers = <keyword,'abstract'> modifiers;" + 90 "modifiers = <keyword,'native'> modifiers;" + 91 "modifiers = ;" + 92 "classOrInterface = modifiers classOrInterface1;" + 93 "classOrInterface1 = <keyword,'class'> class;" + 94 "classOrInterface1 = <keyword,'interface'> interface;" + 95 "class = <identifier> extendsList implementsList classOrInterfaceBody;" + 96 "interface = <identifier> extendsList implementsList classOrInterfaceBody;" + 97 "extendsList = <keyword,'extends'> dottedName extendsList1;" + 98 "extendsList1 = <operator,','> dottedName;" + 99 "extendsList1 = ;" + 100 "implementsList = <keyword,'implements'> dottedName implementsList1;" + 101 "implementsList1 = <operator,','> dottedName;" + 102 "implementsList1 = ;" + 103 "classOrInterfaceBody = <operator,'{'> members <operator,'}'>;" + 104 "members = modifiers members1;" + 105 "members1 = block members;" + 106 "members1 = <identifier, type> <identifier, name> <operator,'('> parametersList <operator,')'> block members;" + 107 "members = ;" + 108 "parametersList =;" + 109 "block = <operator,'{'> body <operator,'}'>;" + 110 "body = <identifier> body;" + 111 "body = block;" + 112 "body = ;" + 113 "otherClasses = ;", 114 mimeType 115 ); 116 CharInput input = new StringInput ( 117 "package org.test.foo;" + 118 "import a.bb.ccc;" + 119 "import qq.ww.ee;" + 120 "public static class Hanz extends aaa.Text implements a.XXX, b.YYY {" + 121 " public static { aaa}" + 122 " public final int test () {" + 123 " test test" + 124 " }" + 125 "}", 126 "source" 127 ); 128 ASTNode n = l.getAnalyser ().read ( 129 TokenInput.create ( 130 "text/test", 131 l.getParser (), 132 input, 133 l.getSkipTokenTypes () 134 ), 135 false 136 ); 137 assertNotNull (n); 139 } 140 141 public void test4 () throws ParseException { 142 Language l = NBSLanguageReader.readLanguage ( 143 "test", 144 "TOKEN:operator:( '{' | '}' | '.' | ',' | '(' | ')' )" + 145 "TOKEN:separator:( ';' )" + 146 "TOKEN:whitespace:( ['\\n' '\\r' ' ' '\\t']+ )" + 147 "TOKEN:keyword:( 'void' | 'public' )" + 148 "TOKEN:identifier:( ['a'-'z' 'A'-'Z' '0'-'9' '_' '-' '$' '%']+ )" + 149 "SKIP:whitespace " + 150 "S = variable S;" + 151 "S = ;" + 152 "variable = modifiers <keyword> <identifier,name> <separator,';'>;" + 153 "variable = modifiers <identifier,type> <identifier,name> <separator,';'>;" + 154 "modifiers = <keyword,'public'> modifiers;" + 155 "modifiers = ;", 156 mimeType 157 ); 158 CharInput input = new StringInput ( 159 "void a;" + 160 "public ii name;", 161 "source" 162 ); 163 ASTNode n = l.getAnalyser ().read ( 164 TokenInput.create ( 165 "text/test", 166 l.getParser (), 167 input, 168 l.getSkipTokenTypes () 169 ), 170 false 171 ); 172 assertNotNull (n); 173 assertTrue (input.eof ()); 174 } 175 270 271 public void test3 () throws ParseException { 272 Language l = NBSLanguageReader.readLanguage ( 273 "test", 274 "TOKEN:operator:( '{' | '}' | '.' | ';' | ',' | '(' | ')' )" + 275 "TOKEN:whitespace:( ['\\n' '\\r' ' ' '\\t']+ )" + 276 "TOKEN:keyword:( 'package' | 'class' | 'import' | 'static' | 'synchronized' | 'final' | 'abstract' | 'native' | 'import' | 'extends' | 'implements' | 'public' | 'protected' | 'private' )" + 277 "TOKEN:identifier:( ['a'-'z' 'A'-'Z' '0'-'9' '_' '-' '$' '%']+ )" + 278 "SKIP:whitespace " + 279 "S = <identifier,'a'> SS <identifier,'b'>;" + 280 "SS = <identifier,'if'> E <identifier,'then'> SS;" + 281 "SS = <identifier,'if'> E <identifier,'then'> SS <identifier,'else'> SS;" + 282 "SS = <identifier, 'b'>;" + 283 "E = <identifier,'e'>;", 284 mimeType 285 ); 286 CharInput input = new StringInput ( 287 "a if e then if e then b else b b", 288 "source" 289 ); 290 ASTNode n = l.getAnalyser ().read ( 291 TokenInput.create ( 292 "text/test", 293 l.getParser (), 294 input, 295 l.getSkipTokenTypes () 296 ), 297 false 298 ); 299 assertTrue (input.eof ()); 300 assertEquals (3, n.getChildren ().size ()); 301 n = (ASTNode) n.getChildren ().get (1); 302 assertEquals (4, n.getChildren ().size ()); 303 n = (ASTNode) n.getChildren ().get (3); 304 assertEquals (6, n.getChildren ().size ()); 305 } 306 307 public void test5 () throws ParseException { 308 Language l = NBSLanguageReader.readLanguage ( 309 "test", 310 "TOKEN:operator:( '{' | '}' | '.' | ';' | ',' | '(' | ')' )" + 311 "TOKEN:whitespace:( ['\\n' '\\r' ' ' '\\t']+ )" + 312 "TOKEN:keyword:( 'package' | 'class' | 'import' | 'static' | 'synchronized' | 'final' | 'abstract' | 'native' | 'import' | 'extends' | 'implements' | 'public' | 'protected' | 'private' )" + 313 "TOKEN:identifier:( ['a'-'z' 'A'-'Z' '0'-'9' '_' '-' '$' '%']+ )" + 314 "SKIP:whitespace " + 315 "S = <identifier,'a'> SS <identifier,'b'>;" + 316 "SS = <identifier,'if'> E <identifier,'then'> SS;" + 317 "SS = <identifier,'if'> E <identifier,'then'> SS <identifier,'else'> SS;" + 318 "SS = <identifier, 'b'>;" + 319 "E = <identifier,'e'>;", 320 mimeType 321 ); 322 CharInput input = new StringInput ( 323 "a if e then if e then b else b b", 324 "source" 325 ); 326 ASTNode n = l.getAnalyser ().read ( 327 TokenInput.create ( 328 "text/test", 329 l.getParser (), 330 input, 331 l.getSkipTokenTypes () 332 ), 333 false 334 ); 335 System.out.println(n.print ()); 336 assertTrue (input.eof ()); 337 assertEquals (3, n.getChildren ().size ()); 338 n = (ASTNode) n.getChildren ().get (1); 339 assertEquals (4, n.getChildren ().size ()); 340 n = (ASTNode) n.getChildren ().get (3); 341 assertEquals (6, n.getChildren ().size ()); 342 } 343 344 private void print (List l, String indent) { 345 Iterator it = l.iterator (); 346 while (it.hasNext ()) { 347 Object next = it.next (); 348 System.out.println (indent + next); 349 if (next instanceof ASTToken) continue; 350 print ((List ) it.next (), indent + " "); 351 } 352 } 353 } 354 | Popular Tags |