progs/parser-combinators/comb2.sc
changeset 980 4f422766763f
parent 976 4be299d9b41b
equal deleted inserted replaced
979:15e49c674b46 980:4f422766763f
    56   def parse(sb: String) = {
    56   def parse(sb: String) = {
    57     val (prefix, suffix) = sb.splitAt(s.length)
    57     val (prefix, suffix) = sb.splitAt(s.length)
    58     if (prefix == s) Set((prefix, suffix)) else Set()
    58     if (prefix == s) Set((prefix, suffix)) else Set()
    59   }
    59   }
    60 }
    60 }
       
    61 
       
    62 
    61 
    63 
    62 // atomic parser for identifiers (variable names)
    64 // atomic parser for identifiers (variable names)
    63 case object IdParser extends Parser[String, String] {
    65 case object IdParser extends Parser[String, String] {
    64   val reg = "[a-z][a-z,0-9]*".r
    66   val reg = "[a-z][a-z,0-9]*".r
    65   def parse(sb: String) = reg.findPrefixOf(sb) match {
    67   def parse(sb: String) = reg.findPrefixOf(sb) match {
    81 }
    83 }
    82 
    84 
    83 // the following string interpolation allows us to write 
    85 // the following string interpolation allows us to write 
    84 // StrParser(_some_string_) more conveniently as 
    86 // StrParser(_some_string_) more conveniently as 
    85 //
    87 //
    86 // p"<_some_string_>" 
    88 // p"_some_string_" 
    87 
    89 
    88 extension (sc: StringContext) 
    90 extension (sc: StringContext) 
    89   def p(args: Any*) = StrParser(sc.s(args*))
    91   def p(args: Any*) = StrParser(sc.s(args*))
       
    92 
       
    93 
       
    94 
       
    95 // the following is an alternative way to achieve the same, except
       
    96 // that the p needs parentheses as in
       
    97 //
       
    98 // p("_some_string_")
       
    99 //
       
   100 //import StrParser.{apply => p}
    90 
   101 
    91 // the abstract syntax trees for the WHILE language
   102 // the abstract syntax trees for the WHILE language
    92 abstract class Stmt
   103 abstract class Stmt
    93 abstract class AExp
   104 abstract class AExp
    94 abstract class BExp 
   105 abstract class BExp