mirror of
https://github.com/odin-lang/Odin.git
synced 2026-07-10 18:09:32 +00:00
Fix parser failing on comments inside ternary expressions
This commit is contained in:
@@ -381,6 +381,9 @@ advance_token :: proc(p: ^Parser) -> tokenizer.Token {
|
||||
#partial switch p.curr_tok.kind {
|
||||
case .Comment:
|
||||
consume_comment_groups(p, prev)
|
||||
if p.curr_tok.kind == .Semicolon && p.expr_level > 0 && p.curr_tok.text == "\n" {
|
||||
advance_token(p)
|
||||
}
|
||||
case .Semicolon:
|
||||
if p.expr_level > 0 && p.curr_tok.text == "\n" {
|
||||
advance_token(p)
|
||||
|
||||
@@ -1529,6 +1529,9 @@ gb_internal Token advance_token(AstFile *f) {
|
||||
switch (f->curr_token.kind) {
|
||||
case Token_Comment:
|
||||
consume_comment_groups(f, prev);
|
||||
if (f->curr_token.kind == Token_Semicolon && ignore_newlines(f) && f->curr_token.string == "\n") {
|
||||
advance_token(f);
|
||||
}
|
||||
break;
|
||||
case Token_Semicolon:
|
||||
if (ignore_newlines(f) && f->curr_token.string == "\n") {
|
||||
|
||||
@@ -131,3 +131,41 @@ my_func :: proc (cond: bool, a: string, b: string) -> string {
|
||||
testing.expect(t, ok, "bad parse")
|
||||
testing.expect(t, file.syntax_error_count == 0, "should contain zero errors")
|
||||
}
|
||||
|
||||
|
||||
@test
|
||||
test_parse_multiline_ternary_with_comment :: proc(t: ^testing.T) {
|
||||
context.allocator = context.temp_allocator
|
||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||
file := ast.File{
|
||||
fullpath = "test.odin",
|
||||
src = `
|
||||
package main
|
||||
|
||||
my_func :: proc (cond: bool, a: string, b: string) -> string {
|
||||
out := (
|
||||
cond
|
||||
? a // This is a comment!
|
||||
: b
|
||||
)
|
||||
return out
|
||||
}
|
||||
`,
|
||||
}
|
||||
|
||||
p := parser.default_parser()
|
||||
|
||||
p.err = proc(pos: tokenizer.Pos, format: string, args: ..any) {
|
||||
message := fmt.tprintf(format, ..args)
|
||||
log.errorf("%s(%d:%d): %s", pos.file, pos.line, pos.column, message)
|
||||
}
|
||||
|
||||
p.warn = proc(pos: tokenizer.Pos, format: string, args: ..any) {
|
||||
message := fmt.tprintf(format, ..args)
|
||||
log.warnf("%s(%d:%d): %s", pos.file, pos.line, pos.column, message)
|
||||
}
|
||||
|
||||
ok := parser.parse_file(&p, &file)
|
||||
testing.expect(t, ok, "bad parse")
|
||||
testing.expect(t, file.syntax_error_count == 0, "should contain zero errors")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user