From 941b4189a17cbb465494621e46f0cef25d43be88 Mon Sep 17 00:00:00 2001 From: StunxFS Date: Mon, 25 Dec 2023 13:05:51 -0400 Subject: [PATCH] self-hosted: remove preprocessor code --- lib/rivet/src/tokenizer/next.ri | 12 +- lib/rivet/src/tokenizer/preprocessor.ri | 208 ------------------------ 2 files changed, 3 insertions(+), 217 deletions(-) delete mode 100644 lib/rivet/src/tokenizer/preprocessor.ri diff --git a/lib/rivet/src/tokenizer/next.ri b/lib/rivet/src/tokenizer/next.ri index 9bc264a23..334f047e0 100644 --- a/lib/rivet/src/tokenizer/next.ri +++ b/lib/rivet/src/tokenizer/next.ri @@ -1,5 +1,5 @@ -// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this -// source code is governed by an MIT license that can be found in the LICENSE +// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this +// source code is governed by an MIT license that can be found in the LICENSE // file. import ../token; @@ -209,13 +209,7 @@ extend Tokenizer { } return self.new_token(.Question); }, - '#' -> { - if nextc == '[' || nextc == '!' { - return self.new_token(.Hash); - } - self.pp_directive(); - continue; - }, + '#' -> return self.new_token(.Hash), '&' -> { if nextc == '&' && self.look_ahead(2).is_space() { self.pos += 1; diff --git a/lib/rivet/src/tokenizer/preprocessor.ri b/lib/rivet/src/tokenizer/preprocessor.ri deleted file mode 100644 index 1fe8a2e52..000000000 --- a/lib/rivet/src/tokenizer/preprocessor.ri +++ /dev/null @@ -1,208 +0,0 @@ -// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this -// source code is governed by an MIT license that can be found in the LICENSE -// file. - -import ../utils; - -extend Tokenizer { - struct Conditional { - mut matched: bool; - mut else_found: bool; - mut skip_section: bool; - } - - func pp_directive(mut self) { - mut pos := self.current_pos(); - self.pos += 1; // skip '#' - self.skip_whitespace(); - pp_dir := self.read_name(); - pos.len += pp_dir.len; - self.skip_whitespace(); - match pp_dir { - "if" -> self.pp_if(), - "else_if" -> self.pp_else_if(), - "else" -> self.pp_else(), - "endif" -> self.pp_endif(), - "error", "warn" -> self.pp_err_warn(pp_dir), - else -> { - self.error("invalid preprocessing directive: `{}`".fmt(pp_dir), pos); - return; - } - } - if self.conditional_stack.len > 0 - && self.conditional_stack[self.conditional_stack.len - 1].skip_section { - // skip tokens until next preprocessing directive - while self.pos < self.text.len : self.pos += 1 { - cc := self.current_char(); - if cc == '#' && !(self.matches("#error", self.pos) || self.matches("#warn", self.pos)) { - self.pos -= 1; - return; - } else if utils.is_new_line(cc) { - self.inc_line_number(); - } - } - // if we get EOF, then no corresponding `#endif` has been written - if self.pos == self.text.len { - self.error("expected `#endif`, found end of file", pos); - } - } - } - - func pp_if(mut self) { - self.pos += 1; - self.skip_whitespace(); - cond := self.pp_expression(); - self.skip_whitespace(); - self.pos -= 1; - self.conditional_stack.push(Conditional()); - if cond && (self.conditional_stack.len == 1 - || !self.conditional_stack[self.conditional_stack.len - 2].skip_section) { - // condition true -> process code within if - self.conditional_stack[self.conditional_stack.len - 1].matched = true; - self.conditional_stack[self.conditional_stack.len - 1].skip_section = false; - } else { - // skip lines until next preprocessing directive - self.conditional_stack[self.conditional_stack.len - 1].skip_section = true; - } - } - - func pp_else_if(mut self) { - self.pos += 1; - pos := self.current_pos(); - self.skip_whitespace(); - cond := self.pp_expression(); - self.skip_whitespace(); - self.pos -= 1; - if self.conditional_stack.is_empty() || - self.conditional_stack[self.conditional_stack.len - 1].else_found { - self.error("unexpected `#else_if`", pos); - } else if cond && !self.conditional_stack[self.conditional_stack.len - 1].matched - && (self.conditional_stack.len == 1 || - !self.conditional_stack[self.conditional_stack.len - 2].skip_section) { - // condition true -> process code within if - self.conditional_stack[self.conditional_stack.len - 1].matched = true; - self.conditional_stack[self.conditional_stack.len - 1].skip_section = false; - } else { - // skip lines until next preprocessing directive - self.conditional_stack[self.conditional_stack.len - 1].skip_section = true; - } - } - - func pp_else(mut self) { - pos := self.current_pos(); - self.skip_whitespace(); - if self.conditional_stack.is_empty() || - self.conditional_stack[self.conditional_stack.len - 1].else_found { - self.error("unexpected `#else`", pos); - } else if !self.conditional_stack[self.conditional_stack.len - 1].matched - && (self.conditional_stack.len == 1 || - !self.conditional_stack[self.conditional_stack.len - 2].skip_section) { - // condition true -> process code within if - self.conditional_stack[self.conditional_stack.len - 1].matched = true; - self.conditional_stack[self.conditional_stack.len - 1].skip_section = false; - } else { - // skip lines until next preprocessing directive - self.conditional_stack[self.conditional_stack.len - 1].skip_section = true; - } - } - - func pp_endif(mut self) { - if self.conditional_stack.is_empty() { - self.error("unexpected `#endif`", self.current_pos()); - } else { - _ = self.conditional_stack.pop(); - } - } - - func pp_err_warn(mut self, kw: string) { - self.pos += 1; - mut pos := self.current_pos(); - self.skip_whitespace(); - start := self.pos; - self.eat_to_end_of_line(); - pos += self.current_pos(); - self.inc_line_number(); - msg := self.text.substr(start, self.pos).trim_space(); - if kw == "error" { - self.error("#error: ".concat(msg), pos); - } else { - self.warn("#warn: ".concat(msg), pos); - } - } - - func pp_expression(mut self) -> bool { - return self.pp_or_expression(); - } - - func pp_or_expression(mut self) -> bool { - mut left := self.pp_and_expression(); - self.skip_whitespace(); - while self.pos < self.text.len && self.matches("||", self.pos) { - self.pos += 2; - self.skip_whitespace(); - right := self.pp_and_expression(); - left = left || right; - } - return left; - } - - func pp_and_expression(mut self) -> bool { - mut left := self.pp_unary_expression(); - self.skip_whitespace(); - while self.pos < self.text.len && self.matches("&&", self.pos) { - self.pos += 2; - self.skip_whitespace(); - right := self.pp_unary_expression(); - left = left && right; - } - return left; - } - - func pp_unary_expression(mut self) -> bool { - if self.pos < self.text.len && self.current_char() == '!' { - self.pos += 1; - self.skip_whitespace(); - return !self.pp_unary_expression(); - } - return self.pp_primary_expression(); - } - - func pp_primary_expression(mut self) -> bool { - pos := self.current_pos(); - mut cc := self.current_char(); - if self.pos >= self.text.len { - self.error("expected name, found end of file", pos); - } else if utils.is_name_char(cc) { - return self.pp_symbol(); - } else if cc == '(' { - self.pos += 1; - self.skip_whitespace(); - result := self.pp_expression(); - self.skip_whitespace(); - cc = self.current_char(); - if self.pos < self.text.len && cc == ')' { - self.pos += 1; - } else { - self.error("expected `)`, found `{}`".fmt(cc.to_ascii()), self.current_pos()); - } - return result; - } - return false; - } - - func pp_symbol(mut self) -> bool { - self.pos -= 1; - mut pos := self.current_pos(); - self.pos += 1; - name := self.read_name(); - self.pos += 1; - pos.len += name.len; - return if name == "true" { - true - } else if name == "false" { - false - } else { - self.env.evalue_pp_symbol(name, pos) - }; - } -}