public Position TranslatePosition(Position pos)
        {
            try
            {
                if (pos.Equals(Position.Unknown))
                    return Position.Unknown;

                if (dirty)
                {
                    _mappings.Sort();
                    dirty = false;
                }

                if (_mappings.Count > 0)
                {
                    int idx = _mappings.BinarySearch(new PositionMapping(pos, Position.Unknown));
                    if (idx >= 0)
                    {
                        return ((PositionMapping) _mappings[idx]).Value;
                    }
                    else
                    {
                        int closestIndex = (~idx) - 1;
                        PositionMapping matchedMapping = (PositionMapping) _mappings[closestIndex];
                        if (!matchedMapping.Value.Equals(Position.Unknown))
                        {
                            if (matchedMapping.Key.Line == pos.Line)
                            {
                                return new Position(
                                    matchedMapping.Value.Line,
                                    matchedMapping.Value.Column + (pos.Column - matchedMapping.Key.Column));
                            }
                            else
                            {
                                return new Position(
                                    matchedMapping.Value.Line + (pos.Line - matchedMapping.Key.Line),
                                    pos.Column);
                            }
                        }
                    }
                }

                return Position.Unknown;
            }
            catch (Exception e)
            {
                Trace.Fail(e.ToString());
                return Position.Unknown;
            }
        }
 public abstract void Literal(string literal, Position startPos);
 public abstract void Expression(string expr, Position startPos);
 public abstract void Code(string code, Position startPos);
 public PositionMapping(Position key, Position value)
 {
     Key = key;
     Value = value;
 }
 public void AddMapping(Position from, Position to)
 {
     dirty = true;
     _mappings.Add(new PositionMapping(from, to));
 }
 public override void Literal(string literal, Position startPos)
 {
     _value.Append("___output.Append(@\"");
     foreach (char c in literal)
     {
         switch (c)
         {
             case '"':
                 _value.Append("\"\"");
                 break;
             default:
                 _value.Append(c.ToString());
                 break;
         }
     }
     _value.Append("\");");
 }
        public override void Expression(string expr, Position startPos)
        {
            _value.Append("___output.Append(");

            _sourcePositionTransposer.AddMapping(
                _value.Position,
                startPos);
            _value.Append(expr);
            /*
            _sourcePositionTransposer.AddMapping(
                new Position(_value.Line, _value.Column + 1),
                Position.Unknown);
             */

            _value.Append(");");
        }
 public override void Code(string code, Position startPos)
 {
     _sourcePositionTransposer.AddMapping(
         _value.Position,
         startPos);
     _value.Append(code);
     /*
     _sourcePositionTransposer.AddMapping(
         new Position(_value.Line, _value.Column),
         Position.Unknown);
      */
 }
 internal TemplateCompilationException(string message, Position position) : base(message)
 {
     _message = message;
     _position = position;
 }