All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fitnesse.wikitext.parser.ListRule Maven / Gradle / Ivy

There is a newer version: 20241026
Show newest version
package fitnesse.wikitext.parser;

public class ListRule implements Rule {
    @Override
    public Maybe parse(Symbol current, Parser parser) {
        return populateList(parser, current);
    }

    private Maybe populateList(Parser parser, Symbol list) {
        Symbol nextSymbol = list;
        while (isList(nextSymbol)) {
            if (indent(nextSymbol) < indent(list)) break;
            if (nextSymbol != list) parser.moveNext(1);
            if (indent(nextSymbol) > indent(list)) {
                Maybe subList = populateList(parser, nextSymbol);
                if (subList.isNothing()) return Symbol.nothing;
                list.lastChild().add(subList.getValue());
            }
            else {
                Symbol body = makeListBody(parser);
                if (parser.atEnd()) return Symbol.nothing;
                list.add(body);
            }
            nextSymbol = parser.peek();
        }
        return new Maybe<>(list);
    }

    private static boolean isList(Symbol symbol) {
        return symbol.isType(SymbolType.OrderedList) || symbol.isType(SymbolType.UnorderedList);
    }

    private Symbol makeListBody(Parser parser) {
        while (!parser.peek(new SymbolType[]{SymbolType.Whitespace}).isEmpty()) {
            parser.moveNext(1);
        }
        return parser.parseTo(SymbolType.Newline, 1);
    }

    private int indent(Symbol symbol) {
        String content = symbol.getContent();
        int result = 0;
        while (Character.isWhitespace(content.charAt(result))) result++;
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy