public void ColorText(WordPointer word, WordType type, TextPointer entered) { FirstTime = false; if (word.EndingPosition == null || word.StartingPoistion == null) { return; } workingBox.Selection.Select(word.StartingPoistion, word.EndingPosition); switch (type.type) { case WordType.boundType.TEXT: workingBox.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, textColor); break; case WordType.boundType.KEYWORD: workingBox.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, keyWordColor); break; case WordType.boundType.LEXERROR: workingBox.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, LexerColor); break; case WordType.boundType.COMMENT: workingBox.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.DarkGreen); break; case WordType.boundType.STRING: workingBox.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.DarkOrange); break; } workingBox.Selection.Select(entered, entered);//in khat bayad bashe ta bad az in ke karemun tamum shod selection //bardashte beshe FirstTime = true; }
public bool staticLexer() { FirstTime = false; bool lexError = false; currentTokenIndex = 0; if (listOfTokens.Count != 0) { listOfTokens.RemoveRange(0, listOfTokens.Count); } DFAOut output = new DFAOut(); output.pointer = workingBox.Document.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward); TextPointer starOfWord = workingBox.Document.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward); WordPointer current; while (true) { output = traceViaDFA(output.pointer); current = new WordPointer(starOfWord, output.pointer); current.lexingOK = output.accept; if (output.text == "comment") { current.isComment = true; } if (output.text == "string") { current.isString = true; } if (current.text == "") //traceDFA return "" when encouters with a delimeter { break; } if (current.lexingOK == false) { lexError = true; } if (current.text.Contains(">")) { current.text = current.text.Replace(">", "\\>"); } if (current.text.Contains("<")) { current.text = current.text.Replace("<", "\\<"); } if (current.text.Contains("$")) { current.text = current.text.Replace("<$", "\\$"); } current.Type = output.text; if (current.text != "\r\n" && current.isComment == false && current.text != " ") { listOfTokens.Add(current); } starOfWord = output.pointer; } //This part is for coloring the text TextPointer enteredPosition = workingBox.Document.ContentStart; WordType text3 = new WordType(); text3.type = WordType.boundType.TEXT; for (int i = 0; i < listOfTokens.Count; i++) { visual.ColorText(listOfTokens[i], text3, enteredPosition); //aval hame matn ro siah mikonim } text3.type = WordType.boundType.LEXERROR; for (int i = 0; i < listOfTokens.Count; i++) { if (listOfTokens[i].lexingOK == false) { visual.ColorText(listOfTokens[i], text3, enteredPosition); } } for (int i = 0; i < listOfTokens.Count; i++) { text3.type = WordType.boundType.KEYWORD; for (int j = 0; j < keyWords.Length; j++) { if (listOfTokens[i].text == keyWords[j]) { listOfTokens[i].Type = keyWords[j]; visual.ColorText(listOfTokens[i], text3, enteredPosition); // yani range keyWordColor break; } } } text3.type = WordType.boundType.COMMENT; for (int i = 0; i < listOfTokens.Count; i++) { if (listOfTokens[i].isComment == true) { visual.ColorText(listOfTokens[i], text3, enteredPosition); } } text3.type = WordType.boundType.STRING; for (int i = 0; i < listOfTokens.Count; i++) { if (listOfTokens[i].isString == true) { visual.ColorText(listOfTokens[i], text3, enteredPosition); } } currentTokenIndex = 0; FirstTime = true; return(lexError); }
/// <summary> /// This function use for lexing while the user is typing the input, it might significantly reduce /// the speed. /// </summary> /// <param name="beforTextEnter"></param> /// <returns></returns> public bool getLastChar(TextPointer beforTextEnter) { if (FirstTime == false) { return(false); } FirstTime = false; bool lexError = false; TextPointer currentPosition; TextPointer endOfCurrent = workingBox.Selection.End; currentPosition = workingBox.Selection.End.GetInsertionPosition(LogicalDirection.Backward); TextPointer enteredPosition = currentPosition; GroupWordPointer GroupOfWords = new GroupWordPointer(enteredPosition); if (currentPosition == null) { FirstTime = true; return(false); } TextPointer startOFWord = getCurrentChar(workingBox.Selection.Start, LogicalDirection.Backward); TextRange delemiter = new TextRange(workingBox.Selection.Start, startOFWord); string text = workingBox.Selection.Text; if (delemiter.Text == "") //a delimeter here { if (startOFWord.GetNextInsertionPosition(LogicalDirection.Backward) != null) { if (new TextRange(startOFWord, startOFWord.GetNextInsertionPosition(LogicalDirection.Backward)).GetPropertyValue(TextElement.ForegroundProperty) == Brushes.DarkGreen) { Brush brush = Brushes.DarkGreen; startOFWord = getColorPoint(startOFWord, LogicalDirection.Backward, enteredPosition, Brushes.DarkGreen); } else if (new TextRange(startOFWord, startOFWord.GetNextInsertionPosition(LogicalDirection.Backward)).GetPropertyValue(TextElement.ForegroundProperty) == Brushes.DarkOrange) { Brush brush = Brushes.DarkOrange; startOFWord = getColorPoint(startOFWord, LogicalDirection.Backward, enteredPosition, brush); } else { if (workingBox.Selection.Start.GetNextInsertionPosition(LogicalDirection.Backward) != null) { startOFWord = getCurrentChar(workingBox.Selection.Start.GetNextInsertionPosition(LogicalDirection.Backward), LogicalDirection.Backward); } } } } else { if (startOFWord.GetNextInsertionPosition(LogicalDirection.Forward) != null) { if (new TextRange(startOFWord, startOFWord.GetNextInsertionPosition(LogicalDirection.Forward)).GetPropertyValue(TextElement.ForegroundProperty) == Brushes.DarkGreen) { startOFWord = getColorPoint(startOFWord, LogicalDirection.Backward, enteredPosition, Brushes.DarkGreen); } else if (new TextRange(startOFWord, startOFWord.GetNextInsertionPosition(LogicalDirection.Forward)).GetPropertyValue(TextElement.ForegroundProperty) == Brushes.DarkOrange) { startOFWord = getColorPoint(startOFWord, LogicalDirection.Backward, enteredPosition, Brushes.DarkOrange); } } } List <WordPointer> listOFWords = new List <WordPointer>(); DFAOut outPut = new DFAOut(); outPut.pointer = startOFWord; while (true) { outPut = traceViaDFA(startOFWord); WordPointer current = new WordPointer(startOFWord, outPut.pointer); current.lexingOK = outPut.accept; if (outPut.text == "comment") { current.isComment = true; } if (outPut.text == "string") { current.isString = true; } if (current.text == "") //traceDFA returns "" when encounters a delimeter { break; } current.Type = outPut.text; if (current.lexingOK == false) { lexError = true; } GroupOfWords.addWord(current); listOFWords.Add(current); startOFWord = outPut.pointer; } workingBox.Selection.Select(enteredPosition, enteredPosition); WordType text3 = new WordType(); text3.type = WordType.boundType.TEXT; for (int i = 0; i < GroupOfWords.arrayIndex; i++) { visual.ColorText(GroupOfWords.GroupOfWords[i], text3, enteredPosition); } text3.type = WordType.boundType.LEXERROR; for (int i = 0; i < GroupOfWords.arrayIndex; i++) { if (GroupOfWords.GroupOfWords[i].lexingOK == false) { visual.ColorText(GroupOfWords.GroupOfWords[i], text3, enteredPosition); } } for (int i = 0; i < GroupOfWords.arrayIndex; i++) { text3.type = WordType.boundType.KEYWORD; for (int j = 0; j < keyWords.Length; j++) { if (GroupOfWords.GroupOfWords[i].text == keyWords[j]) { GroupOfWords.GroupOfWords[i].Type = keyWords[j]; visual.ColorText(GroupOfWords.GroupOfWords[i], text3, enteredPosition); // yani range keyWordColor break; } } } text3.type = WordType.boundType.COMMENT; for (int i = 0; i < GroupOfWords.arrayIndex; i++) { if (GroupOfWords.GroupOfWords[i].isComment == true) { visual.ColorText(GroupOfWords.GroupOfWords[i], text3, enteredPosition); } } text3.type = WordType.boundType.STRING; for (int i = 0; i < GroupOfWords.arrayIndex; i++) { if (GroupOfWords.GroupOfWords[i].isString == true) { visual.ColorText(GroupOfWords.GroupOfWords[i], text3, enteredPosition); } } FirstTime = true; return(lexError); }