📄 test.java
字号:
package lolo.test;import lolo.Input;import lolo.Scan;import lolo.Scanner;import lolo.scans.CComment;import lolo.scans.Char;import lolo.scans.Flt;import lolo.scans.HashComment;import lolo.scans.Int;import lolo.scans.JavaIdentifier;import lolo.scans.JavaWhitespace;import lolo.scans.JavadocComment;import lolo.scans.QuotedChar;import lolo.scans.QuotedText;import lolo.scans.Set;import lolo.scans.SetMN;import lolo.scans.SimpleIdentifier;import lolo.scans.SimpleWhitespace;import lolo.scans.SlashSlashComment;import lolo.scans.Word;import lolo.scans.XMLStartOfElement;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.StringReader;import java.io.Serializable;/** An example to test all features (Serialization, removing symbols, Unicode scanners, * Action objects, non serializable Action objects, ...) and classes. * * @author <a href="http://www.inf.uos.de/bernd" target="_blank">Bernd Kühl</a> (<a href="mailto:bernd@informatik.uni-osnabrueck.de">bernd@informatik.uni-osnabrueck.de</a>) */public class Test { // make Actions serializable... protected static interface Action extends lolo.Scan.Action, Serializable { } public static void main(String args[]) throws Exception { Action wordAction = new Action () { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found word -"+new String(buffer, off, len)+"-\tsender is "+sender); } }; lolo.Scan.Action numberAction = new lolo.Scan.Action () { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found number -"+new String(buffer, off, len)+"-\tsender is "+sender); } }; Action commentAction = new Action () { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found comment -"+new String(buffer, off, len)+"-\tsender is "+sender); } }; Scanner scanner = new Scanner(); scanner.debug = true; boolean changed = scanner.add(new Scan[] { new Word("<=").setAction(wordAction), new Word(">=").setAction(wordAction), new Word("==").setAction(wordAction), new Word("&&").setAction(wordAction), new Word("||").setAction(wordAction) }); System.err.println("add(Scan[]) result: "+changed); changed = scanner.add(new Scan[] { new Word("<=").setAction(wordAction), new Word(">=").setAction(wordAction), new Word("==").setAction(wordAction), new Word("&&").setAction(wordAction), new Word("||").setAction(wordAction) }); System.err.println("add(Scan[]) result: "+changed); changed = scanner.add(new SlashSlashComment().setAction(commentAction).setIgnore(false)); System.err.println("add(new SlashSlashComment) result: "+changed); changed = scanner.add(new SlashSlashComment().setAction(commentAction).setIgnore(false)); System.err.println("add(new SlashSlashComment) result: "+changed); scanner.add(new Int().setAction(numberAction)); scanner.add(new Flt(true).setAction(numberAction)); scanner.add(new SimpleWhitespace().setIgnore(false).setAction(new Action() { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found whitespace -"+new String(buffer, off, len)+"-\tsender is "+sender); } })); scanner.add(new HashComment().setAction(commentAction).setIgnore(false)); scanner.add(new JavadocComment().setAction(commentAction).setIgnore(false)); scanner.add(new CComment(true).setAction(commentAction).setIgnore(false)); scanner.add(new SetMN("abc", true, 2, 5).setAction(new Action() { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found setmn -"+new String(buffer, off, len)+"-\tsender is "+sender); } })); scanner.add(new SetMN("@", true, 1, 1).setAction(new Action() { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found @ -"+new String(buffer, off, len)+"-\tsender is "+sender); } })); scanner.add(new XMLStartOfElement("elem").setAction(new Action() { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found start of element -"+new String(buffer, off, len)+"-\tsender is "+sender); } })); scanner.add(new Set("+-=/*!<>?:;|[]{}().,%&").setAction(new Action() { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found single java character -"+new String(buffer, off, len)+"-\tsender is "+sender); } })); scanner.add(new Word("bjk").setAction(new Action() { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found word -"+new String(buffer, off, len)+"-\tsender is "+sender); } })); scanner.add(new SimpleIdentifier().setAction(new Action() { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found identifier -"+new String(buffer, off, len)+"-\tsender is "+sender); } })); scanner.add(new QuotedText().setAction(new Action() { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found quoted text -"+new String(buffer, off, len)+"-\tsender is "+sender); } })); scanner.add(new QuotedChar().setAction(new Action() { public void action(Scan sender, char [] buffer, int off, int len) { System.err.println("found char constant -"+new String(buffer, off, len)+"-\tsender is "+sender); } })); scanner.pack(); ObjectOutputStream out; ObjectInputStream in;// number actions are not serializable..System.err.println("serialzing scanner..."); out = new ObjectOutputStream(new FileOutputStream("x.ser")); out.writeObject(scanner); out.flush(); out.close(); scanner = null; System.err.println("deserialzing scanner..."); in = new ObjectInputStream(new FileInputStream("x.ser")); scanner = (Scanner) in.readObject(); in.close();System.err.println("go..."); Input input = new Input(new InputStreamReader(System.in), 2); Scan winner; while ((winner = scanner.scan(input)) != null) ; System.err.println("make unicode scanner..."); scanner.setUnicode(true); // forces repack of table scanner.pack();System.err.println("serialzing scanner..."); out = new ObjectOutputStream(new FileOutputStream("x.ser")); out.writeObject(scanner); out.flush(); out.close(); scanner = null; System.err.println("deserialzing scanner...");long old = System.currentTimeMillis();System.err.println("start time deserialzing\t"+old+"..."); in = new ObjectInputStream(new FileInputStream("x.ser")); scanner = (Scanner) in.readObject();long current = System.currentTimeMillis();System.err.println("end time deserialzing\t"+current+"...");TimeUtil.printMilliSecons("time: ",current-old, System.err); in.close();System.err.println("go..."); input = new Input(new InputStreamReader(System.in), 2); while ((winner = scanner.scan(input)) != null) ;System.err.println("scanner.contains(new Word(\"&&\"))..."+scanner.contains(new Word("&&")));System.err.println("scanner.remove(new Word(\"&&\"))..."+scanner.remove(new Word("&&")));System.err.println("go..."); input = new Input(new InputStreamReader(System.in), 2); while ((winner = scanner.scan(input)) != null) ;System.err.println("scanner.contains(new Word(\"&&\"))..."+scanner.contains(new Word("&&")));System.err.println("scanner.remove(new Word(\"&&\"))..."+scanner.remove(new Word("&&")));System.err.println("go..."); input = new Input(new InputStreamReader(System.in), 2); while ((winner = scanner.scan(input)) != null) ;System.err.println("scanner.contains(new JavadocComment())..."+scanner.contains(new JavadocComment()));System.err.println("scanner.remove(new JavadocComment())..."+scanner.remove(new JavadocComment()));System.err.println("go..."); input = new Input(new InputStreamReader(System.in), 2); while ((winner = scanner.scan(input)) != null) ;System.err.println("scanner.contains(new Flt())..."+scanner.contains(new Flt(false)));System.err.println("scanner.remove(new Flt())..."+scanner.remove(new Flt(false)));System.err.println("scanner.contains(new Flt())..."+scanner.contains(new Flt(true)));System.err.println("scanner.remove(new Flt())..."+scanner.remove(new Flt(true)));System.err.println("go..."); input = new Input(new InputStreamReader(System.in), 2); while ((winner = scanner.scan(input)) != null) ;System.err.println("scanner.contains(new Flt())..."+scanner.contains(new Flt(true)));System.err.println("scanner.remove(new Flt())..."+scanner.remove(new Flt(true)));System.err.println("go..."); input = new Input(new InputStreamReader(System.in), 2); while ((winner = scanner.scan(input)) != null) ;System.err.println("scanner.contains(new XMLStartOfElement(\"elem\"))..."+scanner.contains(new XMLStartOfElement("elem")));System.err.println("scanner.remove(new XMLStartOfElement(\"elem\"))..."+scanner.remove(new XMLStartOfElement("elem")));System.err.println("scanner.contains(new XMLStartOfElement(\"elem\"))..."+scanner.contains(new XMLStartOfElement("elem")));System.err.println("go..."); input = new Input(new InputStreamReader(System.in), 2); while ((winner = scanner.scan(input)) != null) ; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -