Advertisement
Ladies_Man

scala lab3 almost done restoration

Apr 16th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.97 KB | None | 0 0
  1. class Pos private(val prog: String, val offs: Int, val line: Int, val col: Int) {
  2.     def this(prog: String) = this(prog, 0, 1, 1)
  3.  
  4.     def ch = if (offs == prog.length) -1 else prog.codePointAt(offs)
  5.  
  6.     def inc = ch match {
  7.         case '\n' => new Pos(prog, offs+1, line+1, 1)
  8.         case -1   => this
  9.         case _    => new Pos(prog, offs+1, line, col+1)
  10.     }
  11.  
  12.     override def toString = "(" + line + ", " + col + ")"
  13.  
  14.     def isLetter = if (ch >= 'a' && ch <= 'z') true else false
  15.  
  16.     def isDigit = if (ch >= '0' && ch <= '9') true else false
  17. }
  18.  
  19. object DomainTags extends Enumeration {
  20.     type Tag = Value
  21.     val WHITESPACE, IDENT, NUMBER, OPERATION, ERROR, END_OF_PROGRAM = Value
  22. }
  23.  
  24. import DomainTags._
  25.  
  26. class Scanner {
  27.     def scan(start: Pos): (Tag, Pos) = {
  28.         //println(" sc")
  29.         sys.error("Syntax error at " + start)
  30.         // println("sys err at " + start)
  31.         // (ERROR, start)
  32.  
  33.         //start.inc
  34.     }
  35. }
  36.  
  37. class Token(val start: Pos, scanner: Scanner) {
  38.     val (tag, follow) = start.ch match {
  39.       case -1 => (END_OF_PROGRAM, start)
  40.       case _  => scanner.scan(start)
  41.     }
  42.  
  43.     def image = start.prog.substring(start.offs, follow.offs)
  44.  
  45.     def next = new Token(follow, scanner)
  46. }
  47.  
  48.  
  49.  
  50. trait Whitespaces extends Scanner {
  51.     private def missWhitespace(pos: Pos): Pos = pos.ch match {
  52.         case ' '  => missWhitespace(pos.inc)
  53.         case '\t' => missWhitespace(pos.inc)
  54.         case '\n' => missWhitespace(pos.inc)
  55.         case _    => pos
  56.     }
  57.  
  58.     override def scan(start: Pos) = {
  59.         println(" ws")
  60.         val follow = missWhitespace(start)
  61.         if (start != follow) (WHITESPACE, follow)
  62.         else super.scan(start)
  63.   }
  64. }
  65.  
  66. //
  67. // Вар 7
  68. // Идентификаторы: последовательности латинских букв и десятичных цифр, оканчивающиеся на цифру.
  69. // Числовые литералы: последовательности десятичных цифр, органиченные знаками «<» и «>».
  70. // Операции: «<=», «=», «==».
  71. //
  72.  
  73. trait Idents extends Scanner {
  74.  
  75.     private def recognizeIdent(pos: Pos): (Pos, Boolean) = pos.ch match {
  76.         case _ if pos.isDigit &&
  77.          (!pos.inc.isLetter && !pos.inc.isDigit)    => (pos.inc, false)
  78.         case _ if pos.isDigit                       => recognizeIdent(pos.inc)
  79.         case _ if pos.isLetter                      => recognizeIdent(pos.inc)
  80.         case _                                      => (pos, true)
  81.     }
  82.  
  83.     override def scan(start: Pos) = {
  84.         println(" id")
  85.         val (follow, err) =
  86.             if (start.isDigit || start.isLetter) recognizeIdent(start)
  87.             else (start, false)
  88.  
  89.         if (start != follow) {
  90.             if (err) (ERROR, follow)
  91.             else (IDENT, follow)
  92.         }
  93.         else super.scan(start)
  94.     }
  95. }
  96.  
  97.  
  98.  
  99. trait Numbers extends Scanner {
  100.  
  101.     private def recognizeNumber(pos: Pos): (Pos, Boolean) = pos.ch match {
  102.         case '<'                    => recognizeNumber(pos.inc)
  103.         case '>'                    => (pos.inc, false)
  104.         case _ if pos.isDigit       => recognizeNumber(pos.inc)
  105.         case _                      => (pos, true)
  106.     }
  107.  
  108.     override def scan(start: Pos) = {
  109.         println(" num")
  110.         val (follow, err) =
  111.             if ('<' == start.ch && start.inc.isDigit) recognizeNumber(start)
  112.             else (start, false)
  113.  
  114.         if (start != follow) {
  115.             if (err) (ERROR, follow)
  116.             else (NUMBER, follow)
  117.         }
  118.         else super.scan(start)
  119.     }
  120. }
  121.  
  122.  
  123.  
  124. trait Operations extends Scanner {
  125.  
  126.     override def scan(start: Pos) = {
  127.         println(" op")
  128.         val follow =
  129.             if ('<' == start.ch && '=' == start.inc.ch) start.inc.inc
  130.             else if ('=' == start.ch&& '=' == start.inc.ch) start.inc.inc
  131.                 else if ('=' == start.ch) start.inc
  132.                     else start
  133.  
  134.         if (start != follow) (OPERATION,  follow)
  135.         else super.scan(start)
  136.   }
  137. }
  138.  
  139.  
  140. /*
  141. trait Error extends Scanner {
  142.  
  143.   override def scan(start: Pos) = {
  144.  
  145.   }
  146.  
  147. }*/
  148.  
  149. //X extends A with B, C, D.
  150. //L(X) = X, D, C, B, A
  151. var t = new Token(
  152.     new Pos(" <456 <== <1337> == 21 brav0 asd "),
  153.     new Scanner
  154.       with Numbers
  155.       with Operations
  156.       with Idents
  157.       with Whitespaces
  158. )
  159.  
  160. while (t.tag != END_OF_PROGRAM) {
  161.     println(t.tag.toString + " " + t.start + "-" + t.follow + ": \t\t" + t.image)
  162.     t = t.next
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement