scala 实现表达式解析

表达式解析

scala 复制代码
import org.junit.Test

import scala.collection.mutable

class ExprTestCase {


  private val orderSource = "source_1"
  private val saleChannel = "saleChannel"
  val datas = new mutable.HashMap[String, String]();
  //  p1, source1, sale1, source_1 & ( sale_channel_1 | sq )"
  datas.put("source_1", "source_1");
  datas.put("saleChannel", "saleChannel");
  datas.put("orderSource", "orderSource");

  val expr = s"orderSource:source_1 & ( saleChannel:saleChannel | orderSource:orderSource )"

  private val tokens: Array[String] = expr.split("\\s+")

  private val optStack = new mutable.ArrayStack[String]()
  private val memberStatck = new mutable.ArrayStack[String]()
  private var flag = false;

  def matchFiled(fieldName: String): String = {
    fieldName match {
      case "orderSource" => orderSource
      case "saleChannel" => saleChannel
      case _ => throw new IllegalArgumentException(s"unknown filed name: ${fieldName}, it's  can only be 'orderSource' or 'saleChannel'")
    }
  }

  @Test
  def expParseTestCase() = {

    var opt = ""

    def exprProcess = {
      if (opt == "" || opt == "(") opt = optStack.pop()
      while (opt != null && opt != "(" && memberStatck.nonEmpty) {
        opt match {
          case "|" => {
            if(memberStatck.size > 1) {
              val m1 = memberStatck.pop().split(":")
              val m2 = memberStatck.pop().split(":")
              flag = datas.getOrElse(m2(1), "N").eq(matchFiled(m2(0))) || datas.getOrElse(m1(1), "N").eq(matchFiled(m1(0)))
              println(
                s"""
                [&]: ${datas.getOrElse(m2(1), "N")}.eq("${matchFiled(m2(0))}") || ${datas.getOrElse(m1(1), "N")}.eq("${matchFiled(m1(0))}")
                """)
            } else {
              val m1 = memberStatck.pop().split(":")
              flag = flag || datas.getOrElse(m1(1), "N").eq(matchFiled(m1(0)))
              println(
                s"""
                [&]: ${flag} || ${datas.getOrElse(m1(1), "N")}.eq("${matchFiled(m1(0))}
                """)
            }
          }
          case "&" => {
            if (memberStatck.size > 1) {
              val m1 = memberStatck.pop().split(":")
              val m2 = memberStatck.pop().split(":")
              flag = datas.getOrElse(m1(1), "N").equals(matchFiled(m1(0))) && datas.getOrElse(m2(1), "N").equals(matchFiled(m2(0)))
              println(
                s"""
                [&]: ${datas.getOrElse(m1(1), "N")}.eq("${matchFiled(m1(0))}") && ${datas.getOrElse(m2(1), "N")}.eq("${matchFiled(m2(0))}")
                """)
            } else {
              val m1 = memberStatck.pop().split(":")
              flag = flag && datas.getOrElse(m1(1), "N").equals(matchFiled(m1(0)))
              println(
                s"""
                [&]: ${false} && ${datas.get(m1(1))}.eq("$saleChannel")}.eq("${matchFiled(m1(0))}")
                """)
            }
          }
        }
        if (optStack.nonEmpty) opt = optStack.pop()
      }
    }

    tokens.foreach {
      case ")" => {
        exprProcess
      }
      case token@("|" | "&" | "(") => {
        optStack.push(token)
      }
      case token => {
        memberStatck.push(token)
      }
    }

    println("expr match complete :) ")
    if(memberStatck.nonEmpty) {
      exprProcess
    }

    println(flag)
    println(optStack)
    println(memberStatck)

  }

}

输出

log 复制代码
                [&]: saleChannel.eq("saleChannel") || orderSource.eq("source_1")
                
expr match complete :) 

                [&]: false && Some(source_1).eq("saleChannel")}.eq("source_1")
                
true
ArrayStack()
ArrayStack()
相关推荐
想做小南娘,发现自己是女生喵1 小时前
【无标题】
数据结构·算法
懒鸟一枚2 小时前
深入理解 Linux 内存、Swap 交换分区与分页机制的关系
java·linux·数据库
Kx_Triumphs3 小时前
HDU4348 To the moon(主席树区间修改模板)
算法·题解
旖-旎3 小时前
《LeetCode647 回文子串 || LeetCode 5 最长回文子串》
c++·算法·leetcode·动态规划·哈希算法
我命由我123454 小时前
执行 Gradle 指令报错,无法将“grep”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
考虑考虑4 小时前
Sentinel安装
java·后端·微服务
轻颂呀4 小时前
约瑟夫环问题
算法
凤凰院凶涛QAQ5 小时前
《Java版数据结构 & 集合类剖析》栈与队列:“push/pop 是栈的灵魂,offer/poll 是队列的骨架——四组 API,两种人生”
java·开发语言·数据结构
科技大视界6 小时前
投资AI项目,传统尽调不够用了——李章虎律师拆解算法、数据、算力三大雷区
人工智能·算法·数据挖掘