parser: refactor script & repl handling
parent
be16c5b21d
commit
c1aba95d7d
|
@ -324,11 +324,8 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if p.pref.is_script && !p.pref.is_test {
|
if p.pref.is_script && !p.pref.is_test {
|
||||||
p.scanner.text = 'fn main() {' + p.scanner.text + '}'
|
p.scanner.add_fn_main_and_rescan()
|
||||||
p.scanner.is_started = false
|
p.read_first_token()
|
||||||
p.scanner.pos = 0
|
|
||||||
p.next()
|
|
||||||
p.next()
|
|
||||||
return p.top_stmt()
|
return p.top_stmt()
|
||||||
} else {
|
} else {
|
||||||
p.error('bad top level statement ' + p.tok.str())
|
p.error('bad top level statement ' + p.tok.str())
|
||||||
|
|
|
@ -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 {
|
fn (s &Scanner) new_token(tok_kind token.Kind, lit string, len int) token.Token {
|
||||||
return token.Token{
|
return token.Token{
|
||||||
kind: tok_kind
|
kind: tok_kind
|
||||||
|
|
Loading…
Reference in New Issue