parser: refactor script & repl handling

pull/4349/head
yuyi 2020-04-11 20:06:26 +08:00 committed by GitHub
parent be16c5b21d
commit c1aba95d7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 5 deletions

View File

@ -324,11 +324,8 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
}
else {
if p.pref.is_script && !p.pref.is_test {
p.scanner.text = 'fn main() {' + p.scanner.text + '}'
p.scanner.is_started = false
p.scanner.pos = 0
p.next()
p.next()
p.scanner.add_fn_main_and_rescan()
p.read_first_token()
return p.top_stmt()
} else {
p.error('bad top level statement ' + p.tok.str())

View File

@ -77,6 +77,14 @@ pub fn new_scanner(text string, comments_mode CommentsMode) &Scanner {
}
}
pub fn (s &Scanner) add_fn_main_and_rescan() {
s.text = 'fn main() {' + s.text + '}'
s.is_started = false
s.pos = 0
s.line_nr = 0
s.last_nl_pos = 0
}
fn (s &Scanner) new_token(tok_kind token.Kind, lit string, len int) token.Token {
return token.Token{
kind: tok_kind