//public static char[] Parse(string text,string separators) //{ // //string Result=""; // System.Text.StringBuilder Result=new System.Text.StringBuilder (); // text= " " + text +" "; // char c; // for(int i = 0; i <text.Length;i++) // { // c = text[i]; // if (separators.IndexOf (c)>=0 ) // Result.Append (' '); // else // Result.Append ('.'); // } // return Result.ToString().ToCharArray (); //} public static void AddPatternString(string text, Row row, Pattern pattern, TextStyle style, Segment segment, bool hasError) { Word x = row.Add(text); x.Style = style; x.Pattern = pattern; x.HasError = hasError; x.Segment = segment; }
public Word GetEndBracketWord(Word Start, Pattern End, Segment FindIn) { if (Start == null || Start.Pattern == null || Start.Segment == null) return null; int CurrentRow = Start.Row.Index; int LastRow = this.Count - 1; if (FindIn.EndRow != null) LastRow = FindIn.EndRow.Index; int x = Start.Index; int count = 0; while (CurrentRow <= LastRow) { for (int i = x; i < this[CurrentRow].Count; i++) { Word w = this[CurrentRow][i]; if (w.Segment == FindIn && w.Type == WordType.xtWord) { if (w.Pattern == Start.Pattern) count++; if (w.Pattern == End) count--; if (count == 0) return w; } } if (!Start.Pattern.IsMultiLineBracket) break; CurrentRow++; x = 0; } return null; }
public Word GetStartBracketWord(Word Start, Pattern End, Segment FindIn) { if (Start == null || Start.Pattern == null || Start.Segment == null) return null; int CurrentRow = Start.Row.Index; int FirstRow = FindIn.StartRow.Index; int x = Start.Index; int count = 0; while (CurrentRow >= FirstRow) { for (int i = x; i >= 0; i--) { Word w = this[CurrentRow][i]; if (w.Segment == FindIn && w.Type == WordType.xtWord) { if (w.Pattern == Start.Pattern) count++; if (w.Pattern == End) count--; if (count == 0) return w; } } if (!Start.Pattern.IsMultiLineBracket) break; CurrentRow--; if (CurrentRow >= 0) x = this[CurrentRow].Count - 1; } return null; }
public void AutoIndentSegment(Segment Segment) { if (Segment == null) Segment = this[0].StartSegment; Row start = Segment.StartRow; Row end = Segment.EndRow; if (start == null) start = this[0]; if (end == null) end = this[this.Count - 1]; for (int i = start.Index; i <= end.Index; i++) { Row r = this[i]; int depth = r.Indent; string text = r.Text.Substring(r.GetLeadingWhitespace().Length); string indent = new string('\t', depth); r.Text = indent + text; } this.ResetVisibleRows(); }
public void SetExpansionSegment() { this.Expansion_StartSegment = null; this.Expansion_EndSegment = null; foreach (Segment s in this.StartSegments) { if (!this.EndSegments.Contains(s)) { this.Expansion_StartSegment = s; break; } } foreach (Segment s in this.EndSegments) { if (!this.StartSegments.Contains(s)) { this.Expansion_EndSegment = s; break; } } if (this.Expansion_EndSegment != null) this.Expansion_StartSegment = null; }
public unsafe static void AddString(string text,Row row,TextStyle style,Segment segment) { if (text == "") return; string[] spl = WordSplitter.Split(text); string w = null; for (int i = 0; i < spl.Length; i++) { w = spl[i]; Word word = row.Add(w); word.Style = style; word.Segment = segment; if (w == " ") { word.Type = WordType.xtSpace; } else if (w == "\t") { word.Type = WordType.xtTab; } } /*if (Text=="") return; System.Text.StringBuilder CurrentWord=new System.Text.StringBuilder(); char[] Buff=Text.ToCharArray(); fixed (char* c = &Buff[0]) { for (int i=0;i<Text.Length;i++) { if (c[i]==' ' || c[i]=='\t') { if (CurrentWord.Length != 0) { Word word = Row.Add (CurrentWord.ToString ()); word.Style =Style; word.Segment =Segment; CurrentWord =new System.Text.StringBuilder(); } Word ws=Row.Add (c[i].ToString ()); if (c[i]==' ') ws.Type= WordType.xtSpace; else ws.Type= WordType.xtTab; ws.Style = Style ; ws.Segment = Segment; } else CurrentWord.Append (c[i].ToString ()); } if (CurrentWord.Length != 0) { Word word=Row.Add (CurrentWord.ToString ()); word.Style =Style; word.Segment =Segment; } }*/ }
private void ParseText(Row Row, Segment CurrentSegment, string Text) { int CurrentPosition = 0; bool HasComplex = true; while (true) { ScanResult_Word Word = GetNextWord(Text, CurrentSegment, CurrentPosition, ref HasComplex); if (!Word.HasContent) { ParseTools.AddString(Text.Substring(CurrentPosition), Row, CurrentSegment.BlockType.Style, CurrentSegment); break; } else { ParseTools.AddString(Text.Substring(CurrentPosition, Word.Position - CurrentPosition), Row, CurrentSegment.BlockType.Style, CurrentSegment); ParseTools.AddPatternString(Word.Token, Row, Word.Pattern, Word.ParentList.Style, CurrentSegment, false); CurrentPosition = Word.Position + Word.Token.Length; } } }
private void InternalParseLine(int index, bool ParseKeywords) { if (this.mLanguage == null) return; // // if (ParseKeywords) // return; // ParseKeywords=true; SyntaxDocument doc = Document; Row Row = doc[index]; Segment OldEndSegment = Row.EndSegment; Segment OldStartSegment = Row.StartSegment; bool Fold = !Row.IsCollapsed; if (Row.IsCollapsedEndPart) { //Row.Expansion_EndSegment.Expanded = true; //Row.Expansion_EndSegment.EndRow = null; Row.Expansion_EndSegment.EndWord = null; } //set startsegment for this row if (index > 0) { Row.StartSegment = Document[index - 1].EndSegment; } else { if (Row.StartSegment == null) { Row.StartSegment = new Segment(Row); Row.StartSegment.BlockType = this.mLanguage.MainBlock; } } int CurrentPosition = 0; Segment CurrentSegment = Row.StartSegment; //kör tills vi kommit till slutet av raden.. Row.EndSegments.Clear(); Row.StartSegments.Clear(); Row.Clear(); // bool HasEndSegment=false; while (true) { ScanResult_Segment ChildSegment = GetNextChildSegment(Row, CurrentSegment, CurrentPosition); ScanResult_Segment EndSegment = GetEndSegment(Row, CurrentSegment, CurrentPosition); if ((EndSegment.HasContent && ChildSegment.HasContent && EndSegment.Position <= ChildSegment.Position) || (EndSegment.HasContent && ChildSegment.HasContent == false)) { //this is an end segment if (ParseKeywords) { string Text = Row.Text.Substring(CurrentPosition, EndSegment.Position - CurrentPosition); ParseText(Row, CurrentSegment, Text); } Segment oldseg = CurrentSegment; while (CurrentSegment != EndSegment.Segment) { Row.EndSegments.Add(CurrentSegment); CurrentSegment = CurrentSegment.Parent; } Row.EndSegments.Add(CurrentSegment); TextStyle st2 = CurrentSegment.Scope.Style; ParseTools.AddPatternString(EndSegment.Token, Row, EndSegment.Pattern, st2, CurrentSegment, false); while (oldseg != EndSegment.Segment) { oldseg.EndRow = Row; oldseg.EndWord = Row[Row.Count - 1]; oldseg = oldseg.Parent; } CurrentSegment.EndRow = Row; CurrentSegment.EndWord = Row[Row.Count - 1]; if (CurrentSegment.Parent != null) CurrentSegment = CurrentSegment.Parent; CurrentPosition = EndSegment.Position + EndSegment.Token.Length; } else if (ChildSegment.HasContent) { //this is a child block if (ParseKeywords) { string Text = Row.Text.Substring(CurrentPosition, ChildSegment.Position - CurrentPosition); //TextStyle st=CurrentSegment.BlockType.Style; ParseText(Row, CurrentSegment, Text); //ParseTools.AddString (Text,Row,st,CurrentSegment); } Segment NewSeg = new Segment(); NewSeg.Parent = CurrentSegment; NewSeg.BlockType = ChildSegment.BlockType; NewSeg.Scope = ChildSegment.Scope; Row.StartSegments.Add(NewSeg); TextStyle st2 = NewSeg.Scope.Style; ParseTools.AddPatternString(ChildSegment.Token, Row, ChildSegment.Pattern, st2, NewSeg, false); NewSeg.StartRow = Row; NewSeg.StartWord = Row[Row.Count - 1]; CurrentSegment = NewSeg; CurrentPosition = ChildSegment.Position + ChildSegment.Token.Length; if (ChildSegment.Scope.SpawnBlockOnStart != null) { Segment SpawnSeg = new Segment(); SpawnSeg.Parent = NewSeg; SpawnSeg.BlockType = ChildSegment.Scope.SpawnBlockOnStart; SpawnSeg.Scope = new Scope(); SpawnSeg.StartWord = NewSeg.StartWord; Row.StartSegments.Add(SpawnSeg); CurrentSegment = SpawnSeg; } } else { if (CurrentPosition < Row.Text.Length) { if (ParseKeywords) { //we did not find a childblock nor an endblock , just output the last pice of text string Text = Row.Text.Substring(CurrentPosition); //TextStyle st=CurrentSegment.BlockType.Style; ParseText(Row, CurrentSegment, Text); //ParseTools.AddString (Text,Row,st,CurrentSegment); } } break; } } while (!CurrentSegment.BlockType.MultiLine) { Row.EndSegments.Add(CurrentSegment); CurrentSegment = CurrentSegment.Parent; } Row.EndSegment = CurrentSegment; Row.SetExpansionSegment(); if (ParseKeywords) Row.RowState = RowState.AllParsed; else Row.RowState = RowState.SegmentParsed; if (IsSameButDifferent(index, OldStartSegment)) { MakeSame(index); //if (!IsSameButDifferent(index)) // System.Diagnostics.Debugger.Break(); } if (Row.CanFold) Row.Expansion_StartSegment.Expanded = Fold; //dont flag next line as needs parsing if only parsing keywords if (!ParseKeywords) { if (OldEndSegment != null) { if (Row.EndSegment != OldEndSegment && index <= Document.Count - 2) { //if (Row.CanFold) // Row.Expansion_StartSegment.Expanded = true; Document[index + 1].AddToParseQueue(); Document.NeedResetRows = true; } } else if (index <= Document.Count - 2) { //if (Row.CanFold) // Row.Expansion_StartSegment.Expanded = true; Document[index + 1].AddToParseQueue(); Document.NeedResetRows = true; } } if (OldEndSegment != null) { //expand segment if this line dont have an end word if (OldEndSegment.EndWord == null) OldEndSegment.Expanded = true; } }
//om denna är true // så ska INTE nästa rad parse'as , utan denna ska fixas så den blir som den förra... (kopiera segment) private bool IsSameButDifferent(int RowIndex, Segment OldStartSegment) { //is this the last row ? , if so , bailout if (RowIndex >= Document.Count - 1) return false; Row row = Document[RowIndex]; Segment seg = row.EndSegment; Segment OldEndSegment = Document[RowIndex + 1].StartSegment; Segment oseg = OldEndSegment; bool diff = false; while (seg != null) { if (oseg == null) { diff = true; break; } //Id1+=seg.BlockType.GetHashCode ().ToString (); if (seg.BlockType != oseg.BlockType) { diff = true; break; } if (seg.Parent != oseg.Parent) { diff = true; break; } seg = seg.Parent; oseg = oseg.Parent; } if (diff || row.StartSegment != OldStartSegment) return false; return true; }
private ScanResult_Word GetNextWord(string Text, Segment CurrentSegment, int StartPos, ref bool HasComplex) { BlockType block = CurrentSegment.BlockType; #region ComplexFind int BestComplexPos = -1; Pattern BestComplexPattern = null; string BestComplexToken = ""; ScanResult_Word complexword = new ScanResult_Word(); if (HasComplex) { foreach (Pattern pattern in block.ComplexPatterns) { PatternScanResult scanres = pattern.IndexIn(Text, StartPos, pattern.Parent.CaseSensitive, this.Separators); if (scanres.Token != "") { if (scanres.Index < BestComplexPos || BestComplexPos == -1) { BestComplexPos = scanres.Index; BestComplexPattern = pattern; BestComplexToken = scanres.Token; } } } if (BestComplexPattern != null) { complexword.HasContent = true; complexword.ParentList = BestComplexPattern.Parent; complexword.Pattern = BestComplexPattern; complexword.Position = BestComplexPos; complexword.Token = BestComplexToken; HasComplex = true; } else { HasComplex = false; } } #endregion #region SimpleFind ScanResult_Word simpleword = new ScanResult_Word(); for (int i = StartPos; i < Text.Length; i++) { //bailout if we found a complex pattern before this char pos if (i > complexword.Position && complexword.HasContent) break; #region 3+ char pattern if (i <= Text.Length - 3) { string key = Text.Substring(i, 3).ToLower(); PatternCollection patterns2 = block.LookupTable.ContainsKey(key) ? (PatternCollection)block.LookupTable[key]: null; //ok , there are patterns that start with this char if (patterns2 != null) { foreach (Pattern pattern in patterns2) { int len = pattern.StringPattern.Length; if (i + len > Text.Length) continue; char lastpatternchar = char.ToLower(pattern.StringPattern[len - 1]); char lasttextchar = char.ToLower(Text[i + len - 1]); #region Case Insensitive if (lastpatternchar == lasttextchar) { if (!pattern.IsKeyword || (pattern.IsKeyword && pattern.HasSeparators(Text, i))) { if (!pattern.Parent.CaseSensitive) { string s = Text.Substring(i, len).ToLower(); if (s == pattern.StringPattern.ToLower()) { simpleword.HasContent = true; simpleword.ParentList = pattern.Parent; simpleword.Pattern = pattern; simpleword.Position = i; if (pattern.Parent.NormalizeCase) simpleword.Token = pattern.StringPattern; else simpleword.Token = Text.Substring(i, len); break; } } else { string s = Text.Substring(i, len); if (s == pattern.StringPattern) { simpleword.HasContent = true; simpleword.ParentList = pattern.Parent; simpleword.Pattern = pattern; simpleword.Position = i; simpleword.Token = pattern.StringPattern; break; } } } } } #endregion } } #endregion if (simpleword.HasContent) break; #region single char pattern char c = Text[i]; PatternCollection patterns = block.LookupTable.ContainsKey(c) ? (PatternCollection)block.LookupTable[c]:null; if (patterns != null) { //ok , there are patterns that start with this char foreach (Pattern pattern in patterns) { int len = pattern.StringPattern.Length; if (i + len > Text.Length) continue; char lastpatternchar = pattern.StringPattern[len - 1]; char lasttextchar = Text[i + len - 1]; if (!pattern.Parent.CaseSensitive) { #region Case Insensitive if (char.ToLower(lastpatternchar) == char.ToLower(lasttextchar)) { if (!pattern.IsKeyword || (pattern.IsKeyword && pattern.HasSeparators(Text, i))) { string s = Text.Substring(i, len).ToLower(); if (s == pattern.StringPattern.ToLower()) { simpleword.HasContent = true; simpleword.ParentList = pattern.Parent; simpleword.Pattern = pattern; simpleword.Position = i; simpleword.Token = Text.Substring(i, len); break; } } } #endregion } else { #region Case Sensitive if (lastpatternchar == lasttextchar) { if (!pattern.IsKeyword || (pattern.IsKeyword && pattern.HasSeparators(Text, i))) { string s = Text.Substring(i, len); if (s == pattern.StringPattern) { simpleword.HasContent = true; simpleword.ParentList = pattern.Parent; simpleword.Pattern = pattern; simpleword.Position = i; simpleword.Token = pattern.StringPattern; break; } } } #endregion } } if (simpleword.HasContent) break; } #endregion } #endregion if (complexword.HasContent && simpleword.HasContent) { if (simpleword.Position == complexword.Position) { if (simpleword.Token.Length > complexword.Token.Length) return simpleword; else return complexword; } if (simpleword.Position < complexword.Position) return simpleword; if (simpleword.Position > complexword.Position) return complexword; } if (simpleword.HasContent) return simpleword; if (complexword.HasContent) return complexword; return new ScanResult_Word(); }
private ScanResult_Word GetNextComplexWord(String Text, Segment CurrentSegment, int StartPositon) { if (StartPositon >= Text.Length) return new ScanResult_Word(); ScanResult_Word Result = new ScanResult_Word(); int CurrentPosition = 0; //look for keywords PatternListList keywordsList = CurrentSegment.BlockType.KeywordsList; PatternList List = null; for (int i = 0; i < keywordsList.Count; i++) { List = keywordsList[i]; PatternCollection complexPatterns = List.ComplexPatterns; Pattern Word = null; for (int j = 0; j < complexPatterns.Count; j++) { Word = complexPatterns[j]; PatternScanResult psr = Word.IndexIn(Text, StartPositon, false, Separators); CurrentPosition = psr.Index; if ((CurrentPosition < Result.Position || Result.HasContent == false) && psr.Token != "") { Result.HasContent = true; Result.Position = CurrentPosition; Result.Token = psr.Token; Result.Pattern = Word; Result.ParentList = List; if (List.NormalizeCase) if (!Word.IsComplex) Result.Token = Word.StringPattern; } } } //look for operators PatternListList pattList = CurrentSegment.BlockType.OperatorsList; PatternList patternList = null; for (int i = 0; i < pattList.Count; i++) { patternList = pattList[i]; PatternCollection complexPatterns = patternList.ComplexPatterns; for (int j = 0; j < complexPatterns.Count; j++) { Pattern Word = complexPatterns[j]; PatternScanResult psr = Word.IndexIn(Text, StartPositon, false, Separators); CurrentPosition = psr.Index; if ((CurrentPosition < Result.Position || Result.HasContent == false) && psr.Token != "") { Result.HasContent = true; Result.Position = CurrentPosition; Result.Token = psr.Token; Result.Pattern = Word; Result.ParentList = patternList; if (patternList.NormalizeCase) if (!Word.IsComplex) Result.Token = Word.StringPattern; } } } if (Result.HasContent) return Result; else return new ScanResult_Word(); }
private ScanResult_Segment GetNextChildSegment(Row Row, Segment CurrentSegment, int StartPos) { //this row has no text , just bail out... if (StartPos >= Row.Text.Length) return new ScanResult_Segment(); ScanResult_Segment Result = new ScanResult_Segment(); Result.HasContent = false; //make sure we have no content in this object Result.IsEndSegment = false; //nope , were looking for start blocks here BlockType ChildBlock; //foreach (BlockType ChildBlock in CurrentSegment.BlockType.ChildBlocks) BlockTypeCollection blocks = CurrentSegment.BlockType.ChildBlocks; for (int i = 0; i < blocks.Count; i++) { ChildBlock = blocks[i]; //scan each scope in each childblock //foreach (Scope Scope in ChildBlock.ScopePatterns) Scope Scope; ScopeCollection scopeColl = ChildBlock.ScopePatterns; for (int j = 0; j < scopeColl.Count; j++) { Scope = scopeColl[j]; PatternScanResult psr = Scope.Start.IndexIn(Row.Text, StartPos, Scope.CaseSensitive, Separators); int CurrentPosition = psr.Index; if ((!Result.HasContent || CurrentPosition < Result.Position) && psr.Token != "") { //we found a better match //store this new match Result.Pattern = Scope.Start; Result.Position = CurrentPosition; Result.Token = psr.Token; Result.HasContent = true; Result.BlockType = ChildBlock; Result.Scope = Scope; if (Scope.NormalizeCase) if (!Scope.Start.IsComplex) Result.Token = Scope.Start.StringPattern; } } } //no result , new ScanResult_Segment(); if (!Result.HasContent) return new ScanResult_Segment(); return Result; }
private ScanResult_Segment GetEndSegment(Row Row, Segment CurrentSegment, int StartPos) { //this row has no text , just bail out... if (StartPos >= Row.Text.Length || CurrentSegment.Scope == null) return new ScanResult_Segment(); ScanResult_Segment Result = new ScanResult_Segment(); Result.HasContent = false; //make sure we have no content in this object Result.IsEndSegment = false; //nope , were looking for start blocks here //-------------------------------------------------------------------------------- //scan for childblocks //scan each scope in each childblock Segment seg = CurrentSegment; while (seg != null) { if (seg == CurrentSegment || seg.BlockType.TerminateChildren) { foreach (Pattern end in seg.Scope.EndPatterns) { PatternScanResult psr = end.IndexIn(Row.Text, StartPos, seg.Scope.CaseSensitive, Separators); int CurrentPosition = psr.Index; if (psr.Token != "") { if ((psr.Index < Result.Position && Result.HasContent) || !Result.HasContent) { //we found a better match //store this new match Result.Pattern = end; Result.Position = CurrentPosition; Result.Token = psr.Token; Result.HasContent = true; Result.Segment = seg; Result.Scope = null; if (!end.IsComplex) { if (seg.Scope.NormalizeCase) if (!seg.Scope.Start.IsComplex) Result.Token = end.StringPattern; } } } } } seg = seg.Parent; } //no result , return new ScanResult_Segment(); if (!Result.HasContent) return new ScanResult_Segment(); return Result; }
/// <summary> /// 根据段落,获取ns名,也就是函数名 /// </summary> /// <param name="seg"></param> /// <returns>函数名</returns> private string GetNameSpace(Segment seg) { string strNs = ""; if (seg != null) { if (seg.Parent != null) { while (seg.Parent.Scope != null) { seg = seg.Parent; } } strNs = seg.StartRow.Text; if (seg.EndRow != null) { int index = strNs.LastIndexOf('('); if (index == -1) { return ""; } strNs = strNs.Substring(0, strNs.LastIndexOf('(')); index = strNs.LastIndexOf(' '); if (index == -1) { return ""; } strNs = strNs.Substring(index).Trim(); } else { strNs = ""; } } return strNs; }