public ParserResult parse(String source) { List<String> inputList = Arrays.asList(source.split("\n", -1)); ListIterator<String> inputIterator = inputList.listIterator(); while (inputIterator.hasNext()) { String line = inputIterator.next(); if (line.isEmpty() || line.matches(PlotConstants.REGEX_COMMENT)) { /* ignore empty lines and comments */ } else if (line.matches(PlotConstants.REGEX_PLOT)) { parserResult.addPlotState(createPlotStateObject(line.split(" "), inputIterator)); } else if (line.matches(PlotConstants.REGEX_PLOT_ADD)) { List<PlotState> plotStates = parserResult.getPlotStateList(); if (plotStates.isEmpty()) { // if no plotStates, create a new one parserResult.addPlotState(createPlotStateObject(line.split(" "), inputIterator)); } else { // if plots exist, add new plotState to last plotState PlotState last = plotStates.get(plotStates.size() - 1); last.addSubPlot(createPlotStateObject(line.split(" "), inputIterator)); } } else if (line.matches(PlotConstants.REGEX_DATA)) { createDatasetObject(line.split(" "), inputIterator); } else if (line.matches(PlotConstants.REGEX_DATA_GUESS)) { inputIterator .previous(); // Must go 1 step back to avoid skipping the first line in // createDatasetObject createDatasetObject(new String[] {PlotConstants.DATA}, inputIterator); } else if (line.matches(PlotConstants.REGEX_VALUE_ASSIGNMENT)) { createKeyValueAssignment(line, inputIterator.nextIndex()); } else { throw new ParserException( "Invalid line: " + line + "(line: " + inputIterator.nextIndex() + ")"); } } analyseDatasets(); addDatasetsToPlotStates(); return parserResult; }