net.hasor.dataql.domain.parser.DataQL.jj Maven / Gradle / Ivy
options {
STATIC = false;
JAVA_UNICODE_ESCAPE = true; // 支持中文必须
UNICODE_INPUT = true; // 支持中文必须
// LOOKAHEAD = 2;
// FORCE_LA_CHECK=true;
}
// ------------------------------------------------------------------------------------------------
PARSER_BEGIN(DataQLParser)
/*
* Copyright 2008-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.hasor.dataql.domain.parser;
import net.hasor.dataql.domain.*;
import net.hasor.dataql.domain.PrimitiveExpression.*;
import java.util.*;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
/**
* DSL 语法解析器
* @author 赵永春([email protected])
* @version : 2017-03-23
*/
public class DataQLParser {
private DataQLParser (){}
//
public static BlockSet parserDataQL(String queryString) throws ParseException {
DataQLParser parser = new DataQLParser(new ByteArrayInputStream(queryString.getBytes()));
return parser.rootBlock();
}
public static BlockSet parserDataQL(InputStream inputStream,String encoding) throws ParseException {
DataQLParser parser = new DataQLParser(inputStream,encoding);
return parser.rootBlock();
}
public static Number automaticBigInteger(java.math.BigInteger bigInt) {
int bitLength = bigInt.bitLength();
if (bitLength < 8) {
return bigInt.byteValue();
}
if (bitLength < 16) {
return bigInt.shortValue();
}
if (bitLength < 32) {
return bigInt.intValue();
}
if (bitLength < 64) {
return bigInt.longValue();
}
return bigInt;
}
public static Number automaticBigDecimal(java.math.BigDecimal bigDec) {
int precisionLength = bigDec.precision();
if (precisionLength < 8) {
return bigDec.floatValue();
}
if (precisionLength < 16) {
return bigDec.doubleValue();
}
return bigDec;
}
public static String fixNumberValue(Token t) {
boolean signBit = t.image.charAt(0) == '-';
if (t.kind == HEX_NUM || t.kind == OCTAL_NUM || t.kind == BINARY_NUM) {
if (signBit) {
return "-" + t.image.substring(3);
} else {
return t.image.substring(2);
}
}
return t.image;
}
}
PARSER_END(DataQLParser)
// ------------------------------------------------------------------------------------------------
/* Ignore Comments */
SKIP : {
" " // 空格
| "\t" // 水平制表符 (垂直制表符 \v)
| "\n" // 换行
| "\r" // 回车
| "\f" // 换页
}
SPECIAL_TOKEN: {
> |
|
}
/* Number */
TOKEN:{
<#ZERO : "0" >
| <#SYMBOL : ["+","-"] >
| <#DIGIT : ["0"-"9"] >
| )? ()+ > // 十进制数:-0000234 or 123
| ["x","X"]) (["0"-"9","a"-"f","A"-"F"])+ > // 十六进制:0x12345
| ["o","O"]) (["0"-"7"])+ > // 八 进 制:0o1234567
| ["b","B"]) (["0"-"7"])+ > // 二 进 制:0b01010101100
| )? ()+ ("." ()*)?
(["e","E"] ()? ()+ )? > // 浮点数,包含科学计数法
}
/* key words */
TOKEN: {
< IF : "if" >
| < ELSEIF : "elseif" >
| < ELSE : "else" >
| < END : "end" >
| < RETURN : "return" >
| < THROW : "throw" >
| < EXIT : "exit" >
| < VAR : "var" >
| < LAMBDA : "lambda" >
| < TRUE : "true" >
| < FALSE : "false" >
| < NULL : "null" >
| < OPTION : "option" >
}
/* OPERATORS */
TOKEN: {
< COMMA : "," >
| < COLON : ":" >
| < QUEM : "?" >
| < SEMI : ";" >
| < ASS : "=">
| < QUOTE_SINGLE : "'" >
| < QUOTE_DOUBLE : "\"" >
| < OPAR : "(" >
| < CPAR : ")" >
| < OBRA : "[" >
| < CBRA : "]" >
| < OCBR : "{" >
| < OCBREXT : (["@","#","$","%","&"] ) >
| < CCBR : "}" >
| < BANG : "!" >
| < PLUS : "+" >
| < MINUS : "-" >
| < STAR : "*" >
| < SLASH : "/" >
| < REM : "%" >
| < ALI : "\\" >
| < GT : ">" >
| < GE : ">=" >
| < LT : "<" >
| < LE : "<=" >
| < EQ : "==" >
| < NE : "!=" >
| < BIT_AND : "&" >
| < BIT_OR : "|" >
| < XOR : "^" >
| < LSHIFT : "<<" >
| < RSIGNEDSHIFT : ">>" >
| < RUNSIGNEDSHIFT : ">>>" >
| < SC_OR : "||" >
| < SC_AND : "&&" >
}
/* IDENTIFIERS */
TOKEN:{
<#LETTER: [ //Unicode "LETTER" characters - as of Javase 6, Unicode 4 is supported
//http://www.unicode.org/Public/4.1.0/ucd/NamesList.txt
//more inclusive than Character.isLetter
"$", //DOLLAR SIGN: milreis, escudo
"A"-"Z", //LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
"_", //LOW LINE: spacing underscore
"a"-"z", //LATIN SMALL LETTER A - LATIN SMALL LETTER Z
"\u00aa"-"\u00ad", // 00AA FEMININE ORDINAL INDICATOR: spanish
// 00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK: left guillemet
// 00AC NOT SIGN: angled dash
// 00AD SOFT HYPHEN: discretionary hyphen
"\u00b5"-"\u00b6", // 00B5 MICRO SIGN: greek small letter mu
// 00B6 PILCROW SIGN: paragraph sign
"\u00ba"-"\u00bb", // 00BA MASCULINE ORDINAL INDICATOR: spanish
// 00BB RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK : right guillemet
"\u00c0"-"\u00d6", //LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
"\u00d8"-"\u00f6", //LATIN CAPITAL LETTER O WITH STROKE - LATIN SMALL LETTER O WITH DIAERESIS
"\u00f8"-"\u00ff", //LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER Y WITH DIAERESIS
"\u0100"-"\u1fff", //LATIN CAPITAL LETTER A WITH MACRON - GREEK DASIA:reversed comma
"\u3040"-"\u318f", //Hiragana - Hangul
"\u3300"-"\u337f", //Squared Katakana
"\u3400"-"\u3d2d", //CJK Unified Ideographs Extension A
"\u4e00"-"\u9fff", //CJK Unified Ideographs
"\uf900"-"\ufaff", //CJK Compatibility Ideographs
"\ufb00"-"\ufb06", //LATIN SMALL LIGATURE FF -LATIN SMALL LIGATURE ST
"\ufb13"-"\ufb17", //ARMENIAN SMALL LIGATURE MEN NOW - ARMENIAN SMALL LIGATURE MEN XEH
"\ufb1f"-"\ufb28", //HEBREW LIGATURE YIDDISH YOD YOD PATAH -HEBREW LETTER WIDE TAV
"\ufb2a"-"\ufb36", //HEBREW LETTER SHIN WITH SHIN DOT - HEBREW LETTER ZAYIN WITH DAGESH
"\ufb38"-"\ufb3e", //HEBREW LETTER TET WITH DAGESH - HEBREW LETTER MEM WITH DAGESH
"\ufb40"-"\ufb41", //HEBREW LETTER NUN WITH DAGESH - HEBREW LETTER SAMEKH WITH DAGESH
"\ufb43"-"\ufb44", //HEBREW LETTER FINAL PE WITH DAGESH - HEBREW LETTER PE WITH DAGESH
"\ufb46"-"\ufb4f", //HEBREW LETTER TSADI WITH DAGESH - HEBREW LIGATURE ALEF LAMED
"\ufb50"-"\ufdd7", //Arabic Presentation Forms(A) - ARABIC LIGATURE BISMILLAH AR-RAHMAN AR-RAHEEM
"\ufdf0"-"\ufdfd", //ARABIC LIGATURE SALLA USED AS KORANIC STOP SIGN ISOLATED FORM -ARABIC LIGATURE BISMILLAH AR-RAHMAN AR-RAHEEM
"\ufe70"-"\ufefc", //Arabic Presentation Forms(B) - ARABIC LIGATURE LAM WITH ALEF FINAL FORM
"\uff1f"-"\uff3a", //FULLWIDTH QUESTION MARK - FULLWIDTH LATIN CAPITAL LETTER Z
"\uff3f"-"\uff5e", //FULLWIDTH LOW LINE - FULLWIDTH TILDE
"\uff61"-"\uffbe", //Halfwidth CJK punctuation:HALFWIDTH IDEOGRAPHIC FULL STOP - HALFWIDTH HANGUL LETTER HIEUH
"\uffc2"-"\uffc7", //HALFWIDTH HANGUL LETTER A - HALFWIDTH HANGUL LETTER E
"\uffca"-"\uffcf", //HALFWIDTH HANGUL LETTER YEO - HALFWIDTH HANGUL LETTER OE
"\uffd2"-"\uffd7", //HALFWIDTH HANGUL LETTER YO - HALFWIDTH HANGUL LETTER YU
"\uffda"-"\uffdc" //HALFWIDTH HANGUL LETTER EU - HALFWIDTH HANGUL LETTER I
] > // 各类字符集
| <#STR_UNICODE : "\\u" (["0"-"9","a"-"f","A"-"F"]){4}> // Unicode字符表示法
| <#STR_DOUBLE_BODY : ( ~["\"","\\","\r","\n","\f","\t"]) |
( "\\"["r","n","f","t","\\","b","\"","'"]) > // 双引号字符串(body部分)
| ()* > // 双引号的字符串(整体)
| <#STR_SINGLE_BODY : ( ~["'","\\","\r","\n","\f","\t"]) |
( "\\"["r","n","f","t","\\","b","\"","'"]) > // 单引号字符串(body部分)
| ()* > // 单引号的字符串(整体)
| // 空字符串
| <#IDENTIFIER_S : ( ) ( ["0"-"9"] | )* > // 标识符
| <#IDENTIFIER_A : ( "[" ( ["0"-"9"] )+ "]")+ > // 表示数组
| ( ("." ) | )* >// 组合的标识符,例: abc.aa[2][2]
}
//-----------------------------------------------------------------------------
// .标识符
String identifier() : { Token dataToken = null;
}{
(
(
dataToken = {return dataToken.image;}
) | (
{Token ocbrToken = null;}
ocbrToken = dataToken =
{return ocbrToken.image + dataToken.image + "}";}
)
)
}
// .带引号的字符串
String stringValue() : { Token dataToken = null;
}{
(
(
dataToken =
) | (
dataToken =
) | (
dataToken =
)
) {String image = dataToken.image;}
{return image.substring(1, image.length()-1);}
}
// .空值
Object nullValue() : {}{
{return null;}
}
// .布尔
Boolean booleanValue() : {}{
(
{return Boolean.TRUE;}
) | (
{return Boolean.FALSE;}
)
}
// .数字
Number numberValue() : { Number number = null;
Token t = null;
}{
(
(
t = {number = automaticBigDecimal(new java.math.BigDecimal(fixNumberValue(t)));}
) | (
t = {number = automaticBigInteger(new java.math.BigInteger(fixNumberValue(t)));}
) | (
t = {number = automaticBigInteger(new java.math.BigInteger(fixNumberValue(t),16));}
) | (
t = {number = automaticBigInteger(new java.math.BigInteger(fixNumberValue(t),8));}
) | (
t = {number = automaticBigInteger(new java.math.BigInteger(fixNumberValue(t),2));}
)
) {return number;}
}
// .列表:[ ... ]
ListExpression list() : { ListExpression listExp = new ListExpression();
Expression valueExp = null;
}{
(
LOOKAHEAD(2)
(
) | (
valueExp = expression() {listExp.addItem(valueExp);}
{valueExp = null;}
(
valueExp = expression() {listExp.addItem(valueExp);}
{valueExp = null;}
)*
)
) {return listExp;}
}
// .对象:
ObjectExpression object() : { ObjectExpression objExp = new ObjectExpression();
String fieldName = null;
Expression valueExp = null;
} {
(
LOOKAHEAD(2)
(
) | (
fieldName = stringValue() {valueExp = new RouteExpression(fieldName);}
( valueExp = expression())? {objExp.addField(fieldName,valueExp);}
{fieldName = null;valueExp=null;}
(
fieldName = stringValue()
{valueExp = new RouteExpression(fieldName);}
( valueExp = expression())?
{objExp.addField(fieldName,valueExp);}
{fieldName = null;valueExp=null;}
)*
)
) {return objExp;}
}
//-----------------------------------------------------------------------------
// .表达式 = 表达式 or 表达式 + 运算符
Expression expression() : { Expression exp;
} {
(
// 优先级
exp = expression() {exp = new PrivilegeExpression(exp);}
(
exp = extExpression(exp)
)? {return exp;}
) | LOOKAHEAD(unaryExpression())(
exp = unaryExpression() {return exp;}
) | (
exp = basicValue()
(
exp = extExpression(exp)
)? {return exp;}
)
}
Expression unaryExpression() : { Token symbol = null;
Expression target = null;
} {
( symbol = | symbol = )
target = basicValue() {return new UnaryExpression(target,symbol.image);}
}
Expression extExpression(Expression fstExp) : { Expression resultExp = null;
} {
(
(
resultExp = dyadicExpression(fstExp)
) | (
resultExp = ternaryExpression(fstExp)
)
) {return resultExp;}
}
DyadicExpression dyadicExpression(Expression fstExp) : {
Token symbol = null;
Expression secExp = null;
} {
(
symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
| symbol =
)
secExp = expression() {return new DyadicExpression(fstExp,symbol.image,secExp);}
}
Expression ternaryExpression(Expression testExp) :{ Expression thenExp = null;
Expression elseExp = null;
} {
thenExp = expression() elseExp = expression()
{return new TernaryExpression(testExp,thenExp,elseExp);}
}
// .值
Expression basicValue() : { Expression finalExp = null;
} {
(
(
finalExp = primitiveValue()
) | (
(
{String mabeCallerName = null;}
mabeCallerName = identifier() {finalExp = new RouteExpression(mabeCallerName);}
(
finalExp = funCall(mabeCallerName)
)?
)
) | (
finalExp =list()
) | (
finalExp = object()
)
) {return finalExp;}
}
PrimitiveExpression primitiveValue() : { Object value = null;
} {
(
value = stringValue() {return new PrimitiveExpression(value,ValueType.String);}
) | (
value = nullValue() {return new PrimitiveExpression(value,ValueType.Null);}
) | (
value = booleanValue() {return new PrimitiveExpression(value,ValueType.Boolean);}
) | (
value = numberValue() {return new PrimitiveExpression(value,ValueType.Number);}
)
}
//-----------------------------------------------------------------------------
// .函数参数
Variable callerParamItem() : { Variable value = null;
} {
(
(
value = lambdaDef()
) | (
value = expression()
)
) {return value;}
}
// .参数集:(xx , xx , xx)
CallerExpression callerParamSet(String callName) : {Variable atParam = null;
String quickSelect = null;
CallerExpression callerExp = new CallerExpression(callName);
} {
(
(
(
(
) | (
atParam = callerParamItem() {callerExp.addParam(atParam);atParam = null;}
(
atParam = callerParamItem()
{callerExp.addParam(atParam);atParam = null;}
)*
)
)
(
"->" quickSelect = stringValue() {callerExp.setQuickSelect(false,quickSelect);}
)?
) | (
"->"
(
quickSelect = stringValue()
)? {callerExp.setQuickSelect(true,quickSelect);}
)
) {return callerExp;}
}
// .函数调用
CallerExpression funCall(String callName) : { CallerExpression callerExp = null;
Format finalFormat = null;
} {
callerExp = callerParamSet(callName)
(
(
"~" {finalFormat = new RawFormat();}
) | (
{ListExpression listExp = null;}
listExp = list() {finalFormat = new ListFormat(listExp);}
) | (
{ObjectExpression objectExp = null;}
objectExp = object() {finalFormat = new ObjectFormat(objectExp);}
)
) {callerExp.setResultFormat(finalFormat);}
{return callerExp;}
}
// .Lambda函数: lambda : (obj,obj2) -> {}
LambdaBlockSet lambdaDef() : { LambdaBlockSet lambdaBlockSet = new LambdaBlockSet();
Token paramName = null;
BlockSet scriptBodys = null;
} {
":"
(
paramName = {lambdaBlockSet.addParam(paramName.image);}
)?
(
paramName = {lambdaBlockSet.addParam(paramName.image);}
)*
"->"
(
(
scriptBodys = blockSet()
) | LOOKAHEAD(block()) (
{Inst inst = null;}
inst = block() {scriptBodys = new BlockSet(Arrays.asList(inst));}
)
) {lambdaBlockSet.addToBody(scriptBodys);}
{return lambdaBlockSet;}
}
//-----------------------------------------------------------------------------
// .函数
VariableInst varBlock() : { String varName = null;
Variable value = null;
} {
varName = identifier()
(
(
value = lambdaDef()
) | (
value = expression()
)
) {return new VariableInst(varName,value);}
}
// .反回
ReturnInst returnBlock() : { Expression result = null;
} {
result = expression() {return new ReturnInst(result);}
}
// .判断
SwitchInst ifBlock() : { SwitchInst switchInst = new SwitchInst();
Expression testExp;
BlockSet instBlockSet;
} {
testExp = expression()
instBlockSet = blockSet() {switchInst.addBlockSet(testExp,instBlockSet);}
{testExp = null; instBlockSet = null;}
(
testExp = expression()
instBlockSet = blockSet() {switchInst.addBlockSet(testExp,instBlockSet);}
{testExp = null; instBlockSet = null;}
)*
(
instBlockSet = blockSet() {switchInst.setElseBlockSet(instBlockSet);}
)?
{return switchInst;}
}
// .异常 or 中断
Inst breakBlock() : { String typeClass = null;
Number throwStatus = null;
Expression throwData = null;
} {
(
(
{typeClass = "ExitInst";}
) | (
{typeClass = "ThrowInst";}
)
)
(
LOOKAHEAD(breakBlockStatus())
(
throwStatus = breakBlockStatus()
throwData = expression()
) | (
throwData = expression()
)
) {return "ThrowInst".equals(typeClass)?
new ThrowInst(throwStatus,throwData):
new ExitInst(throwStatus,throwData);}
}
Number breakBlockStatus() : { Number errorCode = null;
} {
errorCode = numberValue() {return errorCode;}
}
// .选项指令
Inst optionBlock() : { String optKey = null;
PrimitiveExpression optValue = null;
} {
© 2015 - 2025 Weber Informatics LLC | Privacy Policy