scala 连接符解析 基础 @


scala 连接符解析 基础 

import scala.util.parsing.combinator._

object ArithExpr extends JavaTokenParsers {
  

  def expr: Parser[Any] = {

   
     log(term)("加减法表达式  term") ~ rep("+" ~ log(term)("加法 term")  | "-"~ log(term)("减法  term") )
  }
  def term: Parser[Any] = log(factor)("乘除法表达式   factor")  ~ rep("*" ~ log(factor)("乘法 factor") | "/" ~ log(factor)("除法  factor"))
  def factor: Parser[Any] = (wholeNumber | decimalNumber | floatingPointNumber) | "(" ~ log(expr)("factor expr") ~ ")"

  def main(args: Array[String]) {

  
    println(parseAll(expr, "(1*2)* 5"))
  }
}



运行结果 如下 


"C:\Program Files\Java\jdk1.8.0_65\bin\java" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 15.0.2\bin" -......" com.intellij.rt.execution.application.AppMain ArithExpr
trying 加减法表达式  term at scala.util.parsing.input.CharSequenceReader@1fd3711
trying 乘除法表达式   factor at scala.util.parsing.input.CharSequenceReader@1fd3711
trying factor expr at scala.util.parsing.input.CharSequenceReader@f9b8bc
trying 加减法表达式  term at scala.util.parsing.input.CharSequenceReader@f9b8bc
trying 乘除法表达式   factor at scala.util.parsing.input.CharSequenceReader@f9b8bc
乘除法表达式   factor --> [1.3] parsed: 1
trying 乘法 factor at scala.util.parsing.input.CharSequenceReader@146e1f4
乘法 factor --> [1.5] parsed: 2
加减法表达式  term --> [1.5] parsed: (1~List((*~2)))
factor expr --> [1.5] parsed: ((1~List((*~2)))~List())
乘除法表达式   factor --> [1.6] parsed: (((~((1~List((*~2)))~List()))~))
trying 乘法 factor at scala.util.parsing.input.CharSequenceReader@144fc66
乘法 factor --> [1.9] parsed: 5
加减法表达式  term --> [1.9] parsed: ((((~((1~List((*~2)))~List()))~))~List((*~5)))
[1.9] parsed: (((((~((1~List((*~2)))~List()))~))~List((*~5)))~List())


Process finished with exit code 0

scala中 @ 除了是注解 以及 抽取XML的属性 功能以外 , 还有一个功能就是模式匹配中 匹配 类似 some的值



  * Created by admin on 2016/12/28.
  */
object test6 {
  def main(args: Array[String]) {

   val d@(c@Some(a), Some(b)) = (Some(1), Some(2))
    println(d)
    println(c)
    println(a)
    println(b)

    println()
     (Some(1), Some(2)) match {
      case d@(c@Some(a), Some(b)) => println(a, b, c, d)
    }

    println()
    println()
    for (x @ Some(y) <- Seq(None, Some(1))) println(x, y)
    val List(x, xs@_*) = List(1, 2, 3)
    println(x)
    println(xs)
    println()
    val o: Option[Int] = Some(2)
    o match {
      case Some(x) => println(x)
      case None =>
    }
    o match {
      case x@Some(_) => println(x)
      case None =>
    }


  }
}

运行结果 

"C:\Program Files\Java\jdk1.8.0_65\bin\java" -Didea.launcher.port=7537 "-Didea.launcher.bin.path=.......r" com.intellij.rt.execution.application.AppMain test6
(Some(1),Some(2))
Some(1)
1
2


(1,2,Some(1),(Some(1),Some(2)))




(Some(1),1)
1
List(2, 3)


2
Some(2)


Process finished with exit code 0



你可能感兴趣的:(scala 连接符解析 基础 @)