From a69a34353c75b7f047e09b4293f4363af4bd26af Mon Sep 17 00:00:00 2001 From: Livio <193267+javalc6@users.noreply.github.com> Date: Tue, 13 Jun 2023 10:22:49 +0200 Subject: [PATCH] code optimization --- wiki/TemplateParser.java | 69 ++++++++++---------------------- wiki/TestSuite.java | 2 +- wiki/parserfunctions/Invoke.java | 2 +- wiki/tools/WikiScanner.java | 59 +-------------------------- 4 files changed, 24 insertions(+), 108 deletions(-) diff --git a/wiki/TemplateParser.java b/wiki/TemplateParser.java index 04461b4..5125336 100644 --- a/wiki/TemplateParser.java +++ b/wiki/TemplateParser.java @@ -40,7 +40,6 @@ The class TemplateParser implements light wiki template parser. */ final public class TemplateParser { - final static boolean failsafe_parser = true;//must be true in production! false is for debug/test purposes only private final static String template_label = "Template:"; private final static String lc_template_label = template_label.toLowerCase(); @@ -55,11 +54,13 @@ public String parse(String string, WikiPage wp) {//external return sb.toString(); } - public String parseParameter(String string, WikiPage wp, Frame parent) {//internal usage (used only by parserfunctions) - StringBuilder sb = new StringBuilder(); - WikiScanner sh = new WikiScanner(string); - template_body(sh, sb, wp, parent); - return sb.toString().trim(); + public String parseParameter(String string, WikiPage wp, Frame parent) {//internal usage (used by parserfunctions) + if (string.contains("{{")) { + StringBuilder sb = new StringBuilder(); + WikiScanner sh = new WikiScanner(string); + template_body(sh, sb, wp, parent); + return sb.toString(); + } else return string; } //template_body ::= [simple_text] { (parameter_holder | invocation ) [simple_text] }* [any text] @@ -71,7 +72,6 @@ private void template_body(WikiScanner sh, StringBuilder sb, WikiPage wp, Frame int pointer = sh.getPointer(); //save pointer to be ready to retract in case of missing }} if (sh.getChar('{')) { -//parameter_holder ::= "{{{" (parameter_name) [ "|" [default value] ] "}}}" -- default value if (parameter_holder(sh, sb, wp, parent)) { String str = sh.getStringWithoutOpening();//twin if (str != null) @@ -83,35 +83,6 @@ private void template_body(WikiScanner sh, StringBuilder sb, WikiPage wp, Frame sb.append("{");//save orphan { as literal continue; } - - int p1 = sh.getPointer(); - int p2 = sh.findNested(p1);//handling of nested template calls like {{la{{#if:1|be|re}}l|en|activity}} - if (p2 != -1) { - sh.setPointer(p2 + 2); - - String rep = null; - if (sh.getChar('{')) { -//parameter_holder ::= "{{{" (parameter_name) [ "|" [default value] ] "}}}" -- default value - StringBuilder sb2 = new StringBuilder(); - if (parameter_holder(sh, sb2, wp, parent)) { - String str = sh.getStringWithoutOpening();//twin - if (str != null) - sb2.append(str); - rep = sb2.toString(); - }// else rep = null; - } else { - rep = invocation_body(sh, wp, parent); - } - if (rep == null) { - sh.dumpString("Unexpected error, probably caused by unbalanced curly brackets"); - sh.setPointer(p2); - } else { - int end = sh.getPointer(); - sh.replaceNested(p2, end, rep); - sh.setPointer(p1); - } - } - String p = invocation_body(sh, wp, parent); if (p != null) { sb.append(p); @@ -154,12 +125,12 @@ private String invocation_body(WikiScanner sh, WikiPage wp, Frame parent) { //magic_word_call ::= magic_word [ ":" magic_parameter] //parser_function_call ::= parser_function_name ":" parser_function_parameter { "|" [parser_function_parameter] }* //template_call ::= template_identifier { "|" [template_parameter] }* - StringBuilder sb = new StringBuilder(); int pointer0 = sh.getPointer(); //save pointer to be ready to retract - String identifier = sh.getTemplateIdentifier(); + String identifier = sh.getStringParameter(null); if (identifier == null) return null; + identifier = identifier.trim(); if (identifier.startsWith(":")) {//ignore transclusion of ordinary wiki page return null; } @@ -171,6 +142,7 @@ private String invocation_body(WikiScanner sh, WikiPage wp, Frame parent) { identifier = identifier.substring("safesubst:".length()); pointer0 += "safesubst:".length(); } + identifier = parseParameter(identifier, wp, parent); int pointer = sh.getPointer(); //save pointer to be ready to retract in case of invalid magic word or parser function //check & process magic word int idx = identifier.indexOf(":"); @@ -183,7 +155,7 @@ private String invocation_body(WikiScanner sh, WikiPage wp, Frame parent) { sh.moveAfter(":");//move after : to get parameter String param = sh.getStringParameter(null); - parameter = param == null ? "" : parseParameter(param, wp, parent); + parameter = param == null ? "" : parseParameter(param, wp, parent).trim(); while (sh.getChar('|')) {//ignore any further parameter(s) sh.getStringParameter(null); @@ -191,10 +163,9 @@ private String invocation_body(WikiScanner sh, WikiPage wp, Frame parent) { } if (sh.getSequence("}}")) { String result = MagicWords.evaluate(mw, parameter, wp.getPagename(), wp.getRevision()); - if (result != null) { - sb.append(result); - return sb.toString(); - } else sh.setPointer(pointer);//retract scanner + if (result != null) + return result; + sh.setPointer(pointer);//retract scanner } else return null; } //check & process parser function call @@ -232,10 +203,10 @@ private String invocation_body(WikiScanner sh, WikiPage wp, Frame parent) { String paramx = sh.getStringParameter(equalPos); String value = ""; if (paramx != null) { - idx = equalPos[0];//findValidEqualSign(paramx); + idx = equalPos[0]; // System.out.println("parameter splitting: " + paramx + ", idx= " + idx); if (idx != -1 && !(param_name = paramx.substring(0, idx).trim()).isEmpty()) {//named parameter - value = paramx.substring(idx + 1);//skip "=" + value = paramx.substring(idx + 1).trim();//skip "=" } else {//unnamed parameter value = paramx; } @@ -248,9 +219,11 @@ private String invocation_body(WikiScanner sh, WikiPage wp, Frame parent) { if (sh.getSequence("}}")) { return getParsedTemplate(identifier.replace('_', ' '), wp, parameterMap, parent); } else return null; - } else if (failsafe_parser) + } else {//unexpected identifier + if (wp.getTrace_calls()) + System.out.println("unexpected identifier:" + identifier); return null; - else throw new RuntimeException("unexpected identifier: " + identifier); + } } public String getParsedTemplate(String identifier, WikiPage wp, Map parameterMap, Frame parent) { @@ -332,4 +305,4 @@ private static String delete_comments(String str) { // delete html comments b}}", wp, "ab"); testEvaluate(tp, "{{echo|[[ab|cd]]}}", wp, "[[ab|cd]]"); - testEvaluate(tp, "a{{ compose3 | alfa | beta | gamma }}b", wp, "aalfabetagammab"); + testEvaluate(tp, "a{{ compose3 | alfa | beta | gamma }}b", wp, "a alfa beta gamma b"); testEvaluate(tp, "{{subst:temPLAte:echo|alfa}}", wp, "alfa"); testEvaluate(tp, "{{nested|r={{echo|c}}|q={{echo|b}}|p={{subst:temPLAte:echo|a}}}}", wp, "abc"); testEvaluate(tp, "{{checkparam}}", wp, "No text in param"); diff --git a/wiki/parserfunctions/Invoke.java b/wiki/parserfunctions/Invoke.java index bda3f44..f5d49e2 100644 --- a/wiki/parserfunctions/Invoke.java +++ b/wiki/parserfunctions/Invoke.java @@ -61,7 +61,7 @@ public String evaluate(WikiPage wp, ArrayList parameters, Frame parent) int idx = findValidEqualSign(param); //System.out.println("i=" + i + ", parameter splitting: " + param + ", idx= " + idx); if (idx != -1) { - parameterMap.put(param.substring(0, idx).trim(), param.substring(idx + 1)); + parameterMap.put(param.substring(0, idx).trim(), param.substring(idx + 1).trim()); } else { parameterMap.put(Integer.toString(pos++), param);//unnamed parameter } diff --git a/wiki/tools/WikiScanner.java b/wiki/tools/WikiScanner.java index cdac5f0..d8204c9 100644 --- a/wiki/tools/WikiScanner.java +++ b/wiki/tools/WikiScanner.java @@ -28,7 +28,7 @@ This class is an helper for parsing wiki text */ final public class WikiScanner { - protected String str; + protected final String str;//immutable protected int pointer; public WikiScanner(String str) { @@ -200,45 +200,6 @@ public String getStringParameter(int[] equalPos) {//returns parameter, if any return null; } - public int findNested(int start) {//17-05-2023: handling of nested invocations -// System.out.println("findNested-->"+start); - int end = str.length(); - int p2 = findTarget("{{", start); - if (p2 != -1) { -// if (str.charAt(p2 + 2) == '{')//serve ad skippare eventuali parametri {{{param}}} -// return -1; - int close = findTarget("}}", start); - if ((close == -1) || (close < p2)) - return -1; - int bar = findTarget("|", start); - if ((bar == -1) || (bar > p2)) - return p2; - } - return -1; - } - - public void replaceNested(int start, int end, String rep) {//17-05-2023: handling of nested invocations -// System.out.println("replaceNested"); - str = str.substring(0, start) + rep + str.substring(end); - } - - public String getIdentifier() {//returns identifier, if present at current position - Character ch = null; - while ((pointer < str.length()) && isWikiSpace(ch = str.charAt(pointer++))) - ; - if (ch != null) { - if (Character.isLetter(ch)) { - StringBuilder sb = new StringBuilder().append(ch); - while ((pointer < str.length()) && (Character.isLetter(ch = str.charAt(pointer)))) { - sb.append(ch); pointer++; - } - return sb.toString().trim(); - } - pointer--;//retract pointer if ch is not a letter - } - return null; - } - public String getIdentifierOrNumber() {//returns identifier or number, if present at current position Character ch = null; while ((pointer < str.length()) && isWikiSpace(ch = str.charAt(pointer++))) @@ -254,24 +215,6 @@ public String getIdentifierOrNumber() {//returns identifier or number, if presen return null; } - public String getTemplateIdentifier() {//returns template identifier, if present at current position - Character ch = null; - while ((pointer < str.length()) && isWikiSpace(ch = str.charAt(pointer++))) - ; - if (ch != null) { - String restricted_charset = "<>[]|{}";//note: # is allowed, because this method is used also for parser functions - if (restricted_charset.indexOf(ch) == -1) { - StringBuilder sb = new StringBuilder().append(ch); - while ((pointer < str.length()) && (restricted_charset.indexOf(ch = str.charAt(pointer)) == -1)) { - sb.append(ch); pointer++; - } - return sb.toString().trim(); - } - pointer--;//retract pointer if ch is not a letter - } - return null; - } - public boolean moveAfter(String marker) {//moves after marker, if present returns true, otherwise does not move and returns false int idx = str.indexOf(marker, pointer); if (idx != -1) {