首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

EScript 一个Demo已经预备好了

2013-11-08 
EScript一个Demo已经准备好了EScript一个Demo已经准备好了.可以在sourceforge上看看到https://sourceforge

EScript 一个Demo已经准备好了
EScript一个Demo已经准备好了.可以在sourceforge上看看到https://sourceforge.net/projects/esic/

如下文本

Hello,first line ,keep this line#: delnext { Hello this line will be deleted Hello,this line will be deleted #:} Hello,keep this line



通过脚本运行后,输出是
Hello,first line ,keep this lineHello,keep this line


中间俩行被删除了

运行代码如下
InputStreamReader reader = new InputStreamReader(Test.class.getResourceAsStream("/com/jk/escript/antlr/input.txt")) ;PreProcessor processor = new PreProcessor(reader);processor.process();String result = processor.getResult();CommandFactory cf = new CommandFactory();cf.setEnv(new Enviroment());cf.setLineMap(processor.getLineMap());ANTLRStringStream  input = new ANTLRStringStream (result); EScriptLexer lexer = new EScriptLexer(input);// create a buffer of tokens pulled from the lexerCommonTokenStream tokens = new CommonTokenStream(lexer);// create a parser that feeds off the tokens bufferEScriptParser parser = new EScriptParser(tokens);parser.setCommandFactory(cf);parser.txt();StringBuilder output = parser.getStringBuilder();System.out.println("output:\n"+output.toString());



script语法现在很简单:如下

grammar EScript;
options {backtrack=true;}
@header {
package com.jk.escript.antlr;
import com.jk.escript.*;
import java.util.List;
import java.util.ArrayList;
}
@members {
StringBuilder sb = new StringBuilder();
CommandFactory cf = null;
public StringBuilder getStringBuilder(){
return sb;
}

public void setCommandFactory(CommandFactory cf){
this.cf = cf;
}
}
txt: (commandLine|txtLine{sb.append( cf.getOriginalLine($txtLine.text)).append("\n");})*;
txtLine
:contentLine;
commandLine
scope {
boolean  isSingleLine;
boolean  hasEnd;
List options;

}
@init{
$commandLine::isSingleLine = true;
$commandLine::hasEnd = false;
$commandLine::options= new ArrayList();
}
:  commentChar ':' key (csbegin {$commandLine::isSingleLine=false;})? commentCharEnd? NEWLINE  parasLine  (csend )?
{
String output = null;
BaseCommand command = cf.getCommand($key.text,$parasLine.text);
command.setOptions($commandLine::options);
output = command.getOutput();
if(output!=null)sb.append(output);
}
  ;
csbegin
: '{';
csend
: commentChar ':}' commentCharEnd   NEWLINE;
parasLine
: {$commandLine::isSingleLine}?=>singleLine
| mutilLine
;
singleLine
: contentLine;
mutilLine
: contentLine+;

key
: replace
| 'delnext'
;
replace
: 'replace a'
| 'replace b'
;
contentLine
: '$:' STRING NEWLINE;

commentChar

: '#'
| '//'
| '/*' {$commandLine::hasEnd = true;}
| '<!--'{$commandLine::hasEnd = true;}
;
commentCharEnd
: {$commandLine::hasEnd}?=>'*/'
| {$commandLine::hasEnd}?=>'-->'
|
;

WS  :   (' '|'\t')+ {skip();} ;

STRING
: ('0'..'9')*
;
NEWLINE: ('\r'? '\n')+;

热点排行