📄 testchardfaconversion.java
字号:
"lexer grammar t;\n"+ "SLCMT : '//' .+ '\n' ;"); String expecting = ".s0-'\\n'->:s1=>2\n" + ".s0-{'\\u0000'..'\\t', '\\u000B'..'\\uFFFE'}->:s2=>1\n"; checkDecision(g, 1, expecting, null); } public void testNonGreedyByDefaultWildcardPlusWithParens() throws Exception { Grammar g = new Grammar( "lexer grammar t;\n"+ "SLCMT : '//' (.)+ '\n' ;"); String expecting = ".s0-'\\n'->:s1=>2\n" + ".s0-{'\\u0000'..'\\t', '\\u000B'..'\\uFFFE'}->:s2=>1\n"; checkDecision(g, 1, expecting, null); } public void testNonWildcardNonGreedy() throws Exception { Grammar g = new Grammar( "lexer grammar t;\n"+ "DUH : (options {greedy=false;}:'x'|'y')* 'xy' ;"); String expecting = ".s0-'x'->.s1\n" + ".s0-'y'->:s4=>2\n" + ".s1-'x'->:s3=>1\n" + ".s1-'y'->:s2=>3\n"; checkDecision(g, 1, expecting, null); } public void testNonWildcardEOTMakesItWorkWithoutNonGreedyOption() throws Exception { Grammar g = new Grammar( "lexer grammar t;\n"+ "DUH : ('x'|'y')* 'xy' ;"); String expecting = ".s0-'x'->.s1\n" + ".s0-'y'->:s3=>1\n" + ".s1-'x'->:s3=>1\n" + ".s1-'y'->.s2\n" + ".s2-'x'..'y'->:s3=>1\n" + ".s2-<EOT>->:s4=>2\n"; checkDecision(g, 1, expecting, null); } public void testAltConflictsWithLoopThenExit() throws Exception { // \" predicts alt 1, but wildcard then " can predict exit also Grammar g = new Grammar( "lexer grammar t;\n"+ "STRING : '\"' (options {greedy=false;}: '\\\\\"' | .)* '\"' ;\n" ); String expecting = ".s0-'\"'->:s1=>3\n" + ".s0-'\\\\'->.s2\n" + ".s0-{'\\u0000'..'!', '#'..'[', ']'..'\\uFFFE'}->:s4=>2\n" + ".s2-'\"'->:s3=>1\n" + ".s2-{'\\u0000'..'!', '#'..'\\uFFFE'}->:s4=>2\n"; checkDecision(g, 1, expecting, null); } public void testNonGreedyLoopThatNeverLoops() throws Exception { Grammar g = new Grammar( "lexer grammar t;\n"+ "DUH : (options {greedy=false;}:'x')+ ;"); // loop never matched String expecting = ":s0=>2\n"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); checkDecision(g, 1, expecting, new int[] {1}); assertEquals("unexpected number of expected problems", 1, equeue.size()); Message msg = (Message)equeue.warnings.get(0); assertTrue("warning must be an unreachable alt", msg instanceof GrammarUnreachableAltsMessage); GrammarUnreachableAltsMessage u = (GrammarUnreachableAltsMessage)msg; assertEquals("[1]", u.alts.toString()); } public void testRecursive() throws Exception { // this is cool because the 3rd alt includes !(all other possibilities) Grammar g = new Grammar( "lexer grammar duh;\n" + "SUBTEMPLATE\n" + " : '{'\n" + " ( SUBTEMPLATE\n" + " | ESC\n" + " | ~('}'|'\\\\'|'{')\n" + " )*\n" + " '}'\n" + " ;\n" + "fragment\n" + "ESC : '\\\\' . ;"); g.createLookaheadDFAs(); String expecting = ".s0-'\\\\'->:s3=>2\n" + ".s0-'{'->:s2=>1\n" + ".s0-'}'->:s1=>4\n" + ".s0-{'\\u0000'..'[', ']'..'z', '|', '~'..'\\uFFFE'}->:s4=>3\n"; checkDecision(g, 1, expecting, null); } public void testRecursive2() throws Exception { // this is also cool because it resolves \\ to be ESC alt; it's just // less efficient of a DFA Grammar g = new Grammar( "lexer grammar duh;\n" + "SUBTEMPLATE\n" + " : '{'\n" + " ( SUBTEMPLATE\n" + " | ESC\n" + " | ~('}'|'{')\n" + " )*\n" + " '}'\n" + " ;\n" + "fragment\n" + "ESC : '\\\\' . ;"); g.createLookaheadDFAs(); String expecting = ".s0-'\\\\'->.s3\n" + ".s0-'{'->:s2=>1\n" + ".s0-'}'->:s1=>4\n" + ".s0-{'\\u0000'..'[', ']'..'z', '|', '~'..'\\uFFFE'}->:s5=>3\n" + ".s3-'\\\\'->:s8=>2\n" + ".s3-'{'->:s7=>2\n" + ".s3-'}'->.s4\n" + ".s3-{'\\u0000'..'[', ']'..'z', '|', '~'..'\\uFFFE'}->:s6=>2\n" + ".s4-'\\u0000'..'\\uFFFE'->:s6=>2\n" + ".s4-<EOT>->:s5=>3\n"; checkDecision(g, 1, expecting, null); } public void testNotFragmentInLexer() throws Exception { Grammar g = new Grammar( "lexer grammar T;\n"+ "A : 'a' | ~B {;} ;\n" + "fragment B : 'a' ;\n"); g.createLookaheadDFAs(); String expecting = ".s0-'a'->:s1=>1\n" + ".s0-{'\\u0000'..'`', 'b'..'\\uFFFE'}->:s2=>2\n"; checkDecision(g, 1, expecting, null); } public void testNotSetFragmentInLexer() throws Exception { Grammar g = new Grammar( "lexer grammar T;\n"+ "A : B | ~B {;} ;\n" + "fragment B : 'a'|'b' ;\n"); g.createLookaheadDFAs(); String expecting = ".s0-'a'..'b'->:s1=>1\n" + ".s0-{'\\u0000'..'`', 'c'..'\\uFFFE'}->:s2=>2\n"; checkDecision(g, 1, expecting, null); } public void testNotTokenInLexer() throws Exception { Grammar g = new Grammar( "lexer grammar T;\n"+ "A : 'x' ('a' | ~B {;}) ;\n" + "B : 'a' ;\n"); g.createLookaheadDFAs(); String expecting = ".s0-'a'->:s1=>1\n" + ".s0-{'\\u0000'..'`', 'b'..'\\uFFFE'}->:s2=>2\n"; checkDecision(g, 1, expecting, null); } public void testNotComplicatedSetRuleInLexer() throws Exception { Grammar g = new Grammar( "lexer grammar T;\n"+ "A : B | ~B {;} ;\n" + "fragment B : 'a'|'b'|'c'..'e'|C ;\n" + "fragment C : 'f' ;\n"); // has to seen from B to C g.createLookaheadDFAs(); String expecting = ".s0-'a'..'f'->:s1=>1\n" + ".s0-{'\\u0000'..'`', 'g'..'\\uFFFE'}->:s2=>2\n"; checkDecision(g, 1, expecting, null); } public void testNotSetWithRuleInLexer() throws Exception { Grammar g = new Grammar( "lexer grammar T;\n"+ "T : ~('a' | B) | 'a';\n" + "fragment\n" + "B : 'b' ;\n" + "C : ~'x'{;} ;"); // force Tokens to not collapse T|C g.createLookaheadDFAs(); String expecting = ".s0-'b'->:s3=>2\n" + ".s0-'x'->:s2=>1\n" + ".s0-{'\\u0000'..'a', 'c'..'w', 'y'..'\\uFFFE'}->.s1\n" + ".s1-<EOT>->:s2=>1\n"; checkDecision(g, 1, expecting, null); } public void testSetCallsRuleWithNot() throws Exception { Grammar g = new Grammar( "lexer grammar A;\n" + "T : ~'x' ;\n" + "S : 'x' (T | 'x') ;\n"); g.createLookaheadDFAs(); String expecting = ".s0-'x'->:s2=>2\n" + ".s0-{'\\u0000'..'w', 'y'..'\\uFFFE'}->:s1=>1\n"; checkDecision(g, 1, expecting, null); } public void testSynPredInLexer() throws Exception { Grammar g = new Grammar( "lexer grammar T;\n"+ "LT: '<' ' '*\n" + " | ('<' IDENT) => '<' IDENT '>'\n" + // this was causing syntax error " ;\n" + "IDENT: 'a'+;\n"); // basically, Tokens rule should not do set compression test g.createLookaheadDFAs(); String expecting = ".s0-'<'->:s1=>1\n" + ".s0-'a'->:s2=>2\n"; checkDecision(g, 4, expecting, null); // 4 is Tokens rule } // S U P P O R T public void _template() throws Exception { Grammar g = new Grammar( "grammar T;\n"+ "a : A | B;"); g.createLookaheadDFAs(); String expecting = "\n"; checkDecision(g, 1, expecting, null); } protected void checkDecision(Grammar g, int decision, String expecting, int[] expectingUnreachableAlts) throws Exception { // mimic actions of org.antlr.Tool first time for grammar g if ( g.getCodeGenerator()==null ) { CodeGenerator generator = new CodeGenerator(null, g, "Java"); g.setCodeGenerator(generator); g.createNFAs(); g.createLookaheadDFAs(); } DFA dfa = g.getLookaheadDFA(decision); assertNotNull("unknown decision #"+decision, dfa); FASerializer serializer = new FASerializer(g); String result = serializer.serialize(dfa.startState); //System.out.print(result); List nonDetAlts = dfa.getUnreachableAlts(); //System.out.println("alts w/o predict state="+nonDetAlts); // first make sure nondeterministic alts are as expected if ( expectingUnreachableAlts==null ) { if ( nonDetAlts.size()!=0 ) { System.err.println("nondeterministic alts (should be empty): "+nonDetAlts); } assertEquals("unreachable alts mismatch", 0, nonDetAlts.size()); } else { for (int i=0; i<expectingUnreachableAlts.length; i++) { assertTrue("unreachable alts mismatch", nonDetAlts.contains(new Integer(expectingUnreachableAlts[i]))); } } assertEquals(expecting, result); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -