Skip to content

Commit

Permalink
ast: split File.v -> FilePos.v
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 2, 2024
1 parent 979cbbf commit 70e3883
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
34 changes: 0 additions & 34 deletions compiler/ast/File.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,6 @@ mut:
lines ?[]string
}

pub struct FilePos {
pub mut:
file &File = unsafe { nil }
begin FileLoc
end FileLoc
}

pub fn (fp &FilePos) contains(loc &FileLoc) bool {
if loc.line > fp.begin.line && loc.line < fp.end.line {
return true
} else if loc.line == fp.begin.line && loc.line == fp.end.line {
return loc.col >= fp.begin.col && loc.col <= fp.end.col
} else if loc.line == fp.begin.line {
return loc.col >= fp.begin.col
} else if loc.line == fp.end.line {
return loc.col <= fp.end.col
}
return false
}

pub fn (fp &FilePos) str() string {
if fp.begin.line == fp.end.line {
return '${fp.file.filename}:${fp.begin.line + 1}:${fp.begin.col}'
}
return '${fp.file.filename}:${fp.begin.line + 1}:${fp.begin.col}-${fp.end.line + 1}:${fp.end.col}'
}

pub struct FileLoc {
pub:
pos int
line int
col int
}

pub fn File.new(filename string) &File {
content := read_file(filename)
return &File{
Expand Down
39 changes: 39 additions & 0 deletions compiler/ast/FilePos.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) 2024-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

module ast

pub struct FilePos {
pub mut:
file &File = unsafe { nil }
begin FileLoc
end FileLoc
}

pub fn (fp &FilePos) contains(loc &FileLoc) bool {
if loc.line > fp.begin.line && loc.line < fp.end.line {
return true
} else if loc.line == fp.begin.line && loc.line == fp.end.line {
return loc.col >= fp.begin.col && loc.col <= fp.end.col
} else if loc.line == fp.begin.line {
return loc.col >= fp.begin.col
} else if loc.line == fp.end.line {
return loc.col <= fp.end.col
}
return false
}

pub fn (fp &FilePos) str() string {
if fp.begin.line == fp.end.line {
return '${fp.file.filename}:${fp.begin.line + 1}:${fp.begin.col}'
}
return '${fp.file.filename}:${fp.begin.line + 1}:${fp.begin.col}-${fp.end.line + 1}:${fp.end.col}'
}

pub struct FileLoc {
pub:
pos int
line int
col int
}

0 comments on commit 70e3883

Please sign in to comment.