mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
error handling and transformation improvements
Added vscode config for easy debug including all modules.
This commit is contained in:
26
src/.vscode/launch.json
vendored
Normal file
26
src/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch Package",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${fileDirname}"
|
||||
},
|
||||
{
|
||||
"name": "Debug with Build Flags",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "debug",
|
||||
"program": "${workspaceFolder}/mdemory-app-qt/main.go",
|
||||
// "buildFlags": "-gcflags='all=-N -l'",
|
||||
"args": [],
|
||||
"env": {},
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
|
||||
"github.com/jorenchik/mdemory/src/compiler/lexer"
|
||||
"github.com/jorenchik/mdemory/src/compiler/parser"
|
||||
)
|
||||
|
||||
func Compile(fileContents string) ([]parser.Question, error) {
|
||||
tokens, err := lexer.TokenizeMdem([]rune(fileContents))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if (err != nil) {
|
||||
return nil, err
|
||||
}
|
||||
questions, err := parser.ParseQuestions(tokens)
|
||||
if (err != nil) {
|
||||
|
||||
15
src/compiler/comperror/comperror.go
Normal file
15
src/compiler/comperror/comperror.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package comperror
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type PositionErr struct {
|
||||
Message string
|
||||
Row int32
|
||||
Column int32
|
||||
}
|
||||
|
||||
func (e PositionErr) Error() string {
|
||||
return fmt.Sprintf("%d:%d - %s", e.Row, e.Column, e.Message)
|
||||
}
|
||||
@@ -3,14 +3,17 @@ package lexer
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/jorenchik/mdemory/src/compiler/comperror"
|
||||
)
|
||||
|
||||
var buffer []rune
|
||||
var row int32
|
||||
var column int32
|
||||
var previousRow int32
|
||||
var previousColumn int32
|
||||
var textStarted bool
|
||||
var buffer []rune
|
||||
var row int32
|
||||
var column int32
|
||||
var previousRow int32
|
||||
var previousColumn int32
|
||||
var textStarted bool = false
|
||||
var identifierStarted bool = false
|
||||
|
||||
type TokenType int
|
||||
const (
|
||||
@@ -91,7 +94,7 @@ func makeTokenWithTokenBuffer(
|
||||
buffer = []rune{}
|
||||
}
|
||||
|
||||
func TokenizeMdem(fileRunes []rune) ( []Token, error ) {
|
||||
func TokenizeMdem(fileRunes []rune) ([]Token, error) {
|
||||
tokens = []Token{}
|
||||
buffer = []rune{}
|
||||
row = 1
|
||||
@@ -104,7 +107,6 @@ func TokenizeMdem(fileRunes []rune) ( []Token, error ) {
|
||||
return []Token{}, nil
|
||||
}
|
||||
|
||||
|
||||
for i := 0; i < len(fileRunes); i++ {
|
||||
c := fileRunes[i]
|
||||
|
||||
@@ -131,43 +133,53 @@ func TokenizeMdem(fileRunes []rune) ( []Token, error ) {
|
||||
switch c {
|
||||
case '[':
|
||||
makeTokenWithTokenBuffer(IdentifierStart, 1, TextFragment)
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
identifierStarted = true
|
||||
case ']':
|
||||
if !identifierStarted {
|
||||
return []Token{}, comperror.PositionErr{
|
||||
Message: "Cannot end identifier if it is not started",
|
||||
Row: row,
|
||||
Column: column,
|
||||
}
|
||||
|
||||
}
|
||||
makeTokenWithTokenBuffer(IdentifierEnd, 1, Identifier)
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
identifierStarted = false
|
||||
case '#':
|
||||
makeTokenWithTokenBuffer(SectionIdentifierStart, 1, TextFragment)
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
case '{':
|
||||
makeTokenWithTokenBuffer(SectionStart, 1, Identifier)
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
case '}':
|
||||
makeTokenWithTokenBuffer(SectionEnd, 1, TextFragment)
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
case '-':
|
||||
makeTokenWithTokenBuffer(ElementDashStart, 1, TextFragment)
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
case '>':
|
||||
makeTokenWithTokenBuffer(QuestionEnd, 1, TextFragment)
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
case '+':
|
||||
makeTokenWithTokenBuffer(ElementPlusStart, 1, TextFragment)
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
previousRow = row
|
||||
previousColumn = column
|
||||
textStarted = false
|
||||
}
|
||||
column += 1
|
||||
}
|
||||
@@ -187,17 +199,17 @@ func TokenizeMdem(fileRunes []rune) ( []Token, error ) {
|
||||
|
||||
func ToString (ttype *TokenType) string {
|
||||
switch *ttype {
|
||||
case TextFragment: return "TextFragment"
|
||||
case QuestionEnd: return "QuestionEnd"
|
||||
case ElementDashStart: return "ElementDashStart"
|
||||
case ElementPlusStart: return "ElementPlusStart"
|
||||
case Identifier: return "Identifier"
|
||||
case IdentifierStart: return "IdentifierStart"
|
||||
case IdentifierEnd: return "IdentifierEnd"
|
||||
case SectionIdentifierStart: return "SectionIdentifierStart"
|
||||
case SectionStart: return "SectionStart"
|
||||
case SectionEnd: return "SectionEnd"
|
||||
case EOF: return "EndOfFile"
|
||||
default: return "NOT_RECOGNIZED"
|
||||
case TextFragment: return "text fragment"
|
||||
case QuestionEnd: return "question end symbol"
|
||||
case ElementDashStart: return "dash element start"
|
||||
case ElementPlusStart: return "plus element start"
|
||||
case Identifier: return "identifier"
|
||||
case IdentifierStart: return "start of identifier"
|
||||
case IdentifierEnd: return "end of identifier"
|
||||
case SectionIdentifierStart: return "identifier"
|
||||
case SectionStart: return "start of a section"
|
||||
case SectionEnd: return "end of a section"
|
||||
case EOF: return "end of a file"
|
||||
default: return "unrecognized token"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/jorenchik/mdemory/src/compiler/comperror"
|
||||
"github.com/jorenchik/mdemory/src/compiler/lexer"
|
||||
)
|
||||
|
||||
@@ -32,8 +33,8 @@ type MultipleChoiceQuestion struct {
|
||||
|
||||
func (question SingleAnswerQuestion) ToString() string {
|
||||
return fmt.Sprintf(
|
||||
"%20s: section: %-10s id: %-10s %-30s: %-30s",
|
||||
"<Single choice>",
|
||||
"%20s: section: %-10s id: %-10s %-30s: %-30s",
|
||||
"<Single choice>",
|
||||
question.Section,
|
||||
question.ID,
|
||||
strings.Trim(question.Question, "\t\n "),
|
||||
@@ -44,8 +45,8 @@ func (question SingleAnswerQuestion) ToString() string {
|
||||
func (question MultipleChoiceQuestion) ToString() string {
|
||||
acc := ""
|
||||
acc += fmt.Sprintf(
|
||||
"%20s: section: %-10s id: %-10s %-30s",
|
||||
"<Multi choice>",
|
||||
"%20s: section: %-10s id: %-10s %-30s",
|
||||
"<Multi choice>",
|
||||
question.Section,
|
||||
question.ID,
|
||||
question.Question,
|
||||
@@ -67,16 +68,6 @@ type QuestionElement struct {
|
||||
|
||||
var automata map[lexer.TokenType][]lexer.TokenType
|
||||
|
||||
type CompilerErr struct {
|
||||
message string
|
||||
row int32
|
||||
column int32
|
||||
}
|
||||
|
||||
func (e CompilerErr) Error() string {
|
||||
return fmt.Sprintf("%d:%d - %s", e.row, e.column, e.message)
|
||||
}
|
||||
|
||||
func contains(s []lexer.TokenType, e lexer.TokenType) bool {
|
||||
for _, a := range s {
|
||||
if a == e {
|
||||
@@ -99,18 +90,18 @@ func parserAutomata() map[lexer.TokenType][]lexer.TokenType {
|
||||
}
|
||||
automata[lexer.QuestionEnd] = []lexer.TokenType{
|
||||
lexer.ElementDashStart,
|
||||
lexer.ElementPlusStart,
|
||||
lexer.ElementPlusStart,
|
||||
}
|
||||
automata[lexer.ElementDashStart] = []lexer.TokenType{
|
||||
lexer.IdentifierStart,
|
||||
lexer.TextFragment,
|
||||
lexer.TextFragment,
|
||||
}
|
||||
automata[lexer.ElementPlusStart] = []lexer.TokenType{
|
||||
lexer.TextFragment,
|
||||
}
|
||||
automata[lexer.Identifier] = []lexer.TokenType{
|
||||
lexer.IdentifierEnd,
|
||||
lexer.SectionStart,
|
||||
lexer.SectionStart,
|
||||
}
|
||||
automata[lexer.IdentifierStart] = []lexer.TokenType{
|
||||
lexer.Identifier,
|
||||
@@ -123,37 +114,49 @@ func parserAutomata() map[lexer.TokenType][]lexer.TokenType {
|
||||
}
|
||||
automata[lexer.SectionStart] = []lexer.TokenType{
|
||||
lexer.ElementDashStart,
|
||||
lexer.SectionIdentifierStart,
|
||||
lexer.EOF,
|
||||
lexer.SectionIdentifierStart,
|
||||
lexer.EOF,
|
||||
}
|
||||
automata[lexer.SectionEnd] = []lexer.TokenType{
|
||||
lexer.SectionIdentifierStart,
|
||||
lexer.ElementDashStart,
|
||||
lexer.EOF,
|
||||
lexer.ElementDashStart,
|
||||
lexer.EOF,
|
||||
}
|
||||
automata[lexer.SOF] = []lexer.TokenType{
|
||||
lexer.ElementDashStart,
|
||||
lexer.SectionIdentifierStart,
|
||||
lexer.EOF,
|
||||
lexer.SectionIdentifierStart,
|
||||
lexer.EOF,
|
||||
}
|
||||
automata[lexer.EOF] = []lexer.TokenType{}
|
||||
return automata
|
||||
}
|
||||
|
||||
func capitalize(str string) string {
|
||||
if len(str) <= 0 {
|
||||
return str
|
||||
}
|
||||
firstCapital := strings.ToUpper(str[0:1])
|
||||
if len(str) == 1 {
|
||||
return firstCapital
|
||||
} else {
|
||||
return firstCapital + str[1:]
|
||||
}
|
||||
}
|
||||
|
||||
func ValidateGrammar(tokens []lexer.Token) error {
|
||||
automata = parserAutomata()
|
||||
for i := 0; i < len(tokens) - 1; i++ {
|
||||
for i := 0; i < len(tokens)-1; i++ {
|
||||
token := tokens[i]
|
||||
nextToken := tokens[i+1]
|
||||
if !contains(automata[token.TokenType], nextToken.TokenType) {
|
||||
return CompilerErr{
|
||||
message: fmt.Sprintf(
|
||||
"Token %s cannot precede %s\n",
|
||||
lexer.ToString(&token.TokenType),
|
||||
return comperror.PositionErr{
|
||||
Message: fmt.Sprintf(
|
||||
"%s cannot precede %s",
|
||||
capitalize(lexer.ToString(&token.TokenType)),
|
||||
lexer.ToString(&nextToken.TokenType),
|
||||
),
|
||||
row: token.Row,
|
||||
column: token.Column,
|
||||
Row: token.Row,
|
||||
Column: token.Column,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -163,76 +166,76 @@ func ValidateGrammar(tokens []lexer.Token) error {
|
||||
var DEBUG bool = true
|
||||
|
||||
func ParseQuestions(tokens []lexer.Token) ([]Question, error) {
|
||||
err := ValidateGrammar(tokens)
|
||||
err := ValidateGrammar(tokens)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
questions := []Question{}
|
||||
section := ""
|
||||
i := 0
|
||||
if DEBUG {
|
||||
fmt.Printf("Parser output:\n")
|
||||
}
|
||||
if DEBUG {
|
||||
fmt.Printf("Parser output:\n")
|
||||
}
|
||||
for {
|
||||
if i >= len(tokens) {
|
||||
break
|
||||
}
|
||||
// - [identifier] question_token >
|
||||
// - [identifier] question_token >
|
||||
if tokens[i].TokenType == lexer.ElementDashStart {
|
||||
var id string
|
||||
var question string
|
||||
var questionElements []QuestionElement
|
||||
var id string
|
||||
var question string
|
||||
var questionElements []QuestionElement
|
||||
|
||||
if tokens[i + 1].TokenType == lexer.IdentifierStart {
|
||||
id = tokens[i + 2].Content
|
||||
question = tokens[i + 4].Content
|
||||
questionElements = []QuestionElement{}
|
||||
i += 6
|
||||
} else {
|
||||
id = ""
|
||||
question = tokens[i + 1].Content
|
||||
questionElements = []QuestionElement{}
|
||||
i += 3
|
||||
}
|
||||
if tokens[i+1].TokenType == lexer.IdentifierStart {
|
||||
id = tokens[i+2].Content
|
||||
question = tokens[i+4].Content
|
||||
questionElements = []QuestionElement{}
|
||||
i += 6
|
||||
} else {
|
||||
id = ""
|
||||
question = tokens[i+1].Content
|
||||
questionElements = []QuestionElement{}
|
||||
i += 3
|
||||
}
|
||||
|
||||
for {
|
||||
// Pointer is on the start of an element
|
||||
// - a_question >
|
||||
// - [identifier] a_question >
|
||||
// - an_element
|
||||
// terminate if we encounter a question.
|
||||
if i + 3 < len(tokens) &&
|
||||
tokens[i + 3].TokenType != lexer.EOF {
|
||||
// Pointer is on the start of an element
|
||||
// - a_question >
|
||||
// - [identifier] a_question >
|
||||
// - an_element
|
||||
// terminate if we encounter a question.
|
||||
if i+3 < len(tokens) &&
|
||||
tokens[i+3].TokenType != lexer.EOF {
|
||||
|
||||
offset := 0
|
||||
if tokens[i + 1].TokenType == lexer.IdentifierStart {
|
||||
offset = 5
|
||||
} else {
|
||||
offset = 2
|
||||
}
|
||||
if i + offset < len(tokens) &&
|
||||
tokens[i + offset].TokenType == lexer.QuestionEnd {
|
||||
break
|
||||
}
|
||||
if offset == 5 && tokens[i + 5].TokenType != lexer.QuestionEnd {
|
||||
return nil, CompilerErr{
|
||||
message: "Cannot have an identifier here",
|
||||
row: tokens[i].Row,
|
||||
column: tokens[i].Column,
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i + 2 >= len(tokens)) {
|
||||
break;
|
||||
}
|
||||
offset := 0
|
||||
if tokens[i+1].TokenType == lexer.IdentifierStart {
|
||||
offset = 5
|
||||
} else {
|
||||
offset = 2
|
||||
}
|
||||
if i+offset < len(tokens) &&
|
||||
tokens[i+offset].TokenType == lexer.QuestionEnd {
|
||||
break
|
||||
}
|
||||
if offset == 5 && tokens[i+5].TokenType != lexer.QuestionEnd {
|
||||
return nil, comperror.PositionErr{
|
||||
Message: "Cannot have an identifier here",
|
||||
Row: tokens[i].Row,
|
||||
Column: tokens[i].Column,
|
||||
}
|
||||
}
|
||||
}
|
||||
if i+2 >= len(tokens) {
|
||||
break
|
||||
}
|
||||
questionElement := QuestionElement{}
|
||||
if tokens[i].TokenType == lexer.ElementDashStart {
|
||||
questionElement.isDash = true
|
||||
} else {
|
||||
questionElement.isDash = false
|
||||
}
|
||||
questionElement.content = tokens[i + 1].Content
|
||||
questionElement.content = tokens[i+1].Content
|
||||
questionElements = append(questionElements, questionElement)
|
||||
i += 2
|
||||
}
|
||||
@@ -253,9 +256,9 @@ func ParseQuestions(tokens []lexer.Token) ([]Question, error) {
|
||||
}
|
||||
question.Choices = choices
|
||||
questions = append(questions, question)
|
||||
if DEBUG {
|
||||
fmt.Printf("%s", question.ToString())
|
||||
}
|
||||
if DEBUG {
|
||||
fmt.Printf("%s", question.ToString())
|
||||
}
|
||||
} else if len(questionElements) == 1 {
|
||||
question := SingleAnswerQuestion{
|
||||
ID: id,
|
||||
@@ -266,32 +269,32 @@ func ParseQuestions(tokens []lexer.Token) ([]Question, error) {
|
||||
question.Section = section
|
||||
}
|
||||
questions = append(questions, question)
|
||||
if DEBUG {
|
||||
fmt.Printf("%s\n", question.ToString())
|
||||
}
|
||||
if DEBUG {
|
||||
fmt.Printf("%s\n", question.ToString())
|
||||
}
|
||||
}
|
||||
} else if tokens[i].TokenType == lexer.SectionIdentifierStart {
|
||||
section = tokens[i + 1].Content
|
||||
section = tokens[i+1].Content
|
||||
i += 3
|
||||
if DEBUG {
|
||||
fmt.Printf("Started section: %s\n", section)
|
||||
}
|
||||
if DEBUG {
|
||||
fmt.Printf("Started section: %s\n", section)
|
||||
}
|
||||
} else if tokens[i].TokenType == lexer.SectionEnd {
|
||||
section = ""
|
||||
i += 1
|
||||
if DEBUG {
|
||||
fmt.Printf("Section ended: %s\n", section)
|
||||
}
|
||||
if DEBUG {
|
||||
fmt.Printf("Section ended: %s\n", section)
|
||||
}
|
||||
} else if tokens[i].TokenType == lexer.EOF {
|
||||
if DEBUG {
|
||||
fmt.Printf("File terminated: EOF\n")
|
||||
}
|
||||
if DEBUG {
|
||||
fmt.Printf("File terminated: EOF\n")
|
||||
}
|
||||
break
|
||||
} else {
|
||||
return nil, CompilerErr{
|
||||
message: "Unexpeced token: %s",
|
||||
row: tokens[i].Row,
|
||||
column: tokens[i].Column,
|
||||
return nil, comperror.PositionErr{
|
||||
Message: "Unexpeced token",
|
||||
Row: tokens[i].Row,
|
||||
Column: tokens[i].Column,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,16 @@
|
||||
package main
|
||||
|
||||
// iter: 1
|
||||
// [1] 2 3 4 5 6 7
|
||||
|
||||
// 2
|
||||
// press 4
|
||||
// 1 2 3 [4] 5 6 7
|
||||
// 4 (page num) - 2
|
||||
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"strconv"
|
||||
"math/rand"
|
||||
|
||||
"github.com/jorenchik/mdemory/src/compiler/api"
|
||||
"github.com/jorenchik/mdemory/src/compiler/comperror"
|
||||
"github.com/jorenchik/mdemory/src/compiler/parser"
|
||||
"github.com/therecipe/qt/core"
|
||||
"github.com/therecipe/qt/widgets"
|
||||
@@ -42,6 +35,12 @@ var questions [] parser.Question
|
||||
var paginationButtons []*widgets.QToolButton
|
||||
var paginationLabel *widgets.QLabel
|
||||
|
||||
var errorBox *widgets.QWidget
|
||||
var errorLabel *widgets.QLabel
|
||||
|
||||
|
||||
var multispacePattern *regexp.Regexp = regexp.MustCompile(`\s\s+`)
|
||||
|
||||
// var currentPage = 0
|
||||
var PER_PAGE = 10
|
||||
var pages []Page
|
||||
@@ -156,13 +155,7 @@ func CreateMdem() *Mdem {
|
||||
}
|
||||
|
||||
func CreateMdems(questions *[]parser.Question) {
|
||||
if spacerInitialized {
|
||||
hMdemScroll.RemoveItem(mdemSpacer)
|
||||
} else {
|
||||
mdemSpacer = widgets.NewQSpacerItem(
|
||||
0, 0, widgets.QSizePolicy__Maximum, widgets.QSizePolicy__Minimum,
|
||||
)
|
||||
}
|
||||
hMdemScroll.RemoveItem(mdemSpacer)
|
||||
|
||||
for i := range mdems {
|
||||
if mdems[i].wMdem.IsVisible() {
|
||||
@@ -179,6 +172,12 @@ func CreateMdems(questions *[]parser.Question) {
|
||||
}
|
||||
|
||||
// destroy widgets
|
||||
transformAnswer := func(answer string) string {
|
||||
answer = strings.ReplaceAll(answer, "\t", " ")
|
||||
answer = strings.ReplaceAll(answer, "\n", " ")
|
||||
answer = multispacePattern.ReplaceAllString(answer, ` `)
|
||||
return answer
|
||||
}
|
||||
for i := range *questions {
|
||||
question := (*questions)[i]
|
||||
switch question.(type) {
|
||||
@@ -186,10 +185,10 @@ func CreateMdems(questions *[]parser.Question) {
|
||||
mdems[i].wFrontText.SetText(
|
||||
question.(parser.SingleAnswerQuestion).Question,
|
||||
)
|
||||
answer := strings.Trim(
|
||||
question.(parser.SingleAnswerQuestion).Answer, " \t\n",
|
||||
)
|
||||
mdems[i].backLabels[0].SetText(fmt.Sprintf("- %s", answer))
|
||||
answer := question.(parser.SingleAnswerQuestion).Answer
|
||||
answer = transformAnswer(answer)
|
||||
answer = fmt.Sprintf("- %s", answer)
|
||||
mdems[i].backLabels[0].SetText(answer)
|
||||
if mdems[i].wBack.IsVisible() {
|
||||
mdems[i].wBack.Hide()
|
||||
}
|
||||
@@ -200,11 +199,13 @@ func CreateMdems(questions *[]parser.Question) {
|
||||
question.(parser.MultipleChoiceQuestion).Question,
|
||||
)
|
||||
for k := range choices {
|
||||
answer := strings.Trim(choices[k].Answer, " \t\n")
|
||||
answer := choices[k].Answer
|
||||
answer = transformAnswer(answer)
|
||||
answer = fmt.Sprintf("- %s", answer)
|
||||
if k < len(mdems[i].backLabels) {
|
||||
mdems[i].backLabels[k].SetText(answer)
|
||||
} else {
|
||||
label := widgets.NewQLabel2(fmt.Sprintf("- %s", answer), nil, 0)
|
||||
label := widgets.NewQLabel2(answer, nil, 0)
|
||||
mdems[i].backLabels = append(
|
||||
mdems[i].backLabels,
|
||||
label,
|
||||
@@ -219,15 +220,6 @@ func CreateMdems(questions *[]parser.Question) {
|
||||
}
|
||||
}
|
||||
|
||||
if !spacerInitialized {
|
||||
mdemSpacer = widgets.NewQSpacerItem(
|
||||
40,
|
||||
40,
|
||||
widgets.QSizePolicy__Minimum,
|
||||
widgets.QSizePolicy__Expanding,
|
||||
)
|
||||
spacerInitialized = true
|
||||
}
|
||||
hMdemScroll.AddItem(mdemSpacer)
|
||||
}
|
||||
|
||||
@@ -313,6 +305,9 @@ func OpenModelFile(
|
||||
if model.IsDir(index) {
|
||||
return
|
||||
}
|
||||
if errorBox.IsVisible() {
|
||||
errorBox.Hide()
|
||||
}
|
||||
filePath := model.FilePath(index)
|
||||
fileContents, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
@@ -329,13 +324,25 @@ func OpenModelFile(
|
||||
start := time.Now().UnixMicro()
|
||||
questions, err = api.Compile(string(fileContents))
|
||||
if err != nil {
|
||||
widgets.QMessageBox_Critical(
|
||||
nil,
|
||||
"Compilation error",
|
||||
err.Error(),
|
||||
widgets.QMessageBox__Ok,
|
||||
widgets.QMessageBox__Ok,
|
||||
)
|
||||
var errText string
|
||||
switch err.(type) {
|
||||
case comperror.PositionErr:
|
||||
errText = fmt.Sprintf(
|
||||
"%s on line %d, column %d",
|
||||
err.(comperror.PositionErr).Message,
|
||||
err.(comperror.PositionErr).Row,
|
||||
err.(comperror.PositionErr).Column,
|
||||
)
|
||||
default:
|
||||
errText = err.Error()
|
||||
}
|
||||
errorLabel.SetText(errText)
|
||||
errorBox.Show()
|
||||
for i := range(mdems) {
|
||||
if mdems[i].wMdem.IsVisible() {
|
||||
mdems[i].wMdem.Hide()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
duration := float32(time.Now().UnixMicro()-start) / 1000
|
||||
@@ -420,6 +427,24 @@ func main() {
|
||||
mdemScroll.SetWidgetResizable(true)
|
||||
mdemContainer.SetLayout(hMdemScroll)
|
||||
|
||||
errorBox = widgets.NewQWidget(nil, 0)
|
||||
hErrorBox := widgets.NewQHBoxLayout2(nil)
|
||||
errorBox.SetLayout(hErrorBox)
|
||||
errorLabel = widgets.NewQLabel(nil, 0)
|
||||
errorLabel.SetText("Error")
|
||||
errorBox.Layout().AddWidget(errorLabel)
|
||||
hErrorBox.AddStretch(1)
|
||||
hMdemScroll.AddWidget(errorBox, 0, 0)
|
||||
errorBox.SetObjectName("error")
|
||||
errorBox.SetStyleSheet(`
|
||||
QWidget#error {
|
||||
border: 1px solid #dc143c;
|
||||
background-color: white;
|
||||
padding: 10px 0px;
|
||||
}
|
||||
`)
|
||||
errorBox.Hide()
|
||||
|
||||
for i := 0; i < 40; i++ {
|
||||
mdem := CreateMdem()
|
||||
mdems = append(
|
||||
@@ -428,6 +453,13 @@ func main() {
|
||||
)
|
||||
hMdemScroll.AddWidget(mdem.wMdem, 0, 0)
|
||||
}
|
||||
mdemSpacer = widgets.NewQSpacerItem(
|
||||
40,
|
||||
40,
|
||||
widgets.QSizePolicy__Minimum,
|
||||
widgets.QSizePolicy__Expanding,
|
||||
)
|
||||
hMdemScroll.AddItem(mdemSpacer)
|
||||
|
||||
// CreateMdems
|
||||
rightLayout.AddWidget(mdemScroll, 1, 0)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user