Search in sources :

Example 11 with Statement

use of org.apache.felix.gogo.runtime.Parser.Statement in project felix by apache.

the class Parser method doParse.

private ParsedLine doParse(CharSequence line, int cursor) throws SyntaxError {
    org.apache.felix.gogo.runtime.Parser parser = new org.apache.felix.gogo.runtime.Parser(line);
    Program program = parser.program();
    List<Statement> statements = parser.statements();
    // Find corresponding statement
    Statement statement = null;
    for (int i = statements.size() - 1; i >= 0; i--) {
        Statement s = statements.get(i);
        if (s.start() <= cursor) {
            boolean isOk = true;
            // check if there are only spaces after the previous statement
            if (s.start() + s.length() < cursor) {
                for (int j = s.start() + s.length(); isOk && j < cursor; j++) {
                    isOk = Character.isWhitespace(line.charAt(j));
                }
            }
            statement = s;
            break;
        }
    }
    if (statement != null) {
        return new ParsedLineImpl(program, statement, cursor, statement.tokens());
    } else {
        // TODO:
        return new ParsedLineImpl(program, program, cursor, Collections.<Token>singletonList(program));
    }
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement)

Example 12 with Statement

use of org.apache.felix.gogo.runtime.Parser.Statement in project felix by apache.

the class TestParser method testParentheses.

@Test
public void testParentheses() {
    Parser parser = new Parser("(a|b)|(d|f)");
    Program p = parser.program();
    assertEquals("a|b", ((Sequence) ((Statement) ((Pipeline) p.tokens().get(0)).tokens().get(0)).tokens().get(0)).program().toString());
    parser = new Parser("grep (d.*)|grep (d|f)");
    p = parser.program();
    assertEquals("d.*", ((Sequence) ((Statement) ((Pipeline) p.tokens().get(0)).tokens().get(0)).tokens().get(1)).program().toString());
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Sequence(org.apache.felix.gogo.runtime.Parser.Sequence) Pipeline(org.apache.felix.gogo.runtime.Parser.Pipeline) Test(org.junit.Test)

Example 13 with Statement

use of org.apache.felix.gogo.runtime.Parser.Statement in project felix by apache.

the class TestParser method testSimpleValue.

@Test
public void testSimpleValue() {
    Program p = new Parser("abc def.ghi http://www.osgi.org?abc=\\&x=1 [1,2,3] {{{{{{{xyz}}}}}}} (immediate) {'{{{{{'} {\\{} 'abc{}'").program();
    List<Token> x = ((Statement) p.tokens().get(0)).tokens();
    assertEquals("abc", x.get(0).toString());
    assertEquals("def.ghi", x.get(1).toString());
    assertEquals("http://www.osgi.org?abc=\\&x=1", x.get(2).toString());
    assertEquals("[1,2,3]", x.get(3).toString());
    assertEquals("{{{{{{{xyz}}}}}}}", x.get(4).toString());
    assertEquals("(immediate)", x.get(5).toString());
    assertEquals("{'{{{{{'}", x.get(6).toString());
    assertEquals("{\\{}", x.get(7).toString());
    assertEquals("'abc{}'", x.get(8).toString());
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Test(org.junit.Test)

Aggregations

Statement (org.apache.felix.gogo.runtime.Parser.Statement)13 Program (org.apache.felix.gogo.runtime.Parser.Program)12 Test (org.junit.Test)8 Pipeline (org.apache.felix.gogo.runtime.Parser.Pipeline)6 ArrayList (java.util.ArrayList)2 ParsedLineImpl (org.apache.felix.gogo.jline.ParsedLineImpl)2 EOFError (org.apache.felix.gogo.runtime.EOFError)2 Token (org.apache.felix.gogo.runtime.Token)2 Command (org.apache.karaf.shell.api.console.Command)2 CommandLine (org.apache.karaf.shell.api.console.CommandLine)2 Parser (org.apache.karaf.shell.api.console.Parser)2 ParsedLine (org.jline.reader.ParsedLine)2 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 Channel (java.nio.channels.Channel)1 List (java.util.List)1 Executable (org.apache.felix.gogo.runtime.Parser.Executable)1 Operator (org.apache.felix.gogo.runtime.Parser.Operator)1 Sequence (org.apache.felix.gogo.runtime.Parser.Sequence)1 Result (org.apache.felix.gogo.runtime.Pipe.Result)1