1 7 8 package org.netbeans.modules.languages.parser; 9 10 import java.util.Map ; 11 import org.netbeans.api.languages.ASTToken; 12 import org.netbeans.api.languages.CharInput; 13 import org.netbeans.api.languages.ParseException; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 import junit.framework.*; 17 import org.netbeans.modules.languages.Feature; 18 import org.netbeans.modules.languages.Language; 19 import org.netbeans.modules.languages.parser.Parser.Cookie; 20 21 25 public class ParserTest extends TestCase { 26 27 public ParserTest (String testName) { 28 super (testName); 29 } 30 31 43 117 118 private static Parser create (String [] l) throws ParseException { 119 List rules = new ArrayList (); 120 int i, k = l.length; 121 for (i = 0; i < k; i+=4) { 122 String tokenName = (String ) l [i + 1]; 123 String pattern = (String ) l [i + 2]; 124 rules.add (Language.createTokenType ( 125 (String ) l [i], 126 Pattern.create (pattern), 127 tokenName, 128 (String ) l [i + 3], 129 i / 4, 130 null 131 )); 132 } 133 return Parser.create (rules); 134 } 135 136 public void testReadJava () throws ParseException { 137 Parser parser = create (new String [] { 138 null, "comment", "'/*'-'*/'", null, 139 null, "comment", "'//'[^'\n''\r']*", null, 140 null, "keyword", "'if'|'else'|'public'|'static'|'private'|'protected'|'class'|'extends'|'import'|'package'|'try'|'int'|'false'|'true'|'void'", null, 141 null, "operator", "['\\\\''/''*''-''+''.'',''=''{''}''('')''!''@''#''$''%''^''&''~''|'';']", null, 142 null, "string", "'\\\"'[^'\\\"']*'\\\"'", null, 143 null, "char", "'\\\''[^'\\\'']'\\\''", null, 144 null, "number", "['0'-'9']['0'-'9''.']*", null, 145 null, "whitespace", "[' ''\t''\n''\r']+", null, 146 null, "identifier", "['a'-'z''A'-'Z'][^' ''\\t''\\n''\\r''/''*''-''+''.'',''=''{''}''('')''!''@''#''$''%''^''&''~''|''\\\\'';']*", null 147 }); 148 149 System.out.println (parser); 150 CharInput in = new StringInput ( 151 "/** dsfsf\n" + 152 " asd as * asdf */ " + 153 "package ifa.vv.b;\n" + 154 "import a.v;\n" + 155 "\n" + 156 "public class Asd extends sd {\n" + 157 " void main (String[] s) {\n" + 158 " return \"asd\" + \'a\' + 123;\n" + 159 " }\n" + 160 "}", 161 "test" 162 ); 163 Cookie c = new MyCookie (); 164 assertEquals ("state: " + c.getState () + " text: " + in, "comment", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 165 assertEquals ("state: " + c.getState () + " text: " + in, "whitespace", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 166 assertEquals ("state: " + c.getState () + " text: " + in, "keyword", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 167 assertEquals ("state: " + c.getState () + " text: " + in, "whitespace", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 168 assertEquals ("state: " + c.getState () + " text: " + in, "identifier", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 169 assertEquals ("state: " + c.getState () + " text: " + in, "operator", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 170 assertEquals ("state: " + c.getState () + " text: " + in, "identifier", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 171 assertEquals ("state: " + c.getState () + " text: " + in, "operator", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 172 assertEquals ("state: " + c.getState () + " text: " + in, "identifier", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 173 assertEquals ("state: " + c.getState () + " text: " + in, "operator", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 174 } 175 176 public void testReadHtml () throws ParseException { 177 Parser parser = create (new String [] { 178 null, "TEXT", "[^'<']*", null, 179 null, "COMMENT", "'<!'-'-->'", null, 180 Parser.DEFAULT_STATE, "ELEMENT", "'<'[^' ''>''\\t''\\n''\\r']+", "IN_ELEMENT", 181 null, "END_ELEMENT", "'</'[^' ''>''\\t''\\n''\\r']+'>'", null, 182 "IN_ELEMENT", "ELEMENT", "'/'?'>'", Parser.DEFAULT_STATE, 183 "IN_ELEMENT", "ATTRIBUTE_NAME", "[^' ''>''=''\\t''\\n''\\r']+", "IN_ATTRIBUTE", 184 "IN_ATTRIBUTE", "OPERATOR", "'='", "IN_ATTRIBUTE2", 185 "IN_ATTRIBUTE2", "ATTRIBUTE_VALUE", "'\\\"'[^'\\\"']*'\\\"'", "IN_ELEMENT", 186 187 "IN_ATTRIBUTE", "ELEMENT", "'/'?'>'", Parser.DEFAULT_STATE, 188 "IN_ATTRIBUTE", "ATTRIBUTE_NAME", "[^' ''=''\\t''\\n''\\r']+", "IN_ATTRIBUTE", 189 "IN_ELEMENT", "WHITESPACE", "[' ''\\t''\\n''\\r']*", "IN_ELEMENT", 190 "IN_ATTRIBUTE", "WHITESPACE", "[' ''\\t''\\n''\\r']*", "IN_ATTRIBUTE", 191 "IN_ATTRIBUTE2", "WHITESPACE", "[' ''\\t''\\n''\\r']*", "IN_ATTRIBUTE2" 192 }); 193 194 CharInput in = new StringInput ( 195 "<test a=\"asz\"> \n" + 196 " \" sdf\" = sdf" + 197 "<!-- NewPage" + 198 " ?!?@? -- > sdf -> -->\n" + 199 "<HTML><\\HTML> <test t>", 200 "test" 201 ); 202 Cookie c = new MyCookie (); 203 assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 204 assertEquals ("state: " + c + " text: " + in, "WHITESPACE", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 205 assertEquals ("state: " + c + " text: " + in, "ATTRIBUTE_NAME", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 206 assertEquals ("state: " + c + " text: " + in, "OPERATOR", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 207 assertEquals ("state: " + c + " text: " + in, "ATTRIBUTE_VALUE", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 208 assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 209 assertEquals ("state: " + c + " text: " + in, "TEXT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 210 assertEquals ("state: " + c + " text: " + in, "COMMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 211 assertEquals ("state: " + c + " text: " + in, "TEXT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 212 assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 213 assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 214 assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 215 assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 216 assertEquals ("state: " + c + " text: " + in, "TEXT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 217 assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 218 assertEquals ("state: " + c + " text: " + in, "WHITESPACE", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 219 assertEquals ("state: " + c + " text: " + in, "ATTRIBUTE_NAME", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 220 assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ()); 221 assertTrue (in.eof ()); 222 } 223 224 private static class MyCookie implements Parser.Cookie { 225 226 private int state = -1; 227 228 public int getState () { 229 return state; 230 } 231 232 public void setState (int state) { 233 this.state = state; 234 } 235 236 public void setProperties (Feature properties) { 237 } 238 } 239 } 240 | Popular Tags |