mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
Separate parser package
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user