matches text now if not <ID> node

This commit is contained in:
Terence Parr 2013-09-09 15:13:37 -07:00
parent f995e47443
commit 95aa103dc2
2 changed files with 44 additions and 3 deletions

View File

@ -131,13 +131,22 @@ public class ParseTreePatternMatcher {
if ( tree==null || patternTree==null ) {
return new ParseTreeMatchFailed(tree, null, pattern);
}
// x and <ID>
// x and <ID>, x and y, or x and x; or could be mismatched types
if ( tree instanceof TerminalNode && patternTree instanceof TerminalNode ) {
TerminalNode t1 = (TerminalNode)tree;
TerminalNode t2 = (TerminalNode)patternTree;
ParseTreeMatch m = null;
// both are
if ( t1.getSymbol().getType() == t2.getSymbol().getType() ) {
m = new ParseTreeMatch(tree, pattern);
if ( t2.getSymbol() instanceof TokenTagToken ) { // x and <ID>
m = new ParseTreeMatch(tree, pattern);
}
else if ( t1.getText().equals(t2.getText()) ) { // x and x
m = new ParseTreeMatch(tree, pattern);
}
else { // x and y
m = new ParseTreeMatchFailed(tree, t1, pattern);
}
}
else {
m = new ParseTreeMatchFailed(tree, t1, pattern);
@ -176,6 +185,7 @@ public class ParseTreePatternMatcher {
return new ParseTreeMatchFailed(tree, tree, pattern);
}
/** Is t (expr <expr>) subtree? */
public boolean isRuleTag(ParseTree t) {
if ( t instanceof RuleNode ) {
RuleNode r = (RuleNode)t;

View File

@ -9,6 +9,7 @@ import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class TestParseTreeMatcher extends BaseTest {
@ -139,6 +140,26 @@ public class TestParseTreeMatcher extends BaseTest {
checkPatternMatch("X4.g4", grammar, "s", input, pattern, "X4Parser", "X4Lexer");
}
@Test public void testTokenTextMatch() throws Exception {
String grammar =
"grammar X4;\n" +
"s : ID '=' expr ';' ;\n" +
"expr : ID | INT ;\n" +
"ID : [a-z]+ ;\n" +
"INT : [0-9]+ ;\n" +
"WS : [ \\r\\n\\t]+ -> skip ;\n";
String input = "x = 0;";
String pattern = "<ID> = 1;";
boolean invertMatch = true; // 0!=1
checkPatternMatch("X4.g4", grammar, "s", input, pattern, "X4Parser", "X4Lexer", invertMatch);
input = "x = 0;";
pattern = "<ID> = 0;";
invertMatch = false;
checkPatternMatch("X4.g4", grammar, "s", input, pattern, "X4Parser", "X4Lexer", invertMatch);
}
@Test public void testAssign() throws Exception {
String grammar =
"grammar X5;\n" +
@ -185,6 +206,15 @@ public class TestParseTreeMatcher extends BaseTest {
String input, String pattern,
String parserName, String lexerName)
throws Exception
{
checkPatternMatch(grammarName, grammar, startRule, input, pattern, parserName, lexerName, false);
}
public void checkPatternMatch(String grammarName, String grammar, String startRule,
String input, String pattern,
String parserName, String lexerName,
boolean invertMatch)
throws Exception
{
boolean ok =
rawGenerateAndBuildRecognizer(grammarName, grammar, parserName, lexerName, false);
@ -196,6 +226,7 @@ public class TestParseTreeMatcher extends BaseTest {
new ParseTreePatternMatcher(loadLexerClassFromTempDir(lexerName),
loadParserClassFromTempDir(parserName));
boolean matches = p.matches(result, startRule, pattern);
assertTrue(matches);
if ( invertMatch ) assertFalse(matches);
else assertTrue(matches);
}
}