@Override public Optional<String> rewrite(String input) throws Exception { final String[] pieces = input.split(" "); for (int idx = 0; idx < pieces.length; idx++) { final Optional<String> optional = delegate.rewrite(pieces[idx]); if (optional.isPresent()) { pieces[idx] = optional.get(); } } return Optional.of(Joiner.on(" ").join(pieces)); }
private static void reduce(String text, RewriteMode rwMode) { try { Parser parser = new Parser(text); Automaton automaton = new Automaton(); int root = parser.parse(automaton); automaton.setRoot(0, root); PrettyAutomataWriter writer = new PrettyAutomataWriter(System.out, Arithmetic.SCHEMA, "Or", "And"); System.out.println("------------------------------------"); writer.write(automaton); writer.flush(); Rewriter rw; switch (rwMode) { case SIMPLE: rw = new SimpleRewriter(Arithmetic.inferences, Arithmetic.reductions, Arithmetic.SCHEMA); break; case STATIC_DISPATCH: rw = new StaticDispatchRewriter( Arithmetic.inferences, Arithmetic.reductions, Arithmetic.SCHEMA); break; default: rw = null; } rw.apply(automaton); System.out.println("\n\n=> (" + rw.getStats() + ")\n"); writer.write(automaton); writer.flush(); System.out.println("\n"); } catch (RuntimeException e) { // Catching runtime exceptions is actually rather bad style; // see lecture about Exceptions later in the course! System.err.println("error: " + e.getMessage()); e.printStackTrace(System.err); System.err.println("Type \"help\" for help"); } catch (IOException e) { System.err.println("I/O Exception?"); } }
protected ParseTreeNode emulateIE6FunctionConstructors(ParseTreeNode node) { final Rewriter w = new Rewriter(mq, true, false) { /* concrete */ }; w.addRule( new Rule() { @Override @RuleDescription( name = "blockScope", reason = "Set up the root scope and handle block scope statements", synopsis = "") public ParseTreeNode fire(ParseTreeNode node, Scope scope) { if (node instanceof Block) { Scope s2; if (scope == null) { s2 = Scope.fromProgram((Block) node, w); } else { s2 = Scope.fromPlainBlock(scope); } return QuasiBuilder.substV( "@startStmts*; @body*;", "startStmts", new ParseTreeNodeContainer(s2.getStartStatements()), "body", expandAll(new ParseTreeNodeContainer(node.children()), s2)); } return NONE; } }); w.addRule( new Rule() { @Override @RuleDescription( name = "fnDeclarations", reason = "function declarations contain function constructors but don't" + " have the same discrepencies on IE 6 as function constructors", synopsis = "") public ParseTreeNode fire(ParseTreeNode node, Scope scope) { if (node instanceof FunctionDeclaration) { FunctionDeclaration decl = ((FunctionDeclaration) node); FunctionConstructor ctor = decl.getInitializer(); Scope s2 = Scope.fromFunctionConstructor(scope, ctor); FunctionConstructor rewritten = (FunctionConstructor) QuasiBuilder.substV( "function @ident(@formals*) { @stmts*; @body*; }", "ident", ctor.getIdentifier(), "formals", expandAll(new ParseTreeNodeContainer(ctor.getParams()), s2), "stmts", new ParseTreeNodeContainer(s2.getStartStatements()), "body", expandAll(new ParseTreeNodeContainer(ctor.getBody().children()), s2)); return new FunctionDeclaration(rewritten); } return NONE; } }); w.addRule( new Rule() { @Override @RuleDescription( name = "ie6functions", reason = "simulate IE 6's broken scoping of function constructors as " + "described in JScript Deviations Section 2.3", synopsis = "") public ParseTreeNode fire(ParseTreeNode node, Scope scope) { if (node instanceof FunctionConstructor) { FunctionConstructor ctor = (FunctionConstructor) node; Scope s2 = Scope.fromFunctionConstructor(scope, ctor); if (ctor.getIdentifierName() == null) { return expandAll(node, s2); } Identifier ident = ctor.getIdentifier(); Reference identRef = new Reference(ident); identRef.setFilePosition(ident.getFilePosition()); scope.addStartStatement(new Declaration(FilePosition.UNKNOWN, ident, identRef)); return QuasiBuilder.substV( "(@var = function @ident(@formals*) { @stmts*; @body*; })", "var", identRef, "ident", ident, "formals", new ParseTreeNodeContainer(ctor.getParams()), "stmts", new ParseTreeNodeContainer(s2.getStartStatements()), "body", expandAll(new ParseTreeNodeContainer(ctor.getBody().children()), s2)); } return NONE; } }); w.addRule( new Rule() { @Override @RuleDescription( name = "catchAll", reason = "Handles non function constructors.", synopsis = "") public ParseTreeNode fire(ParseTreeNode node, Scope scope) { return expandAll(node, scope); } }); return w.expand(node); }