vet: prohibit spaces after `(`

pull/5801/head
Alexander Medvednikov 2020-07-11 11:41:39 +02:00
parent a1073105e1
commit d4d552fb00
5 changed files with 32 additions and 19 deletions

View File

@ -45,6 +45,7 @@ jobs:
run: ./v build-tools run: ./v build-tools
- name: v vet - name: v vet
run: | run: |
./v vet vlib/v/scanner
./v vet vlib/v/parser ./v vet vlib/v/parser
./v vet vlib/v/ast ./v vet vlib/v/ast
./v vet vlib/v/gen/cgen.v ./v vet vlib/v/gen/cgen.v
@ -56,7 +57,7 @@ jobs:
alpine-docker-musl-gcc: alpine-docker-musl-gcc:
name: alpine-musl name: alpine-musl
runs-on: ubuntu-latest runs-on: ubuntu-latest
container: container:
image: thevlang/vlang:alpine-build image: thevlang/vlang:alpine-build
env: env:
V_CI_MUSL: 1 V_CI_MUSL: 1
@ -66,13 +67,13 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Build V - name: Build V
run: | run: |
make CC=clang make CC=clang
- name: Test V fixed tests - name: Test V fixed tests
run: | run: |
v test-fixed v test-fixed
macos: macos:
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
@ -201,10 +202,10 @@ jobs:
# Ubuntu docker pre-built container # Ubuntu docker pre-built container
ubuntu-musl: ubuntu-musl:
name: ubuntu-musl name: ubuntu-musl
runs-on: ubuntu-latest runs-on: ubuntu-latest
container: container:
image: thevlang/vlang:ubuntu-build image: thevlang/vlang:ubuntu-build
env: env:
V_CI_MUSL: 1 V_CI_MUSL: 1
@ -215,13 +216,13 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Build V - name: Build V
run: | run: |
echo $VFLAGS && make -j4 && ./v -cg -o v cmd/v echo $VFLAGS && make -j4 && ./v -cg -o v cmd/v
- name: Test V fixed tests - name: Test V fixed tests
run: | run: |
v test-fixed v test-fixed
# ubuntu-musl: # ubuntu-musl:
# runs-on: ubuntu-18.04 # runs-on: ubuntu-18.04

View File

@ -60,11 +60,12 @@ mut:
// for tests // for tests
pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt { pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
s := scanner.new_scanner(text, .skip_comments, false) pref:= &pref.Preferences{}
s := scanner.new_scanner(text, .skip_comments, pref)
mut p := Parser{ mut p := Parser{
scanner: s scanner: s
table: table table: table
pref: &pref.Preferences{} pref: pref
scope: scope scope: scope
global_scope: &ast.Scope{ global_scope: &ast.Scope{
start_pos: 0 start_pos: 0
@ -77,7 +78,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
} }
pub fn parse_text(text string, b_table &table.Table, pref &pref.Preferences, scope, global_scope &ast.Scope) ast.File { pub fn parse_text(text string, b_table &table.Table, pref &pref.Preferences, scope, global_scope &ast.Scope) ast.File {
s := scanner.new_scanner(text, .skip_comments, pref.is_fmt) s := scanner.new_scanner(text, .skip_comments, pref)
mut p := Parser{ mut p := Parser{
scanner: s scanner: s
table: b_table table: b_table
@ -100,7 +101,7 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme
// panic(err) // panic(err)
// } // }
mut p := Parser{ mut p := Parser{
scanner: scanner.new_scanner_file(path, comments_mode, pref.is_fmt) scanner: scanner.new_scanner_file(path, comments_mode, pref)
comments_mode: comments_mode comments_mode: comments_mode
table: b_table table: b_table
file_name: path file_name: path
@ -126,6 +127,8 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme
eprintln('NB: You can run `v fmt -w file.v` to fix these automatically') eprintln('NB: You can run `v fmt -w file.v` to fix these automatically')
exit(1) exit(1)
} }
//if pref.is_vet && p.scanner.text.contains('( '\n ') {
//}
return p.parse() return p.parse()
} }

View File

@ -101,7 +101,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
pos := p.tok.position() pos := p.tok.position()
p.next() // sizeof p.next() // sizeof
p.check(.lpar) p.check(.lpar)
is_known_var := p.mark_var_as_used( p.tok.lit ) is_known_var := p.mark_var_as_used(p.tok.lit)
if is_known_var { if is_known_var {
expr := p.parse_ident(table.Language.v) expr := p.parse_ident(table.Language.v)
node = ast.SizeOf{ node = ast.SizeOf{
@ -224,8 +224,9 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
} }
} else if p.tok.kind.is_infix() { } else if p.tok.kind.is_infix() {
// return early for deref assign `*x = 2` goes to prefix expr // return early for deref assign `*x = 2` goes to prefix expr
if p.tok.kind == .mul && p.tok.line_nr != p.prev_tok.line_nr && p.peek_tok2.kind == if p.tok.kind == .mul &&
.assign { p.tok.line_nr != p.prev_tok.line_nr &&
p.peek_tok2.kind == .assign {
return node return node
} }
// continue on infix expr // continue on infix expr

View File

@ -48,6 +48,7 @@ pub mut:
all_tokens []token.Token // *only* used in comments_mode: .toplevel_comments, contains all tokens all_tokens []token.Token // *only* used in comments_mode: .toplevel_comments, contains all tokens
tidx int tidx int
eofs int eofs int
pref &pref.Preferences
} }
/* /*
@ -94,7 +95,8 @@ pub enum CommentsMode {
} }
// new scanner from file. // new scanner from file.
pub fn new_scanner_file(file_path string, comments_mode CommentsMode, is_fmt bool) &Scanner { pub fn new_scanner_file(file_path string, comments_mode CommentsMode, pref &pref.Preferences) &Scanner {
// is_fmt := pref.is_fmt
if !os.exists(file_path) { if !os.exists(file_path) {
verror("$file_path doesn't exist") verror("$file_path doesn't exist")
} }
@ -102,15 +104,17 @@ pub fn new_scanner_file(file_path string, comments_mode CommentsMode, is_fmt boo
verror(err) verror(err)
return voidptr(0) return voidptr(0)
} }
mut s := new_scanner(raw_text, comments_mode, is_fmt) // .skip_comments) mut s := new_scanner(raw_text, comments_mode, pref) // .skip_comments)
// s.init_fmt() // s.init_fmt()
s.file_path = file_path s.file_path = file_path
return s return s
} }
// new scanner from string. // new scanner from string.
pub fn new_scanner(text string, comments_mode CommentsMode, is_fmt bool) &Scanner { pub fn new_scanner(text string, comments_mode CommentsMode, pref &pref.Preferences) &Scanner {
is_fmt := pref.is_fmt
s := &Scanner{ s := &Scanner{
pref: pref
text: text text: text
is_print_line_on_error: true is_print_line_on_error: true
is_print_colored_error: true is_print_colored_error: true
@ -768,6 +772,10 @@ fn (mut s Scanner) text_scan() token.Token {
return s.new_token(.chartoken, ident_char, ident_char.len + 2) // + two quotes return s.new_token(.chartoken, ident_char, ident_char.len + 2) // + two quotes
} }
`(` { `(` {
// TODO `$if vet {` for performance
if s.pref.is_vet && s.text[s.pos + 1] == ` ` {
println('$s.file_path:$s.line_nr: Looks like you are adding a space after `(`')
}
return s.new_token(.lpar, '', 1) return s.new_token(.lpar, '', 1)
} }
`)` { `)` {

View File

@ -42,9 +42,9 @@ fn fn_name_mod_level_high_order(cb fn(int)) {
fn test_at_file() { fn test_at_file() {
// Test @FILE // Test @FILE
f := os.file_name( @FILE ) f := os.file_name(@FILE)
assert f == 'scanner_at_literals_test.v' assert f == 'scanner_at_literals_test.v'
} }
fn test_at_fn() { fn test_at_fn() {
// Test @FN // Test @FN