MakeWeb Powered by JavaCC
こんな感じでできた。
JavaCCでは状態の管理もできるみたいだ。
/** ** MakeWeb **/ //options { // STATIC=false; //} PARSER_BEGIN(MakeWeb) import java.io.*; public class MakeWeb { static StringBuffer sb = new StringBuffer(); public static void main(String args[]) { String line = null; try { try { MakeWeb parser = new MakeWeb(new FileReader("sample.txt")); parser.doLines(); } catch (ParseException e) { System.out.println("### " + e.getMessage()); } } catch (IOException e) { System.out.println("### " + e.getMessage()); } System.out.println(sb.toString()); } } PARSER_END(MakeWeb) SKIP : { " "| "\t"| "\n"| "\r"| "\f" } TOKEN : { <ANOTHER: ~[] > } TOKEN : { <MR: "--" > | <P_BEGIN: "-(" > | <P_END: "-)" > | <PRE_BEGIN: "--(" > | <PRE_END: "--)" > } TOKEN : { <PRE_BEGIN_ESC: "---(" > : ESCAPE } <ESCAPE> TOKEN : { <PRE_END_ESC: "---)" > : DEFAULT } <ESCAPE> TOKEN : { <PLAIN: ~[] > } void doLines() : { Token t = null; } { P() | PRE() | PRE_ESC() | t = <ANOTHER> { sb.append(t.image); } } void P() : {} { <P_BEGIN> { sb.append("<p>" + "\n"); } ( ( <MR> { sb.append("</p><p>" + "\n"); } )* doLines() )* <P_END> { sb.append("</p>" + "\n"); } } void PRE() : {} { <PRE_BEGIN> { sb.append("<pre>" + "\n"); } ( ( <MR> { sb.append("</pre><pre>" + "\n"); } )* doLines() )* <PRE_END> { sb.append("</pre>" + "\n"); } } void PRE_ESC() : { Token t = null; } { <PRE_BEGIN_ESC> { sb.append("<pre>"); } ( t = <PLAIN> { sb.append( escape(t.image) ); } )* <PRE_END_ESC> { sb.append("</pre>" + "\n"); } } JAVACODE String escape(String string) { string = string.replaceAll("&", "&"); string = string.replaceAll("<", "<"); string = string.replaceAll(">", ">"); string = string.replaceAll("\"", """); return string; }