From 4a9cc9e20f61071d7e5358f5468020819e4f3b3b Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Sun, 13 Jan 2019 04:10:52 +0100 Subject: [PATCH] Add support for comment passthrough to expression evaluator --- cpreproc.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/cpreproc.py b/cpreproc.py index 3a18dc2..4d0405e 100644 --- a/cpreproc.py +++ b/cpreproc.py @@ -132,15 +132,26 @@ class Evaluator(object): while True: tok = self.token = self.tokens[self.ptr] self.ptr += 1 - if tok.type != 'CPP_WS' or '\n' in tok.value: - break + # Eat whitespace except newlines, and /* */ comments + if (tok.type == 'CPP_WS' and '\n' not in tok.value + or tok.type == 'CPP_COMMENT1' + ): + continue + break except IndexError: # Synthesize a new CPP_WS token with a newline, to signal # end-of-text (we copy it from the last one in the token stream). - self.token = copy.copy(self.tokens[-1]) - self.token.type = 'CPP_WS' - self.token.value = '\n' + tok = self.token = copy.copy(self.tokens[-1]) + tok.type = 'CPP_WS' + tok.value = '\n' + return + + # Single-line comments are line terminators; convert them + if tok.type == 'CPP_COMMENT2': + tok = self.token = copy.copy(tok) + tok.type = 'CPP_WS' + tok.value = '\n' return # Work around a lexing problem in PCPP