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());
}
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());
}
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());
}
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));
}
}
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));
}
}
Aggregations