annotate ply-3.8/build/lib.linux-x86_64-2.7/ply/cpp.py @ 12285:69f123e9b0a2 draft

<b_jonas> STOP
author HackEso <hackeso@esolangs.org>
date Wed, 01 Jan 2020 09:35:37 +0000
parents a1845676eaa0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7268
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
2 # cpp.py
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
3 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
4 # Author: David Beazley (http://www.dabeaz.com)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
5 # Copyright (C) 2007
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
6 # All rights reserved
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
7 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
8 # This module implements an ANSI-C style lexical preprocessor for PLY.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
9 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
10 from __future__ import generators
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
11
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
12 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
13 # Default preprocessor lexer definitions. These tokens are enough to get
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
14 # a basic preprocessor working. Other modules may import these if they want
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
15 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
16
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
17 tokens = (
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
18 'CPP_ID','CPP_INTEGER', 'CPP_FLOAT', 'CPP_STRING', 'CPP_CHAR', 'CPP_WS', 'CPP_COMMENT1', 'CPP_COMMENT2', 'CPP_POUND','CPP_DPOUND'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
19 )
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
20
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
21 literals = "+-*/%|&~^<>=!?()[]{}.,;:\\\'\""
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
22
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
23 # Whitespace
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
24 def t_CPP_WS(t):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
25 r'\s+'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
26 t.lexer.lineno += t.value.count("\n")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
27 return t
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
28
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
29 t_CPP_POUND = r'\#'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
30 t_CPP_DPOUND = r'\#\#'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
31
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
32 # Identifier
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
33 t_CPP_ID = r'[A-Za-z_][\w_]*'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
34
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
35 # Integer literal
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
36 def CPP_INTEGER(t):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
37 r'(((((0x)|(0X))[0-9a-fA-F]+)|(\d+))([uU][lL]|[lL][uU]|[uU]|[lL])?)'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
38 return t
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
39
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
40 t_CPP_INTEGER = CPP_INTEGER
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
41
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
42 # Floating literal
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
43 t_CPP_FLOAT = r'((\d+)(\.\d+)(e(\+|-)?(\d+))? | (\d+)e(\+|-)?(\d+))([lL]|[fF])?'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
44
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
45 # String literal
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
46 def t_CPP_STRING(t):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
47 r'\"([^\\\n]|(\\(.|\n)))*?\"'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
48 t.lexer.lineno += t.value.count("\n")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
49 return t
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
50
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
51 # Character constant 'c' or L'c'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
52 def t_CPP_CHAR(t):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
53 r'(L)?\'([^\\\n]|(\\(.|\n)))*?\''
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
54 t.lexer.lineno += t.value.count("\n")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
55 return t
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
56
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
57 # Comment
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
58 def t_CPP_COMMENT1(t):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
59 r'(/\*(.|\n)*?\*/)'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
60 ncr = t.value.count("\n")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
61 t.lexer.lineno += ncr
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
62 # replace with one space or a number of '\n'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
63 t.type = 'CPP_WS'; t.value = '\n' * ncr if ncr else ' '
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
64 return t
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
65
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
66 # Line comment
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
67 def t_CPP_COMMENT2(t):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
68 r'(//.*?(\n|$))'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
69 # replace with '/n'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
70 t.type = 'CPP_WS'; t.value = '\n'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
71
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
72 def t_error(t):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
73 t.type = t.value[0]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
74 t.value = t.value[0]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
75 t.lexer.skip(1)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
76 return t
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
77
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
78 import re
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
79 import copy
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
80 import time
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
81 import os.path
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
82
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
83 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
84 # trigraph()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
85 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
86 # Given an input string, this function replaces all trigraph sequences.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
87 # The following mapping is used:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
88 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
89 # ??= #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
90 # ??/ \
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
91 # ??' ^
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
92 # ??( [
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
93 # ??) ]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
94 # ??! |
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
95 # ??< {
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
96 # ??> }
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
97 # ??- ~
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
98 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
99
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
100 _trigraph_pat = re.compile(r'''\?\?[=/\'\(\)\!<>\-]''')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
101 _trigraph_rep = {
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
102 '=':'#',
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
103 '/':'\\',
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
104 "'":'^',
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
105 '(':'[',
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
106 ')':']',
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
107 '!':'|',
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
108 '<':'{',
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
109 '>':'}',
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
110 '-':'~'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
111 }
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
112
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
113 def trigraph(input):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
114 return _trigraph_pat.sub(lambda g: _trigraph_rep[g.group()[-1]],input)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
115
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
116 # ------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
117 # Macro object
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
118 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
119 # This object holds information about preprocessor macros
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
120 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
121 # .name - Macro name (string)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
122 # .value - Macro value (a list of tokens)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
123 # .arglist - List of argument names
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
124 # .variadic - Boolean indicating whether or not variadic macro
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
125 # .vararg - Name of the variadic parameter
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
126 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
127 # When a macro is created, the macro replacement token sequence is
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
128 # pre-scanned and used to create patch lists that are later used
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
129 # during macro expansion
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
130 # ------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
131
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
132 class Macro(object):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
133 def __init__(self,name,value,arglist=None,variadic=False):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
134 self.name = name
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
135 self.value = value
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
136 self.arglist = arglist
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
137 self.variadic = variadic
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
138 if variadic:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
139 self.vararg = arglist[-1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
140 self.source = None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
141
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
142 # ------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
143 # Preprocessor object
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
144 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
145 # Object representing a preprocessor. Contains macro definitions,
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
146 # include directories, and other information
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
147 # ------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
148
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
149 class Preprocessor(object):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
150 def __init__(self,lexer=None):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
151 if lexer is None:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
152 lexer = lex.lexer
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
153 self.lexer = lexer
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
154 self.macros = { }
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
155 self.path = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
156 self.temp_path = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
157
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
158 # Probe the lexer for selected tokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
159 self.lexprobe()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
160
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
161 tm = time.localtime()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
162 self.define("__DATE__ \"%s\"" % time.strftime("%b %d %Y",tm))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
163 self.define("__TIME__ \"%s\"" % time.strftime("%H:%M:%S",tm))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
164 self.parser = None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
165
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
166 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
167 # tokenize()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
168 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
169 # Utility function. Given a string of text, tokenize into a list of tokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
170 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
171
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
172 def tokenize(self,text):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
173 tokens = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
174 self.lexer.input(text)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
175 while True:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
176 tok = self.lexer.token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
177 if not tok: break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
178 tokens.append(tok)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
179 return tokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
180
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
181 # ---------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
182 # error()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
183 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
184 # Report a preprocessor error/warning of some kind
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
185 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
186
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
187 def error(self,file,line,msg):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
188 print("%s:%d %s" % (file,line,msg))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
189
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
190 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
191 # lexprobe()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
192 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
193 # This method probes the preprocessor lexer object to discover
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
194 # the token types of symbols that are important to the preprocessor.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
195 # If this works right, the preprocessor will simply "work"
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
196 # with any suitable lexer regardless of how tokens have been named.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
197 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
198
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
199 def lexprobe(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
200
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
201 # Determine the token type for identifiers
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
202 self.lexer.input("identifier")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
203 tok = self.lexer.token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
204 if not tok or tok.value != "identifier":
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
205 print("Couldn't determine identifier type")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
206 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
207 self.t_ID = tok.type
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
208
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
209 # Determine the token type for integers
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
210 self.lexer.input("12345")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
211 tok = self.lexer.token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
212 if not tok or int(tok.value) != 12345:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
213 print("Couldn't determine integer type")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
214 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
215 self.t_INTEGER = tok.type
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
216 self.t_INTEGER_TYPE = type(tok.value)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
217
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
218 # Determine the token type for strings enclosed in double quotes
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
219 self.lexer.input("\"filename\"")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
220 tok = self.lexer.token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
221 if not tok or tok.value != "\"filename\"":
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
222 print("Couldn't determine string type")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
223 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
224 self.t_STRING = tok.type
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
225
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
226 # Determine the token type for whitespace--if any
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
227 self.lexer.input(" ")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
228 tok = self.lexer.token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
229 if not tok or tok.value != " ":
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
230 self.t_SPACE = None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
231 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
232 self.t_SPACE = tok.type
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
233
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
234 # Determine the token type for newlines
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
235 self.lexer.input("\n")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
236 tok = self.lexer.token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
237 if not tok or tok.value != "\n":
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
238 self.t_NEWLINE = None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
239 print("Couldn't determine token for newlines")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
240 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
241 self.t_NEWLINE = tok.type
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
242
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
243 self.t_WS = (self.t_SPACE, self.t_NEWLINE)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
244
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
245 # Check for other characters used by the preprocessor
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
246 chars = [ '<','>','#','##','\\','(',')',',','.']
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
247 for c in chars:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
248 self.lexer.input(c)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
249 tok = self.lexer.token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
250 if not tok or tok.value != c:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
251 print("Unable to lex '%s' required for preprocessor" % c)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
252
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
253 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
254 # add_path()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
255 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
256 # Adds a search path to the preprocessor.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
257 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
258
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
259 def add_path(self,path):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
260 self.path.append(path)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
261
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
262 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
263 # group_lines()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
264 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
265 # Given an input string, this function splits it into lines. Trailing whitespace
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
266 # is removed. Any line ending with \ is grouped with the next line. This
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
267 # function forms the lowest level of the preprocessor---grouping into text into
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
268 # a line-by-line format.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
269 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
270
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
271 def group_lines(self,input):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
272 lex = self.lexer.clone()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
273 lines = [x.rstrip() for x in input.splitlines()]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
274 for i in xrange(len(lines)):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
275 j = i+1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
276 while lines[i].endswith('\\') and (j < len(lines)):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
277 lines[i] = lines[i][:-1]+lines[j]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
278 lines[j] = ""
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
279 j += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
280
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
281 input = "\n".join(lines)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
282 lex.input(input)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
283 lex.lineno = 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
284
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
285 current_line = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
286 while True:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
287 tok = lex.token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
288 if not tok:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
289 break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
290 current_line.append(tok)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
291 if tok.type in self.t_WS and '\n' in tok.value:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
292 yield current_line
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
293 current_line = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
294
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
295 if current_line:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
296 yield current_line
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
297
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
298 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
299 # tokenstrip()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
300 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
301 # Remove leading/trailing whitespace tokens from a token list
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
302 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
303
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
304 def tokenstrip(self,tokens):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
305 i = 0
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
306 while i < len(tokens) and tokens[i].type in self.t_WS:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
307 i += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
308 del tokens[:i]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
309 i = len(tokens)-1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
310 while i >= 0 and tokens[i].type in self.t_WS:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
311 i -= 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
312 del tokens[i+1:]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
313 return tokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
314
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
315
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
316 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
317 # collect_args()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
318 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
319 # Collects comma separated arguments from a list of tokens. The arguments
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
320 # must be enclosed in parenthesis. Returns a tuple (tokencount,args,positions)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
321 # where tokencount is the number of tokens consumed, args is a list of arguments,
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
322 # and positions is a list of integers containing the starting index of each
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
323 # argument. Each argument is represented by a list of tokens.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
324 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
325 # When collecting arguments, leading and trailing whitespace is removed
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
326 # from each argument.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
327 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
328 # This function properly handles nested parenthesis and commas---these do not
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
329 # define new arguments.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
330 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
331
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
332 def collect_args(self,tokenlist):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
333 args = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
334 positions = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
335 current_arg = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
336 nesting = 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
337 tokenlen = len(tokenlist)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
338
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
339 # Search for the opening '('.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
340 i = 0
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
341 while (i < tokenlen) and (tokenlist[i].type in self.t_WS):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
342 i += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
343
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
344 if (i < tokenlen) and (tokenlist[i].value == '('):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
345 positions.append(i+1)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
346 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
347 self.error(self.source,tokenlist[0].lineno,"Missing '(' in macro arguments")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
348 return 0, [], []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
349
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
350 i += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
351
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
352 while i < tokenlen:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
353 t = tokenlist[i]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
354 if t.value == '(':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
355 current_arg.append(t)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
356 nesting += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
357 elif t.value == ')':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
358 nesting -= 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
359 if nesting == 0:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
360 if current_arg:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
361 args.append(self.tokenstrip(current_arg))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
362 positions.append(i)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
363 return i+1,args,positions
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
364 current_arg.append(t)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
365 elif t.value == ',' and nesting == 1:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
366 args.append(self.tokenstrip(current_arg))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
367 positions.append(i+1)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
368 current_arg = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
369 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
370 current_arg.append(t)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
371 i += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
372
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
373 # Missing end argument
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
374 self.error(self.source,tokenlist[-1].lineno,"Missing ')' in macro arguments")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
375 return 0, [],[]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
376
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
377 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
378 # macro_prescan()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
379 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
380 # Examine the macro value (token sequence) and identify patch points
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
381 # This is used to speed up macro expansion later on---we'll know
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
382 # right away where to apply patches to the value to form the expansion
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
383 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
384
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
385 def macro_prescan(self,macro):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
386 macro.patch = [] # Standard macro arguments
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
387 macro.str_patch = [] # String conversion expansion
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
388 macro.var_comma_patch = [] # Variadic macro comma patch
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
389 i = 0
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
390 while i < len(macro.value):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
391 if macro.value[i].type == self.t_ID and macro.value[i].value in macro.arglist:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
392 argnum = macro.arglist.index(macro.value[i].value)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
393 # Conversion of argument to a string
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
394 if i > 0 and macro.value[i-1].value == '#':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
395 macro.value[i] = copy.copy(macro.value[i])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
396 macro.value[i].type = self.t_STRING
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
397 del macro.value[i-1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
398 macro.str_patch.append((argnum,i-1))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
399 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
400 # Concatenation
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
401 elif (i > 0 and macro.value[i-1].value == '##'):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
402 macro.patch.append(('c',argnum,i-1))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
403 del macro.value[i-1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
404 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
405 elif ((i+1) < len(macro.value) and macro.value[i+1].value == '##'):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
406 macro.patch.append(('c',argnum,i))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
407 i += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
408 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
409 # Standard expansion
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
410 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
411 macro.patch.append(('e',argnum,i))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
412 elif macro.value[i].value == '##':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
413 if macro.variadic and (i > 0) and (macro.value[i-1].value == ',') and \
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
414 ((i+1) < len(macro.value)) and (macro.value[i+1].type == self.t_ID) and \
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
415 (macro.value[i+1].value == macro.vararg):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
416 macro.var_comma_patch.append(i-1)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
417 i += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
418 macro.patch.sort(key=lambda x: x[2],reverse=True)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
419
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
420 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
421 # macro_expand_args()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
422 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
423 # Given a Macro and list of arguments (each a token list), this method
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
424 # returns an expanded version of a macro. The return value is a token sequence
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
425 # representing the replacement macro tokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
426 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
427
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
428 def macro_expand_args(self,macro,args):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
429 # Make a copy of the macro token sequence
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
430 rep = [copy.copy(_x) for _x in macro.value]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
431
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
432 # Make string expansion patches. These do not alter the length of the replacement sequence
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
433
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
434 str_expansion = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
435 for argnum, i in macro.str_patch:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
436 if argnum not in str_expansion:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
437 str_expansion[argnum] = ('"%s"' % "".join([x.value for x in args[argnum]])).replace("\\","\\\\")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
438 rep[i] = copy.copy(rep[i])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
439 rep[i].value = str_expansion[argnum]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
440
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
441 # Make the variadic macro comma patch. If the variadic macro argument is empty, we get rid
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
442 comma_patch = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
443 if macro.variadic and not args[-1]:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
444 for i in macro.var_comma_patch:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
445 rep[i] = None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
446 comma_patch = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
447
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
448 # Make all other patches. The order of these matters. It is assumed that the patch list
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
449 # has been sorted in reverse order of patch location since replacements will cause the
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
450 # size of the replacement sequence to expand from the patch point.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
451
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
452 expanded = { }
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
453 for ptype, argnum, i in macro.patch:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
454 # Concatenation. Argument is left unexpanded
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
455 if ptype == 'c':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
456 rep[i:i+1] = args[argnum]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
457 # Normal expansion. Argument is macro expanded first
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
458 elif ptype == 'e':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
459 if argnum not in expanded:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
460 expanded[argnum] = self.expand_macros(args[argnum])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
461 rep[i:i+1] = expanded[argnum]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
462
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
463 # Get rid of removed comma if necessary
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
464 if comma_patch:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
465 rep = [_i for _i in rep if _i]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
466
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
467 return rep
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
468
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
469
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
470 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
471 # expand_macros()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
472 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
473 # Given a list of tokens, this function performs macro expansion.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
474 # The expanded argument is a dictionary that contains macros already
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
475 # expanded. This is used to prevent infinite recursion.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
476 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
477
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
478 def expand_macros(self,tokens,expanded=None):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
479 if expanded is None:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
480 expanded = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
481 i = 0
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
482 while i < len(tokens):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
483 t = tokens[i]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
484 if t.type == self.t_ID:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
485 if t.value in self.macros and t.value not in expanded:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
486 # Yes, we found a macro match
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
487 expanded[t.value] = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
488
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
489 m = self.macros[t.value]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
490 if not m.arglist:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
491 # A simple macro
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
492 ex = self.expand_macros([copy.copy(_x) for _x in m.value],expanded)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
493 for e in ex:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
494 e.lineno = t.lineno
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
495 tokens[i:i+1] = ex
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
496 i += len(ex)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
497 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
498 # A macro with arguments
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
499 j = i + 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
500 while j < len(tokens) and tokens[j].type in self.t_WS:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
501 j += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
502 if tokens[j].value == '(':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
503 tokcount,args,positions = self.collect_args(tokens[j:])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
504 if not m.variadic and len(args) != len(m.arglist):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
505 self.error(self.source,t.lineno,"Macro %s requires %d arguments" % (t.value,len(m.arglist)))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
506 i = j + tokcount
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
507 elif m.variadic and len(args) < len(m.arglist)-1:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
508 if len(m.arglist) > 2:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
509 self.error(self.source,t.lineno,"Macro %s must have at least %d arguments" % (t.value, len(m.arglist)-1))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
510 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
511 self.error(self.source,t.lineno,"Macro %s must have at least %d argument" % (t.value, len(m.arglist)-1))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
512 i = j + tokcount
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
513 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
514 if m.variadic:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
515 if len(args) == len(m.arglist)-1:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
516 args.append([])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
517 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
518 args[len(m.arglist)-1] = tokens[j+positions[len(m.arglist)-1]:j+tokcount-1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
519 del args[len(m.arglist):]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
520
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
521 # Get macro replacement text
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
522 rep = self.macro_expand_args(m,args)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
523 rep = self.expand_macros(rep,expanded)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
524 for r in rep:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
525 r.lineno = t.lineno
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
526 tokens[i:j+tokcount] = rep
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
527 i += len(rep)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
528 del expanded[t.value]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
529 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
530 elif t.value == '__LINE__':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
531 t.type = self.t_INTEGER
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
532 t.value = self.t_INTEGER_TYPE(t.lineno)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
533
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
534 i += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
535 return tokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
536
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
537 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
538 # evalexpr()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
539 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
540 # Evaluate an expression token sequence for the purposes of evaluating
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
541 # integral expressions.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
542 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
543
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
544 def evalexpr(self,tokens):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
545 # tokens = tokenize(line)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
546 # Search for defined macros
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
547 i = 0
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
548 while i < len(tokens):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
549 if tokens[i].type == self.t_ID and tokens[i].value == 'defined':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
550 j = i + 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
551 needparen = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
552 result = "0L"
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
553 while j < len(tokens):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
554 if tokens[j].type in self.t_WS:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
555 j += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
556 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
557 elif tokens[j].type == self.t_ID:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
558 if tokens[j].value in self.macros:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
559 result = "1L"
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
560 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
561 result = "0L"
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
562 if not needparen: break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
563 elif tokens[j].value == '(':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
564 needparen = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
565 elif tokens[j].value == ')':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
566 break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
567 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
568 self.error(self.source,tokens[i].lineno,"Malformed defined()")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
569 j += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
570 tokens[i].type = self.t_INTEGER
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
571 tokens[i].value = self.t_INTEGER_TYPE(result)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
572 del tokens[i+1:j+1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
573 i += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
574 tokens = self.expand_macros(tokens)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
575 for i,t in enumerate(tokens):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
576 if t.type == self.t_ID:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
577 tokens[i] = copy.copy(t)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
578 tokens[i].type = self.t_INTEGER
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
579 tokens[i].value = self.t_INTEGER_TYPE("0L")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
580 elif t.type == self.t_INTEGER:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
581 tokens[i] = copy.copy(t)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
582 # Strip off any trailing suffixes
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
583 tokens[i].value = str(tokens[i].value)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
584 while tokens[i].value[-1] not in "0123456789abcdefABCDEF":
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
585 tokens[i].value = tokens[i].value[:-1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
586
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
587 expr = "".join([str(x.value) for x in tokens])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
588 expr = expr.replace("&&"," and ")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
589 expr = expr.replace("||"," or ")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
590 expr = expr.replace("!"," not ")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
591 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
592 result = eval(expr)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
593 except StandardError:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
594 self.error(self.source,tokens[0].lineno,"Couldn't evaluate expression")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
595 result = 0
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
596 return result
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
597
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
598 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
599 # parsegen()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
600 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
601 # Parse an input string/
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
602 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
603 def parsegen(self,input,source=None):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
604
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
605 # Replace trigraph sequences
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
606 t = trigraph(input)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
607 lines = self.group_lines(t)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
608
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
609 if not source:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
610 source = ""
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
611
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
612 self.define("__FILE__ \"%s\"" % source)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
613
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
614 self.source = source
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
615 chunk = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
616 enable = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
617 iftrigger = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
618 ifstack = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
619
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
620 for x in lines:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
621 for i,tok in enumerate(x):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
622 if tok.type not in self.t_WS: break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
623 if tok.value == '#':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
624 # Preprocessor directive
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
625
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
626 # insert necessary whitespace instead of eaten tokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
627 for tok in x:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
628 if tok.type in self.t_WS and '\n' in tok.value:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
629 chunk.append(tok)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
630
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
631 dirtokens = self.tokenstrip(x[i+1:])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
632 if dirtokens:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
633 name = dirtokens[0].value
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
634 args = self.tokenstrip(dirtokens[1:])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
635 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
636 name = ""
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
637 args = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
638
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
639 if name == 'define':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
640 if enable:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
641 for tok in self.expand_macros(chunk):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
642 yield tok
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
643 chunk = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
644 self.define(args)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
645 elif name == 'include':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
646 if enable:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
647 for tok in self.expand_macros(chunk):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
648 yield tok
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
649 chunk = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
650 oldfile = self.macros['__FILE__']
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
651 for tok in self.include(args):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
652 yield tok
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
653 self.macros['__FILE__'] = oldfile
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
654 self.source = source
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
655 elif name == 'undef':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
656 if enable:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
657 for tok in self.expand_macros(chunk):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
658 yield tok
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
659 chunk = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
660 self.undef(args)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
661 elif name == 'ifdef':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
662 ifstack.append((enable,iftrigger))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
663 if enable:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
664 if not args[0].value in self.macros:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
665 enable = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
666 iftrigger = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
667 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
668 iftrigger = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
669 elif name == 'ifndef':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
670 ifstack.append((enable,iftrigger))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
671 if enable:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
672 if args[0].value in self.macros:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
673 enable = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
674 iftrigger = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
675 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
676 iftrigger = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
677 elif name == 'if':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
678 ifstack.append((enable,iftrigger))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
679 if enable:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
680 result = self.evalexpr(args)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
681 if not result:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
682 enable = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
683 iftrigger = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
684 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
685 iftrigger = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
686 elif name == 'elif':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
687 if ifstack:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
688 if ifstack[-1][0]: # We only pay attention if outer "if" allows this
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
689 if enable: # If already true, we flip enable False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
690 enable = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
691 elif not iftrigger: # If False, but not triggered yet, we'll check expression
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
692 result = self.evalexpr(args)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
693 if result:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
694 enable = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
695 iftrigger = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
696 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
697 self.error(self.source,dirtokens[0].lineno,"Misplaced #elif")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
698
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
699 elif name == 'else':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
700 if ifstack:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
701 if ifstack[-1][0]:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
702 if enable:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
703 enable = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
704 elif not iftrigger:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
705 enable = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
706 iftrigger = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
707 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
708 self.error(self.source,dirtokens[0].lineno,"Misplaced #else")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
709
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
710 elif name == 'endif':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
711 if ifstack:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
712 enable,iftrigger = ifstack.pop()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
713 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
714 self.error(self.source,dirtokens[0].lineno,"Misplaced #endif")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
715 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
716 # Unknown preprocessor directive
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
717 pass
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
718
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
719 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
720 # Normal text
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
721 if enable:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
722 chunk.extend(x)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
723
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
724 for tok in self.expand_macros(chunk):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
725 yield tok
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
726 chunk = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
727
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
728 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
729 # include()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
730 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
731 # Implementation of file-inclusion
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
732 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
733
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
734 def include(self,tokens):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
735 # Try to extract the filename and then process an include file
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
736 if not tokens:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
737 return
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
738 if tokens:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
739 if tokens[0].value != '<' and tokens[0].type != self.t_STRING:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
740 tokens = self.expand_macros(tokens)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
741
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
742 if tokens[0].value == '<':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
743 # Include <...>
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
744 i = 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
745 while i < len(tokens):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
746 if tokens[i].value == '>':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
747 break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
748 i += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
749 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
750 print("Malformed #include <...>")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
751 return
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
752 filename = "".join([x.value for x in tokens[1:i]])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
753 path = self.path + [""] + self.temp_path
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
754 elif tokens[0].type == self.t_STRING:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
755 filename = tokens[0].value[1:-1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
756 path = self.temp_path + [""] + self.path
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
757 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
758 print("Malformed #include statement")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
759 return
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
760 for p in path:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
761 iname = os.path.join(p,filename)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
762 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
763 data = open(iname,"r").read()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
764 dname = os.path.dirname(iname)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
765 if dname:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
766 self.temp_path.insert(0,dname)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
767 for tok in self.parsegen(data,filename):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
768 yield tok
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
769 if dname:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
770 del self.temp_path[0]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
771 break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
772 except IOError:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
773 pass
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
774 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
775 print("Couldn't find '%s'" % filename)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
776
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
777 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
778 # define()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
779 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
780 # Define a new macro
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
781 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
782
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
783 def define(self,tokens):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
784 if isinstance(tokens,(str,unicode)):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
785 tokens = self.tokenize(tokens)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
786
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
787 linetok = tokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
788 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
789 name = linetok[0]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
790 if len(linetok) > 1:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
791 mtype = linetok[1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
792 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
793 mtype = None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
794 if not mtype:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
795 m = Macro(name.value,[])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
796 self.macros[name.value] = m
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
797 elif mtype.type in self.t_WS:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
798 # A normal macro
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
799 m = Macro(name.value,self.tokenstrip(linetok[2:]))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
800 self.macros[name.value] = m
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
801 elif mtype.value == '(':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
802 # A macro with arguments
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
803 tokcount, args, positions = self.collect_args(linetok[1:])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
804 variadic = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
805 for a in args:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
806 if variadic:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
807 print("No more arguments may follow a variadic argument")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
808 break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
809 astr = "".join([str(_i.value) for _i in a])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
810 if astr == "...":
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
811 variadic = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
812 a[0].type = self.t_ID
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
813 a[0].value = '__VA_ARGS__'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
814 variadic = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
815 del a[1:]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
816 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
817 elif astr[-3:] == "..." and a[0].type == self.t_ID:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
818 variadic = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
819 del a[1:]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
820 # If, for some reason, "." is part of the identifier, strip off the name for the purposes
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
821 # of macro expansion
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
822 if a[0].value[-3:] == '...':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
823 a[0].value = a[0].value[:-3]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
824 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
825 if len(a) > 1 or a[0].type != self.t_ID:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
826 print("Invalid macro argument")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
827 break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
828 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
829 mvalue = self.tokenstrip(linetok[1+tokcount:])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
830 i = 0
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
831 while i < len(mvalue):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
832 if i+1 < len(mvalue):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
833 if mvalue[i].type in self.t_WS and mvalue[i+1].value == '##':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
834 del mvalue[i]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
835 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
836 elif mvalue[i].value == '##' and mvalue[i+1].type in self.t_WS:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
837 del mvalue[i+1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
838 i += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
839 m = Macro(name.value,mvalue,[x[0].value for x in args],variadic)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
840 self.macro_prescan(m)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
841 self.macros[name.value] = m
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
842 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
843 print("Bad macro definition")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
844 except LookupError:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
845 print("Bad macro definition")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
846
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
847 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
848 # undef()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
849 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
850 # Undefine a macro
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
851 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
852
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
853 def undef(self,tokens):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
854 id = tokens[0].value
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
855 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
856 del self.macros[id]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
857 except LookupError:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
858 pass
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
859
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
860 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
861 # parse()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
862 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
863 # Parse input text.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
864 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
865 def parse(self,input,source=None,ignore={}):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
866 self.ignore = ignore
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
867 self.parser = self.parsegen(input,source)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
868
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
869 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
870 # token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
871 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
872 # Method to return individual tokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
873 # ----------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
874 def token(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
875 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
876 while True:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
877 tok = next(self.parser)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
878 if tok.type not in self.ignore: return tok
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
879 except StopIteration:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
880 self.parser = None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
881 return None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
882
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
883 if __name__ == '__main__':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
884 import ply.lex as lex
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
885 lexer = lex.lex()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
886
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
887 # Run a preprocessor
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
888 import sys
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
889 f = open(sys.argv[1])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
890 input = f.read()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
891
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
892 p = Preprocessor(lexer)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
893 p.parse(input,sys.argv[1])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
894 while True:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
895 tok = p.token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
896 if not tok: break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
897 print(p.source, tok)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
898
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
899
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
900
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
901
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
902
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
903
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
904
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
905
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
906
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
907
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
908