A compiler for a single template.
| 47 | |
| 48 | |
| 49 | public class Compiler { |
| 50 | public static final String SUBTEMPLATE_PREFIX = "_sub"; |
| 51 | public static final int TEMPLATE_INITIAL_CODE_SIZE = 15; |
| 52 | public static final Map<String, Interpreter.Option> supportedOptions = new HashMap<String, Interpreter.Option>() {{ |
| 53 | put("anchor", Interpreter.Option.ANCHOR); |
| 54 | put("format", Interpreter.Option.FORMAT); |
| 55 | put("null", Interpreter.Option.NULL); |
| 56 | put("separator", Interpreter.Option.SEPARATOR); |
| 57 | put("wrap", Interpreter.Option.WRAP); |
| 58 | }}; |
| 59 | public static final int NUM_OPTIONS = supportedOptions.size(); |
| 60 | public static final Map<String, String> defaultOptionValues = new HashMap<String, String>() {{ |
| 61 | put("anchor", "true"); |
| 62 | put("wrap", "\n"); |
| 63 | }}; |
| 64 | public static Map<String, Short> funcs = new HashMap<String, Short>() {{ |
| 65 | put("first", Bytecode.INSTR_FIRST); |
| 66 | put("last", Bytecode.INSTR_LAST); |
| 67 | put("rest", Bytecode.INSTR_REST); |
| 68 | put("trunc", Bytecode.INSTR_TRUNC); |
| 69 | put("strip", Bytecode.INSTR_STRIP); |
| 70 | put("trim", Bytecode.INSTR_TRIM); |
| 71 | put("length", Bytecode.INSTR_LENGTH); |
| 72 | put("strlen", Bytecode.INSTR_STRLEN); |
| 73 | put("reverse", Bytecode.INSTR_REVERSE); |
| 74 | }}; |
| 75 | |
| 76 | /** Name subtemplates {@code _sub1}, {@code _sub2}, ... */ |
| 77 | public static int subtemplateCount = 0; |
| 78 | public STGroup group; |
| 79 | |
| 80 | public Compiler() { |
| 81 | this(STGroup.defaultGroup); |
| 82 | } |
| 83 | public Compiler(STGroup group) { |
| 84 | this.group = group; |
| 85 | } |
| 86 | |
| 87 | public CompiledST compile(String template) { |
| 88 | CompiledST code = compile(null, null, null, template, null); |
| 89 | code.hasFormalArgs = false; |
| 90 | return code; |
| 91 | } |
| 92 | |
| 93 | /** Compile full template with unknown formal arguments. */ |
| 94 | |
| 95 | public CompiledST compile(String name, String template) { |
| 96 | CompiledST code = compile(null, name, null, template, null); |
| 97 | code.hasFormalArgs = false; |
| 98 | return code; |
| 99 | } |
| 100 | |
| 101 | /** Compile full template with respect to a list of formal arguments. */ |
| 102 | |
| 103 | public CompiledST compile(String srcName, String name, List<FormalArgument> args, String template, Token templateToken) { |
| 104 | ANTLRStringStream is = new ANTLRStringStream(template); |
| 105 | is.name = srcName!=null ? srcName : name; |
| 106 | STLexer lexer; |