Created
March 22, 2018 13:23
-
-
Save aolo2/4107381fa9829286ca88404154daa2f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pos private(val prog: String, val offs: Int, val line: Int, val col: Int) { | |
def this(prog: String) = this(prog, 0, 1, 1) | |
def ch = if (offs == prog.length) -1 else prog(offs) | |
def inc = ch match { | |
case '\n' => new Pos(prog, offs + 1, line + 1, 1) | |
case -1 => this | |
case _ => new Pos(prog, offs + 1, line, col + 1) | |
} | |
override def toString = "(" + line + " , " + col + ")" | |
} | |
object DomainTags extends Enumeration { | |
type Tag = Value | |
val WHITESPACE, COMMENT, KEYWORD, IDENT, ERROR, END_OF_PROGRAM = Value | |
} | |
import DomainTags._ | |
class Scanner { | |
def scan(start: Pos): (Tag, Pos) = | |
sys.error(" syntax error at " + start) | |
} | |
class Token(val start: Pos, scanner: Scanner) { | |
val (tag, follow) = start.ch match { | |
case -1 => (END_OF_PROGRAM, start) | |
case _ => scanner.scan(start) | |
} | |
def image = start.prog.substring(start.offs, follow.offs) | |
def next = new Token(follow, scanner) | |
} | |
trait Whitespaces extends Scanner { | |
private def missWhitespace(pos: Pos): Pos = pos.ch match { | |
case ' ' => missWhitespace(pos.inc) | |
case '\t' => missWhitespace(pos.inc) | |
case '\n' => missWhitespace(pos.inc) | |
case _ => pos | |
} | |
override def scan(start: Pos) = { | |
val follow = missWhitespace(start) | |
if (start != follow) (WHITESPACE, follow) | |
else super.scan(start) | |
} | |
} | |
trait Comments extends Scanner { | |
private def skipComment(pos: Pos): Pos = pos.ch match { | |
case '\n' => pos | |
case _ => skipComment(pos.inc) | |
} | |
override def scan(start: Pos) = { | |
val follow = if (start.col == 1 && start.ch == '*') skipComment(start) else start | |
if (start != follow) (COMMENT, follow) | |
else super.scan(start) | |
} | |
} | |
trait KeyWords extends Scanner { | |
private def scanKeyWord(pos: Pos, keyword: String, ch: Int): (Pos, Int) = { | |
if (ch == keyword.length()) (pos, ch) | |
else if (keyword(ch) == pos.ch) scanKeyWord(pos.inc, keyword, ch + 1) | |
else (pos, 0) | |
} | |
private def getKeyWord(pos: Pos): Pos = pos.ch match { | |
case '*' => | |
val (follow, len) = scanKeyWord(pos, "**", 0) | |
if (len == 2 && follow.ch != '*') follow | |
else pos | |
case 'w' => | |
val (follow, len) = scanKeyWord(pos, "with", 0) | |
if (len == 4) follow | |
else pos | |
case 'e' => | |
val (follow, len) = scanKeyWord(pos, "end", 0) | |
if (len == 3) follow | |
else pos | |
case _ => pos | |
} | |
override def scan(start: Pos) = { | |
val follow = getKeyWord(start) | |
if (start != follow) (KEYWORD, follow) | |
else super.scan(start) | |
} | |
} | |
trait Idents extends Scanner { | |
private def skipAstIdent(pos: Pos): Pos = pos.ch match { | |
case '*' => skipAstIdent(pos.inc) | |
case _ => pos | |
} | |
private def skipIdent(pos: Pos, len: Int): (Pos, Int) = pos.ch match { | |
case x if x >= 'a' && x <= 'z' => skipIdent(pos.inc, len + 1) | |
case _ => (pos, len + 1) | |
} | |
private def getIdent(pos: Pos): Pos = pos.ch match { | |
case '*' => skipAstIdent(pos) | |
case x if x >= 'a' && x <= 'z' => | |
val (follow, len) = skipIdent(pos, 0) | |
if (len % 2 == 1) follow | |
else pos | |
case _ => pos | |
} | |
override def scan(start: Pos) = { | |
val follow = getIdent(start) | |
if (start != follow) (IDENT, follow) | |
else super.scan(start) | |
} | |
} | |
trait Error extends Scanner { | |
private def skipError(pos: Pos): Pos = pos.ch match { | |
case ' ' => pos | |
case '\t' => pos | |
case '\n' => pos | |
case -1 => pos | |
case _ => skipError(pos.inc) | |
} | |
override def scan(start: Pos) = { | |
val follow = skipError(start) | |
(ERROR, follow) | |
} | |
} | |
object Lab3 extends App { | |
var t = new Token( | |
new Pos("ww wi wita with ea en end * ** *** aaa \n**asdasgasgcomment_line**\n * with aaaaa end"), | |
new Scanner | |
with Error | |
with Whitespaces | |
with Idents | |
with KeyWords | |
with Comments | |
) | |
while (t.tag != END_OF_PROGRAM) { | |
println(t.tag.toString + " " + t.start + "-" + t.follow + ": " + t.image) | |
t = t.next | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment