From 8c289f05ca36c3def6a4e4ab2414b7469c03339e Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Sun, 21 Jul 2019 16:24:47 -0300 Subject: Refactor front-end error-handling This refactors the front-end to use a tree for capturing errors, instead of throwing exceptions. This has the benefit that you will receive notifications about all of the error messages you have, instead of only the first. I'm a bit fuzzy on the details, since it's been a while since I wrote these changes. --- .../rgens/parser/templates/GrammarTemplate.java | 74 +++++++++++++++++++--- 1 file changed, 65 insertions(+), 9 deletions(-) (limited to 'src/main/java/bjc/rgens/parser/templates/GrammarTemplate.java') diff --git a/src/main/java/bjc/rgens/parser/templates/GrammarTemplate.java b/src/main/java/bjc/rgens/parser/templates/GrammarTemplate.java index 8a99188..64db166 100644 --- a/src/main/java/bjc/rgens/parser/templates/GrammarTemplate.java +++ b/src/main/java/bjc/rgens/parser/templates/GrammarTemplate.java @@ -2,25 +2,58 @@ package bjc.rgens.parser.templates; import bjc.rgens.parser.ConfigSet; import bjc.rgens.parser.GenerationState; +import bjc.rgens.parser.LoadOptions; + +import bjc.utils.data.ITree; +import bjc.utils.data.Tree; import java.io.Reader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; +/** + * Represents a grammar template. + * + * @author Ben Culkin + */ public class GrammarTemplate { + /** + * The config set the template belongs to. + */ public ConfigSet belongsTo; + /** + * The name of the template. + */ public String name; + /** + * The elements in the template. + */ public final List elements; + /** + * Whether or not to do spacing of elements. + */ public boolean doSpacing = true; + /** + * Create a new grammar template. + * + * @param elements + * The elements that belong to the template. + */ public GrammarTemplate(List elements) { this.elements = elements; } + /** + * Generate the template. + * + * @param state + * The state for generating a template. + */ public void generate(GenerationState state) { for(TemplateElement element : elements) { element.generate(state); @@ -30,7 +63,18 @@ public class GrammarTemplate { } } - public static GrammarTemplate readTemplate(Reader rdr) { + /** + * Read a template from an input source. + * + * @param rdr + * The reader to get input from. + * + * @param errs + * The errors/information to generate during loading. + * + * @return The generated template. + */ + public static GrammarTemplate readTemplate(Reader rdr, ITree errs) { List elements = new ArrayList<>(); GrammarTemplate template = new GrammarTemplate(elements); @@ -42,34 +86,46 @@ public class GrammarTemplate { String ln = scn.nextLine(); lno += 1; + ITree kid = new Tree<>(String.format("INFO: Line %d", lno)); switch(ln.charAt(0)) { case '#': // Ignore comments break; case '/': - handlePragma(elements, template, ln.substring(1)); + handlePragma(elements, template, ln.substring(1), kid); break; default: - handleLine(elements, template, ln); + handleLine(elements, template, ln, kid); } - } + if (kid.size() > 0) { + errs.addChild(kid); + } + } return template; } - private static void handleLine(List elements, GrammarTemplate template, String ln) { + private static void handleLine(List elements, GrammarTemplate template, String ln, ITree errs) { if(ln.matches("^.*?\\$@.+?@\\$.*$")) { /* * Handle live templates */ - elements.add(new LiveTemplateElement(ln)); + elements.add(new LiveTemplateElement(ln, errs)); } else { - elements.add(new LiteralTemplateElement(ln)); + elements.add(new LiteralTemplateElement(ln, errs)); } } - private static void handlePragma(List elements, GrammarTemplate template, String ln) { - + private static void handlePragma(List elements, GrammarTemplate template, String ln, ITree errs) { + /* + * @TODO 2/8/2019 Ben Culkin :TemplatePragmas + * Implement template pragmas. + * + * Implement template pragmas. Mainly, this means that the 'choose' + * based ones need to be implemented based off of the provided sample + * template. + */ + errs.addChild("ERROR: Template pragmas are not yet implemented"); } } -- cgit v1.2.3