2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-12-22 02:34:37 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module parser
|
|
|
|
|
2020-04-14 19:32:23 +02:00
|
|
|
import v.scanner
|
|
|
|
import v.ast
|
|
|
|
import v.token
|
|
|
|
import v.table
|
|
|
|
import v.pref
|
|
|
|
import v.util
|
2020-05-10 11:26:57 +02:00
|
|
|
import v.errors
|
2020-04-14 19:32:23 +02:00
|
|
|
import os
|
2020-05-22 02:22:56 +02:00
|
|
|
import runtime
|
|
|
|
import time
|
2020-01-22 21:34:38 +01:00
|
|
|
|
2020-05-04 21:56:41 +02:00
|
|
|
pub struct Parser {
|
2020-04-10 12:01:06 +02:00
|
|
|
file_name string // "/home/user/hello.v"
|
|
|
|
file_name_dir string // "/home/user"
|
2019-12-22 02:34:37 +01:00
|
|
|
mut:
|
2020-06-01 15:43:54 +02:00
|
|
|
scanner &scanner.Scanner
|
2020-06-06 17:47:16 +02:00
|
|
|
comments_mode scanner.CommentsMode = .skip_comments // see comment in parse_file
|
2020-04-07 15:48:13 +02:00
|
|
|
tok token.Token
|
2020-04-18 00:22:03 +02:00
|
|
|
prev_tok token.Token
|
2020-04-07 15:48:13 +02:00
|
|
|
peek_tok token.Token
|
2020-04-25 10:07:30 +02:00
|
|
|
peek_tok2 token.Token
|
2020-05-22 18:32:21 +02:00
|
|
|
peek_tok3 token.Token
|
2020-04-07 15:48:13 +02:00
|
|
|
table &table.Table
|
2020-05-19 17:12:47 +02:00
|
|
|
language table.Language
|
2020-04-07 15:48:13 +02:00
|
|
|
inside_if bool
|
2020-05-11 10:13:36 +02:00
|
|
|
inside_if_expr bool
|
2020-09-18 00:58:54 +02:00
|
|
|
inside_ct_if_expr bool
|
2020-05-11 10:13:36 +02:00
|
|
|
inside_or_expr bool
|
2020-04-08 19:08:54 +02:00
|
|
|
inside_for bool
|
|
|
|
inside_fn bool
|
2020-06-05 22:37:34 +02:00
|
|
|
inside_str_interp bool
|
2020-04-07 15:48:13 +02:00
|
|
|
pref &pref.Preferences
|
2020-04-17 17:16:14 +02:00
|
|
|
builtin_mod bool // are we in the `builtin` module?
|
|
|
|
mod string // current module name
|
2020-08-04 20:10:22 +02:00
|
|
|
attrs []table.Attr // attributes before next decl stmt
|
2020-05-04 16:46:36 +02:00
|
|
|
expr_mod string // for constructing full type names in parse_type()
|
2020-04-07 15:48:13 +02:00
|
|
|
scope &ast.Scope
|
|
|
|
global_scope &ast.Scope
|
2020-05-14 17:14:24 +02:00
|
|
|
imports map[string]string // alias => mod_name
|
|
|
|
ast_imports []ast.Import // mod_names
|
|
|
|
used_imports []string // alias
|
2020-05-09 15:23:48 +02:00
|
|
|
is_amp bool // for generating the right code for `&Foo{}`
|
2020-04-07 15:48:13 +02:00
|
|
|
returns bool
|
2020-04-15 16:23:03 +02:00
|
|
|
inside_match bool // to separate `match A { }` from `Struct{}`
|
2020-09-20 03:50:09 +02:00
|
|
|
inside_select bool // to allow `ch <- Struct{} {` inside `select`
|
2020-04-05 03:37:38 +02:00
|
|
|
inside_match_case bool // to separate `match_expr { }` from `Struct{}`
|
2020-05-12 00:26:39 +02:00
|
|
|
inside_match_body bool // to fix eval not used TODO
|
2020-05-11 16:05:59 +02:00
|
|
|
inside_unsafe bool
|
2020-04-25 10:57:12 +02:00
|
|
|
is_stmt_ident bool // true while the beginning of a statement is an ident/selector
|
2020-05-05 14:19:31 +02:00
|
|
|
expecting_type bool // `is Type`, expecting type
|
2020-05-10 11:26:57 +02:00
|
|
|
errors []errors.Error
|
|
|
|
warnings []errors.Warning
|
2020-07-19 19:58:34 +02:00
|
|
|
vet_errors &[]string
|
2020-06-06 21:36:24 +02:00
|
|
|
cur_fn_name string
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:34:38 +01:00
|
|
|
// for tests
|
2020-02-15 13:37:48 +01:00
|
|
|
pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
|
2020-07-11 18:53:50 +02:00
|
|
|
pref := &pref.Preferences{}
|
2020-07-11 11:41:39 +02:00
|
|
|
s := scanner.new_scanner(text, .skip_comments, pref)
|
2020-04-21 05:11:50 +02:00
|
|
|
mut p := Parser{
|
2019-12-22 02:34:37 +01:00
|
|
|
scanner: s
|
2019-12-26 11:27:35 +01:00
|
|
|
table: table
|
2020-07-11 11:41:39 +02:00
|
|
|
pref: pref
|
2020-02-15 13:37:48 +01:00
|
|
|
scope: scope
|
2020-04-04 05:14:40 +02:00
|
|
|
global_scope: &ast.Scope{
|
2020-04-08 00:59:28 +02:00
|
|
|
start_pos: 0
|
|
|
|
parent: 0
|
|
|
|
}
|
2020-07-19 19:58:34 +02:00
|
|
|
vet_errors: 0
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
p.init_parse_fns()
|
2019-12-30 15:06:56 +01:00
|
|
|
p.read_first_token()
|
2020-06-16 11:06:53 +02:00
|
|
|
return p.stmt(false)
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 18:47:51 +02:00
|
|
|
pub fn parse_text(text string, b_table &table.Table, pref &pref.Preferences, scope, global_scope &ast.Scope) ast.File {
|
2020-07-11 11:41:39 +02:00
|
|
|
s := scanner.new_scanner(text, .skip_comments, pref)
|
2020-06-07 12:26:45 +02:00
|
|
|
mut p := Parser{
|
|
|
|
scanner: s
|
|
|
|
table: b_table
|
2020-06-09 18:47:51 +02:00
|
|
|
pref: pref
|
2020-06-07 12:26:45 +02:00
|
|
|
scope: scope
|
|
|
|
errors: []errors.Error{}
|
|
|
|
warnings: []errors.Warning{}
|
|
|
|
global_scope: global_scope
|
2020-07-19 19:58:34 +02:00
|
|
|
vet_errors: 0
|
2020-06-07 12:26:45 +02:00
|
|
|
}
|
|
|
|
return p.parse()
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:12:18 +02:00
|
|
|
pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.CommentsMode, pref &pref.Preferences, global_scope &ast.Scope) ast.File {
|
2020-06-06 17:47:16 +02:00
|
|
|
// NB: when comments_mode == .toplevel_comments,
|
|
|
|
// the parser gives feedback to the scanner about toplevel statements, so that the scanner can skip
|
|
|
|
// all the tricky inner comments. This is needed because we do not have a good general solution
|
|
|
|
// for handling them, and should be removed when we do (the general solution is also needed for vfmt)
|
2020-02-29 17:51:35 +01:00
|
|
|
// println('parse_file("$path")')
|
2020-02-29 17:53:04 +01:00
|
|
|
// text := os.read_file(path) or {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
2020-04-21 05:11:50 +02:00
|
|
|
mut p := Parser{
|
2020-07-11 11:41:39 +02:00
|
|
|
scanner: scanner.new_scanner_file(path, comments_mode, pref)
|
2020-06-06 17:47:16 +02:00
|
|
|
comments_mode: comments_mode
|
2020-05-01 18:12:18 +02:00
|
|
|
table: b_table
|
2020-01-01 10:15:05 +01:00
|
|
|
file_name: path
|
2020-04-10 22:27:51 +02:00
|
|
|
file_name_dir: os.dir(path)
|
2020-04-05 04:05:09 +02:00
|
|
|
pref: pref
|
2020-02-16 11:48:29 +01:00
|
|
|
scope: &ast.Scope{
|
2020-04-08 00:59:28 +02:00
|
|
|
start_pos: 0
|
2020-06-09 09:08:11 +02:00
|
|
|
parent: global_scope
|
2020-04-08 00:59:28 +02:00
|
|
|
}
|
2020-05-11 16:05:59 +02:00
|
|
|
errors: []errors.Error{}
|
|
|
|
warnings: []errors.Warning{}
|
2020-04-04 05:14:40 +02:00
|
|
|
global_scope: global_scope
|
2020-07-19 19:58:34 +02:00
|
|
|
vet_errors: 0
|
2019-12-26 11:27:35 +01:00
|
|
|
}
|
2020-07-19 19:58:34 +02:00
|
|
|
return p.parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_vet_file(path string, table_ &table.Table, pref &pref.Preferences, vet_errors &[]string) ast.File {
|
|
|
|
global_scope := &ast.Scope{
|
|
|
|
parent: 0
|
|
|
|
}
|
|
|
|
mut p := Parser{
|
|
|
|
scanner: scanner.new_vet_scanner_file(path, .parse_comments, pref, vet_errors)
|
|
|
|
comments_mode: .parse_comments
|
|
|
|
table: table_
|
|
|
|
file_name: path
|
|
|
|
file_name_dir: os.dir(path)
|
|
|
|
pref: pref
|
|
|
|
scope: &ast.Scope{
|
|
|
|
start_pos: 0
|
|
|
|
parent: global_scope
|
|
|
|
}
|
|
|
|
errors: []errors.Error{}
|
|
|
|
warnings: []errors.Warning{}
|
|
|
|
global_scope: global_scope
|
|
|
|
vet_errors: vet_errors
|
|
|
|
}
|
|
|
|
if p.scanner.text.contains('\n ') {
|
2020-07-04 23:37:41 +02:00
|
|
|
source_lines := os.read_lines(path) or {
|
|
|
|
[]string{}
|
|
|
|
}
|
2020-07-04 14:29:00 +02:00
|
|
|
for lnumber, line in source_lines {
|
2020-07-19 19:58:34 +02:00
|
|
|
if line.starts_with(' ') {
|
|
|
|
p.vet_error('Looks like you are using spaces for indentation.', lnumber)
|
2020-07-04 14:29:00 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-27 14:50:04 +02:00
|
|
|
}
|
2020-06-07 12:26:45 +02:00
|
|
|
return p.parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Parser) parse() ast.File {
|
2020-04-05 04:05:09 +02:00
|
|
|
// comments_mode: comments_mode
|
2020-05-25 23:00:48 +02:00
|
|
|
p.init_parse_fns()
|
2019-12-28 14:11:05 +01:00
|
|
|
p.read_first_token()
|
2020-06-07 12:26:45 +02:00
|
|
|
mut stmts := []ast.Stmt{}
|
2020-04-10 22:27:51 +02:00
|
|
|
for p.tok.kind == .comment {
|
2020-07-17 19:13:22 +02:00
|
|
|
stmts << p.comment_stmt()
|
2020-04-10 22:27:51 +02:00
|
|
|
}
|
2020-05-10 06:47:20 +02:00
|
|
|
// module
|
2020-04-10 22:27:51 +02:00
|
|
|
module_decl := p.module_decl()
|
2020-06-07 12:26:45 +02:00
|
|
|
stmts << module_decl
|
2020-01-18 23:26:14 +01:00
|
|
|
// imports
|
2020-06-08 08:22:10 +02:00
|
|
|
for {
|
|
|
|
if p.tok.kind == .key_import {
|
|
|
|
stmts << p.import_stmt()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p.tok.kind == .comment {
|
2020-07-17 19:13:22 +02:00
|
|
|
stmts << p.comment_stmt()
|
2020-06-08 08:22:10 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-04-10 22:27:51 +02:00
|
|
|
for {
|
2019-12-28 09:15:32 +01:00
|
|
|
if p.tok.kind == .eof {
|
2020-07-01 00:53:53 +02:00
|
|
|
p.check_unused_imports()
|
2019-12-26 11:27:35 +01:00
|
|
|
break
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
// println('stmt at ' + p.tok.str())
|
2020-08-04 20:10:22 +02:00
|
|
|
stmts << p.top_stmt()
|
|
|
|
// clear the attributes after each statement
|
|
|
|
p.attrs = []
|
2019-12-26 11:27:35 +01:00
|
|
|
}
|
2019-12-29 08:51:55 +01:00
|
|
|
// println('nr stmts = $stmts.len')
|
2019-12-28 14:11:05 +01:00
|
|
|
// println(stmts[0])
|
2020-02-15 13:37:48 +01:00
|
|
|
p.scope.end_pos = p.tok.pos
|
2020-05-25 23:00:48 +02:00
|
|
|
//
|
2019-12-30 12:10:46 +01:00
|
|
|
return ast.File{
|
2020-06-07 12:26:45 +02:00
|
|
|
path: p.file_name
|
2020-01-22 21:34:38 +01:00
|
|
|
mod: module_decl
|
2020-02-29 17:51:35 +01:00
|
|
|
imports: p.ast_imports
|
2020-01-22 21:34:38 +01:00
|
|
|
stmts: stmts
|
2020-02-20 11:13:18 +01:00
|
|
|
scope: p.scope
|
2020-05-11 16:05:59 +02:00
|
|
|
global_scope: p.global_scope
|
|
|
|
errors: p.errors
|
2020-05-10 11:26:57 +02:00
|
|
|
warnings: p.warnings
|
2019-12-28 14:11:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 10:22:18 +02:00
|
|
|
/*
|
2020-03-16 08:33:42 +01:00
|
|
|
struct Queue {
|
|
|
|
mut:
|
|
|
|
idx int
|
2020-05-22 02:22:56 +02:00
|
|
|
mu &sync.Mutex
|
|
|
|
mu2 &sync.Mutex
|
2020-03-16 08:33:42 +01:00
|
|
|
paths []string
|
|
|
|
table &table.Table
|
|
|
|
parsed_ast_files []ast.File
|
2020-05-22 02:22:56 +02:00
|
|
|
pref &pref.Preferences
|
|
|
|
global_scope &ast.Scope
|
2020-03-16 08:33:42 +01:00
|
|
|
}
|
|
|
|
|
2020-05-22 02:22:56 +02:00
|
|
|
fn (mut q Queue) run() {
|
|
|
|
for {
|
|
|
|
q.mu.lock()
|
|
|
|
idx := q.idx
|
|
|
|
if idx >= q.paths.len {
|
|
|
|
q.mu.unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
q.idx++
|
2020-03-16 08:33:42 +01:00
|
|
|
q.mu.unlock()
|
2020-05-22 02:22:56 +02:00
|
|
|
println('run(idx=$idx)')
|
|
|
|
path := q.paths[idx]
|
|
|
|
file := parse_file(path, q.table, .skip_comments, q.pref, q.global_scope)
|
|
|
|
q.mu2.lock()
|
|
|
|
q.parsed_ast_files << file
|
|
|
|
q.mu2.unlock()
|
|
|
|
println('run done(idx=$idx)')
|
2020-03-16 08:33:42 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-31 10:22:18 +02:00
|
|
|
*/
|
2020-04-04 05:14:40 +02:00
|
|
|
pub fn parse_files(paths []string, table &table.Table, pref &pref.Preferences, global_scope &ast.Scope) []ast.File {
|
2020-05-22 02:22:56 +02:00
|
|
|
// println('nr_cpus= $nr_cpus')
|
2020-05-22 02:45:11 +02:00
|
|
|
$if macos {
|
2020-05-31 10:22:18 +02:00
|
|
|
/*
|
2020-05-22 02:45:11 +02:00
|
|
|
if pref.is_parallel && paths[0].contains('/array.v') {
|
|
|
|
println('\n\n\nparse_files() nr_files=$paths.len')
|
|
|
|
println(paths)
|
|
|
|
nr_cpus := runtime.nr_cpus()
|
|
|
|
mut q := &Queue{
|
|
|
|
paths: paths
|
|
|
|
table: table
|
|
|
|
pref: pref
|
|
|
|
global_scope: global_scope
|
|
|
|
mu: sync.new_mutex()
|
|
|
|
mu2: sync.new_mutex()
|
|
|
|
}
|
|
|
|
for _ in 0 .. nr_cpus - 1 {
|
|
|
|
go q.run()
|
|
|
|
}
|
|
|
|
time.sleep_ms(1000)
|
|
|
|
println('all done')
|
|
|
|
return q.parsed_ast_files
|
2020-05-22 02:22:56 +02:00
|
|
|
}
|
2020-05-31 10:22:18 +02:00
|
|
|
*/
|
2020-03-16 08:33:42 +01:00
|
|
|
}
|
2020-05-22 07:36:56 +02:00
|
|
|
if false {
|
|
|
|
// TODO: remove this; it just prevents warnings about unused time and runtime
|
2020-05-23 08:51:15 +02:00
|
|
|
time.sleep_ms(1)
|
2020-05-22 07:36:56 +02:00
|
|
|
println(runtime.nr_cpus())
|
|
|
|
}
|
2020-03-16 08:33:42 +01:00
|
|
|
// ///////////////
|
2020-04-26 09:17:13 +02:00
|
|
|
mut files := []ast.File{}
|
2019-12-30 12:10:46 +01:00
|
|
|
for path in paths {
|
2020-03-27 08:46:54 +01:00
|
|
|
// println('parse_files $path')
|
2020-04-04 05:14:40 +02:00
|
|
|
files << parse_file(path, table, .skip_comments, pref, global_scope)
|
2019-12-30 12:10:46 +01:00
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2020-06-06 17:47:16 +02:00
|
|
|
pub fn (mut p Parser) init_parse_fns() {
|
|
|
|
if p.comments_mode == .toplevel_comments {
|
|
|
|
p.scanner.scan_all_tokens_in_buffer()
|
|
|
|
}
|
2020-01-07 01:57:38 +01:00
|
|
|
// p.prefix_parse_fns = make(100, 100, sizeof(PrefixParseFn))
|
2020-01-06 16:13:12 +01:00
|
|
|
// p.prefix_parse_fns[token.Kind.name] = parse_name
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
pub fn (mut p Parser) read_first_token() {
|
2020-05-22 18:32:21 +02:00
|
|
|
// need to call next() 4 times to get peek token 1,2,3 and current token
|
|
|
|
p.next()
|
2020-04-25 10:07:30 +02:00
|
|
|
p.next()
|
2019-12-28 14:11:05 +01:00
|
|
|
p.next()
|
|
|
|
p.next()
|
2019-12-26 11:27:35 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
pub fn (mut p Parser) open_scope() {
|
2020-02-15 13:37:48 +01:00
|
|
|
p.scope = &ast.Scope{
|
|
|
|
parent: p.scope
|
|
|
|
start_pos: p.tok.pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
pub fn (mut p Parser) close_scope() {
|
2020-08-17 17:51:25 +02:00
|
|
|
// p.scope.end_pos = p.tok.pos
|
|
|
|
// NOTE: since this is usually called after `p.parse_block()`
|
|
|
|
// ie. when `prev_tok` is rcbr `}` we most likely want `prev_tok`
|
|
|
|
// we could do the following, but probably not needed in 99% of cases:
|
|
|
|
// `end_pos = if p.prev_tok.kind == .rcbr { p.prev_tok.pos } else { p.tok.pos }`
|
|
|
|
p.scope.end_pos = p.prev_tok.pos
|
2020-02-15 13:37:48 +01:00
|
|
|
p.scope.parent.children << p.scope
|
|
|
|
p.scope = p.scope.parent
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
pub fn (mut p Parser) parse_block() []ast.Stmt {
|
2020-02-15 13:37:48 +01:00
|
|
|
p.open_scope()
|
2020-03-18 09:56:19 +01:00
|
|
|
// println('parse block')
|
2020-06-06 17:47:16 +02:00
|
|
|
stmts := p.parse_block_no_scope(false)
|
2020-03-18 09:56:19 +01:00
|
|
|
p.close_scope()
|
|
|
|
// println('nr exprs in block = $exprs.len')
|
|
|
|
return stmts
|
|
|
|
}
|
|
|
|
|
2020-06-06 17:47:16 +02:00
|
|
|
pub fn (mut p Parser) parse_block_no_scope(is_top_level bool) []ast.Stmt {
|
2019-12-31 19:42:16 +01:00
|
|
|
p.check(.lcbr)
|
2020-04-26 09:17:13 +02:00
|
|
|
mut stmts := []ast.Stmt{}
|
2020-01-06 16:13:12 +01:00
|
|
|
if p.tok.kind != .rcbr {
|
2020-08-02 16:58:05 +02:00
|
|
|
mut c := 0
|
2020-04-10 22:27:51 +02:00
|
|
|
for {
|
2020-06-16 11:06:53 +02:00
|
|
|
stmts << p.stmt(is_top_level)
|
2020-01-06 16:13:12 +01:00
|
|
|
// p.warn('after stmt(): tok=$p.tok.str()')
|
|
|
|
if p.tok.kind in [.eof, .rcbr] {
|
|
|
|
break
|
|
|
|
}
|
2020-08-02 16:58:05 +02:00
|
|
|
c++
|
|
|
|
if c % 100000 == 0 {
|
|
|
|
eprintln('parsed $c statements so far from fn $p.cur_fn_name ...')
|
|
|
|
}
|
|
|
|
if c > 1000000 {
|
|
|
|
p.error_with_pos('parsed over $c statements from fn $p.cur_fn_name, the parser is probably stuck',
|
|
|
|
p.tok.position())
|
|
|
|
}
|
2019-12-27 13:57:49 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
if is_top_level {
|
|
|
|
p.top_level_statement_end()
|
|
|
|
}
|
2019-12-31 19:42:16 +01:00
|
|
|
p.check(.rcbr)
|
2019-12-28 14:11:05 +01:00
|
|
|
return stmts
|
2019-12-27 13:57:49 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 02:08:10 +02:00
|
|
|
/*
|
2020-04-23 01:16:58 +02:00
|
|
|
fn (mut p Parser) next_with_comment() {
|
2020-04-05 02:08:10 +02:00
|
|
|
p.tok = p.peek_tok
|
|
|
|
p.peek_tok = p.scanner.scan()
|
|
|
|
}
|
|
|
|
*/
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) next() {
|
2020-04-18 00:22:03 +02:00
|
|
|
p.prev_tok = p.tok
|
2019-12-28 09:15:32 +01:00
|
|
|
p.tok = p.peek_tok
|
2020-04-25 10:07:30 +02:00
|
|
|
p.peek_tok = p.peek_tok2
|
2020-05-22 18:32:21 +02:00
|
|
|
p.peek_tok2 = p.peek_tok3
|
|
|
|
p.peek_tok3 = p.scanner.scan()
|
2020-04-05 02:08:10 +02:00
|
|
|
/*
|
|
|
|
if p.tok.kind==.comment {
|
|
|
|
p.comments << ast.Comment{text:p.tok.lit, line_nr:p.tok.line_nr}
|
|
|
|
p.next()
|
|
|
|
}
|
2020-04-25 17:49:16 +02:00
|
|
|
*/
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) check(expected token.Kind) {
|
2020-02-29 17:51:35 +01:00
|
|
|
// for p.tok.kind in [.line_comment, .mline_comment] {
|
|
|
|
// p.next()
|
|
|
|
// }
|
2019-12-28 09:15:32 +01:00
|
|
|
if p.tok.kind != expected {
|
2020-06-21 23:09:17 +02:00
|
|
|
if p.tok.kind == .name {
|
|
|
|
p.error('unexpected name `$p.tok.lit`, expecting `$expected.str()`')
|
|
|
|
} else {
|
|
|
|
p.error('unexpected `$p.tok.kind.str()`, expecting `$expected.str()`')
|
|
|
|
}
|
2019-12-27 13:57:49 +01:00
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
|
2020-05-15 22:26:51 +02:00
|
|
|
// JS functions can have multiple dots in their name:
|
|
|
|
// JS.foo.bar.and.a.lot.more.dots()
|
|
|
|
fn (mut p Parser) check_js_name() string {
|
|
|
|
mut name := ''
|
|
|
|
for p.peek_tok.kind == .dot {
|
|
|
|
name += '${p.tok.lit}.'
|
|
|
|
p.next() // .name
|
|
|
|
p.next() // .dot
|
|
|
|
}
|
2020-05-16 16:12:23 +02:00
|
|
|
// last .name
|
|
|
|
name += p.tok.lit
|
2020-05-15 22:26:51 +02:00
|
|
|
p.next()
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) check_name() string {
|
2019-12-28 09:15:32 +01:00
|
|
|
name := p.tok.lit
|
2020-05-14 17:14:24 +02:00
|
|
|
if p.peek_tok.kind == .dot && name in p.imports {
|
|
|
|
p.register_used_import(name)
|
|
|
|
}
|
2019-12-27 13:57:49 +01:00
|
|
|
p.check(.name)
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
pub fn (mut p Parser) top_stmt() ast.Stmt {
|
2020-07-17 19:13:22 +02:00
|
|
|
$if trace_parser ? {
|
2020-07-12 11:56:01 +02:00
|
|
|
tok_pos := p.tok.position()
|
|
|
|
eprintln('parsing file: ${p.file_name:-30} | tok.kind: ${p.tok.kind:-10} | tok.lit: ${p.tok.lit:-10} | tok_pos: ${tok_pos.str():-45} | top_stmt')
|
|
|
|
}
|
2020-08-04 20:10:22 +02:00
|
|
|
for {
|
|
|
|
match p.tok.kind {
|
|
|
|
.key_pub {
|
|
|
|
match p.peek_tok.kind {
|
|
|
|
.key_const {
|
|
|
|
return p.const_decl()
|
|
|
|
}
|
|
|
|
.key_fn {
|
|
|
|
return p.fn_decl()
|
|
|
|
}
|
|
|
|
.key_struct, .key_union {
|
|
|
|
return p.struct_decl()
|
|
|
|
}
|
|
|
|
.key_interface {
|
|
|
|
return p.interface_decl()
|
|
|
|
}
|
|
|
|
.key_enum {
|
|
|
|
return p.enum_decl()
|
|
|
|
}
|
|
|
|
.key_type {
|
|
|
|
return p.type_decl()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('wrong pub keyword usage')
|
|
|
|
return ast.Stmt{}
|
|
|
|
}
|
2019-12-31 19:42:16 +01:00
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
}
|
2020-08-04 20:10:22 +02:00
|
|
|
.lsbr {
|
2020-08-10 02:00:14 +02:00
|
|
|
// attrs are stored in `p.attrs`
|
2020-08-04 20:10:22 +02:00
|
|
|
p.attributes()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
.key_interface {
|
|
|
|
return p.interface_decl()
|
|
|
|
}
|
|
|
|
.key_import {
|
|
|
|
p.error_with_pos('`import x` can only be declared at the beginning of the file',
|
|
|
|
p.tok.position())
|
|
|
|
return p.import_stmt()
|
|
|
|
}
|
|
|
|
.key_global {
|
|
|
|
return p.global_decl()
|
|
|
|
}
|
|
|
|
.key_const {
|
|
|
|
return p.const_decl()
|
|
|
|
}
|
|
|
|
.key_fn {
|
|
|
|
return p.fn_decl()
|
|
|
|
}
|
|
|
|
.key_struct {
|
|
|
|
return p.struct_decl()
|
|
|
|
}
|
|
|
|
.dollar {
|
2020-09-18 00:58:54 +02:00
|
|
|
return ast.ExprStmt{
|
|
|
|
expr: p.if_expr(true)
|
|
|
|
}
|
2020-08-04 20:10:22 +02:00
|
|
|
}
|
|
|
|
.hash {
|
|
|
|
return p.hash()
|
|
|
|
}
|
|
|
|
.key_type {
|
|
|
|
return p.type_decl()
|
|
|
|
}
|
|
|
|
.key_enum {
|
|
|
|
return p.enum_decl()
|
|
|
|
}
|
|
|
|
.key_union {
|
|
|
|
return p.struct_decl()
|
|
|
|
}
|
|
|
|
.comment {
|
|
|
|
return p.comment_stmt()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if p.pref.is_script && !p.pref.is_test {
|
|
|
|
mut stmts := []ast.Stmt{}
|
|
|
|
for p.tok.kind != .eof {
|
|
|
|
stmts << p.stmt(false)
|
|
|
|
}
|
|
|
|
return ast.FnDecl{
|
|
|
|
name: 'main.main'
|
|
|
|
mod: 'main'
|
|
|
|
stmts: stmts
|
|
|
|
file: p.file_name
|
|
|
|
return_type: table.void_type
|
|
|
|
}
|
|
|
|
} else if p.pref.is_fmt {
|
|
|
|
return p.stmt(false)
|
|
|
|
} else {
|
|
|
|
p.error('bad top level statement ' + p.tok.str())
|
|
|
|
return ast.Stmt{}
|
2020-05-13 14:02:04 +02:00
|
|
|
}
|
2020-04-09 12:46:16 +02:00
|
|
|
}
|
2019-12-28 09:43:22 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-08-04 20:10:22 +02:00
|
|
|
// TODO remove dummy return statement
|
|
|
|
// the compiler complains if it's not there
|
|
|
|
return ast.Stmt{}
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 02:08:10 +02:00
|
|
|
// TODO [if vfmt]
|
2020-04-21 05:11:50 +02:00
|
|
|
pub fn (mut p Parser) check_comment() ast.Comment {
|
2020-04-05 02:08:10 +02:00
|
|
|
if p.tok.kind == .comment {
|
|
|
|
return p.comment()
|
|
|
|
}
|
2020-04-07 15:15:45 +02:00
|
|
|
return ast.Comment{}
|
2020-04-05 02:08:10 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
pub fn (mut p Parser) comment() ast.Comment {
|
2020-04-05 12:25:33 +02:00
|
|
|
pos := p.tok.position()
|
2020-02-29 17:51:35 +01:00
|
|
|
text := p.tok.lit
|
|
|
|
p.next()
|
2020-04-07 15:48:13 +02:00
|
|
|
// p.next_with_comment()
|
2020-04-05 02:08:10 +02:00
|
|
|
return ast.Comment{
|
2020-06-23 18:01:56 +02:00
|
|
|
is_multi: text.contains('\n')
|
2020-02-29 17:51:35 +01:00
|
|
|
text: text
|
2020-04-05 12:25:33 +02:00
|
|
|
pos: pos
|
2020-02-29 17:51:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 19:13:22 +02:00
|
|
|
pub fn (mut p Parser) comment_stmt() ast.ExprStmt {
|
|
|
|
comment := p.comment()
|
|
|
|
return ast.ExprStmt{
|
|
|
|
expr: comment
|
|
|
|
pos: comment.pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 15:44:03 +02:00
|
|
|
pub fn (mut p Parser) eat_comments() []ast.Comment {
|
|
|
|
mut comments := []ast.Comment{}
|
|
|
|
for {
|
|
|
|
if p.tok.kind != .comment {
|
|
|
|
break
|
|
|
|
}
|
2020-07-17 19:13:22 +02:00
|
|
|
comments << p.comment()
|
2020-07-02 15:44:03 +02:00
|
|
|
}
|
|
|
|
return comments
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:06:53 +02:00
|
|
|
pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
|
2020-07-17 19:13:22 +02:00
|
|
|
$if trace_parser ? {
|
2020-07-12 11:56:01 +02:00
|
|
|
tok_pos := p.tok.position()
|
|
|
|
eprintln('parsing file: ${p.file_name:-30} | tok.kind: ${p.tok.kind:-10} | tok.lit: ${p.tok.lit:-10} | tok_pos: ${tok_pos.str():-45} | stmt($is_top_level)')
|
|
|
|
}
|
2020-04-25 10:57:12 +02:00
|
|
|
p.is_stmt_ident = p.tok.kind == .name
|
2020-01-06 16:13:12 +01:00
|
|
|
match p.tok.kind {
|
2020-03-24 15:44:17 +01:00
|
|
|
.lcbr {
|
|
|
|
stmts := p.parse_block()
|
|
|
|
return ast.Block{
|
|
|
|
stmts: stmts
|
|
|
|
}
|
|
|
|
}
|
2020-02-26 15:51:05 +01:00
|
|
|
.key_assert {
|
|
|
|
p.next()
|
2020-04-08 04:47:29 +02:00
|
|
|
assert_pos := p.tok.position()
|
2020-03-05 12:08:43 +01:00
|
|
|
expr := p.expr(0)
|
2020-02-26 15:51:05 +01:00
|
|
|
return ast.AssertStmt{
|
|
|
|
expr: expr
|
2020-04-08 04:47:29 +02:00
|
|
|
pos: assert_pos
|
2020-02-26 15:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 06:16:59 +01:00
|
|
|
.key_for {
|
2020-04-08 19:08:54 +02:00
|
|
|
return p.for_stmt()
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
2020-07-07 01:57:31 +02:00
|
|
|
.name, .key_mut, .key_shared, .key_atomic, .key_static, .mul {
|
2020-06-16 13:20:16 +02:00
|
|
|
if p.tok.kind == .name {
|
2020-06-19 16:43:32 +02:00
|
|
|
if p.tok.lit == 'sql' {
|
2020-06-24 16:35:18 +02:00
|
|
|
return p.sql_stmt()
|
2020-06-19 16:43:32 +02:00
|
|
|
}
|
2020-06-16 13:20:16 +02:00
|
|
|
if p.peek_tok.kind == .colon {
|
|
|
|
// `label:`
|
|
|
|
name := p.check_name()
|
|
|
|
p.next()
|
|
|
|
return ast.GotoLabel{
|
|
|
|
name: name
|
|
|
|
}
|
2020-06-18 20:21:08 +02:00
|
|
|
} else if p.peek_tok.kind == .name {
|
2020-06-16 13:20:16 +02:00
|
|
|
p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position())
|
2020-06-18 20:21:08 +02:00
|
|
|
} else if !p.inside_if_expr && !p.inside_match_body && !p.inside_or_expr &&
|
|
|
|
p.peek_tok.kind in [.rcbr, .eof] {
|
2020-06-16 13:20:16 +02:00
|
|
|
p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position())
|
2020-05-12 17:18:25 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-16 11:06:53 +02:00
|
|
|
return p.parse_multi_expr(is_top_level)
|
2020-05-12 17:18:25 +02:00
|
|
|
}
|
2020-04-05 02:08:10 +02:00
|
|
|
.comment {
|
2020-07-17 19:13:22 +02:00
|
|
|
return p.comment_stmt()
|
2020-02-29 17:51:35 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
.key_return {
|
|
|
|
return p.return_stmt()
|
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
.dollar {
|
2020-05-27 03:33:37 +02:00
|
|
|
if p.peek_tok.kind == .key_if {
|
2020-09-18 00:58:54 +02:00
|
|
|
return ast.ExprStmt{
|
|
|
|
expr: p.if_expr(true)
|
|
|
|
}
|
2020-07-03 15:10:39 +02:00
|
|
|
} else if p.peek_tok.kind == .key_for {
|
|
|
|
return p.comp_for()
|
2020-05-27 03:33:37 +02:00
|
|
|
} else if p.peek_tok.kind == .name {
|
|
|
|
return ast.ExprStmt{
|
|
|
|
expr: p.vweb()
|
|
|
|
}
|
|
|
|
}
|
2020-08-02 17:13:51 +02:00
|
|
|
p.error_with_pos('unexpected \$', p.tok.position())
|
2020-07-09 22:38:43 +02:00
|
|
|
return ast.Stmt{}
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
2020-02-04 08:29:50 +01:00
|
|
|
.key_continue, .key_break {
|
|
|
|
tok := p.tok
|
|
|
|
p.next()
|
|
|
|
return ast.BranchStmt{
|
2020-02-18 03:28:39 +01:00
|
|
|
tok: tok
|
2020-02-04 08:29:50 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-05 10:00:11 +01:00
|
|
|
.key_unsafe {
|
2020-09-19 18:18:36 +02:00
|
|
|
return p.unsafe_stmt()
|
2020-02-05 10:00:11 +01:00
|
|
|
}
|
2020-03-19 07:59:01 +01:00
|
|
|
.hash {
|
|
|
|
return p.hash()
|
|
|
|
}
|
2020-02-11 10:26:46 +01:00
|
|
|
.key_defer {
|
|
|
|
p.next()
|
|
|
|
stmts := p.parse_block()
|
|
|
|
return ast.DeferStmt{
|
|
|
|
stmts: stmts
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 08:33:42 +01:00
|
|
|
.key_go {
|
|
|
|
p.next()
|
2020-04-03 15:18:17 +02:00
|
|
|
expr := p.expr(0)
|
2020-04-07 15:48:13 +02:00
|
|
|
// mut call_expr := &ast.CallExpr(0) // TODO
|
2020-04-20 08:44:14 +02:00
|
|
|
// { call_expr = it }
|
2020-03-16 08:33:42 +01:00
|
|
|
return ast.GoStmt{
|
2020-04-03 15:18:17 +02:00
|
|
|
call_expr: expr
|
2020-03-16 08:33:42 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-17 14:15:42 +01:00
|
|
|
.key_goto {
|
|
|
|
p.next()
|
|
|
|
name := p.check_name()
|
|
|
|
return ast.GotoStmt{
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 23:14:53 +02:00
|
|
|
.key_const {
|
2020-05-16 16:12:23 +02:00
|
|
|
p.error_with_pos('const can only be defined at the top level (outside of functions)',
|
|
|
|
p.tok.position())
|
2020-07-09 22:38:43 +02:00
|
|
|
return ast.Stmt{}
|
2020-05-15 23:14:53 +02:00
|
|
|
}
|
2020-07-04 23:37:41 +02:00
|
|
|
// literals, 'if', etc. in here
|
2019-12-28 14:11:05 +01:00
|
|
|
else {
|
2020-06-16 11:06:53 +02:00
|
|
|
return p.parse_multi_expr(is_top_level)
|
2019-12-28 14:11:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 19:13:22 +02:00
|
|
|
fn (mut p Parser) expr_list() ([]ast.Expr, []ast.Comment) {
|
2020-06-16 13:20:16 +02:00
|
|
|
mut exprs := []ast.Expr{}
|
2020-07-17 19:13:22 +02:00
|
|
|
mut comments := []ast.Comment{}
|
2020-06-16 13:20:16 +02:00
|
|
|
for {
|
2020-07-17 19:13:22 +02:00
|
|
|
expr := p.expr(0)
|
|
|
|
if expr is ast.Comment {
|
|
|
|
comments << expr
|
|
|
|
} else {
|
|
|
|
exprs << expr
|
|
|
|
if p.tok.kind != .comma {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.next()
|
2020-06-16 13:20:16 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-17 19:13:22 +02:00
|
|
|
return exprs, comments
|
2020-06-16 13:20:16 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 12:27:51 +02:00
|
|
|
// when is_top_stmt is true attrs are added to p.attrs
|
2020-08-04 20:10:22 +02:00
|
|
|
fn (mut p Parser) attributes() {
|
2020-02-03 07:02:54 +01:00
|
|
|
p.check(.lsbr)
|
2020-08-04 20:10:22 +02:00
|
|
|
mut has_ctdefine := false
|
2020-05-08 15:09:42 +02:00
|
|
|
for p.tok.kind != .rsbr {
|
2020-06-17 12:27:51 +02:00
|
|
|
start_pos := p.tok.position()
|
2020-05-08 15:09:42 +02:00
|
|
|
attr := p.parse_attr()
|
2020-08-04 20:10:22 +02:00
|
|
|
if p.attrs.contains(attr.name) {
|
2020-06-17 12:27:51 +02:00
|
|
|
p.error_with_pos('duplicate attribute `$attr.name`', start_pos.extend(p.prev_tok.position()))
|
|
|
|
}
|
2020-08-04 20:10:22 +02:00
|
|
|
if attr.is_ctdefine {
|
|
|
|
if has_ctdefine {
|
|
|
|
p.error_with_pos('only one `[if flag]` may be applied at a time `$attr.name`',
|
|
|
|
start_pos.extend(p.prev_tok.position()))
|
|
|
|
} else {
|
|
|
|
has_ctdefine = true
|
|
|
|
}
|
2020-06-17 12:27:51 +02:00
|
|
|
}
|
2020-08-04 20:10:22 +02:00
|
|
|
p.attrs << attr
|
2020-05-08 15:09:42 +02:00
|
|
|
if p.tok.kind != .semicolon {
|
|
|
|
if p.tok.kind == .rsbr {
|
|
|
|
p.next()
|
|
|
|
break
|
|
|
|
}
|
2020-08-04 20:10:22 +02:00
|
|
|
p.error('unexpected `$p.tok.kind.str()`, expecting `;`')
|
2020-05-08 15:09:42 +02:00
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
}
|
2020-08-04 20:10:22 +02:00
|
|
|
if p.attrs.len == 0 {
|
2020-06-17 12:27:51 +02:00
|
|
|
p.error_with_pos('attributes cannot be empty', p.prev_tok.position().extend(p.tok.position()))
|
|
|
|
}
|
2020-05-08 15:09:42 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 20:10:22 +02:00
|
|
|
fn (mut p Parser) parse_attr() table.Attr {
|
2020-08-08 16:24:05 +02:00
|
|
|
if p.tok.kind == .key_unsafe {
|
|
|
|
p.next()
|
|
|
|
return table.Attr{
|
|
|
|
name: 'unsafe'
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 20:10:22 +02:00
|
|
|
mut is_ctdefine := false
|
2020-02-18 17:29:47 +01:00
|
|
|
if p.tok.kind == .key_if {
|
|
|
|
p.next()
|
2020-08-04 20:10:22 +02:00
|
|
|
is_ctdefine = true
|
2020-02-18 17:29:47 +01:00
|
|
|
}
|
2020-07-03 15:10:39 +02:00
|
|
|
mut name := ''
|
2020-08-10 02:00:14 +02:00
|
|
|
mut arg := ''
|
2020-07-04 23:38:12 +02:00
|
|
|
is_string := p.tok.kind == .string
|
2020-08-10 02:00:14 +02:00
|
|
|
mut is_string_arg := false
|
2020-07-04 23:38:12 +02:00
|
|
|
if is_string {
|
2020-07-03 15:10:39 +02:00
|
|
|
name = p.tok.lit
|
2020-04-12 17:45:04 +02:00
|
|
|
p.next()
|
2020-07-03 15:10:39 +02:00
|
|
|
} else {
|
2020-08-09 11:22:11 +02:00
|
|
|
name = p.check_name()
|
2020-08-08 16:24:05 +02:00
|
|
|
if name == 'unsafe_fn' {
|
2020-08-09 11:22:11 +02:00
|
|
|
p.error_with_pos('please use `[unsafe]` instead', p.tok.position())
|
|
|
|
} else if name == 'trusted_fn' {
|
|
|
|
p.error_with_pos('please use `[trusted]` instead', p.tok.position())
|
2020-08-08 16:24:05 +02:00
|
|
|
}
|
2020-07-03 15:10:39 +02:00
|
|
|
if p.tok.kind == .colon {
|
2020-04-12 17:45:04 +02:00
|
|
|
p.next()
|
2020-08-10 02:00:14 +02:00
|
|
|
// `name: arg`
|
2020-07-03 15:10:39 +02:00
|
|
|
if p.tok.kind == .name {
|
2020-08-10 02:00:14 +02:00
|
|
|
arg = p.check_name()
|
|
|
|
} else if p.tok.kind == .string { // `name: 'arg'`
|
|
|
|
arg = p.tok.lit
|
|
|
|
is_string_arg = true
|
2020-07-03 15:10:39 +02:00
|
|
|
p.next()
|
|
|
|
}
|
2020-04-12 17:45:04 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-04 20:10:22 +02:00
|
|
|
return table.Attr{
|
2020-02-03 07:02:54 +01:00
|
|
|
name: name
|
2020-07-04 23:38:12 +02:00
|
|
|
is_string: is_string
|
2020-08-04 20:10:22 +02:00
|
|
|
is_ctdefine: is_ctdefine
|
2020-08-10 02:00:14 +02:00
|
|
|
arg: arg
|
|
|
|
is_string_arg: is_string_arg
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 23:19:50 +01:00
|
|
|
/*
|
2020-04-23 01:16:58 +02:00
|
|
|
fn (mut p Parser) range_expr(low ast.Expr) ast.Expr {
|
2020-02-02 14:31:54 +01:00
|
|
|
// ,table.Type) {
|
2020-02-05 10:00:11 +01:00
|
|
|
if p.tok.kind != .dotdot {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
p.check(.dotdot)
|
2020-02-07 07:34:18 +01:00
|
|
|
mut high := ast.Expr{}
|
|
|
|
if p.tok.kind != .rsbr {
|
2020-03-05 12:08:43 +01:00
|
|
|
high = p.expr(0)
|
2020-02-07 07:34:18 +01:00
|
|
|
// if typ.typ.kind != .int {
|
|
|
|
// p.error('non-integer index `$typ.typ.name`')
|
|
|
|
// }
|
2020-02-02 14:31:54 +01:00
|
|
|
}
|
|
|
|
node := ast.RangeExpr{
|
|
|
|
low: low
|
|
|
|
high: high
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
2020-02-10 23:19:50 +01:00
|
|
|
*/
|
2020-05-10 11:26:57 +02:00
|
|
|
pub fn (mut p Parser) error(s string) {
|
2020-04-10 21:00:54 +02:00
|
|
|
p.error_with_pos(s, p.tok.position())
|
|
|
|
}
|
|
|
|
|
2020-05-10 11:26:57 +02:00
|
|
|
pub fn (mut p Parser) warn(s string) {
|
2020-04-10 21:00:54 +02:00
|
|
|
p.warn_with_pos(s, p.tok.position())
|
|
|
|
}
|
|
|
|
|
2020-05-10 11:26:57 +02:00
|
|
|
pub fn (mut p Parser) error_with_pos(s string, pos token.Position) {
|
2020-04-21 05:11:50 +02:00
|
|
|
mut kind := 'error:'
|
2020-05-10 11:26:57 +02:00
|
|
|
if p.pref.output_mode == .stdout {
|
|
|
|
if p.pref.is_verbose {
|
|
|
|
print_backtrace()
|
|
|
|
kind = 'parser error:'
|
|
|
|
}
|
|
|
|
ferror := util.formatted_error(kind, s, p.file_name, pos)
|
|
|
|
eprintln(ferror)
|
|
|
|
exit(1)
|
|
|
|
} else {
|
|
|
|
p.errors << errors.Error{
|
2020-05-11 16:05:59 +02:00
|
|
|
file_path: p.file_name
|
|
|
|
pos: pos
|
|
|
|
reporter: .parser
|
2020-05-10 11:26:57 +02:00
|
|
|
message: s
|
|
|
|
}
|
2020-01-09 14:08:33 +01:00
|
|
|
}
|
2019-12-30 09:38:12 +01:00
|
|
|
}
|
|
|
|
|
2020-05-10 11:26:57 +02:00
|
|
|
pub fn (mut p Parser) warn_with_pos(s string, pos token.Position) {
|
2020-05-16 22:45:38 +02:00
|
|
|
if p.pref.skip_warnings {
|
|
|
|
return
|
|
|
|
}
|
2020-05-10 11:26:57 +02:00
|
|
|
if p.pref.output_mode == .stdout {
|
|
|
|
ferror := util.formatted_error('warning:', s, p.file_name, pos)
|
|
|
|
eprintln(ferror)
|
|
|
|
} else {
|
|
|
|
p.warnings << errors.Warning{
|
2020-05-11 16:05:59 +02:00
|
|
|
file_path: p.file_name
|
|
|
|
pos: pos
|
|
|
|
reporter: .parser
|
2020-05-10 11:26:57 +02:00
|
|
|
message: s
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 08:51:55 +01:00
|
|
|
}
|
|
|
|
|
2020-07-19 19:58:34 +02:00
|
|
|
pub fn (mut p Parser) vet_error(s string, line int) {
|
|
|
|
p.vet_errors << '$p.scanner.file_path:${line+1}: $s'
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:06:53 +02:00
|
|
|
fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt {
|
2020-05-15 23:14:53 +02:00
|
|
|
// in here might be 1) multi-expr 2) multi-assign
|
|
|
|
// 1, a, c ... } // multi-expression
|
|
|
|
// a, mut b ... :=/= // multi-assign
|
|
|
|
// collect things upto hard boundaries
|
2020-06-16 13:20:16 +02:00
|
|
|
tok := p.tok
|
2020-07-17 19:13:22 +02:00
|
|
|
left, left_comments := p.expr_list()
|
2020-06-16 13:20:16 +02:00
|
|
|
left0 := left[0]
|
2020-09-09 17:27:12 +02:00
|
|
|
if tok.kind == .key_mut && p.tok.kind != .decl_assign {
|
|
|
|
p.error('expecting `:=` (e.g. `mut x :=`)')
|
|
|
|
}
|
2020-06-16 13:20:16 +02:00
|
|
|
if p.tok.kind in [.assign, .decl_assign] || p.tok.kind.is_assign() {
|
2020-07-17 19:13:22 +02:00
|
|
|
return p.partial_assign_stmt(left, left_comments)
|
2020-09-16 15:34:57 +02:00
|
|
|
} else if is_top_level && tok.kind !in
|
|
|
|
[.key_if, .key_match, .key_lock, .key_rlock, .key_select] && left0 !is ast.CallExpr &&
|
|
|
|
left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr &&
|
2020-08-17 20:12:00 +02:00
|
|
|
(left0 as ast.InfixExpr).op in [.left_shift, .arrow]) && left0 !is ast.ComptimeCall {
|
2020-06-16 13:20:16 +02:00
|
|
|
p.error_with_pos('expression evaluated but not used', left0.position())
|
|
|
|
}
|
|
|
|
if left.len == 1 {
|
2020-05-21 22:35:43 +02:00
|
|
|
return ast.ExprStmt{
|
2020-06-16 13:20:16 +02:00
|
|
|
expr: left0
|
|
|
|
pos: tok.position()
|
2020-07-17 19:13:22 +02:00
|
|
|
comments: left_comments
|
2020-06-16 13:20:16 +02:00
|
|
|
is_expr: p.inside_for
|
2020-05-18 18:33:27 +02:00
|
|
|
}
|
2020-06-16 13:20:16 +02:00
|
|
|
}
|
|
|
|
return ast.ExprStmt{
|
|
|
|
expr: ast.ConcatExpr{
|
|
|
|
vals: left
|
2020-05-15 23:14:53 +02:00
|
|
|
}
|
2020-06-16 13:20:16 +02:00
|
|
|
pos: tok.position()
|
2020-07-17 19:13:22 +02:00
|
|
|
comments: left_comments
|
2020-05-15 23:14:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:12:47 +02:00
|
|
|
pub fn (mut p Parser) parse_ident(language table.Language) ast.Ident {
|
2020-02-06 17:38:02 +01:00
|
|
|
// p.warn('name ')
|
2020-07-07 01:57:31 +02:00
|
|
|
is_shared := p.tok.kind == .key_shared
|
|
|
|
is_atomic := p.tok.kind == .key_atomic
|
|
|
|
is_mut := p.tok.kind == .key_mut || is_shared || is_atomic
|
2020-05-15 23:14:53 +02:00
|
|
|
if is_mut {
|
|
|
|
p.next()
|
2020-02-27 11:12:30 +01:00
|
|
|
}
|
2020-05-15 23:14:53 +02:00
|
|
|
is_static := p.tok.kind == .key_static
|
|
|
|
if is_static {
|
|
|
|
p.next()
|
2020-02-28 13:29:04 +01:00
|
|
|
}
|
2020-05-15 23:14:53 +02:00
|
|
|
if p.tok.kind == .name {
|
|
|
|
pos := p.tok.position()
|
|
|
|
mut name := p.check_name()
|
|
|
|
if name == '_' {
|
|
|
|
return ast.Ident{
|
2020-07-21 18:49:53 +02:00
|
|
|
tok_kind: p.tok.kind
|
2020-05-15 23:14:53 +02:00
|
|
|
name: '_'
|
|
|
|
kind: .blank_ident
|
|
|
|
pos: pos
|
2020-05-16 16:12:23 +02:00
|
|
|
info: ast.IdentVar{
|
2020-05-15 23:14:53 +02:00
|
|
|
is_mut: false
|
|
|
|
is_static: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 11:46:08 +02:00
|
|
|
if p.inside_match_body && name == 'it' {
|
|
|
|
// p.warn('it')
|
|
|
|
}
|
2020-05-15 23:14:53 +02:00
|
|
|
if p.expr_mod.len > 0 {
|
|
|
|
name = '${p.expr_mod}.$name'
|
|
|
|
}
|
2020-06-08 00:47:04 +02:00
|
|
|
return ast.Ident{
|
2020-07-21 18:49:53 +02:00
|
|
|
tok_kind: p.tok.kind
|
2020-05-15 23:14:53 +02:00
|
|
|
kind: .unresolved
|
|
|
|
name: name
|
2020-05-19 17:12:47 +02:00
|
|
|
language: language
|
2020-05-15 23:14:53 +02:00
|
|
|
mod: p.mod
|
|
|
|
pos: pos
|
|
|
|
is_mut: is_mut
|
2020-06-08 00:47:04 +02:00
|
|
|
info: ast.IdentVar{
|
|
|
|
is_mut: is_mut
|
|
|
|
is_static: is_static
|
2020-07-07 01:57:31 +02:00
|
|
|
share: table.sharetype_from_flags(is_shared, is_atomic)
|
2020-06-08 00:47:04 +02:00
|
|
|
}
|
2020-05-15 23:14:53 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.error('unexpected token `$p.tok.lit`')
|
2020-02-06 17:38:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
pub fn (mut p Parser) name_expr() ast.Expr {
|
2020-09-20 03:50:09 +02:00
|
|
|
prev_tok_kind := p.prev_tok.kind
|
2020-04-21 05:11:50 +02:00
|
|
|
mut node := ast.Expr{}
|
2020-05-05 14:19:31 +02:00
|
|
|
if p.expecting_type {
|
|
|
|
p.expecting_type = false
|
2020-04-18 00:19:33 +02:00
|
|
|
// get type position before moving to next
|
|
|
|
type_pos := p.tok.position()
|
2020-05-04 16:46:36 +02:00
|
|
|
typ := p.parse_type()
|
2020-04-16 15:32:11 +02:00
|
|
|
return ast.Type{
|
2020-05-04 16:46:36 +02:00
|
|
|
typ: typ
|
2020-04-18 00:19:33 +02:00
|
|
|
pos: type_pos
|
2020-04-16 15:32:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 17:12:47 +02:00
|
|
|
language := if p.tok.lit == 'C' {
|
|
|
|
table.Language.c
|
|
|
|
} else if p.tok.lit == 'JS' {
|
|
|
|
table.Language.js
|
|
|
|
} else {
|
|
|
|
table.Language.v
|
|
|
|
}
|
2020-04-21 05:11:50 +02:00
|
|
|
mut mod := ''
|
2020-02-27 00:12:37 +01:00
|
|
|
// p.warn('resetting')
|
|
|
|
p.expr_mod = ''
|
|
|
|
// `map[string]int` initialization
|
|
|
|
if p.tok.lit == 'map' && p.peek_tok.kind == .lsbr {
|
|
|
|
map_type := p.parse_map_type()
|
2020-05-04 17:26:28 +02:00
|
|
|
if p.tok.kind == .lcbr && p.peek_tok.kind == .rcbr {
|
|
|
|
p.next()
|
|
|
|
p.next()
|
|
|
|
}
|
2020-03-07 16:23:10 +01:00
|
|
|
return ast.MapInit{
|
2020-03-02 12:34:02 +01:00
|
|
|
typ: map_type
|
|
|
|
}
|
2020-02-27 00:12:37 +01:00
|
|
|
}
|
2020-08-14 21:18:42 +02:00
|
|
|
// `chan typ{...}`
|
|
|
|
if p.tok.lit == 'chan' {
|
|
|
|
first_pos := p.tok.position()
|
2020-08-19 02:37:17 +02:00
|
|
|
mut last_pos := first_pos
|
2020-08-14 21:18:42 +02:00
|
|
|
chan_type := p.parse_chan_type()
|
|
|
|
mut has_cap := false
|
|
|
|
mut cap_expr := ast.Expr{}
|
|
|
|
p.check(.lcbr)
|
|
|
|
if p.tok.kind == .rcbr {
|
|
|
|
last_pos = p.tok.position()
|
|
|
|
p.next()
|
|
|
|
} else {
|
|
|
|
key := p.check_name()
|
|
|
|
p.check(.colon)
|
|
|
|
match key {
|
|
|
|
'cap' {
|
|
|
|
has_cap = true
|
|
|
|
cap_expr = p.expr(0)
|
|
|
|
}
|
|
|
|
'len', 'init' {
|
|
|
|
p.error('`$key` cannot be initialized for `chan`. Did you mean `cap`?')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('wrong field `$key`, expecting `cap`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_pos = p.tok.position()
|
|
|
|
p.check(.rcbr)
|
|
|
|
}
|
|
|
|
return ast.ChanInit{
|
2020-08-19 02:37:17 +02:00
|
|
|
pos: first_pos.extend(last_pos)
|
2020-08-14 21:18:42 +02:00
|
|
|
has_cap: has_cap
|
|
|
|
cap_expr: cap_expr
|
|
|
|
typ: chan_type
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 17:07:27 +01:00
|
|
|
// Raw string (`s := r'hello \n ')
|
2020-06-05 22:37:34 +02:00
|
|
|
if p.tok.lit in ['r', 'c', 'js'] && p.peek_tok.kind == .string && !p.inside_str_interp {
|
2020-03-24 17:07:27 +01:00
|
|
|
return p.string_expr()
|
|
|
|
}
|
2020-07-03 15:10:39 +02:00
|
|
|
known_var := p.mark_var_as_used(p.tok.lit)
|
2020-06-29 17:15:40 +02:00
|
|
|
mut is_mod_cast := false
|
2020-06-24 14:35:00 +02:00
|
|
|
if p.peek_tok.kind == .dot && !known_var &&
|
2020-07-03 15:10:39 +02:00
|
|
|
(language != .v || p.known_import(p.tok.lit) || p.mod.all_after_last('.') == p.tok.lit) {
|
2020-07-22 19:33:43 +02:00
|
|
|
// p.tok.lit has been recognized as a module
|
2020-05-19 17:12:47 +02:00
|
|
|
if language == .c {
|
2020-02-29 11:47:47 +01:00
|
|
|
mod = 'C'
|
2020-05-19 17:12:47 +02:00
|
|
|
} else if language == .js {
|
2020-04-15 23:16:49 +02:00
|
|
|
mod = 'JS'
|
2020-04-07 15:48:13 +02:00
|
|
|
} else {
|
2020-05-14 17:14:24 +02:00
|
|
|
if p.tok.lit in p.imports {
|
2020-07-22 19:33:43 +02:00
|
|
|
// mark the imported module as used
|
2020-05-14 17:14:24 +02:00
|
|
|
p.register_used_import(p.tok.lit)
|
2020-06-29 17:15:40 +02:00
|
|
|
if p.peek_tok.kind == .dot && p.peek_tok2.lit[0].is_capital() {
|
|
|
|
is_mod_cast = true
|
|
|
|
}
|
2020-05-14 17:14:24 +02:00
|
|
|
}
|
2020-02-20 11:13:18 +01:00
|
|
|
// prepend the full import
|
|
|
|
mod = p.imports[p.tok.lit]
|
2020-02-19 07:16:38 +01:00
|
|
|
}
|
2020-01-07 16:06:37 +01:00
|
|
|
p.next()
|
|
|
|
p.check(.dot)
|
2020-02-27 00:12:37 +01:00
|
|
|
p.expr_mod = mod
|
2020-02-05 10:00:11 +01:00
|
|
|
}
|
2020-06-29 20:09:09 +02:00
|
|
|
lit0_is_capital := p.tok.lit[0].is_capital()
|
2020-02-18 17:29:47 +01:00
|
|
|
// p.warn('name expr $p.tok.lit $p.peek_tok.str()')
|
2020-08-19 02:37:17 +02:00
|
|
|
same_line := p.tok.line_nr == p.peek_tok.line_nr
|
|
|
|
// `(` must be on same line as name token otherwise it's a ParExpr
|
|
|
|
if !same_line && p.peek_tok.kind == .lpar {
|
|
|
|
node = p.parse_ident(language)
|
|
|
|
} else if p.peek_tok.kind == .lpar ||
|
2020-07-03 15:10:39 +02:00
|
|
|
(p.peek_tok.kind == .lt && !lit0_is_capital && p.peek_tok2.kind == .name && p.peek_tok3.kind == .gt) {
|
2020-08-19 02:37:17 +02:00
|
|
|
// foo(), foo<int>() or type() cast
|
2020-04-21 05:11:50 +02:00
|
|
|
mut name := p.tok.lit
|
2020-02-29 11:47:47 +01:00
|
|
|
if mod.len > 0 {
|
|
|
|
name = '${mod}.$name'
|
|
|
|
}
|
2020-03-01 16:14:52 +01:00
|
|
|
name_w_mod := p.prepend_mod(name)
|
2020-02-07 21:29:28 +01:00
|
|
|
// type cast. TODO: finish
|
2020-02-10 20:33:34 +01:00
|
|
|
// if name in table.builtin_type_names {
|
2020-07-03 15:10:39 +02:00
|
|
|
if (!known_var && (name in p.table.type_idxs ||
|
2020-07-23 22:20:00 +02:00
|
|
|
name_w_mod in p.table.type_idxs) && name !in ['C.stat', 'C.sigaction']) ||
|
|
|
|
is_mod_cast {
|
2020-02-11 10:26:46 +01:00
|
|
|
// TODO handle C.stat()
|
2020-04-21 05:11:50 +02:00
|
|
|
mut to_typ := p.parse_type()
|
2020-03-10 23:21:26 +01:00
|
|
|
if p.is_amp {
|
|
|
|
// Handle `&Foo(0)`
|
2020-04-25 09:08:53 +02:00
|
|
|
to_typ = to_typ.to_ptr()
|
2020-03-10 23:21:26 +01:00
|
|
|
}
|
2020-09-18 01:04:56 +02:00
|
|
|
// this prevents inner casts to also have an `&`
|
|
|
|
// example: &Foo(malloc(int(num)))
|
|
|
|
// without the next line int would result in int*
|
|
|
|
p.is_amp = false
|
2020-02-10 14:43:17 +01:00
|
|
|
p.check(.lpar)
|
2020-04-21 05:11:50 +02:00
|
|
|
mut expr := ast.Expr{}
|
|
|
|
mut arg := ast.Expr{}
|
|
|
|
mut has_arg := false
|
2020-03-05 12:08:43 +01:00
|
|
|
expr = p.expr(0)
|
2020-02-10 20:33:34 +01:00
|
|
|
// TODO, string(b, len)
|
2020-04-25 09:08:53 +02:00
|
|
|
if p.tok.kind == .comma && to_typ.idx() == table.string_type_idx {
|
2020-05-07 06:51:36 +02:00
|
|
|
p.next()
|
2020-04-20 08:30:42 +02:00
|
|
|
arg = p.expr(0) // len
|
2020-03-07 16:23:10 +01:00
|
|
|
has_arg = true
|
2020-02-07 21:29:28 +01:00
|
|
|
}
|
2020-02-10 14:43:17 +01:00
|
|
|
p.check(.rpar)
|
|
|
|
node = ast.CastExpr{
|
|
|
|
typ: to_typ
|
|
|
|
expr: expr
|
2020-03-07 16:23:10 +01:00
|
|
|
arg: arg
|
|
|
|
has_arg: has_arg
|
2020-05-06 12:05:24 +02:00
|
|
|
pos: expr.position()
|
2020-02-10 14:43:17 +01:00
|
|
|
}
|
2020-02-27 00:12:37 +01:00
|
|
|
p.expr_mod = ''
|
2020-02-18 08:58:20 +01:00
|
|
|
return node
|
2020-04-07 15:48:13 +02:00
|
|
|
} else {
|
2020-04-05 04:05:09 +02:00
|
|
|
// fn call
|
2020-02-18 08:58:20 +01:00
|
|
|
// println('calling $p.tok.lit')
|
2020-05-19 17:12:47 +02:00
|
|
|
node = p.call_expr(language, mod)
|
2020-02-07 21:29:28 +01:00
|
|
|
}
|
2020-07-03 15:10:39 +02:00
|
|
|
} else if (p.peek_tok.kind == .lcbr ||
|
2020-07-23 22:20:00 +02:00
|
|
|
(p.peek_tok.kind == .lt && lit0_is_capital)) &&
|
2020-09-20 03:50:09 +02:00
|
|
|
(!p.inside_match || (p.inside_select && prev_tok_kind == .arrow && lit0_is_capital)) && !p.inside_match_case &&
|
2020-09-21 07:34:04 +02:00
|
|
|
(!p.inside_if || p.inside_select) &&
|
|
|
|
(!p.inside_for || p.inside_select) { // && (p.tok.lit[0].is_capital() || p.builtin_mod) {
|
2020-04-20 08:30:42 +02:00
|
|
|
return p.struct_init(false) // short_syntax: false
|
2020-06-29 20:09:09 +02:00
|
|
|
} else if p.peek_tok.kind == .dot && (lit0_is_capital && !known_var && language == .v) {
|
2020-02-27 00:12:37 +01:00
|
|
|
// `Color.green`
|
2020-04-21 05:11:50 +02:00
|
|
|
mut enum_name := p.check_name()
|
2020-02-27 00:12:37 +01:00
|
|
|
if mod != '' {
|
|
|
|
enum_name = mod + '.' + enum_name
|
2020-04-07 15:48:13 +02:00
|
|
|
} else {
|
2020-02-27 00:12:37 +01:00
|
|
|
enum_name = p.prepend_mod(enum_name)
|
|
|
|
}
|
|
|
|
// p.warn('Color.green $enum_name ' + p.prepend_mod(enum_name) + 'mod=$mod')
|
2020-02-25 15:02:34 +01:00
|
|
|
p.check(.dot)
|
|
|
|
val := p.check_name()
|
|
|
|
// println('enum val $enum_name . $val')
|
2020-02-27 00:12:37 +01:00
|
|
|
p.expr_mod = ''
|
2020-02-25 15:02:34 +01:00
|
|
|
return ast.EnumVal{
|
2020-04-07 15:48:13 +02:00
|
|
|
enum_name: enum_name
|
2020-02-25 15:02:34 +01:00
|
|
|
val: val
|
2020-02-27 00:12:37 +01:00
|
|
|
pos: p.tok.position()
|
2020-03-15 00:46:08 +01:00
|
|
|
mod: mod
|
2020-02-25 15:02:34 +01:00
|
|
|
}
|
2020-05-05 02:12:40 +02:00
|
|
|
} else if p.peek_tok.kind == .colon && p.prev_tok.kind != .str_dollar {
|
|
|
|
// `foo(key:val, key2:val2)`
|
|
|
|
return p.struct_init(true) // short_syntax:true
|
2020-05-16 16:12:23 +02:00
|
|
|
// JS. function call with more than 1 dot
|
2020-05-19 17:12:47 +02:00
|
|
|
} else if language == .js && p.peek_tok.kind == .dot && p.peek_tok2.kind == .name {
|
|
|
|
node = p.call_expr(language, mod)
|
2020-04-07 15:48:13 +02:00
|
|
|
} else {
|
2020-05-19 17:12:47 +02:00
|
|
|
node = p.parse_ident(language)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-02-27 00:12:37 +01:00
|
|
|
p.expr_mod = ''
|
2020-02-18 08:58:20 +01:00
|
|
|
return node
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) index_expr(left ast.Expr) ast.IndexExpr {
|
2020-02-10 23:19:50 +01:00
|
|
|
// left == `a` in `a[0]`
|
2020-07-20 18:52:03 +02:00
|
|
|
start_pos := p.tok.position()
|
2020-04-20 08:30:42 +02:00
|
|
|
p.next() // [
|
2020-04-21 05:11:50 +02:00
|
|
|
mut has_low := true
|
2020-02-10 23:19:50 +01:00
|
|
|
if p.tok.kind == .dotdot {
|
2020-03-06 22:24:39 +01:00
|
|
|
has_low = false
|
2020-02-10 23:19:50 +01:00
|
|
|
// [..end]
|
|
|
|
p.next()
|
2020-03-05 12:08:43 +01:00
|
|
|
high := p.expr(0)
|
2020-07-20 18:52:03 +02:00
|
|
|
pos := start_pos.extend(p.tok.position())
|
2020-02-10 23:19:50 +01:00
|
|
|
p.check(.rsbr)
|
|
|
|
return ast.IndexExpr{
|
|
|
|
left: left
|
2020-07-20 18:52:03 +02:00
|
|
|
pos: pos
|
2020-02-10 23:19:50 +01:00
|
|
|
index: ast.RangeExpr{
|
2020-04-08 00:59:28 +02:00
|
|
|
low: ast.Expr{}
|
|
|
|
high: high
|
|
|
|
has_high: true
|
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
}
|
2020-02-02 14:31:54 +01:00
|
|
|
}
|
2020-07-20 18:52:03 +02:00
|
|
|
expr := p.expr(0) // `[expr]` or `[expr..`
|
2020-04-21 05:11:50 +02:00
|
|
|
mut has_high := false
|
2020-02-10 23:19:50 +01:00
|
|
|
if p.tok.kind == .dotdot {
|
|
|
|
// [start..end] or [start..]
|
2020-05-07 06:51:36 +02:00
|
|
|
p.next()
|
2020-04-21 05:11:50 +02:00
|
|
|
mut high := ast.Expr{}
|
2020-02-10 23:19:50 +01:00
|
|
|
if p.tok.kind != .rsbr {
|
2020-03-06 22:24:39 +01:00
|
|
|
has_high = true
|
2020-03-05 12:08:43 +01:00
|
|
|
high = p.expr(0)
|
2020-02-10 23:19:50 +01:00
|
|
|
}
|
2020-07-20 18:52:03 +02:00
|
|
|
pos := start_pos.extend(p.tok.position())
|
2020-02-10 23:19:50 +01:00
|
|
|
p.check(.rsbr)
|
|
|
|
return ast.IndexExpr{
|
|
|
|
left: left
|
2020-07-20 18:52:03 +02:00
|
|
|
pos: pos
|
2020-02-10 23:19:50 +01:00
|
|
|
index: ast.RangeExpr{
|
2020-04-08 00:59:28 +02:00
|
|
|
low: expr
|
|
|
|
high: high
|
|
|
|
has_high: has_high
|
|
|
|
has_low: has_low
|
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
}
|
2020-02-11 12:59:40 +01:00
|
|
|
}
|
2020-02-10 23:19:50 +01:00
|
|
|
// [expr]
|
2020-07-20 18:52:03 +02:00
|
|
|
pos := start_pos.extend(p.tok.position())
|
2020-01-07 12:14:10 +01:00
|
|
|
p.check(.rsbr)
|
2020-02-10 23:19:50 +01:00
|
|
|
return ast.IndexExpr{
|
2020-01-07 12:14:10 +01:00
|
|
|
left: left
|
2020-02-10 23:19:50 +01:00
|
|
|
index: expr
|
2020-07-20 18:52:03 +02:00
|
|
|
pos: pos
|
2020-02-15 13:37:48 +01:00
|
|
|
}
|
2020-01-07 12:14:10 +01:00
|
|
|
}
|
|
|
|
|
2020-05-02 15:26:58 +02:00
|
|
|
fn (mut p Parser) scope_register_it() {
|
2020-05-02 15:46:53 +02:00
|
|
|
p.scope.register('it', ast.Var{
|
2020-02-15 13:37:48 +01:00
|
|
|
name: 'it'
|
2020-04-27 15:16:31 +02:00
|
|
|
pos: p.tok.position()
|
|
|
|
is_used: true
|
2020-02-15 13:37:48 +01:00
|
|
|
})
|
2020-02-10 14:42:57 +01:00
|
|
|
}
|
|
|
|
|
2020-08-12 05:54:51 +02:00
|
|
|
fn (mut p Parser) scope_register_ab() {
|
|
|
|
p.scope.register('a', ast.Var{
|
|
|
|
name: 'a'
|
|
|
|
pos: p.tok.position()
|
|
|
|
is_used: true
|
|
|
|
})
|
|
|
|
p.scope.register('b', ast.Var{
|
|
|
|
name: 'b'
|
|
|
|
pos: p.tok.position()
|
|
|
|
is_used: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
|
2020-01-06 16:13:12 +01:00
|
|
|
p.next()
|
2020-05-27 03:20:22 +02:00
|
|
|
if p.tok.kind == .dollar {
|
|
|
|
return p.comptime_method_call(left)
|
|
|
|
}
|
2020-04-21 05:11:50 +02:00
|
|
|
mut name_pos := p.tok.position()
|
2020-01-06 16:13:12 +01:00
|
|
|
field_name := p.check_name()
|
2020-03-25 10:09:50 +01:00
|
|
|
is_filter := field_name in ['filter', 'map']
|
|
|
|
if is_filter {
|
2020-02-15 13:37:48 +01:00
|
|
|
p.open_scope()
|
2020-04-12 14:19:07 +02:00
|
|
|
name_pos = p.tok.position()
|
2020-05-02 15:26:58 +02:00
|
|
|
p.scope_register_it()
|
2020-03-06 10:52:03 +01:00
|
|
|
// wrong tok position when using defer
|
|
|
|
// defer {
|
2020-03-06 14:14:33 +01:00
|
|
|
// p.close_scope()
|
2020-03-06 10:52:03 +01:00
|
|
|
// }
|
2020-08-12 05:54:51 +02:00
|
|
|
} else if field_name == 'sort' {
|
|
|
|
p.open_scope()
|
|
|
|
name_pos = p.tok.position()
|
|
|
|
p.scope_register_ab()
|
2020-02-10 14:42:57 +01:00
|
|
|
}
|
2020-06-06 12:43:35 +02:00
|
|
|
// ! in mutable methods
|
|
|
|
if p.tok.kind == .not && p.peek_tok.kind == .lpar {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-03-18 09:56:19 +01:00
|
|
|
// Method call
|
2020-06-06 12:28:03 +02:00
|
|
|
// TODO move to fn.v call_expr()
|
2020-01-07 01:08:24 +01:00
|
|
|
if p.tok.kind == .lpar {
|
|
|
|
p.next()
|
2020-03-14 11:11:43 +01:00
|
|
|
args := p.call_args()
|
2020-05-02 15:26:58 +02:00
|
|
|
if is_filter && args.len != 1 {
|
|
|
|
p.error('needs exactly 1 argument')
|
|
|
|
}
|
2020-04-10 14:53:06 +02:00
|
|
|
p.check(.rpar)
|
2020-04-26 09:17:13 +02:00
|
|
|
mut or_stmts := []ast.Stmt{}
|
2020-05-23 08:51:15 +02:00
|
|
|
mut or_kind := ast.OrKind.absent
|
2020-02-04 17:44:39 +01:00
|
|
|
if p.tok.kind == .key_orelse {
|
|
|
|
p.next()
|
2020-03-18 09:56:19 +01:00
|
|
|
p.open_scope()
|
2020-04-04 20:47:57 +02:00
|
|
|
p.scope.register('errcode', ast.Var{
|
|
|
|
name: 'errcode'
|
|
|
|
typ: table.int_type
|
2020-04-27 15:16:31 +02:00
|
|
|
pos: p.tok.position()
|
|
|
|
is_used: true
|
2020-04-04 20:47:57 +02:00
|
|
|
})
|
2020-04-04 05:14:40 +02:00
|
|
|
p.scope.register('err', ast.Var{
|
2020-03-18 09:56:19 +01:00
|
|
|
name: 'err'
|
2020-03-18 10:00:33 +01:00
|
|
|
typ: table.string_type
|
2020-04-27 15:16:31 +02:00
|
|
|
pos: p.tok.position()
|
|
|
|
is_used: true
|
2020-03-18 09:56:19 +01:00
|
|
|
})
|
2020-05-23 08:51:15 +02:00
|
|
|
or_kind = .block
|
2020-06-06 17:47:16 +02:00
|
|
|
or_stmts = p.parse_block_no_scope(false)
|
2020-03-18 09:56:19 +01:00
|
|
|
p.close_scope()
|
2020-02-04 17:44:39 +01:00
|
|
|
}
|
2020-06-06 12:43:35 +02:00
|
|
|
// `foo()?`
|
2020-05-23 08:51:15 +02:00
|
|
|
if p.tok.kind == .question {
|
|
|
|
p.next()
|
|
|
|
or_kind = .propagate
|
|
|
|
}
|
2020-06-06 12:43:35 +02:00
|
|
|
//
|
2020-04-12 14:19:07 +02:00
|
|
|
end_pos := p.tok.position()
|
|
|
|
pos := token.Position{
|
|
|
|
line_nr: name_pos.line_nr
|
|
|
|
pos: name_pos.pos
|
|
|
|
len: end_pos.pos - name_pos.pos
|
|
|
|
}
|
2020-03-30 12:39:20 +02:00
|
|
|
mcall_expr := ast.CallExpr{
|
|
|
|
left: left
|
2020-01-07 01:08:24 +01:00
|
|
|
name: field_name
|
|
|
|
args: args
|
2020-03-06 10:52:03 +01:00
|
|
|
pos: pos
|
2020-03-30 12:39:20 +02:00
|
|
|
is_method: true
|
2020-02-28 15:36:41 +01:00
|
|
|
or_block: ast.OrExpr{
|
2020-04-07 16:36:00 +02:00
|
|
|
stmts: or_stmts
|
2020-05-23 08:51:15 +02:00
|
|
|
kind: or_kind
|
2020-05-23 10:43:20 +02:00
|
|
|
pos: pos
|
2020-04-07 16:36:00 +02:00
|
|
|
}
|
2020-01-07 01:08:24 +01:00
|
|
|
}
|
2020-08-12 05:54:51 +02:00
|
|
|
if is_filter || field_name == 'sort' {
|
2020-03-06 10:52:03 +01:00
|
|
|
p.close_scope()
|
|
|
|
}
|
2020-05-21 22:35:43 +02:00
|
|
|
return mcall_expr
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
sel_expr := ast.SelectorExpr{
|
2020-01-06 16:13:12 +01:00
|
|
|
expr: left
|
2020-05-09 15:16:48 +02:00
|
|
|
field_name: field_name
|
2020-04-12 14:19:07 +02:00
|
|
|
pos: name_pos
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
2020-04-21 05:11:50 +02:00
|
|
|
mut node := ast.Expr{}
|
2020-01-18 23:26:14 +01:00
|
|
|
node = sel_expr
|
2020-03-25 10:09:50 +01:00
|
|
|
if is_filter {
|
2020-03-06 10:52:03 +01:00
|
|
|
p.close_scope()
|
|
|
|
}
|
2020-02-15 13:37:48 +01:00
|
|
|
return node
|
2019-12-24 18:54:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:33:34 +01:00
|
|
|
// `.green`
|
2020-02-20 15:42:56 +01:00
|
|
|
// `pref.BuildMode.default_mode`
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) enum_val() ast.EnumVal {
|
2020-02-10 20:33:34 +01:00
|
|
|
p.check(.dot)
|
2020-02-25 15:02:34 +01:00
|
|
|
val := p.check_name()
|
2020-02-26 15:51:05 +01:00
|
|
|
return ast.EnumVal{
|
2020-02-25 15:02:34 +01:00
|
|
|
val: val
|
2020-02-27 00:12:37 +01:00
|
|
|
pos: p.tok.position()
|
2020-02-10 20:33:34 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-03 11:36:17 +01:00
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) string_expr() ast.Expr {
|
2020-03-24 17:07:27 +01:00
|
|
|
is_raw := p.tok.kind == .name && p.tok.lit == 'r'
|
|
|
|
is_cstr := p.tok.kind == .name && p.tok.lit == 'c'
|
|
|
|
if is_raw || is_cstr {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-04-21 05:11:50 +02:00
|
|
|
mut node := ast.Expr{}
|
2020-03-21 07:01:06 +01:00
|
|
|
val := p.tok.lit
|
2020-04-10 14:40:52 +02:00
|
|
|
pos := p.tok.position()
|
2020-01-07 01:08:24 +01:00
|
|
|
if p.peek_tok.kind != .str_dollar {
|
|
|
|
p.next()
|
2020-04-10 10:59:07 +02:00
|
|
|
node = ast.StringLiteral{
|
|
|
|
val: val
|
|
|
|
is_raw: is_raw
|
2020-06-18 20:21:08 +02:00
|
|
|
language: if is_cstr { table.Language.c } else { table.Language.v }
|
2020-04-10 10:59:07 +02:00
|
|
|
pos: pos
|
|
|
|
}
|
2020-03-05 12:08:43 +01:00
|
|
|
return node
|
2020-01-07 01:08:24 +01:00
|
|
|
}
|
2020-04-26 09:17:13 +02:00
|
|
|
mut exprs := []ast.Expr{}
|
|
|
|
mut vals := []string{}
|
2020-06-16 10:41:51 +02:00
|
|
|
mut has_fmts := []bool{}
|
|
|
|
mut fwidths := []int{}
|
|
|
|
mut precisions := []int{}
|
|
|
|
mut visible_pluss := []bool{}
|
|
|
|
mut fills := []bool{}
|
|
|
|
mut fmts := []byte{}
|
|
|
|
mut fposs := []token.Position{}
|
2020-01-07 01:08:24 +01:00
|
|
|
// Handle $ interpolation
|
2020-06-05 22:37:34 +02:00
|
|
|
p.inside_str_interp = true
|
2020-03-16 03:19:26 +01:00
|
|
|
for p.tok.kind == .string {
|
2020-03-21 07:01:06 +01:00
|
|
|
vals << p.tok.lit
|
2020-01-07 01:08:24 +01:00
|
|
|
p.next()
|
|
|
|
if p.tok.kind != .str_dollar {
|
2020-06-13 22:38:10 +02:00
|
|
|
break
|
2020-01-07 01:08:24 +01:00
|
|
|
}
|
2020-05-07 06:51:36 +02:00
|
|
|
p.next()
|
2020-03-21 07:01:06 +01:00
|
|
|
exprs << p.expr(0)
|
2020-06-16 10:41:51 +02:00
|
|
|
mut has_fmt := false
|
|
|
|
mut fwidth := 0
|
|
|
|
mut fwidthneg := false
|
|
|
|
mut precision := 0
|
|
|
|
mut visible_plus := false
|
|
|
|
mut fill := false
|
|
|
|
mut fmt := `_` // placeholder
|
2020-02-10 20:33:34 +01:00
|
|
|
if p.tok.kind == .colon {
|
2020-02-04 09:54:15 +01:00
|
|
|
p.next()
|
2020-04-11 17:25:39 +02:00
|
|
|
// ${num:-2d}
|
|
|
|
if p.tok.kind == .minus {
|
2020-06-16 10:41:51 +02:00
|
|
|
fwidthneg = true
|
|
|
|
p.next()
|
|
|
|
} else if p.tok.kind == .plus {
|
|
|
|
visible_plus = true
|
2020-04-11 17:25:39 +02:00
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
// ${num:2d}
|
|
|
|
if p.tok.kind == .number {
|
2020-06-16 10:41:51 +02:00
|
|
|
fields := p.tok.lit.split('.')
|
|
|
|
if fields[0].len > 0 && fields[0][0] == `0` {
|
|
|
|
fill = true
|
|
|
|
}
|
2020-07-11 18:53:50 +02:00
|
|
|
fwidth = fields[0].int()
|
2020-06-16 10:41:51 +02:00
|
|
|
if fwidthneg {
|
|
|
|
fwidth = -fwidth
|
|
|
|
}
|
|
|
|
if fields.len > 1 {
|
2020-07-11 18:53:50 +02:00
|
|
|
precision = fields[1].int()
|
2020-06-16 10:41:51 +02:00
|
|
|
}
|
2020-04-11 17:25:39 +02:00
|
|
|
p.next()
|
|
|
|
}
|
2020-06-16 10:41:51 +02:00
|
|
|
if p.tok.kind == .name {
|
|
|
|
if p.tok.lit.len == 1 {
|
|
|
|
fmt = p.tok.lit[0]
|
2020-06-17 19:49:13 +02:00
|
|
|
has_fmt = true
|
2020-06-16 10:41:51 +02:00
|
|
|
p.next()
|
|
|
|
} else {
|
|
|
|
p.error('format specifier may only be one letter')
|
|
|
|
}
|
2020-02-11 10:26:46 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-16 10:41:51 +02:00
|
|
|
fwidths << fwidth
|
|
|
|
has_fmts << has_fmt
|
|
|
|
precisions << precision
|
|
|
|
visible_pluss << visible_plus
|
|
|
|
fmts << fmt
|
|
|
|
fills << fill
|
|
|
|
fposs << p.prev_tok.position()
|
2020-01-07 01:08:24 +01:00
|
|
|
}
|
2020-03-21 07:01:06 +01:00
|
|
|
node = ast.StringInterLiteral{
|
|
|
|
vals: vals
|
|
|
|
exprs: exprs
|
2020-06-18 20:21:08 +02:00
|
|
|
need_fmts: has_fmts
|
2020-06-16 10:41:51 +02:00
|
|
|
fwidths: fwidths
|
|
|
|
precisions: precisions
|
|
|
|
pluss: visible_pluss
|
|
|
|
fills: fills
|
|
|
|
fmts: fmts
|
|
|
|
fmt_poss: fposs
|
2020-04-10 10:59:07 +02:00
|
|
|
pos: pos
|
2020-03-21 07:01:06 +01:00
|
|
|
}
|
2020-06-18 20:21:08 +02:00
|
|
|
// need_fmts: prelimery - until checker finds out if really needed
|
2020-06-05 22:37:34 +02:00
|
|
|
p.inside_str_interp = false
|
2020-03-05 12:08:43 +01:00
|
|
|
return node
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) parse_number_literal() ast.Expr {
|
2019-12-28 09:15:32 +01:00
|
|
|
lit := p.tok.lit
|
2020-04-10 00:09:34 +02:00
|
|
|
pos := p.tok.position()
|
2020-04-21 05:11:50 +02:00
|
|
|
mut node := ast.Expr{}
|
2020-04-17 20:31:32 +02:00
|
|
|
if lit.index_any('.eE') >= 0 && lit[..2] !in ['0x', '0X', '0o', '0O', '0b', '0B'] {
|
2019-12-28 09:15:32 +01:00
|
|
|
node = ast.FloatLiteral{
|
|
|
|
val: lit
|
2020-04-20 14:49:26 +02:00
|
|
|
pos: pos
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
2020-04-07 15:48:13 +02:00
|
|
|
} else {
|
2019-12-28 09:15:32 +01:00
|
|
|
node = ast.IntegerLiteral{
|
2020-03-17 02:49:15 +01:00
|
|
|
val: lit
|
2020-04-10 00:09:34 +02:00
|
|
|
pos: pos
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p.next()
|
2020-03-05 12:08:43 +01:00
|
|
|
return node
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) module_decl() ast.Module {
|
|
|
|
mut name := 'main'
|
2020-04-10 22:32:52 +02:00
|
|
|
is_skipped := p.tok.kind != .key_module
|
2020-05-16 16:12:23 +02:00
|
|
|
mut module_pos := token.Position{}
|
2020-04-10 22:32:52 +02:00
|
|
|
if !is_skipped {
|
2020-05-16 16:12:23 +02:00
|
|
|
module_pos = p.tok.position()
|
2020-05-07 06:51:36 +02:00
|
|
|
p.next()
|
2020-05-10 02:28:56 +02:00
|
|
|
mut pos := p.tok.position()
|
2020-04-10 22:27:51 +02:00
|
|
|
name = p.check_name()
|
2020-05-10 02:28:56 +02:00
|
|
|
if module_pos.line_nr != pos.line_nr {
|
|
|
|
p.error_with_pos('`module` and `$name` must be at same line', pos)
|
|
|
|
}
|
|
|
|
pos = p.tok.position()
|
|
|
|
if module_pos.line_nr == pos.line_nr {
|
|
|
|
if p.tok.kind != .name {
|
|
|
|
p.error_with_pos('`module x` syntax error', pos)
|
|
|
|
} else {
|
|
|
|
p.error_with_pos('`module x` can only declare one module', pos)
|
|
|
|
}
|
|
|
|
}
|
2020-05-16 16:12:23 +02:00
|
|
|
module_pos = module_pos.extend(pos)
|
2020-04-10 22:27:51 +02:00
|
|
|
}
|
2020-07-25 01:37:52 +02:00
|
|
|
mut full_mod := p.table.qualify_module(name, p.file_name)
|
|
|
|
if p.pref.build_mode == .build_module && !full_mod.contains('.') {
|
|
|
|
// A hack to make building vlib modules work
|
|
|
|
// `v build-module v.gen` will result in `full_mod = "gen"`, not "v.gen",
|
|
|
|
// because the module being built
|
|
|
|
// is not imported.
|
|
|
|
// So here we fetch the name of the module by looking at the path that's being built.
|
|
|
|
word := p.pref.path.after('/')
|
|
|
|
if full_mod == word {
|
|
|
|
full_mod = p.pref.path.after('vlib/').replace('/', '.')
|
|
|
|
// println('new full mod =$full_mod')
|
|
|
|
}
|
|
|
|
// println('file_name=$p.file_name path=$p.pref.path')
|
|
|
|
}
|
2020-02-19 03:08:10 +01:00
|
|
|
p.mod = full_mod
|
2020-04-10 22:27:51 +02:00
|
|
|
p.builtin_mod = p.mod == 'builtin'
|
2020-01-18 23:26:14 +01:00
|
|
|
return ast.Module{
|
2020-02-19 03:08:10 +01:00
|
|
|
name: full_mod
|
2020-04-10 22:32:52 +02:00
|
|
|
is_skipped: is_skipped
|
2020-05-16 16:12:23 +02:00
|
|
|
pos: module_pos
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2019-12-28 09:43:22 +01:00
|
|
|
}
|
2019-12-28 09:15:32 +01:00
|
|
|
|
2020-05-07 04:32:29 +02:00
|
|
|
fn (mut p Parser) import_stmt() ast.Import {
|
2020-05-08 15:01:54 +02:00
|
|
|
import_pos := p.tok.position()
|
2020-05-07 04:32:29 +02:00
|
|
|
p.check(.key_import)
|
2020-03-11 18:52:51 +01:00
|
|
|
pos := p.tok.position()
|
2020-05-07 04:32:29 +02:00
|
|
|
if p.tok.kind == .lpar {
|
|
|
|
p.error_with_pos('`import()` has been deprecated, use `import x` instead', pos)
|
|
|
|
}
|
2020-04-21 05:11:50 +02:00
|
|
|
mut mod_name := p.check_name()
|
2020-05-08 15:01:54 +02:00
|
|
|
if import_pos.line_nr != pos.line_nr {
|
2020-07-18 21:34:38 +02:00
|
|
|
p.error_with_pos('`import` statements must be a single line', pos)
|
2020-05-08 15:01:54 +02:00
|
|
|
}
|
2020-04-21 05:11:50 +02:00
|
|
|
mut mod_alias := mod_name
|
2020-02-19 03:08:10 +01:00
|
|
|
for p.tok.kind == .dot {
|
2020-05-07 06:51:36 +02:00
|
|
|
p.next()
|
2020-05-08 15:01:54 +02:00
|
|
|
pos_t := p.tok.position()
|
|
|
|
if p.tok.kind != .name {
|
|
|
|
p.error_with_pos('module syntax error, please use `x.y.z`', pos)
|
|
|
|
}
|
|
|
|
if import_pos.line_nr != pos_t.line_nr {
|
|
|
|
p.error_with_pos('`import` and `submodule` must be at same line', pos)
|
|
|
|
}
|
2020-02-19 03:08:10 +01:00
|
|
|
submod_name := p.check_name()
|
|
|
|
mod_name += '.' + submod_name
|
|
|
|
mod_alias = submod_name
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
if p.tok.kind == .key_as {
|
2020-05-07 06:51:36 +02:00
|
|
|
p.next()
|
2020-01-18 23:26:14 +01:00
|
|
|
mod_alias = p.check_name()
|
2020-08-23 02:12:25 +02:00
|
|
|
if mod_alias == mod_name.split('.').last() {
|
|
|
|
p.error_with_pos('import alias `$mod_name as $mod_alias` is redundant', p.prev_tok.position())
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-07-18 21:34:38 +02:00
|
|
|
node := ast.Import{
|
2020-07-19 19:58:34 +02:00
|
|
|
pos: pos
|
|
|
|
mod: mod_name
|
|
|
|
alias: mod_alias
|
2020-07-18 21:34:38 +02:00
|
|
|
}
|
|
|
|
if p.tok.kind == .lcbr { // import module { fn1, Type2 } syntax
|
|
|
|
p.import_syms(node)
|
|
|
|
p.register_used_import(mod_name) // no `unused import` msg for parent
|
|
|
|
}
|
2020-05-08 15:01:54 +02:00
|
|
|
pos_t := p.tok.position()
|
|
|
|
if import_pos.line_nr == pos_t.line_nr {
|
2020-09-19 02:15:47 +02:00
|
|
|
if p.tok.kind !in [.lcbr, .eof] {
|
2020-05-08 15:01:54 +02:00
|
|
|
p.error_with_pos('cannot import multiple modules at a time', pos_t)
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 03:08:10 +01:00
|
|
|
p.imports[mod_alias] = mod_name
|
2020-07-23 22:20:00 +02:00
|
|
|
// if mod_name !in p.table.imports {
|
|
|
|
p.table.imports << mod_name
|
|
|
|
p.ast_imports << node
|
|
|
|
// }
|
2020-05-07 04:32:29 +02:00
|
|
|
return node
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
|
2020-07-18 21:34:38 +02:00
|
|
|
// import_syms parses the inner part of `import module { submod1, submod2 }`
|
|
|
|
fn (mut p Parser) import_syms(mut parent ast.Import) {
|
|
|
|
p.next()
|
|
|
|
pos_t := p.tok.position()
|
|
|
|
if p.tok.kind == .rcbr { // closed too early
|
|
|
|
p.error_with_pos('empty `$parent.mod` import set, remove `{}`', pos_t)
|
|
|
|
}
|
|
|
|
if p.tok.kind != .name { // not a valid inner name
|
2020-07-19 19:58:34 +02:00
|
|
|
p.error_with_pos('import syntax error, please specify a valid fn or type name',
|
|
|
|
pos_t)
|
2020-07-18 21:34:38 +02:00
|
|
|
}
|
|
|
|
for p.tok.kind == .name {
|
|
|
|
pos := p.tok.position()
|
|
|
|
alias := p.check_name()
|
|
|
|
name := '$parent.mod\.$alias'
|
|
|
|
if alias[0].is_capital() {
|
|
|
|
idx := p.table.add_placeholder_type(name)
|
|
|
|
typ := table.new_type(idx)
|
2020-08-22 12:29:15 +02:00
|
|
|
prepend_mod_name := p.prepend_mod(alias)
|
2020-07-18 21:34:38 +02:00
|
|
|
p.table.register_type_symbol({
|
|
|
|
kind: .alias
|
2020-08-22 12:29:15 +02:00
|
|
|
name: prepend_mod_name
|
|
|
|
source_name: prepend_mod_name
|
2020-07-18 21:34:38 +02:00
|
|
|
parent_idx: idx
|
|
|
|
mod: p.mod
|
|
|
|
info: table.Alias{
|
|
|
|
parent_type: typ
|
|
|
|
language: table.Language.v
|
|
|
|
}
|
|
|
|
is_public: false
|
|
|
|
})
|
|
|
|
// so we can work with the fully declared type in fmt+checker
|
|
|
|
parent.syms << ast.ImportSymbol{
|
|
|
|
pos: pos
|
|
|
|
name: alias
|
|
|
|
kind: .type_
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !p.table.known_fn(name) {
|
|
|
|
p.table.fns[alias] = table.Fn{
|
|
|
|
is_placeholder: true
|
|
|
|
mod: parent.mod
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// so we can work with this in fmt+checker
|
|
|
|
parent.syms << ast.ImportSymbol{
|
|
|
|
pos: pos
|
|
|
|
name: alias
|
|
|
|
kind: .fn_
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.tok.kind == .comma { // go again if more than one
|
|
|
|
p.next()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p.tok.kind == .rcbr { // finish if closing `}` is seen
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.tok.kind != .rcbr {
|
|
|
|
p.error_with_pos('import syntax error, no closing `}`', p.tok.position())
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) const_decl() ast.ConstDecl {
|
2020-06-06 17:47:16 +02:00
|
|
|
p.top_level_statement_start()
|
2020-04-19 00:07:57 +02:00
|
|
|
start_pos := p.tok.position()
|
2020-02-10 23:19:50 +01:00
|
|
|
is_pub := p.tok.kind == .key_pub
|
|
|
|
if is_pub {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-04-19 00:07:57 +02:00
|
|
|
end_pos := p.tok.position()
|
2020-01-18 23:26:14 +01:00
|
|
|
p.check(.key_const)
|
2020-04-16 06:00:05 +02:00
|
|
|
if p.tok.kind != .lpar {
|
|
|
|
p.error('consts must be grouped, e.g.\nconst (\n\ta = 1\n)')
|
|
|
|
}
|
2020-04-20 08:30:42 +02:00
|
|
|
p.next() // (
|
2020-04-26 09:17:13 +02:00
|
|
|
mut fields := []ast.ConstField{}
|
2020-08-27 14:07:49 +02:00
|
|
|
mut comments := []ast.Comment{}
|
|
|
|
for {
|
|
|
|
comments = p.eat_comments()
|
|
|
|
if p.tok.kind == .rpar {
|
|
|
|
break
|
2020-04-05 02:08:10 +02:00
|
|
|
}
|
2020-04-30 12:17:31 +02:00
|
|
|
pos := p.tok.position()
|
2020-05-22 17:36:09 +02:00
|
|
|
name := p.check_name()
|
|
|
|
if util.contains_capital(name) {
|
2020-05-27 03:20:22 +02:00
|
|
|
p.warn_with_pos('const names cannot contain uppercase letters, use snake_case instead',
|
|
|
|
pos)
|
2020-05-22 17:36:09 +02:00
|
|
|
}
|
|
|
|
full_name := p.prepend_mod(name)
|
2020-04-04 05:14:40 +02:00
|
|
|
// name := p.check_name()
|
2020-02-22 17:07:03 +01:00
|
|
|
// println('!!const: $name')
|
2020-01-18 23:26:14 +01:00
|
|
|
p.check(.assign)
|
2020-03-05 12:08:43 +01:00
|
|
|
expr := p.expr(0)
|
2020-04-04 05:14:40 +02:00
|
|
|
field := ast.ConstField{
|
2020-05-22 17:36:09 +02:00
|
|
|
name: full_name
|
2020-07-08 13:19:58 +02:00
|
|
|
mod: p.mod
|
2020-04-04 05:14:40 +02:00
|
|
|
expr: expr
|
2020-04-30 12:17:31 +02:00
|
|
|
pos: pos
|
2020-06-23 18:01:56 +02:00
|
|
|
comments: comments
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
2020-04-04 05:14:40 +02:00
|
|
|
fields << field
|
|
|
|
p.global_scope.register(field.name, field)
|
2020-08-27 14:07:49 +02:00
|
|
|
comments = []
|
2019-12-31 19:42:16 +01:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
p.top_level_statement_end()
|
2020-01-18 23:26:14 +01:00
|
|
|
p.check(.rpar)
|
2020-02-03 07:02:54 +01:00
|
|
|
return ast.ConstDecl{
|
2020-04-19 00:07:57 +02:00
|
|
|
pos: start_pos.extend(end_pos)
|
2020-02-03 07:02:54 +01:00
|
|
|
fields: fields
|
2020-02-26 22:43:37 +01:00
|
|
|
is_pub: is_pub
|
2020-08-27 14:07:49 +02:00
|
|
|
end_comments: comments
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) return_stmt() ast.Return {
|
2020-04-17 16:16:56 +02:00
|
|
|
first_pos := p.tok.position()
|
2019-12-28 09:15:32 +01:00
|
|
|
p.next()
|
2020-06-16 13:20:16 +02:00
|
|
|
// no return
|
2020-03-05 13:57:05 +01:00
|
|
|
if p.tok.kind == .rcbr {
|
2020-01-22 21:34:38 +01:00
|
|
|
return ast.Return{
|
2020-04-17 16:16:56 +02:00
|
|
|
pos: first_pos
|
2020-01-22 21:34:38 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-16 13:20:16 +02:00
|
|
|
// return exprs
|
2020-07-17 19:13:22 +02:00
|
|
|
exprs, comments := p.expr_list()
|
2020-04-19 00:07:57 +02:00
|
|
|
end_pos := exprs.last().position()
|
2020-04-17 16:16:56 +02:00
|
|
|
return ast.Return{
|
2020-01-07 12:10:07 +01:00
|
|
|
exprs: exprs
|
2020-07-17 19:13:22 +02:00
|
|
|
comments: comments
|
2020-04-19 00:07:57 +02:00
|
|
|
pos: first_pos.extend(end_pos)
|
2019-12-28 09:43:22 +01:00
|
|
|
}
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
|
|
|
|
2020-06-11 20:26:46 +02:00
|
|
|
const (
|
2020-06-09 10:49:15 +02:00
|
|
|
// modules which allow globals by default
|
2020-08-19 07:10:42 +02:00
|
|
|
global_enabled_mods = ['rand', 'sokol.sapp']
|
2020-06-09 10:49:15 +02:00
|
|
|
)
|
|
|
|
|
2020-02-28 13:29:04 +01:00
|
|
|
// left hand side of `=` or `:=` in `a,b,c := 1,2,3`
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) global_decl() ast.GlobalDecl {
|
2020-05-04 16:46:36 +02:00
|
|
|
if !p.pref.translated && !p.pref.is_livemain && !p.builtin_mod && !p.pref.building_v &&
|
2020-07-23 22:20:00 +02:00
|
|
|
p.mod != 'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !p.pref.enable_globals && !p.pref.is_fmt &&
|
2020-06-24 14:35:00 +02:00
|
|
|
p.mod !in global_enabled_mods {
|
2020-02-03 07:02:54 +01:00
|
|
|
p.error('use `v --enable-globals ...` to enable globals')
|
|
|
|
}
|
2020-05-16 16:12:23 +02:00
|
|
|
start_pos := p.tok.position()
|
2020-02-03 07:02:54 +01:00
|
|
|
p.next()
|
2020-05-16 16:12:23 +02:00
|
|
|
pos := start_pos.extend(p.tok.position())
|
2020-02-03 07:02:54 +01:00
|
|
|
name := p.check_name()
|
2020-03-31 19:58:44 +02:00
|
|
|
// println(name)
|
2020-02-03 07:02:54 +01:00
|
|
|
typ := p.parse_type()
|
2020-04-21 05:11:50 +02:00
|
|
|
mut expr := ast.Expr{}
|
2020-04-07 18:51:39 +02:00
|
|
|
has_expr := p.tok.kind == .assign
|
|
|
|
if has_expr {
|
2020-02-03 07:02:54 +01:00
|
|
|
p.next()
|
2020-04-07 18:51:39 +02:00
|
|
|
expr = p.expr(0)
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
// p.genln(p.table.cgen_name_type_pair(name, typ))
|
|
|
|
/*
|
2020-04-04 05:14:40 +02:00
|
|
|
mut g := p.table.cgen_name_type_pair(name, typ)
|
|
|
|
if p.tok == .assign {
|
|
|
|
p.next()
|
|
|
|
g += ' = '
|
|
|
|
_,expr := p.tmp_expr()
|
|
|
|
g += expr
|
|
|
|
}
|
|
|
|
// p.genln('; // global')
|
|
|
|
g += '; // global'
|
|
|
|
if !p.cgen.nogen {
|
|
|
|
p.cgen.consts << g
|
|
|
|
}
|
2020-04-25 17:49:16 +02:00
|
|
|
*/
|
2020-04-04 05:14:40 +02:00
|
|
|
glob := ast.GlobalDecl{
|
2020-02-03 07:02:54 +01:00
|
|
|
name: name
|
2020-03-06 16:31:40 +01:00
|
|
|
typ: typ
|
2020-05-16 16:12:23 +02:00
|
|
|
pos: pos
|
2020-04-07 18:51:39 +02:00
|
|
|
has_expr: has_expr
|
|
|
|
expr: expr
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
2020-04-04 05:14:40 +02:00
|
|
|
p.global_scope.register(name, glob)
|
|
|
|
return glob
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) enum_decl() ast.EnumDecl {
|
2020-06-06 17:47:16 +02:00
|
|
|
p.top_level_statement_start()
|
2020-02-10 23:19:50 +01:00
|
|
|
is_pub := p.tok.kind == .key_pub
|
2020-04-19 00:07:57 +02:00
|
|
|
start_pos := p.tok.position()
|
2020-02-10 23:19:50 +01:00
|
|
|
if is_pub {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
p.check(.key_enum)
|
2020-04-19 00:07:57 +02:00
|
|
|
end_pos := p.tok.position()
|
2020-04-23 05:16:10 +02:00
|
|
|
enum_name := p.check_name()
|
|
|
|
name := p.prepend_mod(enum_name)
|
2020-02-10 23:19:50 +01:00
|
|
|
p.check(.lcbr)
|
2020-07-02 15:44:03 +02:00
|
|
|
enum_decl_comments := p.eat_comments()
|
2020-04-26 09:17:13 +02:00
|
|
|
mut vals := []string{}
|
2020-04-26 16:25:54 +02:00
|
|
|
// mut default_exprs := []ast.Expr{}
|
2020-04-26 09:17:13 +02:00
|
|
|
mut fields := []ast.EnumField{}
|
2020-02-10 23:19:50 +01:00
|
|
|
for p.tok.kind != .eof && p.tok.kind != .rcbr {
|
2020-04-09 19:23:49 +02:00
|
|
|
pos := p.tok.position()
|
2020-04-10 00:09:34 +02:00
|
|
|
val := p.check_name()
|
2020-02-10 23:19:50 +01:00
|
|
|
vals << val
|
2020-04-21 05:11:50 +02:00
|
|
|
mut expr := ast.Expr{}
|
|
|
|
mut has_expr := false
|
2020-02-19 11:31:33 +01:00
|
|
|
// p.warn('enum val $val')
|
2020-02-12 01:16:38 +01:00
|
|
|
if p.tok.kind == .assign {
|
|
|
|
p.next()
|
2020-04-10 14:44:01 +02:00
|
|
|
expr = p.expr(0)
|
|
|
|
has_expr = true
|
2020-02-12 01:16:38 +01:00
|
|
|
}
|
2020-04-17 18:01:02 +02:00
|
|
|
fields << ast.EnumField{
|
2020-04-17 18:11:04 +02:00
|
|
|
name: val
|
|
|
|
pos: pos
|
|
|
|
expr: expr
|
|
|
|
has_expr: has_expr
|
2020-07-02 15:44:03 +02:00
|
|
|
comments: p.eat_comments()
|
2020-04-17 18:01:02 +02:00
|
|
|
}
|
2020-02-10 23:19:50 +01:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
p.top_level_statement_end()
|
2020-02-10 23:19:50 +01:00
|
|
|
p.check(.rcbr)
|
2020-08-04 20:10:22 +02:00
|
|
|
is_flag := p.attrs.contains('flag')
|
|
|
|
is_multi_allowed := p.attrs.contains('_allow_multiple_values')
|
2020-05-25 23:00:48 +02:00
|
|
|
if is_flag {
|
|
|
|
if fields.len > 32 {
|
|
|
|
p.error('when an enum is used as bit field, it must have a max of 32 fields')
|
|
|
|
}
|
|
|
|
pubfn := if p.mod == 'main' { 'fn' } else { 'pub fn' }
|
|
|
|
p.scanner.codegen('
|
|
|
|
//
|
2020-07-01 00:53:53 +02:00
|
|
|
$pubfn ( e &$enum_name) has(flag $enum_name) bool { return (int(*e) & (1 << int(flag))) != 0 }
|
|
|
|
$pubfn (mut e $enum_name) set(flag $enum_name) { unsafe{ *e = int(*e) | (1 << int(flag)) } }
|
|
|
|
$pubfn (mut e $enum_name) clear(flag $enum_name) { unsafe{ *e = int(*e) & ~(1 << int(flag)) } }
|
|
|
|
$pubfn (mut e $enum_name) toggle(flag $enum_name) { unsafe{ *e = int(*e) ^ (1 << int(flag)) } }
|
2020-05-25 23:00:48 +02:00
|
|
|
//
|
2020-06-27 14:50:04 +02:00
|
|
|
')
|
2020-05-25 23:00:48 +02:00
|
|
|
}
|
2020-02-25 13:30:43 +01:00
|
|
|
p.table.register_type_symbol(table.TypeSymbol{
|
|
|
|
kind: .enum_
|
2020-03-14 05:20:12 +01:00
|
|
|
name: name
|
2020-08-22 12:29:15 +02:00
|
|
|
source_name: name
|
2020-05-11 08:59:55 +02:00
|
|
|
mod: p.mod
|
2020-02-25 13:30:43 +01:00
|
|
|
info: table.Enum{
|
2020-04-08 00:59:28 +02:00
|
|
|
vals: vals
|
2020-05-25 23:00:48 +02:00
|
|
|
is_flag: is_flag
|
2020-07-10 16:43:02 +02:00
|
|
|
is_multi_allowed: is_multi_allowed
|
2020-04-08 00:59:28 +02:00
|
|
|
}
|
2020-02-25 13:30:43 +01:00
|
|
|
})
|
2020-02-10 23:19:50 +01:00
|
|
|
return ast.EnumDecl{
|
|
|
|
name: name
|
|
|
|
is_pub: is_pub
|
2020-05-25 23:00:48 +02:00
|
|
|
is_flag: is_flag
|
2020-07-10 16:43:02 +02:00
|
|
|
is_multi_allowed: is_multi_allowed
|
2020-04-09 19:23:49 +02:00
|
|
|
fields: fields
|
2020-04-19 00:07:57 +02:00
|
|
|
pos: start_pos.extend(end_pos)
|
2020-07-02 15:44:03 +02:00
|
|
|
comments: enum_decl_comments
|
2020-02-10 23:19:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) type_decl() ast.TypeDecl {
|
2020-04-19 00:07:57 +02:00
|
|
|
start_pos := p.tok.position()
|
2020-02-10 23:19:50 +01:00
|
|
|
is_pub := p.tok.kind == .key_pub
|
|
|
|
if is_pub {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
p.check(.key_type)
|
2020-04-19 00:07:57 +02:00
|
|
|
end_pos := p.tok.position()
|
|
|
|
decl_pos := start_pos.extend(end_pos)
|
2020-02-10 23:19:50 +01:00
|
|
|
name := p.check_name()
|
2020-06-29 20:09:09 +02:00
|
|
|
if name.len == 1 && name[0].is_capital() {
|
2020-07-03 15:10:39 +02:00
|
|
|
p.error_with_pos('single letter capital names are reserved for generic template types.',
|
|
|
|
decl_pos)
|
2020-06-29 20:09:09 +02:00
|
|
|
}
|
2020-04-26 09:17:13 +02:00
|
|
|
mut sum_variants := []table.Type{}
|
2020-02-10 23:19:50 +01:00
|
|
|
if p.tok.kind == .assign {
|
2020-04-20 08:30:42 +02:00
|
|
|
p.next() // TODO require `=`
|
2020-04-13 15:06:02 +02:00
|
|
|
}
|
|
|
|
if p.tok.kind == .key_fn {
|
|
|
|
// function type: `type mycallback fn(string, int)`
|
|
|
|
fn_name := p.prepend_mod(name)
|
|
|
|
fn_type := p.parse_fn_type(fn_name)
|
|
|
|
return ast.FnTypeDecl{
|
|
|
|
name: fn_name
|
|
|
|
is_pub: is_pub
|
|
|
|
typ: fn_type
|
2020-04-19 00:07:57 +02:00
|
|
|
pos: decl_pos
|
2020-04-13 15:06:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-20 08:30:42 +02:00
|
|
|
first_type := p.parse_type() // need to parse the first type before we can check if it's `type A = X | Y`
|
2020-04-13 15:06:02 +02:00
|
|
|
if p.tok.kind == .pipe {
|
2020-05-07 06:51:36 +02:00
|
|
|
p.next()
|
2020-04-13 15:06:02 +02:00
|
|
|
sum_variants << first_type
|
2020-03-07 17:37:55 +01:00
|
|
|
// type SumType = A | B | c
|
2020-04-10 22:27:51 +02:00
|
|
|
for {
|
2020-03-02 06:40:18 +01:00
|
|
|
variant_type := p.parse_type()
|
|
|
|
sum_variants << variant_type
|
2020-02-10 23:19:50 +01:00
|
|
|
if p.tok.kind != .pipe {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.check(.pipe)
|
|
|
|
}
|
2020-08-22 12:29:15 +02:00
|
|
|
prepend_mod_name := p.prepend_mod(name)
|
2020-03-02 06:40:18 +01:00
|
|
|
p.table.register_type_symbol(table.TypeSymbol{
|
|
|
|
kind: .sum_type
|
2020-08-22 12:29:15 +02:00
|
|
|
name: prepend_mod_name
|
|
|
|
source_name: prepend_mod_name
|
2020-05-11 08:59:55 +02:00
|
|
|
mod: p.mod
|
2020-03-02 06:40:18 +01:00
|
|
|
info: table.SumType{
|
2020-04-08 00:59:28 +02:00
|
|
|
variants: sum_variants
|
|
|
|
}
|
2020-05-04 21:56:41 +02:00
|
|
|
is_public: is_pub
|
2020-03-02 06:40:18 +01:00
|
|
|
})
|
2020-03-07 17:37:55 +01:00
|
|
|
return ast.SumTypeDecl{
|
|
|
|
name: name
|
|
|
|
is_pub: is_pub
|
|
|
|
sub_types: sum_variants
|
2020-04-19 00:07:57 +02:00
|
|
|
pos: decl_pos
|
2020-03-07 17:37:55 +01:00
|
|
|
}
|
2020-03-02 23:19:04 +01:00
|
|
|
}
|
|
|
|
// type MyType int
|
2020-04-13 15:06:02 +02:00
|
|
|
parent_type := first_type
|
2020-05-18 21:38:06 +02:00
|
|
|
parent_name := p.table.get_type_symbol(parent_type).name
|
2020-04-25 09:08:53 +02:00
|
|
|
pid := parent_type.idx()
|
2020-05-19 17:12:47 +02:00
|
|
|
language := if parent_name.len > 2 && parent_name.starts_with('C.') {
|
|
|
|
table.Language.c
|
|
|
|
} else if parent_name.len > 2 && parent_name.starts_with('JS.') {
|
|
|
|
table.Language.js
|
|
|
|
} else {
|
|
|
|
table.Language.v
|
|
|
|
}
|
2020-08-22 12:29:15 +02:00
|
|
|
prepend_mod_name := p.prepend_mod(name)
|
2020-06-02 09:00:51 +02:00
|
|
|
p.table.register_type_symbol({
|
2020-03-07 17:37:55 +01:00
|
|
|
kind: .alias
|
2020-08-22 12:29:15 +02:00
|
|
|
name: prepend_mod_name
|
|
|
|
source_name: prepend_mod_name
|
2020-03-07 17:37:55 +01:00
|
|
|
parent_idx: pid
|
2020-05-11 08:59:55 +02:00
|
|
|
mod: p.mod
|
2020-03-07 17:37:55 +01:00
|
|
|
info: table.Alias{
|
2020-06-24 22:12:33 +02:00
|
|
|
parent_type: parent_type
|
2020-05-19 17:12:47 +02:00
|
|
|
language: language
|
2020-04-08 00:59:28 +02:00
|
|
|
}
|
2020-05-04 21:56:41 +02:00
|
|
|
is_public: is_pub
|
2020-03-07 17:37:55 +01:00
|
|
|
})
|
|
|
|
return ast.AliasTypeDecl{
|
2020-02-10 23:19:50 +01:00
|
|
|
name: name
|
2020-03-07 17:37:55 +01:00
|
|
|
is_pub: is_pub
|
|
|
|
parent_type: parent_type
|
2020-04-19 00:07:57 +02:00
|
|
|
pos: decl_pos
|
2020-02-10 23:19:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) assoc() ast.Assoc {
|
2020-04-01 23:23:20 +02:00
|
|
|
var_name := p.check_name()
|
|
|
|
pos := p.tok.position()
|
2020-04-27 15:16:31 +02:00
|
|
|
mut v := p.scope.find_var(var_name) or {
|
2020-04-01 23:23:20 +02:00
|
|
|
p.error('unknown variable `$var_name`')
|
|
|
|
return ast.Assoc{}
|
|
|
|
}
|
2020-04-27 15:16:31 +02:00
|
|
|
v.is_used = true
|
2020-04-01 23:23:20 +02:00
|
|
|
// println('assoc var $name typ=$var.typ')
|
2020-04-26 09:17:13 +02:00
|
|
|
mut fields := []string{}
|
|
|
|
mut vals := []ast.Expr{}
|
2020-04-01 23:23:20 +02:00
|
|
|
p.check(.pipe)
|
2020-04-10 22:27:51 +02:00
|
|
|
for {
|
2020-04-01 23:23:20 +02:00
|
|
|
fields << p.check_name()
|
|
|
|
p.check(.colon)
|
|
|
|
expr := p.expr(0)
|
|
|
|
vals << expr
|
|
|
|
if p.tok.kind == .comma {
|
2020-05-07 06:51:36 +02:00
|
|
|
p.next()
|
2020-04-01 23:23:20 +02:00
|
|
|
}
|
|
|
|
if p.tok.kind == .rcbr {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ast.Assoc{
|
|
|
|
var_name: var_name
|
|
|
|
fields: fields
|
|
|
|
exprs: vals
|
|
|
|
pos: pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:26:15 +02:00
|
|
|
fn (p &Parser) new_true_expr() ast.Expr {
|
|
|
|
return ast.BoolLiteral{
|
|
|
|
val: true
|
2020-04-20 14:49:26 +02:00
|
|
|
pos: p.tok.position()
|
2020-03-31 20:26:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:21:41 +01:00
|
|
|
fn verror(s string) {
|
2020-04-03 17:38:41 +02:00
|
|
|
util.verror('parser error', s)
|
2019-12-26 11:21:41 +01:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
|
|
|
|
fn (mut p Parser) top_level_statement_start() {
|
|
|
|
if p.comments_mode == .toplevel_comments {
|
|
|
|
p.scanner.set_is_inside_toplevel_statement(true)
|
|
|
|
p.rewind_scanner_to_current_token_in_new_mode()
|
|
|
|
$if debugscanner ? {
|
|
|
|
eprintln('>> p.top_level_statement_start | tidx:${p.tok.tidx:-5} | p.tok.kind: ${p.tok.kind:-10} | p.tok.lit: $p.tok.lit $p.peek_tok.lit $p.peek_tok2.lit $p.peek_tok3.lit ...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Parser) top_level_statement_end() {
|
|
|
|
if p.comments_mode == .toplevel_comments {
|
|
|
|
p.scanner.set_is_inside_toplevel_statement(false)
|
|
|
|
p.rewind_scanner_to_current_token_in_new_mode()
|
|
|
|
$if debugscanner ? {
|
|
|
|
eprintln('>> p.top_level_statement_end | tidx:${p.tok.tidx:-5} | p.tok.kind: ${p.tok.kind:-10} | p.tok.lit: $p.tok.lit $p.peek_tok.lit $p.peek_tok2.lit $p.peek_tok3.lit ...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Parser) rewind_scanner_to_current_token_in_new_mode() {
|
|
|
|
// Go back and rescan some tokens, ensuring that the parser's
|
|
|
|
// lookahead buffer p.peek_tok .. p.peek_tok3, will now contain
|
|
|
|
// the correct tokens (possible comments), for the new mode
|
|
|
|
// This refilling of the lookahead buffer is needed for the
|
|
|
|
// .toplevel_comments parsing mode.
|
|
|
|
tidx := p.tok.tidx
|
|
|
|
p.scanner.set_current_tidx(tidx - 5)
|
|
|
|
no_token := token.Token{}
|
|
|
|
p.prev_tok = no_token
|
|
|
|
p.tok = no_token
|
|
|
|
p.peek_tok = no_token
|
|
|
|
p.peek_tok2 = no_token
|
|
|
|
p.peek_tok3 = no_token
|
|
|
|
for {
|
|
|
|
p.next()
|
2020-06-08 13:30:17 +02:00
|
|
|
// eprintln('rewinding to ${p.tok.tidx:5} | goal: ${tidx:5}')
|
2020-06-06 17:47:16 +02:00
|
|
|
if tidx == p.tok.tidx {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 00:53:53 +02:00
|
|
|
|
|
|
|
pub fn (mut p Parser) mark_var_as_used(varname string) bool {
|
|
|
|
if obj := p.scope.find(varname) {
|
|
|
|
match mut obj {
|
|
|
|
ast.Var {
|
|
|
|
obj.is_used = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-09-19 18:18:36 +02:00
|
|
|
|
|
|
|
fn (mut p Parser) unsafe_stmt() ast.Stmt {
|
|
|
|
pos := p.tok.position()
|
|
|
|
p.next()
|
|
|
|
if p.tok.kind != .lcbr {
|
|
|
|
p.error_with_pos('please use `unsafe {`', p.tok.position())
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
assert !p.inside_unsafe
|
|
|
|
if p.tok.kind == .rcbr {
|
|
|
|
// `unsafe {}`
|
|
|
|
p.next()
|
|
|
|
return ast.Block{
|
|
|
|
is_unsafe: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.inside_unsafe = true
|
|
|
|
p.open_scope() // needed in case of `unsafe {stmt}`
|
|
|
|
defer {
|
|
|
|
p.inside_unsafe = false
|
|
|
|
p.close_scope()
|
|
|
|
}
|
|
|
|
stmt := p.stmt(false)
|
|
|
|
if p.tok.kind == .rcbr {
|
|
|
|
if stmt is ast.ExprStmt {
|
|
|
|
// `unsafe {expr}`
|
|
|
|
if stmt.expr.is_expr() {
|
|
|
|
p.next()
|
|
|
|
ue := ast.UnsafeExpr{
|
|
|
|
expr: stmt.expr
|
|
|
|
pos: pos
|
|
|
|
}
|
|
|
|
// parse e.g. `unsafe {expr}.foo()`
|
|
|
|
expr := p.expr_with_left(ue, 0, p.is_stmt_ident)
|
|
|
|
return ast.ExprStmt{
|
|
|
|
expr: expr
|
|
|
|
pos: pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// unsafe {stmts}
|
|
|
|
mut stmts := [stmt]
|
|
|
|
for p.tok.kind != .rcbr {
|
|
|
|
stmts << p.stmt(false)
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
return ast.Block{
|
|
|
|
stmts: stmts
|
|
|
|
is_unsafe: true
|
|
|
|
}
|
|
|
|
}
|