Skip to content

Commit

Permalink
Add multiline comment support to lexical analyzer
Browse files Browse the repository at this point in the history
Now if lexical analyzer sees start symbol of multiline comment (/*), it tries to find ending multiline comment (*/) and ignores all code withing that block of comment
  • Loading branch information
Reza Kargar committed Nov 27, 2023
1 parent e3e5cc8 commit 5546653
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 1_lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ package main
import (
"bufio"
"io"
"log"
"strings"
"unicode"
)
Expand Down Expand Up @@ -255,6 +256,26 @@ func (l *lexer) lex() {
l.lex()
}

func (l *lexer) lexMultiLineComment() {
for {
str, err := l.reader.ReadString('*')
_ = str
if err != nil {
log.Fatal(err)
return
}
nextChar, _, err := l.reader.ReadRune()
if err != nil {
log.Fatal(err)
return
}
if nextChar == '/' {
break
}
l.reader.UnreadRune()
}
}

/*
این متد وظیفه ساخت یک توکن با نوع و مقدار مورد نظر و اضافه کردن به چنل توکن ها را به عهده دارد
*/
Expand Down Expand Up @@ -382,6 +403,10 @@ func (l *lexer) lexSymbol(r rune) {
l.reader.ReadLine()
return
}
if doubleCharSymbol == "/*" {
l.lexMultiLineComment()
return
}
if t, ok := symobls[singleCharSymbol]; ok {
l.reader.UnreadRune()
l.emit(t, singleCharSymbol)
Expand Down

0 comments on commit 5546653

Please sign in to comment.