fmt: add EOL

pull/4518/head
Alexander Medvednikov 2020-04-20 08:30:42 +02:00
parent 639dc02cc8
commit ee2f1652b5
4 changed files with 22 additions and 17 deletions

View File

@ -54,7 +54,7 @@ pub fn fmt(file ast.File, table &table.Table) string {
// for comment in file.comments { println('$comment.line_nr $comment.text') } // for comment in file.comments { println('$comment.line_nr $comment.text') }
f.imports(f.file.imports) // now that we have all autoimports, handle them f.imports(f.file.imports) // now that we have all autoimports, handle them
res := f.out.str().trim_space() + '\n' res := f.out.str().trim_space() + '\n'
return res[..f.import_pos] + f.out_imports.str() + res[f.import_pos..] return res[..f.import_pos] + f.out_imports.str() + res[f.import_pos..] + '\n'
} }
/* /*
@ -933,3 +933,4 @@ fn (var f Fmt) mark_module_as_used(name string) {
f.used_imports << mod f.used_imports << mod
// println('marking module $mod as used') // println('marking module $mod as used')
} }

View File

@ -18,9 +18,11 @@ const (
'default' 'default'
'do' 'do'
'double' 'double'
'extern', 'float', 'inline', 'int', 'long', 'register', 'restrict', 'short', 'signed' 'extern'
'sizeof' 'float'
'static', 'switch', 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'] 'inline', 'int', 'long', 'register', 'restrict', 'short', 'signed', 'sizeof', 'static'
'switch'
'typedef', 'union', 'unsigned', 'void', 'volatile', 'while']
) )
fn foo(t token.Token) { fn foo(t token.Token) {
@ -2930,3 +2932,4 @@ fn (g Gen) type_to_fmt(typ table.Type) string {
} }
return '%d' return '%d'
} }

View File

@ -72,3 +72,4 @@ fn test_x64() {
exit(1) exit(1)
} }
} }

View File

@ -82,7 +82,7 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
// comments_mode: comments_mode // comments_mode: comments_mode
p.read_first_token() p.read_first_token()
for p.tok.kind == .comment { for p.tok.kind == .comment {
var stmt := ast.Stmt{} // TODO sum type << bug var stmt := ast.Stmt{} // TODO sum type << bug
com := p.comment() com := p.comment()
stmt = com stmt = com
stmts << stmt stmts << stmt
@ -419,9 +419,7 @@ pub fn (var p Parser) stmt() ast.Stmt {
expr := p.expr(0) expr := p.expr(0)
// mut call_expr := &ast.CallExpr(0) // TODO // mut call_expr := &ast.CallExpr(0) // TODO
match expr { match expr {
ast.CallExpr { ast.CallExpr { // call_expr = it }
// call_expr = it
}
else {} else {}
} }
return ast.GoStmt{ return ast.GoStmt{
@ -602,7 +600,8 @@ pub fn (var p Parser) name_expr() ast.Expr {
// type cast. TODO: finish // type cast. TODO: finish
// if name in table.builtin_type_names { // if name in table.builtin_type_names {
if (name in p.table.type_idxs || name_w_mod in p.table.type_idxs) && !(name in ['C.stat', if (name in p.table.type_idxs || name_w_mod in p.table.type_idxs) && !(name in ['C.stat',
'C.sigaction']) { 'C.sigaction'
]) {
// TODO handle C.stat() // TODO handle C.stat()
var to_typ := p.parse_type() var to_typ := p.parse_type()
if p.is_amp { if p.is_amp {
@ -617,7 +616,7 @@ pub fn (var p Parser) name_expr() ast.Expr {
// TODO, string(b, len) // TODO, string(b, len)
if p.tok.kind == .comma && table.type_idx(to_typ) == table.string_type_idx { if p.tok.kind == .comma && table.type_idx(to_typ) == table.string_type_idx {
p.check(.comma) p.check(.comma)
arg = p.expr(0) // len arg = p.expr(0) // len
has_arg = true has_arg = true
} }
p.check(.rpar) p.check(.rpar)
@ -632,12 +631,12 @@ pub fn (var p Parser) name_expr() ast.Expr {
} else { } else {
// fn call // fn call
// println('calling $p.tok.lit') // println('calling $p.tok.lit')
x := p.call_expr(is_c, is_js, mod) // TODO `node,typ :=` should work x := p.call_expr(is_c, is_js, mod) // TODO `node,typ :=` should work
node = x node = x
} }
} else if p.peek_tok.kind == .lcbr && !p.inside_match && !p.inside_match_case && !p.inside_if && } else if p.peek_tok.kind == .lcbr && !p.inside_match && !p.inside_match_case && !p.inside_if &&
!p.inside_for { !p.inside_for {
return p.struct_init(false) // short_syntax: false return p.struct_init(false) // short_syntax: false
} else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var) { } else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var) {
// `Color.green` // `Color.green`
var enum_name := p.check_name() var enum_name := p.check_name()
@ -668,7 +667,7 @@ pub fn (var p Parser) name_expr() ast.Expr {
fn (var p Parser) index_expr(left ast.Expr) ast.IndexExpr { fn (var p Parser) index_expr(left ast.Expr) ast.IndexExpr {
// left == `a` in `a[0]` // left == `a` in `a[0]`
p.next() // [ p.next() // [
var has_low := true var has_low := true
if p.tok.kind == .dotdot { if p.tok.kind == .dotdot {
has_low = false has_low = false
@ -686,7 +685,7 @@ fn (var p Parser) index_expr(left ast.Expr) ast.IndexExpr {
} }
} }
} }
expr := p.expr(0) // `[expr]` or `[expr..]` expr := p.expr(0) // `[expr]` or `[expr..]`
var has_high := false var has_high := false
if p.tok.kind == .dotdot { if p.tok.kind == .dotdot {
// [start..end] or [start..] // [start..end] or [start..]
@ -955,7 +954,7 @@ fn (var p Parser) const_decl() ast.ConstDecl {
if p.tok.kind != .lpar { if p.tok.kind != .lpar {
p.error('consts must be grouped, e.g.\nconst (\n\ta = 1\n)') p.error('consts must be grouped, e.g.\nconst (\n\ta = 1\n)')
} }
p.next() // ( p.next() // (
var fields := []ast.ConstField var fields := []ast.ConstField
for p.tok.kind != .rpar { for p.tok.kind != .rpar {
if p.tok.kind == .comment { if p.tok.kind == .comment {
@ -1116,7 +1115,7 @@ fn (var p Parser) type_decl() ast.TypeDecl {
name := p.check_name() name := p.check_name()
var sum_variants := []table.Type var sum_variants := []table.Type
if p.tok.kind == .assign { if p.tok.kind == .assign {
p.next() // TODO require `=` p.next() // TODO require `=`
} }
if p.tok.kind == .key_fn { if p.tok.kind == .key_fn {
// function type: `type mycallback fn(string, int)` // function type: `type mycallback fn(string, int)`
@ -1129,7 +1128,7 @@ fn (var p Parser) type_decl() ast.TypeDecl {
pos: decl_pos pos: decl_pos
} }
} }
first_type := p.parse_type() // need to parse the first type before we can check if it's `type A = X | Y` first_type := p.parse_type() // need to parse the first type before we can check if it's `type A = X | Y`
if p.tok.kind == .pipe { if p.tok.kind == .pipe {
p.check(.pipe) p.check(.pipe)
sum_variants << first_type sum_variants << first_type
@ -1216,3 +1215,4 @@ fn (p &Parser) new_true_expr() ast.Expr {
fn verror(s string) { fn verror(s string) {
util.verror('parser error', s) util.verror('parser error', s)
} }