Search in sources :

Example 6 with XPathLexer

use of org.antlr.v4.runtime.tree.xpath.XPathLexer in project JsoupXpath by zhegexiaohuozi.

the class ExprTest method exp.

@Test
public void exp() {
    // CharStream input = CharStreams.fromString("//ul[@class='subject-list']/li[./div/div/span[@class='pl']/num()>10000]/div[@class='info']/h2/allText()");
    // CharStream input = CharStreams.fromString("//ul[@class='subject-list']/li[contains(self::li/div/div/span[@class='pl']//text(),'14582')]/div/h2//text()");
    CharStream input = CharStreams.fromString("//ul[@class='subject-list']/li[contains(./div/div/span[@class='pl']//text(),'14582')]/div/h2//text()");
    // CharStream input = CharStreams.fromString("//*[@id=\"subject_list\"]/ul/li[2]/div[2]/h2/a//text()");
    // CharStream input = CharStreams.fromString("//*[contains(@id,\"subject_\")]/ul/li[2]/div[2]/h2/a//text()");
    // CharStream input = CharStreams.fromString("//*[contains(@id,\"subject_\")]/ul/li[2]/div[2]/h2/a//text()");
    XpathLexer lexer = new XpathLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    XpathParser parser = new XpathParser(tokens);
    parser.setErrorHandler(new DoFailOnErrorHandler());
    ParseTree tree = parser.main();
    XpathProcessor processor = new XpathProcessor(root);
    XValue value = processor.visit(tree);
    logger.info("visit res = {}", value);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) XpathParser(cn.wanghaomiao.xpath.antlr.XpathParser) XpathLexer(cn.wanghaomiao.xpath.antlr.XpathLexer) XpathProcessor(cn.wanghaomiao.xpath.core.XpathProcessor) XValue(cn.wanghaomiao.xpath.core.XValue) CharStream(org.antlr.v4.runtime.CharStream) DoFailOnErrorHandler(cn.wanghaomiao.xpath.exception.DoFailOnErrorHandler) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Test(org.junit.Test) BaseTest(cn.wanghaomiao.xpath.BaseTest)

Example 7 with XPathLexer

use of org.antlr.v4.runtime.tree.xpath.XPathLexer in project antlr4 by tunnelvisionlabs.

the class TestTokenStream method testBufferedTokenStreamReuseAfterFill.

/**
 * This is a targeted regression test for antlr/antlr4#1584 ({@link BufferedTokenStream} cannot be reused after EOF).
 */
@Test
public void testBufferedTokenStreamReuseAfterFill() {
    CharStream firstInput = CharStreams.fromString("A");
    BufferedTokenStream tokenStream = new BufferedTokenStream(new XPathLexer(firstInput));
    tokenStream.fill();
    Assert.assertEquals(2, tokenStream.size());
    Assert.assertEquals(XPathLexer.TOKEN_REF, tokenStream.get(0).getType());
    Assert.assertEquals(Token.EOF, tokenStream.get(1).getType());
    CharStream secondInput = CharStreams.fromString("A/");
    tokenStream.setTokenSource(new XPathLexer(secondInput));
    tokenStream.fill();
    Assert.assertEquals(3, tokenStream.size());
    Assert.assertEquals(XPathLexer.TOKEN_REF, tokenStream.get(0).getType());
    Assert.assertEquals(XPathLexer.ROOT, tokenStream.get(1).getType());
    Assert.assertEquals(Token.EOF, tokenStream.get(2).getType());
}
Also used : XPathLexer(org.antlr.v4.runtime.tree.xpath.XPathLexer) BufferedTokenStream(org.antlr.v4.runtime.BufferedTokenStream) CharStream(org.antlr.v4.runtime.CharStream) Test(org.junit.Test)

Aggregations

CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)6 CharStream (org.antlr.v4.runtime.CharStream)5 ParseTree (org.antlr.v4.runtime.tree.ParseTree)4 Test (org.junit.Test)3 XpathLexer (cn.wanghaomiao.xpath.antlr.XpathLexer)2 XpathParser (cn.wanghaomiao.xpath.antlr.XpathParser)2 XValue (cn.wanghaomiao.xpath.core.XValue)2 XpathProcessor (cn.wanghaomiao.xpath.core.XpathProcessor)2 DoFailOnErrorHandler (cn.wanghaomiao.xpath.exception.DoFailOnErrorHandler)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 LexerNoViableAltException (org.antlr.v4.runtime.LexerNoViableAltException)2 Token (org.antlr.v4.runtime.Token)2 Element (org.jsoup.nodes.Element)2 XpathLexer (org.seimicrawler.xpath.antlr.XpathLexer)2 XpathParser (org.seimicrawler.xpath.antlr.XpathParser)2 XValue (org.seimicrawler.xpath.core.XValue)2 XpathProcessor (org.seimicrawler.xpath.core.XpathProcessor)2 DoFailOnErrorHandler (org.seimicrawler.xpath.exception.DoFailOnErrorHandler)2 BaseTest (cn.wanghaomiao.xpath.BaseTest)1