vfmt: handle string attrs

pull/5674/head
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 |
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 |
@ -587,6 +587,7 @@ pub mut:
pub struct Attr {
pub:
name string
is_string bool // `['xxx']`
}
pub fn (attrs []Attr) contains(attr Attr) bool {

View File

@ -278,8 +278,12 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
f.writeln('')
}
ast.Attr {
if node.is_string {
f.writeln("['$node.name']")
} else {
f.writeln('[$node.name]')
}
}
ast.Block {
f.writeln('{')
f.stmts(node.stmts)
@ -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
}

View File

@ -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
}
}