Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma indent
- using Nemerle;
- using Nemerle.Collections;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using NLocation = Nemerle.Compiler.Location;
- using Nemerle.Peg;
- using Nemerle.Compiler;
- namespace YobaBasic.Macro
- public abstract class LocatedParser
- _location : NLocation
- _lineOffsetMap : array[int]
- public this(location : NLocation, text : string)
- _location = location
- def fillLineOffsetMap()
- def map = List(text.Length / 10)
- map.Add(0)
- for (mutable i = 0; i < text.Length; i++)
- if (text[i] == '\n')
- map.Add(i + 1);
- else
- when (text[i] == '\r')
- def next = i + 1
- when (next < text.Length && text[next] != '\n')
- map.Add(i + 1)
- map.ToArray()
- _lineOffsetMap = fillLineOffsetMap()
- public ToLocation(startPos : int, endPos : int) : NLocation
- def getTextPoint(pos : int) : TextPoint
- def result = _lineOffsetMap.BinarySearch(e => e - pos)
- def index = if (result < 0) (~result) - 1 else result
- def offset = _lineOffsetMap[index]
- def ch = pos - offset
- TextPoint(index + 1, ch + 1)
- NLocation(_location.FileIndex,
- _location.Begin.Offcet <| getTextPoint <| startPos,
- _location.Begin.Offcet <| getTextPoint <| endPos)
- public ToLocation(tok : NToken) : NLocation
- ToLocation(tok.StartPos, tok.EndPos)
Advertisement
Add Comment
Please, Sign In to add comment