DefaultInterpreterParser { public ParsedCommand parseCommand(ParserBuffer reader) throws IOException, SoarException { startOfCommand = -1; lineOfCommand = -1; final List<String> result = new ArrayList<String>(); skipWhitespaceAndComments(reader); if(!atEndOfCommand(reader)) { String word = parseWord(reader); while(word != null) { result.add(word); if(atEndOfCommand(reader)) { break; } skipWhitespaceAndComments(reader); word = parseWord(reader); } } final SourceLocation loc = DefaultSourceLocation.newBuilder(). file(reader.getFile()). offset(startOfCommand). length(reader.getCurrentOffset() - startOfCommand). line(lineOfCommand). build(); return new ParsedCommand(loc, result); }  ParsedCommand parseCommand(ParserBuffer reader); void skipWhitespace(ParserBuffer reader); void skipWhitespaceAndComments(ParserBuffer reader); String parseWord(ParserBuffer reader);  }
@Test public void testParseCommandTerminatedByEndOfFile() throws Exception { final ParserBuffer reader = reader(" watch {5}"); assertEquals(Arrays.asList("watch", "5"), parser.parseCommand(reader).getArgs()); checkRemainder("", reader); }
@Test public void testSpCommandParseCommandTerminatedByCarriageReturn() throws Exception { final ParserBuffer reader = reader(" sp {test\n(state <s> ^superstate nil)\n-->\r\n(write (crlf))\n}\r\nx"); assertEquals(Arrays.asList("sp", "test\n(state <s> ^superstate nil)\n-->\r\n(write (crlf))\n"), parser.parseCommand(reader).getArgs()); checkRemainder("\r\nx", reader); }
@Test public void testParseCommandTerminatedByLineFeed() throws Exception { final ParserBuffer reader = reader(" watch {5}\nx"); assertEquals(Arrays.asList("watch", "5"), parser.parseCommand(reader).getArgs()); checkRemainder("\nx", reader); }
@Test public void testParseCommandTerminatedByComment() throws Exception { final ParserBuffer reader = reader(" watch {5} #comment"); assertEquals(Arrays.asList("watch", "5"), parser.parseCommand(reader).getArgs()); checkRemainder("#comment", reader); }
@Test public void testParseCommandReturnsEmptyListAtEof() throws Exception { final ParserBuffer reader = reader(" #comment \n"); final ParsedCommand parsedCommand = parser.parseCommand(reader); assertTrue(parsedCommand.isEof()); assertTrue(parsedCommand.getArgs().isEmpty()); checkRemainder("", reader); }
@Test public void testCanParseSemicolon() throws Exception { final ParserBuffer reader = reader("sp {test (state <s> ^superstate nil) --> (<s> ^test true)}; watch {5}"); assertEquals(Arrays.asList("sp", "test (state <s> ^superstate nil) --> (<s> ^test true)"), parser.parseCommand(reader).getArgs()); checkRemainder(" watch {5}", reader); }
@Test public void testCanParseTrailingSemicolon() throws Exception { final ParserBuffer reader = reader("sp {test (state <s> ^superstate nil) --> (<s> ^test true)};"); assertEquals(Arrays.asList("sp", "test (state <s> ^superstate nil) --> (<s> ^test true)"), parser.parseCommand(reader).getArgs()); checkRemainder("", reader); }
@Test public void testCanParseMultipleCommandsOnOneLine() throws Exception { final ParserBuffer reader = reader("sp {test (state <s> ^superstate nil) --> (<s> ^test true)}; watch {5}"); assertEquals(Arrays.asList("sp", "test (state <s> ^superstate nil) --> (<s> ^test true)"), parser.parseCommand(reader).getArgs()); assertEquals(Arrays.asList("watch", "5"), parser.parseCommand(reader).getArgs()); checkRemainder("", reader); }
@Test public void testCanParseSemicolonHash() throws Exception { final ParserBuffer reader = reader("sp {test (state <s> ^superstate nil) --> (<s> ^test true)} ;# a comment"); assertEquals(Arrays.asList("sp", "test (state <s> ^superstate nil) --> (<s> ^test true)"), parser.parseCommand(reader).getArgs()); checkRemainder("# a comment", reader); }
@Test public void testCanParseSemicolonWithinCommand() throws Exception { final ParserBuffer reader = reader("sp {test (state <s> ^superstate nil;) --> (<s> ^test true)};"); assertEquals(Arrays.asList("sp", "test (state <s> ^superstate nil;) --> (<s> ^test true)"), parser.parseCommand(reader).getArgs()); checkRemainder("", reader); }
@Test public void testCanParseEmptyCommand() throws Exception { final ParserBuffer reader = reader(";"); assertEquals(Arrays.asList(), parser.parseCommand(reader).getArgs()); checkRemainder("", reader); }
@Test public void testCanParseEmptyCommands() throws Exception { final ParserBuffer reader = reader(";;;;"); assertEquals(Arrays.asList(), parser.parseCommand(reader).getArgs()); checkRemainder(";;;", reader); }