Search in sources :

Example 6 with Program

use of org.apache.felix.gogo.runtime.Parser.Program 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 7 with Program

use of org.apache.felix.gogo.runtime.Parser.Program 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 8 with Program

use of org.apache.felix.gogo.runtime.Parser.Program 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 9 with Program

use of org.apache.felix.gogo.runtime.Parser.Program 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)

Example 10 with Program

use of org.apache.felix.gogo.runtime.Parser.Program 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)

Aggregations

Program (org.apache.felix.gogo.runtime.Parser.Program)12 Statement (org.apache.felix.gogo.runtime.Parser.Statement)12 Test (org.junit.Test)8 Pipeline (org.apache.felix.gogo.runtime.Parser.Pipeline)5 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 ArrayList (java.util.ArrayList)1 Sequence (org.apache.felix.gogo.runtime.Parser.Sequence)1 SyntaxError (org.apache.felix.gogo.runtime.SyntaxError)1 Function (org.apache.felix.service.command.Function)1 RegionType (org.jline.reader.LineReader.RegionType)1 AttributedString (org.jline.utils.AttributedString)1 AttributedStringBuilder (org.jline.utils.AttributedStringBuilder)1