[svn] r7227: nemerle/trunk/ncc/parsing/AST.n

VladD2 svnadmin at nemerle.org
Mon Jan 8 07:35:03 CET 2007


Log:
Move Location related methods to Location struct.

Author: VladD2
Date: Mon Jan  8 07:35:01 2007
New Revision: 7227

Modified:
   nemerle/trunk/ncc/parsing/AST.n

Modified: nemerle/trunk/ncc/parsing/AST.n
==============================================================================
--- nemerle/trunk/ncc/parsing/AST.n	(original)
+++ nemerle/trunk/ncc/parsing/AST.n	Mon Jan  8 07:35:01 2007
@@ -26,12 +26,14 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+using System.Diagnostics;
 using System.Globalization;
+using System.Math;
+using Nemerle.Assertions;
 using Nemerle.Collections;
 using Nemerle.Utility;
-using System.Math;
+
 using SCG = System.Collections.Generic;
-using System.Diagnostics;
 
 namespace Nemerle.Compiler {
 
@@ -248,6 +250,170 @@
       else
         false
     }
+
+    /// Returns whether the first location is strictly inside the second
+    public StrictlyContains(second : Location) : bool
+    {
+      Contains(second) && this != second
+    }
+
+    /// Returns whether the first location is (not strictly) inside the second
+    public Contains(second : Location) : bool
+    {
+      if (FileIndex == second.FileIndex)
+        Contains(second.Line, second.Column) && Contains(second.EndLine, second.EndColumn)
+      else
+        false
+    }
+
+    /// Returns whether the first location is (not strictly) 
+    /// before the second (the left of first is to the left of second)
+    public StartsBefore(second: Location) : bool
+    {
+      if (Line < second.Line)
+        true
+      else if (Line > second.Line)
+        false
+      else if (Column < second.Column)
+        true
+      else if (Column > second.Column)
+        false
+      else
+        true
+    }
+
+    public Contains(testFileIndex : int, 
+      testLine : int, testCol : int) : bool
+    {
+      if (FileIndex == testFileIndex)
+        Contains(testLine, testCol)
+      else
+        false
+    }
+
+    /// The 'lst' must be ordered. This function test only first and last elements.
+    public static EnclosingLocation(this lst : list[Location]) : Location
+    {
+      Debug.Assert(lst.IsOrdered((x, y) => x.CompareTo(y) > 0));
+      Debug.Assert(!lst.IsEmpty);
+      
+      lst.Head + lst.Last
+    }
+
+    public TrimStart(second : Location) : Location
+    {
+      TrimStart(second, true)
+    }
+
+    public TrimStart(l2 : Location, adjustBegin : bool) : Location
+    {
+      mutable lbeg;
+      mutable cbeg;
+
+      def adj = if (adjustBegin) 1 else 0;
+
+      if      (Line < l2.EndLine) { lbeg = l2.EndLine; cbeg = l2.EndColumn + adj; }
+      else if (Line > l2.EndLine) { lbeg = Line;    cbeg = Column; }
+      else
+      {
+        lbeg = Line;
+        cbeg = if (Column < l2.EndColumn) l2.EndColumn + adj else Column;
+      }
+
+      Location(this, lbeg, cbeg, EndLine, EndColumn)
+    }
+
+    public TrimEnd(l2 : Location) : Location
+    {
+      mutable lend;
+      mutable cend;
+
+      if      (EndLine > l2.Line) { lend = l2.Line;    cend = l2.Column - 1; }
+      else if (EndLine < l2.Line) { lend = EndLine; cend = l2.EndColumn; }
+      else
+      {
+        lend = EndLine;
+        cend = if (EndColumn > l2.Column) l2.Column else EndColumn;
+      }
+
+      Location(this, Line, Column, lend, cend)
+    }
+
+    public Trim(l2 : Location, line : int, col : int) : Location
+    {
+      if (line < l2.Line || line == l2.Line && col < l2.Column)
+        TrimEnd(l2)
+      else if (line > l2.EndLine || line == l2.EndLine && col > l2.EndColumn)
+        TrimStart(l2, true)
+      else
+        this
+    }
+
+    public Combine(l2 : Location) : Location
+    {
+      mutable lbeg;
+      mutable cbeg;
+
+      if      (Line < l2.Line) { lbeg = Line; cbeg = Column; }
+      else if (Line > l2.Line) { lbeg = l2.Line; cbeg = l2.Column; }
+      else
+      {
+        lbeg = Line;
+        cbeg = if (Column < l2.Column) Column else l2.Column;
+      }
+
+      mutable lend;
+      mutable cend;
+
+      if      (EndLine > l2.EndLine) { lend = EndLine; cend = EndColumn; }
+      else if (EndLine < l2.EndLine) { lend = l2.EndLine; cend = l2.EndColumn; }
+      else
+      {
+        lend = EndLine;
+        cend = if (EndColumn > l2.EndColumn) EndColumn else l2.EndColumn;
+      }
+
+      Location(this, lbeg, cbeg, lend, cend)
+    }
+
+    public Intersect(l2 : Location) : Location
+      requires FileIndex == l2.FileIndex || FileIndex == 0 || l2.FileIndex == 0
+    {
+      mutable lbeg;
+      mutable cbeg;
+
+      if      (Line < l2.Line) { lbeg = l2.Line; cbeg = l2.Column; }
+      else if (Line > l2.Line) { lbeg = Line;    cbeg = Column; }
+      else
+      {
+        lbeg = Line;
+        cbeg = if (Column < l2.Column) l2.Column else Column;
+      }
+
+      mutable lend;
+      mutable cend;
+
+      if      (EndLine > l2.EndLine) { lend = l2.EndLine; cend = l2.EndColumn; }
+      else if (EndLine < l2.EndLine) { lend = EndLine;    cend = EndColumn; }
+      else
+      {
+        lend = EndLine;
+        cend = if (EndColumn > l2.EndColumn) l2.EndColumn else EndColumn;
+      }
+
+      Location(this, lbeg, cbeg, lend, cend)
+    }
+
+    public IsEqualExcludingFile(l2 : Location) : bool
+    {
+      Line   == l2.Line   && EndLine   == l2.EndLine &&
+      Column == l2.Column && EndColumn == l2.EndColumn
+    }
+
+    public IsEmpty() : bool
+    {
+      EndLine == 0 || EndLine < Line || (EndLine == Line && EndColumn <= Column)
+    }
   }
 
   public class Located



More information about the svn mailing list