Search in sources :

Example 1 with Parser

use of org.petitparser.parser.Parser in project MassBank-web by MassBank.

the class RecordParserDefinitionTest method validate.

private <T> T validate(String source, String production) {
    Parser parser = recordparser.build(production).end();
    Result result = parser.parse(source);
    return result.get();
}
Also used : Parser(org.petitparser.parser.Parser) Result(org.petitparser.context.Result)

Example 2 with Parser

use of org.petitparser.parser.Parser in project MassBank-web by MassBank.

the class Validator2 method validate.

public static Record validate(String recordstring, String contributor) {
    // test non standard ASCII chars and print warnings
    for (int i = 0; i < recordstring.length(); i++) {
        if (recordstring.charAt(i) > 0x7F) {
            String[] tokens = recordstring.split("\\r?\\n");
            System.out.println("Warning: non standard ASCII charactet found. This might be an error. Please check carefully.");
            int line = 0, col = 0, offset = 0;
            for (String token : tokens) {
                offset = offset + token.length() + 1;
                if (i < offset) {
                    col = i - (offset - (token.length() + 1));
                    System.out.println(tokens[line]);
                    StringBuilder error_at = new StringBuilder(StringUtils.repeat(" ", tokens[line].length()));
                    error_at.setCharAt(col, '^');
                    System.out.println(error_at);
                    break;
                }
                line++;
            }
        }
    }
    Record record = new Record(contributor);
    Parser recordparser = new RecordParser(record);
    Result res = recordparser.parse(recordstring);
    if (res.isFailure()) {
        System.err.println();
        System.err.println(res.getMessage());
        int position = res.getPosition();
        String[] tokens = recordstring.split("\\n");
        int line = 0, col = 0, offset = 0;
        for (String token : tokens) {
            offset = offset + token.length() + 1;
            if (position < offset) {
                col = position - (offset - (token.length() + 1));
                System.err.println(tokens[line]);
                StringBuilder error_at = new StringBuilder(StringUtils.repeat(" ", col));
                error_at.append('^');
                System.err.println(error_at);
                // record = new Record();
                break;
            }
            line++;
        }
        return null;
    } else
        return record;
}
Also used : RecordParser(massbank.RecordParser) Record(massbank.Record) Parser(org.petitparser.parser.Parser) RecordParser(massbank.RecordParser) Result(org.petitparser.context.Result)

Aggregations

Result (org.petitparser.context.Result)2 Parser (org.petitparser.parser.Parser)2 Record (massbank.Record)1 RecordParser (massbank.RecordParser)1