Advertisement
study64

Kleene recursivo

Jul 4th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.67 KB | None | 0 0
  1. sealed trait ParserResult
  2. case class ParserSuccess[a](head: a, tail: String) extends ParserResult
  3. case object ParserFailure extends ParserResult
  4.  
  5.  
  6. //Objeto que wrappea la lógica de los parsers básicos y me permite implementar los avanzados
  7. class ParserWrapper(callback: (String) => ParserResult) {
  8.  
  9.   //Esto se llama al ejecutar el wrapper como función
  10.   def apply(input: String): ParserResult = {
  11.     callback(input)
  12.   }
  13.  
  14.   //Combinators
  15.  
  16.   //OrCombinator
  17.   def <|>(after: ParserWrapper): ParserWrapper = {
  18.     val logic = (input: String) => {
  19.       this.apply(input) match {
  20.         case ParserFailure => after(input)
  21.         case a => a
  22.       }
  23.     }
  24.     new ParserWrapper(logic)
  25.   }
  26.  
  27.   //ConcatCombinator
  28.   def <>(after: ParserWrapper): ParserWrapper = {
  29.     val logic = (input: String) => {
  30.       val res = this.apply(input)
  31.       res match {
  32.         case ParserSuccess(h,t) => after(t) match {
  33.           case ParserSuccess(h2,t2) => ParserSuccess((h, h2), t2)
  34.           case _ => ParserFailure
  35.         }
  36.         case _ => res
  37.       }
  38.     }
  39.     new ParserWrapper(logic)
  40.   }
  41.  
  42.   //RightmostCombinator
  43.   def ~>(after: ParserWrapper): ParserWrapper = {
  44.     val logic = (input: String) => {
  45.       val res = this.apply(input)
  46.       res match {
  47.         case ParserSuccess(_, t) => after(t)
  48.         case ParserFailure => res
  49.       }
  50.     }
  51.     new ParserWrapper(logic)
  52.   }
  53.  
  54.   //LeftmostCombinator
  55.   def <~(after: ParserWrapper): ParserWrapper = {
  56.     val logic = (input: String) => {
  57.       val res = this.apply(input)
  58.       res match {
  59.         case ParserSuccess(h,t) => after(t) match {
  60.           case ParserSuccess(_,t2) => ParserSuccess(h, t2)
  61.           case _ => ParserFailure
  62.         }
  63.         case _ => res
  64.       }
  65.     }
  66.     new ParserWrapper(logic)
  67.   }
  68.  
  69.   //Métodos auxiliares
  70.   private def recursiveParsing(prevRes: ParserResult): ParserResult = {
  71.     prevRes match {
  72.       case ParserSuccess(h,t) => this.apply(t) match {
  73.         case ParserSuccess(h2,t2) => this.recursiveParsing(ParserSuccess(h :: h2, t2))
  74.         case _ => prevRes
  75.       }
  76.       case _ => prevRes
  77.     }
  78.   }
  79.  
  80.   //Parsers avanzados
  81.   def *(): ParserWrapper = {
  82.     val logic = (input: String) => {
  83.       this.recursiveParsing(ParserSuccess[List[Any]](List.empty[Any], input))
  84.     }
  85.     new ParserWrapper(logic)
  86.   }
  87.  
  88.   def opt(): ParserWrapper = {
  89.     val logic = (input: String) => {
  90.       val res = this.apply(input)
  91.       res match {
  92.         case ParserSuccess(_, _) => res
  93.         case _ => ParserSuccess[Unit]((), input.drop(1))
  94.       }
  95.     }
  96.     new ParserWrapper(logic)
  97.   }
  98. }
  99.  
  100. object MasterParser extends App {
  101.  
  102.   //Parsers básicos
  103.   def digit(): ParserWrapper = {
  104.     val logic = (input: String) => {
  105.       input.headOption match {
  106.         case Some(a) if a.isDigit => ParserSuccess[Char](a, input.drop(1))
  107.         case _ => ParserFailure
  108.       }
  109.     }
  110.     new ParserWrapper(logic)
  111.   }
  112.  
  113.   def char(aChar: Char): ParserWrapper = {
  114.     val logic = (input: String) => {
  115.       input.headOption match {
  116.         case Some(a) if a == aChar => ParserSuccess[Char](input.head, input.drop(1))
  117.         case _ => ParserFailure
  118.       }
  119.     }
  120.     new ParserWrapper(logic)
  121.   }
  122.  
  123.   def anychar(): ParserWrapper = {
  124.     val logic = (input: String) => {
  125.       input.headOption match {
  126.         case Some(a) => ParserSuccess[Char](a, input.drop(1))
  127.         case _ => ParserFailure
  128.       }
  129.     }
  130.     new ParserWrapper(logic)
  131.   }
  132.  
  133.   def letter(): ParserWrapper = {
  134.     val logic = (input: String) => {
  135.       input.headOption match {
  136.         case Some(a) if a.isLetter => ParserSuccess[Char](input.head, input.drop(1))
  137.         case _ => ParserFailure
  138.       }
  139.     }
  140.     new ParserWrapper(logic)
  141.   }
  142.  
  143.   def alphaNum(): ParserWrapper = {
  144.     val logic = (input: String) => {
  145.       input.headOption match {
  146.         case Some(a) if a.isLetterOrDigit => ParserSuccess[Char](input.head, input.drop(1))
  147.         case _ => ParserFailure
  148.       }
  149.     }
  150.     new ParserWrapper(logic)
  151.   }
  152.   def void(): ParserWrapper = {
  153.     val logic = (input: String) => {
  154.       input.headOption match {
  155.         case Some(_)  => ParserSuccess[Unit]((),input.drop(1))
  156.         case _ => ParserFailure
  157.       }
  158.     }
  159.     new ParserWrapper(logic)
  160.   }
  161.  
  162.   def string(aString: String): ParserWrapper = {
  163.     val logic = (input: String) => {
  164.       input.substring(0, Math.min(input.length, aString.length)) match {
  165.         case a if a == aString => ParserSuccess[String](a,input.drop(aString.length))
  166.         case _ => ParserFailure
  167.       }
  168.     }
  169.     new ParserWrapper(logic)
  170.   }
  171.  
  172.  
  173.   val parserJ = char('j')
  174.   val parserI = char('i')
  175.   val parserA = char('a')
  176.   val saludo = string("hola")
  177.   val JoI = parserJ <|> parserI
  178.   val JoIoA = parserJ <|> parserI <|> parserA
  179.   val JoIoAOPT = JoIoA.opt
  180.   val JyI = parserJ <> parserI
  181.   val JyIyA = JyI <> parserA
  182.   val JrightI = parserJ ~> parserI
  183.   val JleftI = parserJ <~ parserI
  184.   val parserJkleene = parserJ.*
  185.   println(JoI("ijaaah"))  //ParserSuccess(i,jaaah)
  186.   println(JoI("jijaaah")) //ParserSuccess(j,ijaaah)
  187.   println(JoI("aaah"))    //ParserFailure
  188.   println(JoIoA("aaah"))  //ParserSuccess(a,aah)
  189.   println(JoIoA("xxx"))   //ParserFailure
  190.   println(JoIoAOPT("xxx"))//ParserSuccess((),xxx)
  191.   println(saludo("!hola mundo")) //ParserFailure
  192.   println(saludo("hola mundo")) //ParserSuccess(hola, mundo)
  193.   println(JrightI("jijaaah")) //ParserSuccess(i,jaaah)
  194.   println(JleftI("jijaaah")) //ParserSuccess(j,jaaah)
  195.   println(JyI("jojo")) //ParserFailure
  196.   println(JyI("jiji")) //ParserSuccess((j,i),ji)
  197.   println(JyIyA("jio")) //ParserFailure
  198.   println(JyIyA("jia")) //ParserSuccess(((j,i),a),)
  199.   println(parserJkleene("jjjkkk"))
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement