This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
forked from ocen-lang/ocen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspan.oc
74 lines (61 loc) · 1.89 KB
/
span.oc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//* Represents locations / spans in source code
import std // This is just here to separate docs for modules and structs
//* A location in a source file
struct Location {
filename: str
line: u32
col: u32
index: u32
}
def Location::default(): Location {
let loc: Location
loc.filename = "<default>"
loc.line = 0
loc.col = 0
loc.index = 0
return loc
}
def Location::str(&this): str { return `{.filename}:{.line}:{.col}`}
def Location::is_valid(&this): bool {
return .line > 0 and .col > 0 and .index >= 0
}
//* Checks if the location is before the other location
def Location::is_before(&this, other: Location): bool {
if .line > other.line return false
if .line < other.line return true
return .col <= other.col
}
//* A span in a source file (start and end location)
struct Span {
start: Location
end: Location
}
def Span::str(this): str => `{.start.str()} => {.end.str()}`
def Span::default(): Span {
let span: Span
span.start = Location(filename: "<default>", line: 0, col: 0, index: 0)
span.end = Location(filename: "<default>", line: 0, col: 0, index: 0)
return span
}
def Span::is_valid(this): bool {
return .start.is_valid() and .end.is_valid()
}
// Needs to be called in the correct order!
def Span::join(this, other: Span): Span {
let span: Span
span.start = this.start
span.end = other.end
return span
}
//* Checks if the location is inside the span
def Span::contains_loc(this, loc: Location): bool {
if not .is_valid() return false
if not .start.filename.eq(loc.filename) return false
return .start.is_before(loc) and loc.is_before(.end)
}
//* Checks if the span starts right after the other span
def Span::starts_right_after(this, other: Span): bool {
if not .is_valid() return false
if not .start.filename.eq(other.start.filename) return false
return .start.index == other.end.index
}