Search in sources :

Example 6 with Statement

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

the class TestParser method testPipeAndOr.

@Test
public void testPipeAndOr() {
    Program x = new Parser("abc|def&&ghi || jkl").program();
    Pipeline p0 = (Pipeline) x.tokens().get(0);
    Statement s00 = (Statement) p0.tokens().get(0);
    assertEquals("|", p0.tokens().get(1).toString());
    Statement s01 = (Statement) p0.tokens().get(2);
    assertEquals("&&", x.tokens().get(1).toString());
    Statement s1 = (Statement) x.tokens().get(2);
    assertEquals("||", x.tokens().get(3).toString());
    Statement s2 = (Statement) x.tokens().get(4);
    assertEquals("abc", s00.tokens().get(0).toString());
    assertEquals("def", s01.tokens().get(0).toString());
    assertEquals("ghi", s1.tokens().get(0).toString());
    assertEquals("jkl", s2.tokens().get(0).toString());
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Pipeline(org.apache.felix.gogo.runtime.Parser.Pipeline) Test(org.junit.Test)

Example 7 with Statement

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

the class TestParser method testStatements.

@Test
public void testStatements() {
    Program x = new Parser("abc def|ghi jkl|mno pqr").program();
    Pipeline p0 = (Pipeline) x.tokens().get(0);
    Statement s00 = (Statement) p0.tokens().get(0);
    Statement s01 = (Statement) p0.tokens().get(2);
    Statement s02 = (Statement) p0.tokens().get(4);
    assertEquals("abc", s00.tokens().get(0).toString());
    assertEquals("def", s00.tokens().get(1).toString());
    assertEquals("ghi", s01.tokens().get(0).toString());
    assertEquals("jkl", s01.tokens().get(1).toString());
    assertEquals("mno", s02.tokens().get(0).toString());
    assertEquals("pqr", s02.tokens().get(1).toString());
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Pipeline(org.apache.felix.gogo.runtime.Parser.Pipeline) Test(org.junit.Test)

Example 8 with Statement

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

the class TestParser method testRedir.

@Test
public void testRedir() {
    Program x = new Parser("echo foo&>bar").program();
    Statement s0 = (Statement) x.tokens().get(0);
    assertEquals("echo", s0.tokens().get(0).toString());
    assertEquals("foo", s0.tokens().get(1).toString());
    assertEquals("&>", s0.redirections().get(0).toString());
    assertEquals("bar", s0.redirections().get(1).toString());
    x = new Parser("echo foo1>bar").program();
    s0 = (Statement) x.tokens().get(0);
    assertEquals("echo", s0.tokens().get(0).toString());
    assertEquals("foo1", s0.tokens().get(1).toString());
    assertEquals(">", s0.redirections().get(0).toString());
    assertEquals("bar", s0.redirections().get(1).toString());
    x = new Parser("echo foo 1>bar").program();
    s0 = (Statement) x.tokens().get(0);
    assertEquals("echo", s0.tokens().get(0).toString());
    assertEquals("foo", s0.tokens().get(1).toString());
    assertEquals("1>", s0.redirections().get(0).toString());
    assertEquals("bar", s0.redirections().get(1).toString());
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Test(org.junit.Test)

Example 9 with Statement

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

the class KarafParser method doParse.

private ParsedLine doParse(String line, int cursor, ParseContext parseContext) throws SyntaxError {
    Program program = null;
    List<Statement> statements = null;
    String repaired = line;
    while (program == null) {
        try {
            org.apache.felix.gogo.runtime.Parser parser = new org.apache.felix.gogo.runtime.Parser(repaired);
            program = parser.program();
            statements = parser.statements();
        } catch (EOFError e) {
            // Make sure we don't loop forever
            if (parseContext == ParseContext.COMPLETE && repaired.length() < line.length() + 1024) {
                repaired = repaired + " " + e.repair();
            } else {
                throw e;
            }
        }
    }
    // 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 && statement.tokens() != null && !statement.tokens().isEmpty()) {
        String cmdName = session.resolveCommand(statement.tokens().get(0).toString());
        String[] parts = cmdName.split(":");
        Command cmd = parts.length == 2 ? session.getRegistry().getCommand(parts[0], parts[1]) : null;
        Parser cmdParser = cmd != null ? cmd.getParser() : null;
        if (cmdParser != null) {
            final CommandLine cmdLine = cmdParser.parse(session, statement.toString(), cursor - statement.start());
            return new ParsedLine() {

                @Override
                public String word() {
                    return cmdLine.getCursorArgument();
                }

                @Override
                public int wordCursor() {
                    return cmdLine.getArgumentPosition();
                }

                @Override
                public int wordIndex() {
                    return cmdLine.getCursorArgumentIndex();
                }

                @Override
                public List<String> words() {
                    return Arrays.asList(cmdLine.getArguments());
                }

                @Override
                public String line() {
                    return cmdLine.getBuffer();
                }

                @Override
                public int cursor() {
                    return cmdLine.getBufferPosition();
                }
            };
        }
        if (repaired != line) {
            Token stmt = statement.subSequence(0, line.length() - statement.start());
            List<Token> tokens = new ArrayList<>(statement.tokens());
            Token last = tokens.get(tokens.size() - 1);
            tokens.set(tokens.size() - 1, last.subSequence(0, line.length() - last.start()));
            return new ParsedLineImpl(program, stmt, cursor, tokens);
        }
        return new ParsedLineImpl(program, statement, cursor, statement.tokens());
    } else {
        // TODO:
        return new ParsedLineImpl(program, program, cursor, Collections.singletonList(program));
    }
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) ArrayList(java.util.ArrayList) Token(org.apache.felix.gogo.runtime.Token) Parser(org.apache.karaf.shell.api.console.Parser) CommandLine(org.apache.karaf.shell.api.console.CommandLine) Command(org.apache.karaf.shell.api.console.Command) ParsedLineImpl(org.apache.felix.gogo.jline.ParsedLineImpl) ParsedLine(org.jline.reader.ParsedLine) EOFError(org.apache.felix.gogo.runtime.EOFError)

Example 10 with Statement

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

the class KarafParser 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 && statement.tokens() != null && !statement.tokens().isEmpty()) {
        String cmdName = session.resolveCommand(statement.tokens().get(0).toString());
        String[] parts = cmdName.split(":");
        Command cmd = parts.length == 2 ? session.getRegistry().getCommand(parts[0], parts[1]) : null;
        Parser cmdParser = cmd != null ? cmd.getParser() : null;
        if (cmdParser != null) {
            final CommandLine cmdLine = cmdParser.parse(session, statement.toString(), cursor - statement.start());
            return new ParsedLine() {

                @Override
                public String word() {
                    return cmdLine.getCursorArgument();
                }

                @Override
                public int wordCursor() {
                    return cmdLine.getArgumentPosition();
                }

                @Override
                public int wordIndex() {
                    return cmdLine.getCursorArgumentIndex();
                }

                @Override
                public List<String> words() {
                    return Arrays.asList(cmdLine.getArguments());
                }

                @Override
                public String line() {
                    return cmdLine.getBuffer();
                }

                @Override
                public int cursor() {
                    return cmdLine.getBufferPosition();
                }
            };
        }
        return new ParsedLineImpl(program, statement, cursor, statement.tokens());
    } else {
        // TODO:
        return new ParsedLineImpl(program, program, cursor, Collections.singletonList(program));
    }
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Parser(org.apache.karaf.shell.api.console.Parser) CommandLine(org.apache.karaf.shell.api.console.CommandLine) Command(org.apache.karaf.shell.api.console.Command) ParsedLineImpl(org.apache.felix.gogo.jline.ParsedLineImpl) ParsedLine(org.jline.reader.ParsedLine)

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