Skip to content

支持对象和数组嵌套型变量,前面不用加 # #487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/main/java/com/googlecode/aviator/lexer/ExpressionLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,37 @@ public Token<?> scan(final boolean analyse) {
variable.setQuote(true);
return this.symbolTable.reserve(variable);
}
// 支持对象和数组嵌套型变量,前面不用加 #
if (Character.isJavaIdentifierStart(this.peek)) {
char oldPeek = peek;
int startIndex = this.iterator.getIndex();

StringBuilder sb = new StringBuilder();
int dots = 0;
int brackets = 0;

while (Character.isJavaIdentifierPart(this.peek) || this.peek == '.' || this.peek == '['
|| this.peek == ']') {
if(this.peek == '.'){
dots++;
}
if(this.peek == '[' || this.peek == ']'){
brackets++;
}
sb.append(this.peek);
nextChar();
}
String lexeme = sb.toString();
// 同时出现点号和中括号,是对象和数组混合型变量
if(dots > 0 && brackets > 0){
Variable variable = new Variable(lexeme, this.lineNo, startIndex);
variable.setQuote(true);
return this.symbolTable.reserve(variable);
}else{
this.iterator.setIndex(startIndex);
this.peek = oldPeek;
}
}
if (Character.isJavaIdentifierStart(this.peek)) {
int startIndex = this.iterator.getIndex();
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ public static void main(final String[] args) {
System.out.println("Execute expression: " + exp);
System.out.println("Result: " + result);
System.out.println(foo.bars[0].getName());

// 不加 # 也可以
exp = "foo.bars[0].name='hello aviator' ; foo.bars[0].name";
result = (String) AviatorEvaluator.execute(exp, env);
System.out.println("Execute expression without sharp: " + exp);
System.out.println("Result: " + result);
System.out.println(foo.bars[0].getName());

exp = "foo.bars[0] = nil ; foo.bars[0]";
result = (String) AviatorEvaluator.execute(exp, env);
Expand Down