Separate parser package

This commit is contained in:
jorenchik
2024-08-04 22:38:01 +03:00
parent 1164a10846
commit e824115fae
2 changed files with 29 additions and 25 deletions

View File

@@ -4,38 +4,25 @@ import (
"fmt"
"log"
"os"
"github.com/jorenchik/mdemory/src/compiler/lexer"
"github.com/jorenchik/mdemory/src/compiler/parser"
)
func main() {
log.Println("Compilation started")
file, err := os.ReadFile("/home/jorenchik/Code/mdemory/src/compiler/input.mdem")
file, err := os.ReadFile(
"/home/jorenchik/Code/mdemory/src/compiler/input.mdem",
)
if (err != nil) {
log.Fatalf("Cannot open the input file: %s", err.Error())
log.Fatalf(
"Cannot open the input file: %s",
err.Error(),
)
return
}
fileContents := string(file)
tokens, err := lexer.TokenizeMdem([]rune(fileContents))
if (err != nil) {
fmt.Printf("%s\n", err.Error())
return
}
if (true) {
log.Println("Lexer output:")
for _, el := range tokens {
fmt.Print(el.ToString())
}
}
automata = parserAutomata()
err = validateGrammar(tokens)
if (err != nil) {
log.Fatal(err.Error())
}
questions, err := ParseQuestions(tokens)
questions, err := parser.ParseQuestions(fileContents)
if (err != nil) {
log.Fatal(err.Error())
}

View File

@@ -1,4 +1,4 @@
package main
package parser
import (
"fmt"
@@ -127,7 +127,8 @@ func parserAutomata() map[lexer.TokenType][]lexer.TokenType {
return automata
}
func validateGrammar(tokens []lexer.Token) error {
func ValidateGrammar(tokens []lexer.Token) error {
automata = parserAutomata()
for i := 0; i < len(tokens) - 1; i++ {
token := tokens[i]
nextToken := tokens[i + 1]
@@ -146,7 +147,23 @@ func validateGrammar(tokens []lexer.Token) error {
return nil
}
func ParseQuestions(tokens []lexer.Token) ([]Question, error) {
func ParseQuestions(fileContents string) ([]Question, error) {
tokens, err := lexer.TokenizeMdem([]rune(fileContents))
if (err != nil) {
return nil, err
}
if (true) {
log.Println("Lexer output:")
for _, el := range tokens {
fmt.Print(el.ToString())
}
}
err = ValidateGrammar(tokens)
if (err != nil) {
log.Fatal(err.Error())
}
questions := []Question{}
section := ""
i := 0