progs/fun/fun_parser.sc
changeset 975 ae5c03560d4d
parent 961 c0600f8b6427
equal deleted inserted replaced
974:0cb4bf2469d1 975:ae5c03560d4d
     8 //     amm fun_parser.sc defs.fun
     8 //     amm fun_parser.sc defs.fun
     9 //
     9 //
    10 // this will generate a parse-tree from a list
    10 // this will generate a parse-tree from a list
    11 // of tokens
    11 // of tokens
    12 
    12 
    13 //> using toolkit 0.5.0
       
    14 // > using file fun_tokens.scala
       
    15 
    13 
       
    14 import $file.fun_tokens, fun_tokens._ 
    16 
    15 
    17 import scala.language.implicitConversions    
    16 import scala.language.implicitConversions    
    18 import scala.language.reflectiveCalls
    17 import scala.language.reflectiveCalls
    19 
    18 
    20 
    19 
    21 //import $file.fun_tokens, fun_tokens._ 
    20 
    22 
    21 
    23 
    22 
    24 // Parser combinators
    23 // Parser combinators
    25 //    type parameter I needs to be of Seq-type
    24 //    type parameter I needs to be of Seq-type
    26 //
    25 //
    27 type IsSeq[I] = I => Seq[?]
    26 type IsSeq[I] = I => Seq[?]
    28 
       
    29 /*
       
    30 abstract class Parser[I, T](using is: I => Seq[_])  {
       
    31   def parse(in: I): Set[(T, I)]  
       
    32 
       
    33   def parse_all(in: I) : Set[T] =
       
    34     for ((hd, tl) <- parse(in); 
       
    35         if is(tl).isEmpty) yield hd
       
    36 }
       
    37 */
       
    38 
    27 
    39 
    28 
    40 abstract class Parser[I, T](using is: I => Seq[?]) {
    29 abstract class Parser[I, T](using is: I => Seq[?]) {
    41   def parse(ts: I): Set[(T, I)]
    30   def parse(ts: I): Set[(T, I)]
    42 
    31 
   177 
   166 
   178 
   167 
   179 
   168 
   180 // Reading tokens and Writing parse trees
   169 // Reading tokens and Writing parse trees
   181 
   170 
   182 // pre-2.5.0 ammonite 
       
   183 // import ammonite.ops._
       
   184 
       
   185 // post 2.5.0 ammonite
       
   186 // import os._
       
   187 
       
   188 def parse_tks(tks: List[Token]) : List[Decl] = 
   171 def parse_tks(tks: List[Token]) : List[Decl] = 
   189   Prog.parse_single(tks)
   172   Prog.parse_single(tks)
   190 
   173 
   191 //@doc("Parses a file.")
   174 
   192 //@main
   175 @main
   193 def main(fname: String) : Unit = {
   176 def main(fname: String) : Unit = {
   194   val tks = tokenise(os.read(os.pwd / fname))
   177   val tks = tokenise(os.read(os.pwd / fname))
   195   println(parse_tks(tks))
   178   println(parse_tks(tks))
   196 }
   179 }
   197 
   180