vfmt: handle comments in enum Abc{} and after match branches
							parent
							
								
									6ec86fa344
								
							
						
					
					
						commit
						132170f54c
					
				|  | @ -462,6 +462,7 @@ pub: | |||
| 	pos     token.Position | ||||
| 	comment Comment // comment above `xxx {`
 | ||||
| 	is_else bool | ||||
| 	post_comments []Comment | ||||
| } | ||||
| 
 | ||||
| /* | ||||
|  | @ -586,6 +587,7 @@ pub struct EnumField { | |||
| pub: | ||||
| 	name     string | ||||
| 	pos      token.Position | ||||
| 	comments []Comment | ||||
| 	expr     Expr | ||||
| 	has_expr bool | ||||
| } | ||||
|  | @ -595,6 +597,7 @@ pub: | |||
| 	name    string | ||||
| 	is_pub  bool | ||||
| 	is_flag bool // true when the enum has [flag] tag
 | ||||
| 	comments []Comment // enum Abc { /* comments */ ... }
 | ||||
| 	fields  []EnumField | ||||
| 	pos     token.Position | ||||
| } | ||||
|  |  | |||
|  | @ -315,12 +315,14 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) { | |||
| 			} | ||||
| 			name := it.name.after('.') | ||||
| 			f.writeln('enum $name {') | ||||
| 			f.comments(it.comments) | ||||
| 			for field in it.fields { | ||||
| 				f.write('\t$field.name') | ||||
| 				if field.has_expr { | ||||
| 					f.write(' = ') | ||||
| 					f.expr(field.expr) | ||||
| 				} | ||||
| 				f.comments(it.comments) | ||||
| 				f.writeln('') | ||||
| 			} | ||||
| 			f.writeln('}\n') | ||||
|  | @ -1098,6 +1100,15 @@ pub fn (mut f Fmt) comment(node ast.Comment) { | |||
| 	f.writeln('*/') | ||||
| } | ||||
| 
 | ||||
| pub fn (mut f Fmt) comments(some_comments []ast.Comment) { | ||||
| 	for c in some_comments { | ||||
| 		if !f.out.last_n(1)[0].is_space() { | ||||
| 			f.write('\t') | ||||
| 		} | ||||
| 		f.comment(c) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| pub fn (mut f Fmt) fn_decl(node ast.FnDecl) { | ||||
| 	// println('$it.name find_comment($it.pos.line_nr)')
 | ||||
| 	// f.find_comment(it.pos.line_nr)
 | ||||
|  | @ -1317,6 +1328,7 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) { | |||
| 				f.writeln('}') | ||||
| 			} | ||||
| 		} | ||||
| 		f.comments(branch.post_comments) | ||||
| 	} | ||||
| 	f.indent-- | ||||
| 	f.write('}') | ||||
|  |  | |||
|  | @ -180,6 +180,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr { | |||
| 		p.inside_match_body = true | ||||
| 		stmts := p.parse_block() | ||||
| 		p.inside_match_body = false | ||||
| 		post_comments := p.eat_comments() | ||||
| 		pos := token.Position{ | ||||
| 			line_nr: branch_first_pos.line_nr | ||||
| 			pos: branch_first_pos.pos | ||||
|  | @ -191,6 +192,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr { | |||
| 			pos: pos | ||||
| 			comment: comment | ||||
| 			is_else: is_else | ||||
| 			post_comments: post_comments | ||||
| 		} | ||||
| 		p.close_scope() | ||||
| 		if p.tok.kind == .rcbr { | ||||
|  |  | |||
|  | @ -474,6 +474,17 @@ pub fn (mut p Parser) comment() ast.Comment { | |||
| 	} | ||||
| } | ||||
| 
 | ||||
| pub fn (mut p Parser) eat_comments() []ast.Comment { | ||||
| 	mut comments := []ast.Comment{} | ||||
| 	for { | ||||
| 		if p.tok.kind != .comment { | ||||
| 			break | ||||
| 		} | ||||
| 		comments << p.check_comment() | ||||
| 	} | ||||
| 	return comments | ||||
| } | ||||
| 
 | ||||
| pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt { | ||||
| 	p.is_stmt_ident = p.tok.kind == .name | ||||
| 	match p.tok.kind { | ||||
|  | @ -1441,6 +1452,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl { | |||
| 	enum_name := p.check_name() | ||||
| 	name := p.prepend_mod(enum_name) | ||||
| 	p.check(.lcbr) | ||||
| 	enum_decl_comments := p.eat_comments() | ||||
| 	mut vals := []string{} | ||||
| 	// mut default_exprs := []ast.Expr{}
 | ||||
| 	mut fields := []ast.EnumField{} | ||||
|  | @ -1461,6 +1473,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl { | |||
| 			pos: pos | ||||
| 			expr: expr | ||||
| 			has_expr: has_expr | ||||
| 			comments: p.eat_comments() | ||||
| 		} | ||||
| 	} | ||||
| 	p.top_level_statement_end() | ||||
|  | @ -1495,6 +1508,7 @@ $pubfn (mut e  $enum_name) toggle(flag $enum_name)   { unsafe{ *e = int(*e) ^  ( | |||
| 		is_flag: is_flag | ||||
| 		fields: fields | ||||
| 		pos: start_pos.extend(end_pos) | ||||
| 		comments: enum_decl_comments | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -13,6 +13,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { | |||
| 	mut node := ast.Expr{} | ||||
| 	is_stmt_ident := p.is_stmt_ident | ||||
| 	p.is_stmt_ident = false | ||||
| 	p.eat_comments() | ||||
| 	// Prefix
 | ||||
| 	match p.tok.kind { | ||||
| 		.key_mut, .key_static { | ||||
|  | @ -108,7 +109,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { | |||
| 			} else { | ||||
| 				sizeof_type := p.parse_type() | ||||
| 				node = ast.SizeOf{ | ||||
| 					is_type: true                 | ||||
| 					is_type: true | ||||
| 					typ: sizeof_type | ||||
| 					type_name: p.table.get_type_symbol(sizeof_type).name | ||||
| 					pos: pos | ||||
|  | @ -165,9 +166,6 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { | |||
| 			return node | ||||
| 		} | ||||
| 		else { | ||||
| 			if p.tok.kind == .comment { | ||||
| 				println(p.tok.lit) | ||||
| 			} | ||||
| 			p.error('expr(): bad token `$p.tok.kind.str()`') | ||||
| 		} | ||||
| 	} | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue