mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: move comments
This commit is contained in:
parent
2a06263a5b
commit
05fbe5ab7a
@ -65,20 +65,17 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
|
|||||||
// }
|
// }
|
||||||
mut stmts := []ast.Stmt
|
mut stmts := []ast.Stmt
|
||||||
mut p := Parser{
|
mut p := Parser{
|
||||||
// scanner: scanner.new_scanner(text, comments_mode)
|
|
||||||
scanner: scanner.new_scanner_file(path, comments_mode)
|
scanner: scanner.new_scanner_file(path, comments_mode)
|
||||||
table: table
|
table: table
|
||||||
file_name: path
|
file_name: path
|
||||||
pref: pref // &pref.Preferences{}
|
pref: pref
|
||||||
|
|
||||||
scope: &ast.Scope{
|
scope: &ast.Scope{
|
||||||
start_pos: 0
|
start_pos: 0
|
||||||
parent: 0
|
parent: 0
|
||||||
}
|
}
|
||||||
global_scope: global_scope
|
global_scope: global_scope
|
||||||
// comments_mode: comments_mode
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// comments_mode: comments_mode
|
||||||
p.read_first_token()
|
p.read_first_token()
|
||||||
// module decl
|
// module decl
|
||||||
module_decl := if p.tok.kind == .key_module { p.module_decl() } else { ast.Module{name: 'main'
|
module_decl := if p.tok.kind == .key_module { p.module_decl() } else { ast.Module{name: 'main'
|
||||||
@ -435,8 +432,8 @@ pub fn (p mut Parser) stmt() ast.Stmt {
|
|||||||
if p.tok.kind == .name && p.peek_tok.kind in [.decl_assign, .comma] {
|
if p.tok.kind == .name && p.peek_tok.kind in [.decl_assign, .comma] {
|
||||||
return p.assign_stmt()
|
return p.assign_stmt()
|
||||||
}
|
}
|
||||||
// `label:`
|
|
||||||
else if p.tok.kind == .name && p.peek_tok.kind == .colon {
|
else if p.tok.kind == .name && p.peek_tok.kind == .colon {
|
||||||
|
// `label:`
|
||||||
name := p.check_name()
|
name := p.check_name()
|
||||||
p.check(.colon)
|
p.check(.colon)
|
||||||
return ast.GotoLabel{
|
return ast.GotoLabel{
|
||||||
@ -669,19 +666,16 @@ pub fn (p mut Parser) name_expr() ast.Expr {
|
|||||||
p.expr_mod = ''
|
p.expr_mod = ''
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
// fn call
|
|
||||||
else {
|
else {
|
||||||
|
// fn call
|
||||||
// println('calling $p.tok.lit')
|
// println('calling $p.tok.lit')
|
||||||
x := p.call_expr(is_c, mod) // TODO `node,typ :=` should work
|
x := p.call_expr(is_c, mod) // TODO `node,typ :=` should work
|
||||||
node = x
|
node = x
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c ||
|
else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c ||
|
||||||
//
|
|
||||||
(p.builtin_mod && p.tok.lit in table.builtin_type_names)) &&
|
(p.builtin_mod && p.tok.lit in table.builtin_type_names)) &&
|
||||||
//
|
|
||||||
(p.tok.lit.len in [1,2] || !p.tok.lit[p.tok.lit.len - 1].is_capital()) &&
|
(p.tok.lit.len in [1,2] || !p.tok.lit[p.tok.lit.len - 1].is_capital()) &&
|
||||||
//
|
|
||||||
!p.inside_match_case
|
!p.inside_match_case
|
||||||
{
|
{
|
||||||
// || p.table.known_type(p.tok.lit)) {
|
// || p.table.known_type(p.tok.lit)) {
|
||||||
@ -745,13 +739,10 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
|
|||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
// -1, -a, !x, &x, ~x
|
|
||||||
.minus, .amp, .mul, .not, .bit_not {
|
.minus, .amp, .mul, .not, .bit_not {
|
||||||
|
// -1, -a, !x, &x, ~x
|
||||||
node = p.prefix_expr()
|
node = p.prefix_expr()
|
||||||
}
|
}
|
||||||
// .amp {
|
|
||||||
// p.next()
|
|
||||||
// }
|
|
||||||
.key_true, .key_false {
|
.key_true, .key_false {
|
||||||
node = ast.BoolLiteral{
|
node = ast.BoolLiteral{
|
||||||
val: p.tok.kind == .key_true
|
val: p.tok.kind == .key_true
|
||||||
@ -811,8 +802,8 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
|
|||||||
expr: expr
|
expr: expr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Map `{"age": 20}` or `{ x | foo:bar, a:10 }`
|
|
||||||
.lcbr {
|
.lcbr {
|
||||||
|
// Map `{"age": 20}` or `{ x | foo:bar, a:10 }`
|
||||||
p.next()
|
p.next()
|
||||||
if p.tok.kind == .string {
|
if p.tok.kind == .string {
|
||||||
node = p.map_init()
|
node = p.map_init()
|
||||||
@ -858,10 +849,10 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
|
|||||||
pos: pos
|
pos: pos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO: handle in later stages since this
|
|
||||||
// will fudge left shift as it makes it right assoc
|
|
||||||
// `arr << 'a'` | `arr << 'a' + 'b'`
|
|
||||||
else if p.tok.kind == .left_shift {
|
else if p.tok.kind == .left_shift {
|
||||||
|
// TODO: handle in later stages since this
|
||||||
|
// will fudge left shift as it makes it right assoc
|
||||||
|
// `arr << 'a'` | `arr << 'a' + 'b'`
|
||||||
tok := p.tok
|
tok := p.tok
|
||||||
p.next()
|
p.next()
|
||||||
right := p.expr(precedence - 1)
|
right := p.expr(precedence - 1)
|
||||||
@ -875,8 +866,8 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
|
|||||||
else if p.tok.kind.is_infix() {
|
else if p.tok.kind.is_infix() {
|
||||||
node = p.infix_expr(node)
|
node = p.infix_expr(node)
|
||||||
}
|
}
|
||||||
// Postfix
|
|
||||||
else if p.tok.kind in [.inc, .dec] {
|
else if p.tok.kind in [.inc, .dec] {
|
||||||
|
// Postfix
|
||||||
node = ast.PostfixExpr{
|
node = ast.PostfixExpr{
|
||||||
op: p.tok.kind
|
op: p.tok.kind
|
||||||
expr: node
|
expr: node
|
||||||
@ -1084,8 +1075,8 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
|||||||
else if p.tok.kind == .key_mut {
|
else if p.tok.kind == .key_mut {
|
||||||
p.error('`mut` is not required in for loops')
|
p.error('`mut` is not required in for loops')
|
||||||
}
|
}
|
||||||
// for i := 0; i < 10; i++ {
|
|
||||||
else if p.peek_tok.kind in [.decl_assign, .assign, .semicolon] || p.tok.kind == .semicolon {
|
else if p.peek_tok.kind in [.decl_assign, .assign, .semicolon] || p.tok.kind == .semicolon {
|
||||||
|
// for i := 0; i < 10; i++ {
|
||||||
mut init := ast.Stmt{}
|
mut init := ast.Stmt{}
|
||||||
mut cond := p.new_true_expr()
|
mut cond := p.new_true_expr()
|
||||||
// mut inc := ast.Stmt{}
|
// mut inc := ast.Stmt{}
|
||||||
@ -1127,8 +1118,8 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
|||||||
pos: pos
|
pos: pos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// `for i in vals`, `for i in start .. end`
|
|
||||||
else if p.peek_tok.kind in [.key_in, .comma] {
|
else if p.peek_tok.kind in [.key_in, .comma] {
|
||||||
|
// `for i in vals`, `for i in start .. end`
|
||||||
mut key_var_name := ''
|
mut key_var_name := ''
|
||||||
mut val_var_name := p.check_name()
|
mut val_var_name := p.check_name()
|
||||||
if p.tok.kind == .comma {
|
if p.tok.kind == .comma {
|
||||||
|
Loading…
Reference in New Issue
Block a user