in nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLCodeGenerator.scala [40:238]
def syntaxError(errMsg: String, srcName: String, line: Int, pos: Int): NCException
def runtimeError(errMsg: String, srcName: String, line: Int, pos: Int, cause: Exception = null): NCException
protected def SE[T](msg: String)(implicit ctx: PRC): T = throw newSyntaxError(msg)(ctx)
protected def RE[T](msg: String, cause: Exception = null)(implicit ctx: PRC): T = throw newRuntimeError(msg, cause)(ctx)
/**
*
* @param errMsg
* @param ctx
*/
def newSyntaxError(errMsg: String)(implicit ctx: PRC): NCException =
val tok = ctx.start
syntaxError(errMsg, tok.getTokenSource.getSourceName, tok.getLine, tok.getCharPositionInLine)
/**
*
* @param errMsg
* @param cause
* @param ctx
*/
def newRuntimeError(errMsg: String, cause: Exception = null)(implicit ctx: PRC): NCException =
val tok = ctx.start
runtimeError(errMsg, tok.getTokenSource.getSourceName, tok.getLine, tok.getCharPositionInLine, cause)
/**
* Check if given object is mathematically an integer number.
*
* @param v
*/
def isInt(v: Object): Boolean =
v.isInstanceOf[JLong] || v.isInstanceOf[JInt] || v.isInstanceOf[JByte] || v.isInstanceOf[JShort]
/**
* Check if given object is mathematically an real number.
*
* @param v
*/
def isReal(v: Object): Boolean = v.isInstanceOf[JDouble] || v.isInstanceOf[JFloat]
/**
*
* @param v
*/
def asInt(v: Object): JLong = v match
case l: JLong => l
case i: JInt => i.longValue()
case b: JByte => b.longValue()
case s: JShort => s.longValue()
case _ => throw new AssertionError(s"Unexpected int value: $v")
/**
*
* @param v
*/
def asReal(v: Object): JDouble = v match
case l: JLong => l.doubleValue()
case i: JInt => i.doubleValue()
case b: JByte => b.doubleValue()
case s: JShort => s.doubleValue()
case d: JDouble => d
case f: JFloat => f.doubleValue()
case _ => throw new AssertionError(s"Unexpected real value: $v")
/**
*
* @param v
*/
def box(v: Object): Object =
if v == null then null
else if isInt(v) then asInt(v)
else if isReal(v) then asReal(v)
else if isList(v) || isMap(v) then v
else if isColl(v) then // Convert any other Java collections to ArrayList.
new java.util.ArrayList(asColl(v)).asInstanceOf[Object]
else
v
//noinspection ComparingUnrelatedTypes
def isBool(v: Object): Boolean = v.isInstanceOf[Boolean]
def isList(v: Object): Boolean = v.isInstanceOf[JList[_]]
def isColl(v: Object): Boolean = v.isInstanceOf[JColl[_]]
def isMap(v: Object): Boolean = v.isInstanceOf[JMap[_, _]]
def isStr(v: Object): Boolean = v.isInstanceOf[String]
def isEntity(v: Object): Boolean = v.isInstanceOf[NCIDLEntity]
def asList(v: Object): JList[_] = v.asInstanceOf[JList[_]]
def asColl(v: Object): JColl[_] = v.asInstanceOf[JColl[_]]
def asMap(v: Object): JMap[_, _] = v.asInstanceOf[JMap[_, _]]
def asStr(v: Object): String = v.asInstanceOf[String]
def asEntity(v: Object): NCIDLEntity = v.asInstanceOf[NCIDLEntity]
def asBool(v: Object): Boolean = v.asInstanceOf[Boolean]
// Runtime errors.
def rtUnaryOpError(op: String, v: Object)(implicit ctx: PRC): NCException =
newRuntimeError(s"Unexpected '$op' IDL operation for value: $v")
def rtBinaryOpError(op: String, v1: Object, v2: Object)(implicit ctx: PRC): NCException =
newRuntimeError(s"Unexpected '$op' IDL operation for values: $v1, $v2")
def rtUnknownFunError(fun: String)(implicit ctx: PRC): NCException =
newRuntimeError(s"Unknown IDL function: $fun()")
def rtMissingParamError(argNum: Int, fun: String)(implicit ctx: PRC): NCException =
newRuntimeError(s"Missing parameters for IDL function ($argNum is required): $fun()")
def rtTooManyParamsError(argNum: Int, fun: String)(implicit ctx: PRC): NCException =
newRuntimeError(s"Too many parameters for IDL function ($argNum is required): $fun()")
def rtParamTypeError(fun: String, invalid: Object, expectType: String)(implicit ctx: PRC): NCException =
newRuntimeError(s"Expected '$expectType' type of parameter for IDL function '$fun()', found: $invalid")
def rtParamNullError(fun: String)(implicit ctx: PRC): NCException =
newRuntimeError(s"Unexpected 'null' parameter for IDL function: $fun()")
def rtListTypeError(fun: String, cause: Exception)(implicit ctx: PRC): NCException =
newRuntimeError(s"Expected uniform list type for IDL function '$fun()', found polymorphic list.", cause)
def rtFunError(fun: String, cause: Exception)(implicit ctx: PRC): NCException =
newRuntimeError(s"Runtime error in IDL function: $fun()", cause)
def rtUnavailFunError(fun: String)(implicit ctx: PRC): NCException =
newRuntimeError(s"Function '$fun()' is unavailable in this IDL context.")
def rtEmptyListError(fun: String)(implicit ctx: PRC): NCException =
newRuntimeError(s"Unexpected empty list in IDL function: $fun()")
/**
*
* @param stack
*/
def pop1()(implicit stack: S, ctx: PRC): ST =
require(stack.nonEmpty, ctx.getText)
stack.pop()
/**
*
* @param stack
*/
def pop2()(implicit stack: S, ctx: PRC): (ST, ST) =
require(stack.size >= 2, ctx.getText)
// Stack pops in reverse order of push...
val v2 = stack.pop()
val v1 = stack.pop()
(v1, v2)
/**
*
* @param stack
*/
def pop3()(implicit stack: S, ctx: PRC): (ST, ST, ST) =
require(stack.size >= 3, ctx.getText)
// Stack pops in reverse order of push...
val v3 = stack.pop()
val v2 = stack.pop()
val v1 = stack.pop()
(v1, v2, v3)
/**
*
* @param x1
* @param x2
*/
def extract2(x1: ST, x2: ST): (Object, Object, Int) =
val Z(v1, n1) = x1()
val Z(v2, n2) = x2()
(v1, v2, n1 + n2)
/**
*
* @param x1
* @param x2
* @param x3
*/
def extract3(x1: ST, x2: ST, x3: ST): (Object, Object, Object, Int) =
val Z(v1, n1) = x1()
val Z(v2, n2) = x2()
val Z(v3, n3) = x3()
(v1, v2, v3, n1 + n2 + n3)
/**
*
* @param lt
* @param gt
* @param lteq
* @param gteq
*/
def parseCompExpr(lt: TN, gt: TN, lteq: TN, gteq: TN)(implicit ctx: PRC): SI = (_, stack: S, _) =>
val (x1, x2) = pop2()(stack, ctx)
if lt != null then
stack.push(() => {
val (v1, v2, n) = extract2(x1, x2)
val f =
if isInt(v1) && isInt(v2) then asInt(v1) < asInt(v2)
else if isInt(v1) && isReal(v2) then asInt(v1) < asReal(v2)
else if isReal(v1) && isInt(v2) then asReal(v1) < asInt(v2)
else if isReal(v1) && isReal(v2) then asReal(v1) < asReal(v2)
else
throw rtBinaryOpError("<", v1, v2)
Z(f, n)
})