func printStringElement(level int, word models.StringElement) { if word.Type() == models.ST_SEQ { for _, child := range word.Words { Par.Send(level, COL_BLUE_B, AddColors(COL_BLUE_U, "seq")) printStringElement(level+1, child) } } else { Par.Send(level, elementColors[word.XMLName.Local][0], //TODO simplify type string call AddColors(elementColors[word.XMLName.Local][1], word.XMLName.Local), word.Value) //PrintString(level, word) } }
func StepConvert(se *models.StringElement) *Step { val := NewStep(se.Value) switch se.Type() { case models.ST_SEQ: result := *seqConvert(se) ret := Step(&result) return &ret case models.ST_CLASS: result := ClassStep(val) ret := Step(&result) return &ret case models.ST_RULE: result := RuleStep(val) ret := Step(&result) return &ret case models.ST_TERM: result := TermStep(val) ret := Step(&result) return &ret } return nil }
//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 }