1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vfmt: handle string attrs

This commit is contained in:
Alexander Medvednikov 2020-07-04 23:38:12 +02:00
parent 7778cbe9f5
commit 188bad4f38
3 changed files with 36 additions and 30 deletions

View File

@ -11,9 +11,9 @@ pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl
pub type Expr = AnonFn | ArrayInit | AsCast | Assoc | BoolLiteral | CallExpr | CastExpr | pub type Expr = AnonFn | ArrayInit | AsCast | Assoc | BoolLiteral | CallExpr | CastExpr |
CharLiteral | ComptimeCall | ConcatExpr | EnumVal | FloatLiteral | Ident | IfExpr | IfGuardExpr | CharLiteral | ComptimeCall | ConcatExpr | EnumVal | FloatLiteral | Ident | IfExpr | IfGuardExpr |
IndexExpr | InfixExpr | IntegerLiteral | Likely | LockExpr | MapInit | MatchExpr | None | OrExpr | IndexExpr | InfixExpr | IntegerLiteral | Likely | LockExpr | MapInit | MatchExpr | None |
ParExpr | PostfixExpr | PrefixExpr | RangeExpr | SelectorExpr | SizeOf | SqlExpr | StringInterLiteral | OrExpr | ParExpr | PostfixExpr | PrefixExpr | RangeExpr | SelectorExpr | SizeOf | SqlExpr |
StringLiteral | StructInit | Type | TypeOf StringInterLiteral | StringLiteral | StructInit | Type | TypeOf
pub type Stmt = AssertStmt | AssignStmt | Attr | Block | BranchStmt | Comment | CompFor | pub type Stmt = AssertStmt | AssignStmt | Attr | Block | BranchStmt | Comment | CompFor |
CompIf | ConstDecl | DeferStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | DeferStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt |
@ -586,7 +586,8 @@ pub mut:
// e.g. `[unsafe_fn]` // e.g. `[unsafe_fn]`
pub struct Attr { pub struct Attr {
pub: pub:
name string name string
is_string bool // `['xxx']`
} }
pub fn (attrs []Attr) contains(attr Attr) bool { pub fn (attrs []Attr) contains(attr Attr) bool {
@ -902,7 +903,7 @@ pub fn (expr Expr) position() token.Position {
AsCast { AsCast {
return expr.pos return expr.pos
} }
// ast.Ident { } // ast.Ident { }
CastExpr { CastExpr {
return expr.pos return expr.pos
} }
@ -930,7 +931,7 @@ pub fn (expr Expr) position() token.Position {
IfExpr { IfExpr {
return expr.pos return expr.pos
} }
// ast.IfGuardExpr { } // ast.IfGuardExpr { }
IndexExpr { IndexExpr {
return expr.pos return expr.pos
} }
@ -961,11 +962,11 @@ pub fn (expr Expr) position() token.Position {
PostfixExpr { PostfixExpr {
return expr.pos return expr.pos
} }
// ast.None { } // ast.None { }
PrefixExpr { PrefixExpr {
return expr.pos return expr.pos
} }
// ast.ParExpr { } // ast.ParExpr { }
SelectorExpr { SelectorExpr {
return expr.pos return expr.pos
} }
@ -978,14 +979,14 @@ pub fn (expr Expr) position() token.Position {
StringInterLiteral { StringInterLiteral {
return expr.pos return expr.pos
} }
// ast.Type { } // ast.Type { }
StructInit { StructInit {
return expr.pos return expr.pos
} }
Likely { Likely {
return expr.pos return expr.pos
} }
// ast.TypeOf { } // ast.TypeOf { }
else { else {
return token.Position{} return token.Position{}
} }
@ -996,29 +997,29 @@ pub fn (stmt Stmt) position() token.Position {
match stmt { match stmt {
AssertStmt { return stmt.pos } AssertStmt { return stmt.pos }
AssignStmt { return stmt.pos } AssignStmt { return stmt.pos }
/* /*
// Attr { // Attr {
// } // }
// Block { // Block {
// } // }
// BranchStmt { // BranchStmt {
// } // }
*/ */
Comment { return stmt.pos } Comment { return stmt.pos }
CompIf { return stmt.pos } CompIf { return stmt.pos }
ConstDecl { return stmt.pos } ConstDecl { return stmt.pos }
/* /*
// DeferStmt { // DeferStmt {
// } // }
*/ */
EnumDecl { return stmt.pos } EnumDecl { return stmt.pos }
ExprStmt { return stmt.pos } ExprStmt { return stmt.pos }
FnDecl { return stmt.pos } FnDecl { return stmt.pos }
ForCStmt { return stmt.pos } ForCStmt { return stmt.pos }
ForInStmt { return stmt.pos } ForInStmt { return stmt.pos }
ForStmt { return stmt.pos } ForStmt { return stmt.pos }
/* /*
// GlobalDecl { // GlobalDecl {
// } // }
// GoStmt { // GoStmt {
// } // }
@ -1028,23 +1029,23 @@ pub fn (stmt Stmt) position() token.Position {
// } // }
// HashStmt { // HashStmt {
// } // }
*/ */
Import { return stmt.pos } Import { return stmt.pos }
/* /*
// InterfaceDecl { // InterfaceDecl {
// } // }
// Module { // Module {
// } // }
*/ */
Return { return stmt.pos } Return { return stmt.pos }
StructDecl { return stmt.pos } StructDecl { return stmt.pos }
/* /*
// TypeDecl { // TypeDecl {
// } // }
// UnsafeStmt { // UnsafeStmt {
// } // }
*/ */
// //
else { return token.Position{} } else { return token.Position{} }
} }
} }

View File

@ -278,7 +278,11 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
f.writeln('') f.writeln('')
} }
ast.Attr { ast.Attr {
f.writeln('[$node.name]') if node.is_string {
f.writeln("['$node.name']")
} else {
f.writeln('[$node.name]')
}
} }
ast.Block { ast.Block {
f.writeln('{') f.writeln('{')
@ -1375,8 +1379,7 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
pub fn (mut f Fmt) remove_new_line() { pub fn (mut f Fmt) remove_new_line() {
mut i := 0 mut i := 0
for i = f.out.len - 1; i >= 0; i-- for i = f.out.len - 1; i >= 0; i-- {
{
if !f.out.buf[i].is_space() { // != `\n` { if !f.out.buf[i].is_space() { // != `\n` {
break break
} }

View File

@ -656,7 +656,8 @@ fn (mut p Parser) parse_attr() ast.Attr {
is_if_attr = true is_if_attr = true
} }
mut name := '' mut name := ''
if p.tok.kind == .string { is_string := p.tok.kind == .string
if is_string {
name = p.tok.lit name = p.tok.lit
p.next() p.next()
} else { } else {
@ -677,6 +678,7 @@ fn (mut p Parser) parse_attr() ast.Attr {
} }
return ast.Attr{ return ast.Attr{
name: name name: name
is_string: is_string
} }
} }