enjoy 4.4 release ^_^

This commit is contained in:
James
2019-08-20 21:29:19 +08:00
parent 6156051e16
commit 442a920366
14 changed files with 280 additions and 107 deletions

View File

@@ -18,6 +18,7 @@ package com.jfinal.template.stat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* DKFF(Dynamic Key Feature Forward) Lexer
@@ -35,10 +36,14 @@ class Lexer {
int forwardRow = 1;
TextToken previousTextToken = null;
List<Token> tokens = new ArrayList<Token>();
String fileName;
Set<String> keepLineBlankDirectives;
public Lexer(StringBuilder content, String fileName) {
List<Token> tokens = new ArrayList<Token>();
public Lexer(StringBuilder content, String fileName, Set<String> keepLineBlankDirectives) {
this.keepLineBlankDirectives = keepLineBlankDirectives;
int len = content.length();
buf = new char[len + 1];
content.getChars(0, content.length(), buf, 0);
@@ -110,7 +115,7 @@ class Lexer {
para = scanPara("");
idToken = new Token(Symbol.OUTPUT, beginRow);
paraToken = new ParaToken(para, beginRow);
return addOutputToken(idToken, paraToken);
return addIdParaToken(idToken, paraToken);
}
if (CharTable.isLetter(peek())) { // # id
state = 10;
@@ -472,31 +477,6 @@ class Lexer {
}
}
// 输出指令不对前后空白与换行进行任何处理,直接调用 tokens.add(...)
boolean addOutputToken(Token idToken, Token paraToken) {
tokens.add(idToken);
tokens.add(paraToken);
previousTextToken = null;
return prepareNextScan(0);
}
// 向前看后续是否跟随的是空白 + 换行或者是空白 + EOF是则表示当前指令后续没有其它有用内容
boolean lookForwardLineFeedAndEof() {
int forwardBak = forward;
int forwardRowBak = forwardRow;
for (char c=peek(); true; c=next()) {
if (CharTable.isBlank(c)) {
continue ;
}
if (c == '\n' || c == EOF) {
return true;
}
forward = forwardBak;
forwardRow = forwardRowBak;
return false;
}
}
/**
* 带参指令处于独立行时删除前后空白字符,并且再删除一个后续的换行符
* 处于独立行是指:向前看无有用内容,在前面情况成立的基础之上
@@ -509,32 +489,68 @@ class Lexer {
tokens.add(idToken);
tokens.add(paraToken);
skipFollowingComment();
// 保留指令所在行空白字符
// #define xxx() 模板函数名、#@xxx() 模板函数名,可以与指令同名,需要排除掉这三种 Symbol
if (keepLineBlankDirectives.contains(idToken.value())
&& idToken.symbol != Symbol.DEFINE
&& idToken.symbol != Symbol.CALL
&& idToken.symbol != Symbol.CALL_IF_DEFINED
) {
prepareNextScan(0);
} else {
trimLineBlank();
}
previousTextToken = null;
return true;
}
// #set 这类指令,处在独立一行时,需要删除当前行的前后空白字符以及行尾字符 '\n'
void trimLineBlank() {
// if (lookForwardLineFeed() && (deletePreviousTextTokenBlankTails() || lexemeBegin == 0)) {
if (lookForwardLineFeedAndEof() && deletePreviousTextTokenBlankTails()) {
prepareNextScan(peek() != EOF ? 1 : 0);
} else {
prepareNextScan(0);
}
previousTextToken = null;
return true;
}
// 处理前后空白的逻辑与 addIdParaToken() 基本一样,仅仅多了一个对于紧随空白的 next() 操作
// 无参指令无条件调用 trimLineBlank()
boolean addNoParaToken(Token noParaToken) {
tokens.add(noParaToken);
skipFollowingComment();
if (CharTable.isBlank(peek())) {
next(); // 无参指令之后紧随的一个空白字符仅为分隔符,不参与后续扫描
}
if (lookForwardLineFeedAndEof() && deletePreviousTextTokenBlankTails()) {
prepareNextScan(peek() != EOF ? 1 : 0);
} else {
prepareNextScan(0);
}
trimLineBlank();
previousTextToken = null;
return true;
}
// 向前看后续是否跟随的是空白 + 换行或者是空白 + EOF是则表示当前指令后续没有其它有用内容
boolean lookForwardLineFeedAndEof() {
int fp = forward;
for (char c=buf[fp]; true; c=buf[++fp]) {
if (CharTable.isBlank(c)) {
continue ;
}
if (c == '\n' || c == EOF) {
forward = fp;
return true;
}
return false;
}
}
/**
* 1当前指令前方仍然是指令 (previousTextToken 为 null),直接返回 true
* 2当前指令前方为 TextToken 时的处理逻辑与返回值完全依赖于 TextToken.deleteBlankTails()
@@ -543,6 +559,54 @@ class Lexer {
// return previousTextToken != null ? previousTextToken.deleteBlankTails() : false;
return previousTextToken == null || previousTextToken.deleteBlankTails();
}
/**
* 跳过指令后方跟随的注释,以便正确处理各类换行逻辑
*/
void skipFollowingComment() {
int fp = forward;
for (char c=buf[fp]; true; c=buf[++fp]) {
if (CharTable.isBlank(c)) {
continue ;
}
// 勿使用 next()
if (c == '#') {
if (buf[fp + 1] == '#' && buf[fp + 2] == '#') {
forward = fp;
skipFollowingSingleLineComment();
} else if (buf[fp + 1] == '-' && buf[fp + 2] == '-') {
forward = fp;
skipFollowingMultiLineComment();
}
}
return ;
}
}
void skipFollowingSingleLineComment() {
forward = forward + 3;
for (char c=peek(); true; c=next()) {
if (c == '\n' || c == EOF) {
break ;
}
}
}
void skipFollowingMultiLineComment() {
forward = forward + 3;
for (char c=peek(); true; c=next()) {
if (c == '-' && buf[forward + 1] == '-' && buf[forward + 2] == '#') {
forward = forward + 3;
break ;
}
if (c == EOF) {
throw new ParseException("The multiline comment start block \"#--\" can not match the end block: \"--#\"", new Location(fileName, beginRow));
}
}
}
}

View File

@@ -71,7 +71,7 @@ public class Parser {
}
public StatList parse() {
tokenList = new Lexer(content, fileName).scan();
tokenList = new Lexer(content, fileName, env.getEngineConfig().getKeepLineBlankDirectives()).scan();
tokenList.add(EOF);
StatList statList = statList();
if (peek() != EOF) {
@@ -207,11 +207,11 @@ public class Parser {
matchEnd(name);
}
return ret;
case EOF:
case PARA:
case ELSEIF:
case ELSE:
case END:
case EOF:
case CASE:
case DEFAULT:
return null;

View File

@@ -89,7 +89,8 @@ public class Scope {
* 自内向外在作用域栈中查找变量,返回最先找到的变量
*/
public Object get(Object key) {
for (Scope cur=this; cur!=null; cur=cur.parent) {
Scope cur = this;
do {
// if (cur.data != null && cur.data.containsKey(key)) {
// return cur.data.get(key);
// }
@@ -104,7 +105,10 @@ public class Scope {
return null;
}
}
}
cur = cur.parent;
} while (cur != null);
// return null;
return sharedObjectMap != null ? sharedObjectMap.get(key) : null;
}

View File

@@ -63,6 +63,9 @@ class TextToken extends Token {
}
// 两个指令之间全是空白字符, 设置其长度为 0为 Parser 过滤内容为空的 Text 节点做准备
// 典型测试用例:两个带有前导空格,并且都在独立一行的 #set(...) 指令,前一个 #set 指令
// 虽然是 '\n' 结尾,但已在 Lexer 中被 prepareNextScan(...) 删掉
// 另一典型用例:#date() #date(),可通过配置 keepLineBlank 为 true 保留指令间的空白字符
text.setLength(0);
return true; // 当两指令之间全为空白字符时,告知调用方需要吃掉行尾的 '\n'
}