From 0088fda87508ade459d8b0f6fc3311e6decd5724 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sun, 8 May 2016 17:26:07 +0200 Subject: PicoRec: propagate errors --- src/PicoRec.java | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'src/PicoRec.java') diff --git a/src/PicoRec.java b/src/PicoRec.java index f601ce3..191d2f8 100644 --- a/src/PicoRec.java +++ b/src/PicoRec.java @@ -48,18 +48,11 @@ public class PicoRec { /** * Try to parse the input. * - * @return true if the language is recognized, false otherwise. + * @throws ParseError if the language is not recognized. */ - public boolean parse(String text) { + public void parse(String text) { input = text; offset = 0; - try { - // TODO implement this - return true; - } catch (ParseError ex) { - System.err.println("Parsing failed due to: " + ex); - return false; - } } /** Skip layout characters. */ @@ -111,9 +104,9 @@ public class PicoRec { } /** Exception that is raised for parser errors. */ - private static class ParseError extends RuntimeException { + public static class ParseError extends RuntimeException { private static final long serialVersionUID = 1L; - ParseError(String message) { + private ParseError(String message) { super(message); } } @@ -122,10 +115,16 @@ public class PicoRec { /* Convenience functions for testing. */ private void check(String input, boolean expectOk) { - boolean accepted = parse(input); System.out.println("Testing input: " + input); - if (expectOk && !accepted || !expectOk && accepted) { - throw new RuntimeException("Unexpected result for " + input); + try { + parse(input); + if (!expectOk) { + throw new RuntimeException("Unexpected pass for " + input); + } + } catch (ParseError ex) { + if (expectOk) { + throw new RuntimeException(ex); + } } } -- cgit v1.2.1