Skip to content

Commit

Permalink
code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
javalc6 committed Jun 13, 2023
1 parent 525762b commit a69a343
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 108 deletions.
69 changes: 21 additions & 48 deletions wiki/TemplateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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]
Expand All @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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(":");
Expand All @@ -183,18 +155,17 @@ 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);
}
}
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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<String, String> parameterMap, Frame parent) {
Expand Down Expand Up @@ -332,4 +305,4 @@ private static String delete_comments(String str) { // delete html comments <!--
return text.toString();
}

}
}
2 changes: 1 addition & 1 deletion wiki/TestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private void do_parser_eval_tests(TemplateParser tp) throws ParseException {//au
testEvaluate(tp, "{{echo|a<nowiki>=</nowiki>b}}", wp, "a=b");
testEvaluate(tp, "{{echo|a<!--=-->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");
Expand Down
2 changes: 1 addition & 1 deletion wiki/parserfunctions/Invoke.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public String evaluate(WikiPage wp, ArrayList<String> 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
}
Expand Down
59 changes: 1 addition & 58 deletions wiki/tools/WikiScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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++)))
Expand All @@ -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) {
Expand Down

0 comments on commit a69a343

Please sign in to comment.