Skip to content

Commit

Permalink
read module name from filename
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 2, 2024
1 parent 8b3135e commit 979cbbf
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Local files
rivetc
main.ri
main.ri
tests/gen_out_files
tests/run_tests
9 changes: 6 additions & 3 deletions compiler/ast/File.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct File {
pub:
filename string
content string
mod_name string
pub mut:
errors int
mut:
Expand Down Expand Up @@ -51,18 +52,20 @@ pub:
col int
}

pub fn File.new(file string) &File {
content := read_file(file)
pub fn File.new(filename string) &File {
content := read_file(filename)
return &File{
filename: file
filename: filename
content: content
mod_name: os.base(filename).all_before('.')
}
}

pub fn File.from_memory(content string) &File {
return &File{
filename: '<memory>'
content: content
mod_name: '<memory>'
}
}

Expand Down
5 changes: 3 additions & 2 deletions compiler/context/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub mut:
options Options
report Report

files []&ast.File
root_file &ast.File = unsafe { nil }
files []&ast.File
}

pub fn (ctx &CContext) abort_if_errors() {
Expand All @@ -46,6 +47,6 @@ pub fn (ctx &CContext) abort_if_errors() {
} else {
'aborting due to ${ctx.report.errors} previous errors'
}
ic_error('could not compile source code, ${reason}')
ic_error('could not compile `${ctx.root_file.mod_name}` module, ${reason}')
}
}
7 changes: 5 additions & 2 deletions compiler/parser/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ pub fn new(ctx &context.CContext) &Parser {
}

pub fn (mut p Parser) parse() {
p.parse_file(p.ctx.options.input)
p.parse_file(p.ctx.options.input, true)
}

fn (mut p Parser) parse_file(filename string) {
fn (mut p Parser) parse_file(filename string, is_root bool) {
p.file = ast.File.new(filename)
if is_root {
p.ctx.root_file = p.file
}
p.ctx.files << p.file

p.tokenizer = tokenizer.from_file(p.ctx, p.file)
Expand Down
2 changes: 1 addition & 1 deletion tests/tokenizer/invalid_character.err.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ tests/tokenizer/invalid_character.err.ri:1:1: error: invalid character `¿`
|
1 | ¿
| ^
error: could not compile source code, aborting due to previous error
error: could not compile `invalid_character` module, aborting due to previous error
2 changes: 1 addition & 1 deletion tests/tokenizer/invalid_character_literal.err.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ tests/tokenizer/invalid_character_literal.err.ri:2:5: error: character literal m
2 | 'abc'
| ^
= help: if you meant to write a string literal, use double quotes
error: could not compile source code, aborting due to 2 previous errors
error: could not compile `invalid_character_literal` module, aborting due to 2 previous errors
2 changes: 1 addition & 1 deletion tests/tokenizer/invalid_number_literal.err.out
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ tests/tokenizer/invalid_number_literal.err.ri:22:4: error: cannot use `_` at the
|
22 | 123_ // fail
| ^
error: could not compile source code, aborting due to 15 previous errors
error: could not compile `invalid_number_literal` module, aborting due to 15 previous errors
2 changes: 1 addition & 1 deletion tests/tokenizer/invalid_string_literal.err.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ tests/tokenizer/invalid_string_literal.err.ri:1:1: error: unfinished string lite
|
1 | "unfinished string literal
| ^
error: could not compile source code, aborting due to previous error
error: could not compile `invalid_string_literal` module, aborting due to previous error
2 changes: 1 addition & 1 deletion tests/tokenizer/unterminated_multiline_comment.err.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ tests/tokenizer/unterminated_multiline_comment.err.ri:2:1: error: unterminated m
|
2 | unterminated multiline comment :(
| ^
error: could not compile source code, aborting due to previous error
error: could not compile `unterminated_multiline_comment` module, aborting due to previous error

0 comments on commit 979cbbf

Please sign in to comment.