From 0ea49dd4a52358f053c9be7138c392b16de05899 Mon Sep 17 00:00:00 2001 From: student Date: Fri, 17 Mar 2017 10:49:27 -0400 Subject: Move things around, and start on new parser. --- .../java/bjc/rgens/newparser/RGrammarParser.java | 110 +++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java (limited to 'RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java') diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java new file mode 100644 index 0000000..d60c6d4 --- /dev/null +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java @@ -0,0 +1,110 @@ +package bjc.rgens.newparser; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.LineNumberReader; +import java.io.StringReader; +import java.util.Scanner; + +/** + * Reads {@link RGrammar} from a input stream. + * + * @author student + * + */ +public class RGrammarParser { + /** + * The exception thrown when something goes wrong while parsing a grammar. + * + * @author student + * + */ + public static class GrammarException extends Exception { + /* + * Serialization ID. + */ + private static final long serialVersionUID = -7287427479316953668L; + + /** + * Create a new grammar exception with the specified message. + * + * @param msg + * The message for this exception. + */ + public GrammarException(String msg) { + super(msg); + } + + /** + * Create a new grammar exception with the specified message and cause. + * + * @param msg + * The message for this exception. + * + * @param cause + * The cause of this exception. + */ + public GrammarException(String msg, Exception cause) { + super(msg, cause); + } + } + + /** + * Read a {@link RGrammar} from an input stream. + * + * @param is + * The input stream to read from. + * + * @return The grammar represented by the stream. + * + * @throws GrammarException + * Thrown if the grammar has a syntax error. + */ + public RGrammar readGrammar(InputStream is) throws GrammarException { + LineNumberReader lnReader = new LineNumberReader(new InputStreamReader(is)); + + try (Scanner scn = new Scanner(lnReader)) { + scn.useDelimiter("\\n\\.?\\n"); + + RGrammarBuilder build = new RGrammarBuilder(); + + while (scn.hasNext()) { + String block = scn.next(); + + if (block.startsWith("pragma")) { + handlePragmaBlock(block, build); + } else if (block.startsWith("[")) { + handleRuleBlock(block, build); + } else { + throw new GrammarException(String.format("Unknown block: %s", lnReader.getLineNumber(), block)); + } + } + } catch (GrammarException gex) { + throw new GrammarException(String.format("Error at line %d", lnReader.getLineNumber()), gex); + } + + return null; + } + + private void handlePragmaBlock(String block, RGrammarBuilder build) throws GrammarException { + LineNumberReader lnReader = new LineNumberReader(new StringReader(block)); + + try (Scanner deblocker = new Scanner(lnReader)) { + deblocker.useDelimiter("\\n(?!\\t)"); + + while (deblocker.hasNext()) { + String pragma = deblocker.next(); + + if (!pragma.startsWith("pragma")) { + throw new GrammarException(String.format("Illegal line at line %d of pragma block: %s", + lnReader.getLineNumber(), pragma)); + } + } + } + } + + private void handleRuleBlock(String block, RGrammarBuilder build) { + // TODO Auto-generated method stub + + } +} -- cgit v1.2.3