| 1 |
structure SourceMapping :> SOURCE_MAPPING = |
| 2 |
struct |
| 3 |
|
| 4 |
type charpos = int |
| 5 |
|
| 6 |
type region = charpos * charpos |
| 7 |
|
| 8 |
datatype location = LOC of {srcFile : UniqueSymbol.symbol, |
| 9 |
beginLine : int, |
| 10 |
beginCol : int, |
| 11 |
endLine : int, |
| 12 |
endCol : int |
| 13 |
} |
| 14 |
datatype state = STATE of {lineNum : int, |
| 15 |
file : UniqueSymbol.symbol, |
| 16 |
charPos : charpos |
| 17 |
} |
| 18 |
|
| 19 |
datatype sourcemap = SOURCEMAP of |
| 20 |
{ linePos : charpos list ref, |
| 21 |
filePos : {linePos:charpos list, |
| 22 |
line :int, |
| 23 |
srcFile:UniqueSymbol.symbol} list ref, |
| 24 |
lineNum : int ref |
| 25 |
} |
| 26 |
|
| 27 |
val dummyLoc = LOC{srcFile=UniqueSymbol.fromString "???", |
| 28 |
beginLine=1,beginCol=1, |
| 29 |
endLine=1,endCol=1} |
| 30 |
fun newmap{srcFile} = SOURCEMAP |
| 31 |
{ linePos = ref [0], |
| 32 |
filePos = ref [{linePos=[],line=1, |
| 33 |
srcFile=UniqueSymbol.fromString srcFile}], |
| 34 |
lineNum = ref 1 |
| 35 |
} |
| 36 |
|
| 37 |
fun newline (SOURCEMAP{linePos,lineNum,...}) pos = |
| 38 |
(linePos := pos :: !linePos; lineNum := 1 + !lineNum) |
| 39 |
|
| 40 |
fun state(SOURCEMAP{linePos,lineNum,filePos, ...}) = |
| 41 |
let val {srcFile, ...} = hd(! filePos) |
| 42 |
val charPos = hd(! linePos) |
| 43 |
val lineNum = ! lineNum |
| 44 |
in STATE{file=srcFile, charPos=charPos, lineNum=lineNum} end |
| 45 |
|
| 46 |
fun resynch (SOURCEMAP{linePos,filePos,lineNum,...}) {pos,srcFile,line} = |
| 47 |
(filePos := {linePos= !linePos, |
| 48 |
line= !lineNum, |
| 49 |
srcFile=UniqueSymbol.fromString srcFile |
| 50 |
} :: !filePos; |
| 51 |
linePos := [pos]; |
| 52 |
lineNum := line |
| 53 |
) |
| 54 |
fun reset srcMap (STATE{file, lineNum, charPos}) = |
| 55 |
(print(UniqueSymbol.toString file^" "^Int.toString lineNum^"\n"); |
| 56 |
resynch srcMap {pos=charPos, |
| 57 |
srcFile=UniqueSymbol.toString file, line=lineNum} |
| 58 |
) |
| 59 |
|
| 60 |
fun parseDirective sourceMap (pos,directive) = |
| 61 |
let fun sep #" " = true |
| 62 |
| sep #"\"" = true |
| 63 |
| sep #"#" = true |
| 64 |
| sep #"\n" = true |
| 65 |
| sep _ = false |
| 66 |
in case String.tokens sep directive of |
| 67 |
line::srcFile::_ => |
| 68 |
(case Int.fromString line of |
| 69 |
SOME line => |
| 70 |
resynch sourceMap {pos=pos,srcFile=srcFile,line=line} |
| 71 |
| _ => newline sourceMap pos |
| 72 |
) |
| 73 |
| _ => newline sourceMap pos |
| 74 |
end |
| 75 |
|
| 76 |
fun currPos(SOURCEMAP{linePos,...}) = hd (!linePos) |
| 77 |
|
| 78 |
fun location(SOURCEMAP{linePos,filePos,lineNum,...}) (x,y) = |
| 79 |
let fun findPos(p,currPos,currFile,pos::rest,filePos,line) = |
| 80 |
if p > pos then |
| 81 |
{srcFile=currFile,line=line,column=p - pos} |
| 82 |
else findPos(p,pos,currFile,rest,filePos,line-1) |
| 83 |
| findPos(p,currPos,currFile,[],{linePos,line,srcFile}::filePos,_) = |
| 84 |
findPos(p,currPos,srcFile,linePos,filePos,line) |
| 85 |
| findPos(p,currPos,currFile,[],[],line) = |
| 86 |
{srcFile=currFile,line=line,column=0} |
| 87 |
|
| 88 |
val {srcFile=currFile,...} = hd(!filePos) |
| 89 |
val {srcFile,line=l1,column=c1} = |
| 90 |
findPos(x,x,currFile,!linePos,!filePos,!lineNum) |
| 91 |
val {srcFile,line=l2,column=c2} = |
| 92 |
findPos(y,y,currFile,!linePos,!filePos,!lineNum) |
| 93 |
in LOC{srcFile = srcFile, |
| 94 |
beginLine = l1, |
| 95 |
beginCol = c1, |
| 96 |
endLine = l2, |
| 97 |
endCol = c2 |
| 98 |
} |
| 99 |
end |
| 100 |
|
| 101 |
fun toString(LOC{srcFile,beginLine,beginCol,endLine,endCol}) = |
| 102 |
let val int = Int.toString |
| 103 |
in UniqueSymbol.toString srcFile^":"^int beginLine^"."^int beginCol^ |
| 104 |
(if beginLine = endLine andalso beginCol = endCol then "" |
| 105 |
else "-"^int endLine^"."^int endCol) |
| 106 |
end |
| 107 |
|
| 108 |
fun directive(LOC{srcFile,beginLine,beginCol,endLine,endCol}) = |
| 109 |
let val int = Int.toString |
| 110 |
in "(*#line "^int beginLine^"."^int beginCol^" \""^ |
| 111 |
UniqueSymbol.toString srcFile^"\"*)" |
| 112 |
end |
| 113 |
|
| 114 |
end |
| 115 |
|