func (cs ClassStep) RuleMatch(parent *SeqIterator) bool { _, element, e := GetCurrent(parent) if isError(e) { return false } return lexer.GetClassByName(cs.String()) == element.Cat }
func main() { output.InitWriter() fmt.Println("start") lexemeTypes, syntaxRules := utils.ParseXML(utils.Loadfile(utils.TermsFilePath, utils.TermsFileName)) // utils.ParseJSONLexemes("lexemes.json") sourceFile := "code1.txt" fileText := utils.LoadfileDefault(sourceFile) lexer.SetTokens(lexemeTypes) text := lexer.HandleSource(fileText) seq := lexer.ScanCycle(text) output.PrintString(0, []lexer.Lexeme(*seq)) tree := controller.SyntaxCycle(seq, syntaxRules) output.CloseWriter() //fmt.Println(tree.ChildList[0].Type) htmlreport.MakeReport(tree) classStep := controller.ClassStep(controller.NewStep("SIGN")) step := controller.Step(&classStep) facade := controller.NewFacade(&step, nil, nil) fmt.Println("Test ", lexer.GetClassByName(facade.StepValue())) ruleStep := controller.RuleStep(controller.NewStep("program")) step = controller.Step(&ruleStep) facade = controller.NewFacade(&step, nil, nil) fmt.Println(facade.StepType(), facade.StepValue()) b, ch := facade.HasChilds() fmt.Println("Test ", b, *ch) }
func ParseXML(rawData []byte) (tokens lexer.TokenList, rules []models.Rule) { var xmlFile models.XMLRoot parseError := xml.Unmarshal(rawData, &xmlFile) if parseError != nil { output.ColorPrint(COL_RED_BB, 2, parseError.Error()) } for _, term := range xmlFile.Terms { //TODO rename "terms" tokens = append(tokens, *lexer.NewToken(term.Name, lexer.GetClassByName(term.Class), term.Value)) } rules = xmlFile.Rules return }
//Main syntax parsing function func Translate(word *models.StringElement, parentCursor *SeqIterator) (result bool) { deepInc() defer deepDec() cursor, element, error := GetCurrent(parentCursor) if isError(error) { return false } cursor.buffer.StringElement = *word switch word.Type() { case models.ST_CLASS: output.Par.SendPair(deep-1, COL_BLUE_B, "CLASS", word.Value, "curs.i", strconv.Itoa(cursor.int), (*element.Value).String()) result = lexer.GetClassByName(word.Value) == element.Cat case models.ST_TERM: output.Par.SendPair(deep-1, COL_BLUE_B, "TERM", word.Value, (*element.Value).String()) result = element.Name == word.Value case models.ST_RULE: output.Par.Send(deep-1, COL_BLUE_B, "RULE", word.Value) result = Translate(&GetRule(word.Value).TopWord, cursor) case models.ST_SEQ: output.Par.SendPair(deep, COL_BLUE_U, "SEQ", deep, "O", word.Optional, "C", word.Choises, "I", word.Iterative) checkChilds := func(curs *SeqIterator) bool { isLast := func(n int) bool { if n == len(word.Words)-1 { return true } else { return false } } for i, child := range word.Words { childResult := Translate(&child, curs) if word.Choises { if childResult { return true } else if isLast(i) { return false } } else { if childResult { if isLast(i) { return true } } else { return false } } } return false } if word.Iterative { Nrep := 0 cycleCursor, error := InitIter(cursor) cycleCursor.buffer.XMLName.Local = string(models.ST_SEQ) if !isError(error) { for iterSucc := true; iterSucc; { iterSucc = checkChilds(cycleCursor) output.Par.SendPair(deep, COL_RED, iterSucc, Nrep, cursor.int, cycleCursor.int) if iterSucc { Nrep++ } } } if Nrep > 0 { cycleCursor.ApplyToParent() result = true } else { result = false } } else { result = checkChilds(cursor) } if word.Optional && !result { result = true cursor.buffer = models.NewTreeNode() cursor.buffer.XMLName.Local = string(models.ST_SEQ) output.Par.Send(0, COL_DEEP_LIM, "OPTIONAL") } } if result { if word.Type() == models.ST_RULE || word.Type() == models.ST_SEQ { cursor.ApplyToParent() } else { fmt.Println("Add to parent", *word, "|", *element) cursor.AddToParent(word, element) } output.Par.SendPair(deep, COL_GREEN, "RES", result, word.Type()) //TODO print output for failures too } return }