progs/pprint/tree.sc
changeset 873 a25da86f7c8c
parent 742 b5b5583a3a08
equal deleted inserted replaced
872:5f5e165c9a57 873:a25da86f7c8c
     1 // scalatags 
       
     2 import ammonite.ops._
       
     3 
     1 
     4 import $ivy.`com.lihaoyi::scalatags:0.8.2` 
       
     5 import scalatags.Text.all._
       
     6 import scalatags.Text._
       
     7 
     2 
     8 // regular expressions including records
     3 // regular expressions including records
     9 abstract class Rexp 
     4 abstract class Rexp 
    10 case object ZERO extends Rexp
     5 case object ZERO extends Rexp
    11 case object ONE extends Rexp
     6 case object ONE extends Rexp
    60   case Nil => r
    55   case Nil => r
    61   case c::s => ders_simp(s, simp(der(c, r)))
    56   case c::s => ders_simp(s, simp(der(c, r)))
    62 }
    57 }
    63 
    58 
    64 
    59 
       
    60 // scalatags 
       
    61 import $ivy.`com.lihaoyi::scalatags:0.8.2` 
       
    62 import scalatags.Text.all._
       
    63 import scalatags.Text._
       
    64 
    65 def pp(r: Rexp) : TypedTag[String] = r match {
    65 def pp(r: Rexp) : TypedTag[String] = r match {
    66   case CHAR(c) => li(code(c.toString))
    66   case CHAR(c) => li(code(c.toString))
    67   case ALT(r1, r2) => li(code("+"), ul(pp(r1), pp(r2)))
    67   case ALT(r1, r2) => li(code("+"), ul(pp(r1), pp(r2)))
    68   case SEQ(r1, r2) => li(code(raw("&middot")), ul(pp(r1), pp(r2)))
    68   case SEQ(r1, r2) => li(code(raw("&middot")), ul(pp(r1), pp(r2)))
    69   case STAR(r1) => li(code("*"), ul(pp(r1)))
    69   case STAR(r1) => li(code("*"), ul(pp(r1)))
   102 val r1 = STAR(ALT(SPECIAL("<i>r<sub>1</sub></i>"), SPECIAL("<i>r<sub>2</sub></i>")))
   102 val r1 = STAR(ALT(SPECIAL("<i>r<sub>1</sub></i>"), SPECIAL("<i>r<sub>2</sub></i>")))
   103 val r2 = ALT(SPECIAL("<i>r<sub>1</sub></i>"), STAR(SPECIAL("<i>r<sub>2</sub></i>")))
   103 val r2 = ALT(SPECIAL("<i>r<sub>1</sub></i>"), STAR(SPECIAL("<i>r<sub>2</sub></i>")))
   104 
   104 
   105 @main
   105 @main
   106 def main(fname: String) = {
   106 def main(fname: String) = {
   107   val content = index(List(r1))
   107   val content = index(List(r1, r2, evil2))
   108   write.over(pwd / fname, content)
   108   os.write.over(os.pwd / fname, content)
   109 }  
   109 }  
       
   110 
       
   111 
       
   112   
       
   113 // scalatags 
       
   114 import $ivy.`com.lihaoyi::scalatags:0.8.2` 
       
   115 import scalatags.Text.all._
       
   116 import scalatags.Text._
       
   117 
       
   118 def pp(r: Rexp) : TypedTag[String] = r match {
       
   119   case CHAR(c) => li(code(c.toString))
       
   120   case ALTs(rs) => li(code("+"), ul(rs.map(pp)))
       
   121   case SEQs(rs) => li(code(raw("&middot")), ul(rs.map(pp)))
       
   122   case STAR(r1) => li(code("*"), ul(pp(r1)))
       
   123   case ZERO => li(code("0"))
       
   124   case ONE => li(code("1"))
       
   125   //case SPECIAL(s) => li(code(raw(s)))
       
   126 } 
       
   127 
       
   128 def mktree(r: Rexp) = 
       
   129    ul(cls := "tree")(pp(r))
       
   130 
       
   131 
       
   132 def index(rs: List[Rexp]) = 
       
   133   html(
       
   134     head(link(rel := "stylesheet", href := "./style.css")),
       
   135     body(rs.map(mktree))
       
   136   )
       
   137 
       
   138 
       
   139 @main
       
   140 def main(fname: String) = {
       
   141   val r = ("a" | "ab" | "a") ~ ("c" | "bc")
       
   142   val strs = List("","a","ab","abc")
       
   143   val content = index(strs.map(s => ders(s.toList, r)))
       
   144   os.write.over(os.pwd / fname, content)
       
   145 }