diff options
Diffstat (limited to 'pycparser/ply/cpp.py')
-rw-r--r-- | pycparser/ply/cpp.py | 61 |
1 files changed, 26 insertions, 35 deletions
diff --git a/pycparser/ply/cpp.py b/pycparser/ply/cpp.py index 7288ddd..bd28721 100644 --- a/pycparser/ply/cpp.py +++ b/pycparser/ply/cpp.py @@ -3,9 +3,10 @@ # # Author: David Beazley (http://www.dabeaz.com) # Eli Bendersky [http://eli.thegreenplace.net] +# Copyright (C) 2017 # All rights reserved # -# This module implements an ANSI-C style lexical preprocessor for PLY. +# This module implements an ANSI-C style lexical preprocessor for PLY. # ----------------------------------------------------------------------------- from __future__ import generators @@ -77,7 +78,8 @@ def t_CPP_COMMENT2(t): r'(//.*?(\n|$))' # replace with '/n' t.type = 'CPP_WS'; t.value = '\n' - + return t + def t_error(t): t.type = t.value[0] t.value = t.value[0] @@ -91,8 +93,8 @@ import os.path # ----------------------------------------------------------------------------- # trigraph() -# -# Given an input string, this function replaces all trigraph sequences. +# +# Given an input string, this function replaces all trigraph sequences. # The following mapping is used: # # ??= # @@ -262,7 +264,7 @@ class Preprocessor(object): # ---------------------------------------------------------------------- # add_path() # - # Adds a search path to the preprocessor. + # Adds a search path to the preprocessor. # ---------------------------------------------------------------------- def add_path(self,path): @@ -306,7 +308,7 @@ class Preprocessor(object): # ---------------------------------------------------------------------- # tokenstrip() - # + # # Remove leading/trailing whitespace tokens from a token list # ---------------------------------------------------------------------- @@ -332,7 +334,7 @@ class Preprocessor(object): # argument. Each argument is represented by a list of tokens. # # When collecting arguments, leading and trailing whitespace is removed - # from each argument. + # from each argument. # # This function properly handles nested parenthesis and commas---these do not # define new arguments. @@ -344,7 +346,7 @@ class Preprocessor(object): current_arg = [] nesting = 1 tokenlen = len(tokenlist) - + # Search for the opening '('. i = 0 while (i < tokenlen) and (tokenlist[i].type in self.t_WS): @@ -378,7 +380,7 @@ class Preprocessor(object): else: current_arg.append(t) i += 1 - + # Missing end argument self.error(self.source,tokenlist[-1].lineno,"Missing ')' in macro arguments") return 0, [],[] @@ -390,9 +392,9 @@ class Preprocessor(object): # This is used to speed up macro expansion later on---we'll know # right away where to apply patches to the value to form the expansion # ---------------------------------------------------------------------- - + def macro_prescan(self,macro): - macro.patch = [] # Standard macro arguments + macro.patch = [] # Standard macro arguments macro.str_patch = [] # String conversion expansion macro.var_comma_patch = [] # Variadic macro comma patch i = 0 @@ -439,7 +441,7 @@ class Preprocessor(object): rep = [copy.copy(_x) for _x in macro.value] # Make string expansion patches. These do not alter the length of the replacement sequence - + str_expansion = {} for argnum, i in macro.str_patch: if argnum not in str_expansion: @@ -457,7 +459,7 @@ class Preprocessor(object): # Make all other patches. The order of these matters. It is assumed that the patch list # has been sorted in reverse order of patch location since replacements will cause the # size of the replacement sequence to expand from the patch point. - + expanded = { } for ptype, argnum, i in macro.patch: # Concatenation. Argument is left unexpanded @@ -494,7 +496,7 @@ class Preprocessor(object): if t.value in self.macros and t.value not in expanded: # Yes, we found a macro match expanded[t.value] = True - + m = self.macros[t.value] if not m.arglist: # A simple macro @@ -526,7 +528,7 @@ class Preprocessor(object): else: args[len(m.arglist)-1] = tokens[j+positions[len(m.arglist)-1]:j+tokcount-1] del args[len(m.arglist):] - + # Get macro replacement text rep = self.macro_expand_args(m,args) rep = self.expand_macros(rep,expanded) @@ -539,13 +541,13 @@ class Preprocessor(object): elif t.value == '__LINE__': t.type = self.t_INTEGER t.value = self.t_INTEGER_TYPE(t.lineno) - + i += 1 return tokens - # ---------------------------------------------------------------------- + # ---------------------------------------------------------------------- # evalexpr() - # + # # Evaluate an expression token sequence for the purposes of evaluating # integral expressions. # ---------------------------------------------------------------------- @@ -592,7 +594,7 @@ class Preprocessor(object): tokens[i].value = str(tokens[i].value) while tokens[i].value[-1] not in "0123456789abcdefABCDEF": tokens[i].value = tokens[i].value[:-1] - + expr = "".join([str(x.value) for x in tokens]) expr = expr.replace("&&"," and ") expr = expr.replace("||"," or ") @@ -617,7 +619,7 @@ class Preprocessor(object): if not source: source = "" - + self.define("__FILE__ \"%s\"" % source) self.source = source @@ -636,7 +638,7 @@ class Preprocessor(object): for tok in x: if tok.type in self.t_WS and '\n' in tok.value: chunk.append(tok) - + dirtokens = self.tokenstrip(x[i+1:]) if dirtokens: name = dirtokens[0].value @@ -644,7 +646,7 @@ class Preprocessor(object): else: name = "" args = [] - + if name == 'define': if enable: for tok in self.expand_macros(chunk): @@ -704,7 +706,7 @@ class Preprocessor(object): iftrigger = True else: self.error(self.source,dirtokens[0].lineno,"Misplaced #elif") - + elif name == 'else': if ifstack: if ifstack[-1][0]: @@ -874,7 +876,7 @@ class Preprocessor(object): def parse(self,input,source=None,ignore={}): self.ignore = ignore self.parser = self.parsegen(input,source) - + # ---------------------------------------------------------------------- # token() # @@ -904,14 +906,3 @@ if __name__ == '__main__': tok = p.token() if not tok: break print(p.source, tok) - - - - - - - - - - - |