From edd470648045dfffc9d689f2b4275ecc6e2e5802 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Mon, 25 Nov 2019 23:29:55 +1100 Subject: [PATCH] compiler: parser fixes for vweb --- vlib/compiler/comptime.v | 2 ++ vlib/compiler/parser.v | 40 +++++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/vlib/compiler/comptime.v b/vlib/compiler/comptime.v index 8478065c3d..e3ca37a294 100644 --- a/vlib/compiler/comptime.v +++ b/vlib/compiler/comptime.v @@ -169,7 +169,9 @@ fn (p mut Parser) comp_time() { } p.import_table.register_used_import('strings') p.genln('/////////////////// tmpl start') + p.is_vweb = true p.statements_from_text(v_code, false) + p.is_vweb = false p.genln('/////////////////// tmpl end') receiver := p.cur_fn.args[0] dot := if receiver.is_mut { '->' } else { '.' } diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index ca718eba5e..feb555159b 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -90,13 +90,15 @@ const ( ) struct ParserState { - scanner_text string - tokens []Token - token_idx int - tok TokenKind - prev_tok TokenKind - prev_tok2 TokenKind - lit string + scanner_line_nr int + scanner_text string + scanner_pos int + tokens []Token + token_idx int + tok TokenKind + prev_tok TokenKind + prev_tok2 TokenKind + lit string } // new parser from string. unique id specified in `id`. @@ -277,7 +279,9 @@ fn (p &Parser) log(s string) { pub fn (p mut Parser) save_state() ParserState { return ParserState{ + scanner_line_nr: p.scanner.line_nr scanner_text : p.scanner.text + scanner_pos : p.scanner.pos tokens : p.tokens token_idx : p.token_idx tok : p.tok @@ -288,19 +292,24 @@ pub fn (p mut Parser) save_state() ParserState { } pub fn (p mut Parser) restore_state(state ParserState) { - p.scanner.text = state.scanner_text - p.tokens = state.tokens - p.token_idx = state.token_idx - p.tok = state.tok - p.prev_tok = state.prev_tok - p.prev_tok2 = state.prev_tok2 - p.lit = state.lit + p.scanner.line_nr = state.scanner_line_nr + p.scanner.text = state.scanner_text + p.scanner.pos = state.scanner_pos + p.tokens = state.tokens + p.token_idx = state.token_idx + p.tok = state.tok + p.prev_tok = state.prev_tok + p.prev_tok2 = state.prev_tok2 + p.lit = state.lit } fn (p mut Parser) clear_state() { + p.scanner.line_nr = 0 + p.scanner.text = '' + p.scanner.pos = 0 p.tokens = [] p.token_idx = 0 - p.scanner.text = '' + p.lit = '' } pub fn (p mut Parser) add_text(text string) { @@ -315,6 +324,7 @@ fn (p mut Parser) statements_from_text(text string, rcbr bool) { saved_state := p.save_state() p.clear_state() p.add_text(text) + p.next() if rcbr { p.statements() } else {