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

@ for escaping keywords

This commit is contained in:
Alexander Medvednikov 2019-07-24 02:22:34 +02:00
parent facf55cbc0
commit c28a490c17
4 changed files with 21 additions and 6 deletions

View File

@ -1335,7 +1335,7 @@ fn (p mut Parser) bterm() string {
return typ return typ
} }
// also called on *, &, . (enum) // also called on *, &, @, . (enum)
fn (p mut Parser) name_expr() string { fn (p mut Parser) name_expr() string {
p.log('\nname expr() pass=$p.run tok=${p.tok.str()} $p.lit') p.log('\nname expr() pass=$p.run tok=${p.tok.str()} $p.lit')
// print('known type:') // print('known type:')
@ -2167,11 +2167,8 @@ fn (p mut Parser) factor() string {
p.gen('$sizeof_typ)') p.gen('$sizeof_typ)')
p.fgen('$sizeof_typ)') p.fgen('$sizeof_typ)')
return 'int' return 'int'
case Token.amp: case Token.amp, Token.dot, Token.mul:
return p.name_expr() // (dot is for enum vals: `.green`)
case Token.dot:
return p.name_expr()// `.green` (enum)
case Token.mul:
return p.name_expr() return p.name_expr()
case Token.name: case Token.name:
// map[string]int // map[string]int

View File

@ -388,6 +388,13 @@ fn (s mut Scanner) scan() ScanRes {
return scan_res(.pipe, '') return scan_res(.pipe, '')
case `,`: case `,`:
return scan_res(.comma, '') return scan_res(.comma, '')
case `@`:
s.pos++
name := s.ident_name()
if !is_key(name) {
s.error('@ must be used before keywords (e.g. `@type string`)')
}
return scan_res(.name, name)
case `\r`: case `\r`:
if nextc == `\n` { if nextc == `\n` {
s.pos++ s.pos++

View File

@ -22,6 +22,10 @@ struct User {
age int age int
} }
struct Foo {
@type string
}
fn test_struct_levels() { fn test_struct_levels() {
mut c := C{} mut c := C{}
assert c.nums.len == 0 assert c.nums.len == 0
@ -54,3 +58,8 @@ fn test_struct_str() {
println(u) // make sure the struct is printable println(u) // make sure the struct is printable
// assert u.str() == '{name:"Bob", age:30}' // TODO // assert u.str() == '{name:"Bob", age:30}' // TODO
} }
fn test_at() {
foo := Foo{ @type: 'test' }
println(foo.@type)
}

View File

@ -34,6 +34,7 @@ enum Token {
dollar dollar
left_shift left_shift
righ_shift righ_shift
//at // @
// = := += -= // = := += -=
assign assign
decl_assign decl_assign
@ -148,6 +149,7 @@ fn build_token_str() []string {
s[Token.dot] = '.' s[Token.dot] = '.'
s[Token.dotdot] = '..' s[Token.dotdot] = '..'
s[Token.comma] = ',' s[Token.comma] = ','
//s[Token.at] = '@'
s[Token.semicolon] = ';' s[Token.semicolon] = ';'
s[Token.colon] = ':' s[Token.colon] = ':'
s[Token.arrow] = '=>' s[Token.arrow] = '=>'