diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index daf6015832..fcc16e8352 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -11,9 +11,9 @@ pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl pub type Expr = AnonFn | ArrayInit | AsCast | Assoc | BoolLiteral | CallExpr | CastExpr | CharLiteral | ComptimeCall | ConcatExpr | EnumVal | FloatLiteral | Ident | IfExpr | IfGuardExpr | - IndexExpr | InfixExpr | IntegerLiteral | Likely | LockExpr | MapInit | MatchExpr | None | OrExpr | - ParExpr | PostfixExpr | PrefixExpr | RangeExpr | SelectorExpr | SizeOf | SqlExpr | StringInterLiteral | - StringLiteral | StructInit | Type | TypeOf + IndexExpr | InfixExpr | IntegerLiteral | Likely | LockExpr | MapInit | MatchExpr | None | + OrExpr | ParExpr | PostfixExpr | PrefixExpr | RangeExpr | SelectorExpr | SizeOf | SqlExpr | + StringInterLiteral | StringLiteral | StructInit | Type | TypeOf pub type Stmt = AssertStmt | AssignStmt | Attr | Block | BranchStmt | Comment | CompFor | CompIf | ConstDecl | DeferStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | @@ -586,7 +586,8 @@ pub mut: // e.g. `[unsafe_fn]` pub struct Attr { pub: - name string + name string + is_string bool // `['xxx']` } pub fn (attrs []Attr) contains(attr Attr) bool { @@ -902,7 +903,7 @@ pub fn (expr Expr) position() token.Position { AsCast { return expr.pos } - // ast.Ident { } + // ast.Ident { } CastExpr { return expr.pos } @@ -930,7 +931,7 @@ pub fn (expr Expr) position() token.Position { IfExpr { return expr.pos } - // ast.IfGuardExpr { } + // ast.IfGuardExpr { } IndexExpr { return expr.pos } @@ -961,11 +962,11 @@ pub fn (expr Expr) position() token.Position { PostfixExpr { return expr.pos } - // ast.None { } + // ast.None { } PrefixExpr { return expr.pos } - // ast.ParExpr { } + // ast.ParExpr { } SelectorExpr { return expr.pos } @@ -978,14 +979,14 @@ pub fn (expr Expr) position() token.Position { StringInterLiteral { return expr.pos } - // ast.Type { } + // ast.Type { } StructInit { return expr.pos } Likely { return expr.pos } - // ast.TypeOf { } + // ast.TypeOf { } else { return token.Position{} } @@ -996,29 +997,29 @@ pub fn (stmt Stmt) position() token.Position { match stmt { AssertStmt { return stmt.pos } AssignStmt { return stmt.pos } - /* - // Attr { + /* + // Attr { // } // Block { // } // BranchStmt { // } - */ + */ Comment { return stmt.pos } CompIf { return stmt.pos } ConstDecl { return stmt.pos } - /* - // DeferStmt { + /* + // DeferStmt { // } - */ + */ EnumDecl { return stmt.pos } ExprStmt { return stmt.pos } FnDecl { return stmt.pos } ForCStmt { return stmt.pos } ForInStmt { return stmt.pos } ForStmt { return stmt.pos } - /* - // GlobalDecl { + /* + // GlobalDecl { // } // GoStmt { // } @@ -1028,23 +1029,23 @@ pub fn (stmt Stmt) position() token.Position { // } // HashStmt { // } - */ + */ Import { return stmt.pos } - /* - // InterfaceDecl { + /* + // InterfaceDecl { // } // Module { // } - */ + */ Return { return stmt.pos } StructDecl { return stmt.pos } - /* - // TypeDecl { + /* + // TypeDecl { // } // UnsafeStmt { // } - */ - // + */ + // else { return token.Position{} } } } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 6e54d38703..68163157f1 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -278,7 +278,11 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) { f.writeln('') } ast.Attr { - f.writeln('[$node.name]') + if node.is_string { + f.writeln("['$node.name']") + } else { + f.writeln('[$node.name]') + } } ast.Block { f.writeln('{') @@ -1375,8 +1379,7 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) { pub fn (mut f Fmt) remove_new_line() { 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` { break } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index c0cd23783d..38c2861307 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -656,7 +656,8 @@ fn (mut p Parser) parse_attr() ast.Attr { is_if_attr = true } mut name := '' - if p.tok.kind == .string { + is_string := p.tok.kind == .string + if is_string { name = p.tok.lit p.next() } else { @@ -677,6 +678,7 @@ fn (mut p Parser) parse_attr() ast.Attr { } return ast.Attr{ name: name + is_string: is_string } }