annotate ply-3.8/build/lib.linux-x86_64-2.7/ply/lex.py @ 12254:616be78bd12e draft

<oerjan> revert
author HackEso <hackeso@esolangs.org>
date Fri, 06 Dec 2019 07:54:58 +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 # ply: lex.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 # Copyright (C) 2001-2015,
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
5 # David M. Beazley (Dabeaz LLC)
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 # Redistribution and use in source and binary forms, with or without
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
9 # modification, are permitted provided that the following conditions are
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
10 # met:
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 # * Redistributions of source code must retain the above copyright notice,
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
13 # this list of conditions and the following disclaimer.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
14 # * Redistributions in binary form must reproduce the above copyright notice,
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
15 # this list of conditions and the following disclaimer in the documentation
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
16 # and/or other materials provided with the distribution.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
17 # * Neither the name of the David Beazley or Dabeaz LLC may be used to
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
18 # endorse or promote products derived from this software without
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
19 # specific prior written permission.
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 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
24 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
25 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
32 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
33
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
34 __version__ = '3.8'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
35 __tabversion__ = '3.8'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
36
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
37 import re
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
38 import sys
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
39 import types
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
40 import copy
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
41 import os
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
42 import inspect
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
43
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
44 # This tuple contains known string types
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
45 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
46 # Python 2.6
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
47 StringTypes = (types.StringType, types.UnicodeType)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
48 except AttributeError:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
49 # Python 3.0
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
50 StringTypes = (str, bytes)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
51
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
52 # This regular expression is used to match valid token names
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
53 _is_identifier = re.compile(r'^[a-zA-Z0-9_]+$')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
54
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
55 # Exception thrown when invalid token encountered and no default error
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
56 # handler is defined.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
57 class LexError(Exception):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
58 def __init__(self, message, s):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
59 self.args = (message,)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
60 self.text = s
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
61
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
62
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
63 # Token class. This class is used to represent the tokens produced.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
64 class LexToken(object):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
65 def __str__(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
66 return 'LexToken(%s,%r,%d,%d)' % (self.type, self.value, self.lineno, self.lexpos)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
67
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
68 def __repr__(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
69 return str(self)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
70
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 # This object is a stand-in for a logging object created by the
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
73 # logging module.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
74
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
75 class PlyLogger(object):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
76 def __init__(self, f):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
77 self.f = f
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
78
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
79 def critical(self, msg, *args, **kwargs):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
80 self.f.write((msg % args) + '\n')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
81
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
82 def warning(self, msg, *args, **kwargs):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
83 self.f.write('WARNING: ' + (msg % args) + '\n')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
84
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
85 def error(self, msg, *args, **kwargs):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
86 self.f.write('ERROR: ' + (msg % args) + '\n')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
87
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
88 info = critical
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
89 debug = critical
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 # Null logger is used when no output is generated. Does nothing.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
93 class NullLogger(object):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
94 def __getattribute__(self, name):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
95 return self
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 def __call__(self, *args, **kwargs):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
98 return self
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
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
101 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
102 # === Lexing Engine ===
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 # The following Lexer class implements the lexer runtime. There are only
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
105 # a few public methods and attributes:
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 # input() - Store a new string in the lexer
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
108 # token() - Get the next token
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
109 # clone() - Clone the lexer
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 # lineno - Current line number
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
112 # lexpos - Current position in the input string
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
113 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
114
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
115 class Lexer:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
116 def __init__(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
117 self.lexre = None # Master regular expression. This is a list of
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
118 # tuples (re, findex) where re is a compiled
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
119 # regular expression and findex is a list
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
120 # mapping regex group numbers to rules
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
121 self.lexretext = None # Current regular expression strings
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
122 self.lexstatere = {} # Dictionary mapping lexer states to master regexs
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
123 self.lexstateretext = {} # Dictionary mapping lexer states to regex strings
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
124 self.lexstaterenames = {} # Dictionary mapping lexer states to symbol names
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
125 self.lexstate = 'INITIAL' # Current lexer state
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
126 self.lexstatestack = [] # Stack of lexer states
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
127 self.lexstateinfo = None # State information
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
128 self.lexstateignore = {} # Dictionary of ignored characters for each state
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
129 self.lexstateerrorf = {} # Dictionary of error functions for each state
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
130 self.lexstateeoff = {} # Dictionary of eof functions for each state
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
131 self.lexreflags = 0 # Optional re compile flags
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
132 self.lexdata = None # Actual input data (as a string)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
133 self.lexpos = 0 # Current position in input text
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
134 self.lexlen = 0 # Length of the input text
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
135 self.lexerrorf = None # Error rule (if any)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
136 self.lexeoff = None # EOF rule (if any)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
137 self.lextokens = None # List of valid tokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
138 self.lexignore = '' # Ignored characters
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
139 self.lexliterals = '' # Literal characters that can be passed through
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
140 self.lexmodule = None # Module
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
141 self.lineno = 1 # Current line number
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
142 self.lexoptimize = False # Optimized mode
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
143
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
144 def clone(self, object=None):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
145 c = copy.copy(self)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
146
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
147 # If the object parameter has been supplied, it means we are attaching the
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
148 # lexer to a new object. In this case, we have to rebind all methods in
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
149 # the lexstatere and lexstateerrorf tables.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
150
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
151 if object:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
152 newtab = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
153 for key, ritem in self.lexstatere.items():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
154 newre = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
155 for cre, findex in ritem:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
156 newfindex = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
157 for f in findex:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
158 if not f or not f[0]:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
159 newfindex.append(f)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
160 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
161 newfindex.append((getattr(object, f[0].__name__), f[1]))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
162 newre.append((cre, newfindex))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
163 newtab[key] = newre
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
164 c.lexstatere = newtab
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
165 c.lexstateerrorf = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
166 for key, ef in self.lexstateerrorf.items():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
167 c.lexstateerrorf[key] = getattr(object, ef.__name__)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
168 c.lexmodule = object
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
169 return c
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 # writetab() - Write lexer information to a table file
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
173 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
174 def writetab(self, lextab, outputdir=''):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
175 if isinstance(lextab, types.ModuleType):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
176 raise IOError("Won't overwrite existing lextab module")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
177 basetabmodule = lextab.split('.')[-1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
178 filename = os.path.join(outputdir, basetabmodule) + '.py'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
179 with open(filename, 'w') as tf:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
180 tf.write('# %s.py. This file automatically created by PLY (version %s). Don\'t edit!\n' % (basetabmodule, __version__))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
181 tf.write('_tabversion = %s\n' % repr(__tabversion__))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
182 tf.write('_lextokens = %s\n' % repr(self.lextokens))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
183 tf.write('_lexreflags = %s\n' % repr(self.lexreflags))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
184 tf.write('_lexliterals = %s\n' % repr(self.lexliterals))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
185 tf.write('_lexstateinfo = %s\n' % repr(self.lexstateinfo))
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 # Rewrite the lexstatere table, replacing function objects with function names
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
188 tabre = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
189 for statename, lre in self.lexstatere.items():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
190 titem = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
191 for (pat, func), retext, renames in zip(lre, self.lexstateretext[statename], self.lexstaterenames[statename]):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
192 titem.append((retext, _funcs_to_names(func, renames)))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
193 tabre[statename] = titem
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
194
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
195 tf.write('_lexstatere = %s\n' % repr(tabre))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
196 tf.write('_lexstateignore = %s\n' % repr(self.lexstateignore))
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 taberr = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
199 for statename, ef in self.lexstateerrorf.items():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
200 taberr[statename] = ef.__name__ if ef else None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
201 tf.write('_lexstateerrorf = %s\n' % repr(taberr))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
202
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
203 tabeof = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
204 for statename, ef in self.lexstateeoff.items():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
205 tabeof[statename] = ef.__name__ if ef else None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
206 tf.write('_lexstateeoff = %s\n' % repr(tabeof))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
207
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 # readtab() - Read lexer information from a tab file
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
210 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
211 def readtab(self, tabfile, fdict):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
212 if isinstance(tabfile, types.ModuleType):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
213 lextab = tabfile
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 exec('import %s' % tabfile)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
216 lextab = sys.modules[tabfile]
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 if getattr(lextab, '_tabversion', '0.0') != __tabversion__:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
219 raise ImportError('Inconsistent PLY version')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
220
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
221 self.lextokens = lextab._lextokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
222 self.lexreflags = lextab._lexreflags
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
223 self.lexliterals = lextab._lexliterals
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
224 self.lextokens_all = self.lextokens | set(self.lexliterals)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
225 self.lexstateinfo = lextab._lexstateinfo
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
226 self.lexstateignore = lextab._lexstateignore
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
227 self.lexstatere = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
228 self.lexstateretext = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
229 for statename, lre in lextab._lexstatere.items():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
230 titem = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
231 txtitem = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
232 for pat, func_name in lre:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
233 titem.append((re.compile(pat, lextab._lexreflags | re.VERBOSE), _names_to_funcs(func_name, fdict)))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
234
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
235 self.lexstatere[statename] = titem
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
236 self.lexstateretext[statename] = txtitem
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
237
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
238 self.lexstateerrorf = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
239 for statename, ef in lextab._lexstateerrorf.items():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
240 self.lexstateerrorf[statename] = fdict[ef]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
241
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
242 self.lexstateeoff = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
243 for statename, ef in lextab._lexstateeoff.items():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
244 self.lexstateeoff[statename] = fdict[ef]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
245
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
246 self.begin('INITIAL')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
247
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
248 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
249 # input() - Push a new string into the lexer
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
250 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
251 def input(self, s):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
252 # Pull off the first character to see if s looks like a string
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
253 c = s[:1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
254 if not isinstance(c, StringTypes):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
255 raise ValueError('Expected a string')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
256 self.lexdata = s
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
257 self.lexpos = 0
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
258 self.lexlen = len(s)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
259
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
260 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
261 # begin() - Changes the lexing state
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 def begin(self, state):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
264 if state not in self.lexstatere:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
265 raise ValueError('Undefined state')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
266 self.lexre = self.lexstatere[state]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
267 self.lexretext = self.lexstateretext[state]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
268 self.lexignore = self.lexstateignore.get(state, '')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
269 self.lexerrorf = self.lexstateerrorf.get(state, None)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
270 self.lexeoff = self.lexstateeoff.get(state, None)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
271 self.lexstate = state
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
272
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
273 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
274 # push_state() - Changes the lexing state and saves old on stack
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
275 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
276 def push_state(self, state):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
277 self.lexstatestack.append(self.lexstate)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
278 self.begin(state)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
279
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 # pop_state() - Restores the previous state
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
282 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
283 def pop_state(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
284 self.begin(self.lexstatestack.pop())
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
285
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
286 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
287 # current_state() - Returns the current lexing state
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
288 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
289 def current_state(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
290 return self.lexstate
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
291
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
292 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
293 # skip() - Skip ahead n characters
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 def skip(self, n):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
296 self.lexpos += n
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 # opttoken() - Return the next token from the Lexer
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 # Note: This function has been carefully implemented to be as fast
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
302 # as possible. Don't make changes unless you really know what
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
303 # you are doing
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
304 # ------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
305 def token(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
306 # Make local copies of frequently referenced attributes
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
307 lexpos = self.lexpos
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
308 lexlen = self.lexlen
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
309 lexignore = self.lexignore
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
310 lexdata = self.lexdata
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
311
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
312 while lexpos < lexlen:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
313 # This code provides some short-circuit code for whitespace, tabs, and other ignored characters
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
314 if lexdata[lexpos] in lexignore:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
315 lexpos += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
316 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
317
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
318 # Look for a regular expression match
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
319 for lexre, lexindexfunc in self.lexre:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
320 m = lexre.match(lexdata, lexpos)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
321 if not m:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
322 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
323
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
324 # Create a token for return
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
325 tok = LexToken()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
326 tok.value = m.group()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
327 tok.lineno = self.lineno
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
328 tok.lexpos = lexpos
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
329
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
330 i = m.lastindex
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
331 func, tok.type = lexindexfunc[i]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
332
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
333 if not func:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
334 # If no token type was set, it's an ignored token
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
335 if tok.type:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
336 self.lexpos = m.end()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
337 return tok
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
338 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
339 lexpos = m.end()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
340 break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
341
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
342 lexpos = m.end()
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 token is processed by a function, call it
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
345
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
346 tok.lexer = self # Set additional attributes useful in token rules
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
347 self.lexmatch = m
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
348 self.lexpos = lexpos
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 newtok = func(tok)
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 # Every function must return a token, if nothing, we just move to next token
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
353 if not newtok:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
354 lexpos = self.lexpos # This is here in case user has updated lexpos.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
355 lexignore = self.lexignore # This is here in case there was a state change
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
356 break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
357
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
358 # Verify type of the token. If not in the token map, raise an error
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
359 if not self.lexoptimize:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
360 if newtok.type not in self.lextokens_all:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
361 raise LexError("%s:%d: Rule '%s' returned an unknown token type '%s'" % (
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
362 func.__code__.co_filename, func.__code__.co_firstlineno,
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
363 func.__name__, newtok.type), lexdata[lexpos:])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
364
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
365 return newtok
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
366 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
367 # No match, see if in literals
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
368 if lexdata[lexpos] in self.lexliterals:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
369 tok = LexToken()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
370 tok.value = lexdata[lexpos]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
371 tok.lineno = self.lineno
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
372 tok.type = tok.value
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
373 tok.lexpos = lexpos
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
374 self.lexpos = lexpos + 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
375 return tok
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 # No match. Call t_error() if defined.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
378 if self.lexerrorf:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
379 tok = LexToken()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
380 tok.value = self.lexdata[lexpos:]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
381 tok.lineno = self.lineno
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
382 tok.type = 'error'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
383 tok.lexer = self
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
384 tok.lexpos = lexpos
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
385 self.lexpos = lexpos
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
386 newtok = self.lexerrorf(tok)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
387 if lexpos == self.lexpos:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
388 # Error method didn't change text position at all. This is an error.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
389 raise LexError("Scanning error. Illegal character '%s'" % (lexdata[lexpos]), lexdata[lexpos:])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
390 lexpos = self.lexpos
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
391 if not newtok:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
392 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
393 return newtok
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
394
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
395 self.lexpos = lexpos
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
396 raise LexError("Illegal character '%s' at index %d" % (lexdata[lexpos], lexpos), lexdata[lexpos:])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
397
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
398 if self.lexeoff:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
399 tok = LexToken()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
400 tok.type = 'eof'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
401 tok.value = ''
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
402 tok.lineno = self.lineno
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
403 tok.lexpos = lexpos
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
404 tok.lexer = self
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
405 self.lexpos = lexpos
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
406 newtok = self.lexeoff(tok)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
407 return newtok
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
408
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
409 self.lexpos = lexpos + 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
410 if self.lexdata is None:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
411 raise RuntimeError('No input string given with input()')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
412 return None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
413
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
414 # Iterator interface
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
415 def __iter__(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
416 return self
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
417
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
418 def next(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
419 t = self.token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
420 if t is None:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
421 raise StopIteration
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
422 return t
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
423
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
424 __next__ = next
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
425
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 # ==== Lex Builder ===
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
428 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
429 # The functions and classes below are used to collect lexing information
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
430 # and build a Lexer object from it.
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
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 # _get_regex(func)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
435 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
436 # Returns the regular expression assigned to a function either as a doc string
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
437 # or as a .regex attribute attached by the @TOKEN decorator.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
438 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
439 def _get_regex(func):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
440 return getattr(func, 'regex', func.__doc__)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
441
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
442 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
443 # get_caller_module_dict()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
444 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
445 # This function returns a dictionary containing all of the symbols defined within
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
446 # a caller further down the call stack. This is used to get the environment
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
447 # associated with the yacc() call if none was provided.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
448 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
449 def get_caller_module_dict(levels):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
450 f = sys._getframe(levels)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
451 ldict = f.f_globals.copy()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
452 if f.f_globals != f.f_locals:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
453 ldict.update(f.f_locals)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
454 return ldict
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
455
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
456 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
457 # _funcs_to_names()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
458 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
459 # Given a list of regular expression functions, this converts it to a list
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
460 # suitable for output to a table file
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
461 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
462 def _funcs_to_names(funclist, namelist):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
463 result = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
464 for f, name in zip(funclist, namelist):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
465 if f and f[0]:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
466 result.append((name, f[1]))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
467 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
468 result.append(f)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
469 return result
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 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
472 # _names_to_funcs()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
473 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
474 # Given a list of regular expression function names, this converts it back to
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
475 # functions.
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 def _names_to_funcs(namelist, fdict):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
478 result = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
479 for n in namelist:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
480 if n and n[0]:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
481 result.append((fdict[n[0]], n[1]))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
482 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
483 result.append(n)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
484 return result
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
485
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
486 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
487 # _form_master_re()
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 # This function takes a list of all of the regex components and attempts to
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
490 # form the master regular expression. Given limitations in the Python re
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
491 # module, it may be necessary to break the master regex into separate expressions.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
492 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
493 def _form_master_re(relist, reflags, ldict, toknames):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
494 if not relist:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
495 return []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
496 regex = '|'.join(relist)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
497 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
498 lexre = re.compile(regex, re.VERBOSE | reflags)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
499
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
500 # Build the index to function map for the matching engine
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
501 lexindexfunc = [None] * (max(lexre.groupindex.values()) + 1)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
502 lexindexnames = lexindexfunc[:]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
503
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
504 for f, i in lexre.groupindex.items():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
505 handle = ldict.get(f, None)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
506 if type(handle) in (types.FunctionType, types.MethodType):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
507 lexindexfunc[i] = (handle, toknames[f])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
508 lexindexnames[i] = f
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
509 elif handle is not None:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
510 lexindexnames[i] = f
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
511 if f.find('ignore_') > 0:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
512 lexindexfunc[i] = (None, None)
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 lexindexfunc[i] = (None, toknames[f])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
515
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
516 return [(lexre, lexindexfunc)], [regex], [lexindexnames]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
517 except Exception:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
518 m = int(len(relist)/2)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
519 if m == 0:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
520 m = 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
521 llist, lre, lnames = _form_master_re(relist[:m], reflags, ldict, toknames)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
522 rlist, rre, rnames = _form_master_re(relist[m:], reflags, ldict, toknames)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
523 return (llist+rlist), (lre+rre), (lnames+rnames)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
524
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
525 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
526 # def _statetoken(s,names)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
527 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
528 # Given a declaration name s of the form "t_" and a dictionary whose keys are
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
529 # state names, this function returns a tuple (states,tokenname) where states
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
530 # is a tuple of state names and tokenname is the name of the token. For example,
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
531 # calling this with s = "t_foo_bar_SPAM" might return (('foo','bar'),'SPAM')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
532 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
533 def _statetoken(s, names):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
534 nonstate = 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
535 parts = s.split('_')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
536 for i, part in enumerate(parts[1:], 1):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
537 if part not in names and part != 'ANY':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
538 break
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 if i > 1:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
541 states = tuple(parts[1:i])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
542 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
543 states = ('INITIAL',)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
544
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
545 if 'ANY' in states:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
546 states = tuple(names)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
547
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
548 tokenname = '_'.join(parts[i:])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
549 return (states, tokenname)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
550
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
551
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
552 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
553 # LexerReflect()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
554 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
555 # This class represents information needed to build a lexer as extracted from a
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
556 # user's input file.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
557 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
558 class LexerReflect(object):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
559 def __init__(self, ldict, log=None, reflags=0):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
560 self.ldict = ldict
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
561 self.error_func = None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
562 self.tokens = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
563 self.reflags = reflags
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
564 self.stateinfo = {'INITIAL': 'inclusive'}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
565 self.modules = set()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
566 self.error = False
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
567 self.log = PlyLogger(sys.stderr) if log is None else log
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
568
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
569 # Get all of the basic information
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
570 def get_all(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
571 self.get_tokens()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
572 self.get_literals()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
573 self.get_states()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
574 self.get_rules()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
575
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
576 # Validate all of the information
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
577 def validate_all(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
578 self.validate_tokens()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
579 self.validate_literals()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
580 self.validate_rules()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
581 return self.error
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
582
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
583 # Get the tokens map
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
584 def get_tokens(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
585 tokens = self.ldict.get('tokens', None)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
586 if not tokens:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
587 self.log.error('No token list is defined')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
588 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
589 return
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
590
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
591 if not isinstance(tokens, (list, tuple)):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
592 self.log.error('tokens must be a list or tuple')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
593 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
594 return
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
595
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
596 if not tokens:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
597 self.log.error('tokens is empty')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
598 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
599 return
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 self.tokens = tokens
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 # Validate the tokens
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
604 def validate_tokens(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
605 terminals = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
606 for n in self.tokens:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
607 if not _is_identifier.match(n):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
608 self.log.error("Bad token name '%s'", n)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
609 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
610 if n in terminals:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
611 self.log.warning("Token '%s' multiply defined", n)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
612 terminals[n] = 1
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 # Get the literals specifier
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
615 def get_literals(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
616 self.literals = self.ldict.get('literals', '')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
617 if not self.literals:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
618 self.literals = ''
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 # Validate literals
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
621 def validate_literals(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
622 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
623 for c in self.literals:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
624 if not isinstance(c, StringTypes) or len(c) > 1:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
625 self.log.error('Invalid literal %s. Must be a single character', repr(c))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
626 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
627
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
628 except TypeError:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
629 self.log.error('Invalid literals specification. literals must be a sequence of characters')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
630 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
631
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
632 def get_states(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
633 self.states = self.ldict.get('states', None)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
634 # Build statemap
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
635 if self.states:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
636 if not isinstance(self.states, (tuple, list)):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
637 self.log.error('states must be defined as a tuple or list')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
638 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
639 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
640 for s in self.states:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
641 if not isinstance(s, tuple) or len(s) != 2:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
642 self.log.error("Invalid state specifier %s. Must be a tuple (statename,'exclusive|inclusive')", repr(s))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
643 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
644 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
645 name, statetype = s
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
646 if not isinstance(name, StringTypes):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
647 self.log.error('State name %s must be a string', repr(name))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
648 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
649 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
650 if not (statetype == 'inclusive' or statetype == 'exclusive'):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
651 self.log.error("State type for state %s must be 'inclusive' or 'exclusive'", name)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
652 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
653 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
654 if name in self.stateinfo:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
655 self.log.error("State '%s' already defined", name)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
656 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
657 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
658 self.stateinfo[name] = statetype
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
659
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
660 # Get all of the symbols with a t_ prefix and sort them into various
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
661 # categories (functions, strings, error functions, and ignore characters)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
662
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
663 def get_rules(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
664 tsymbols = [f for f in self.ldict if f[:2] == 't_']
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
665
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
666 # Now build up a list of functions and a list of strings
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
667 self.toknames = {} # Mapping of symbols to token names
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
668 self.funcsym = {} # Symbols defined as functions
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
669 self.strsym = {} # Symbols defined as strings
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
670 self.ignore = {} # Ignore strings by state
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
671 self.errorf = {} # Error functions by state
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
672 self.eoff = {} # EOF functions by state
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
673
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
674 for s in self.stateinfo:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
675 self.funcsym[s] = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
676 self.strsym[s] = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
677
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
678 if len(tsymbols) == 0:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
679 self.log.error('No rules of the form t_rulename are defined')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
680 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
681 return
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
682
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
683 for f in tsymbols:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
684 t = self.ldict[f]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
685 states, tokname = _statetoken(f, self.stateinfo)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
686 self.toknames[f] = tokname
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
687
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
688 if hasattr(t, '__call__'):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
689 if tokname == 'error':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
690 for s in states:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
691 self.errorf[s] = t
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
692 elif tokname == 'eof':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
693 for s in states:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
694 self.eoff[s] = t
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
695 elif tokname == 'ignore':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
696 line = t.__code__.co_firstlineno
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
697 file = t.__code__.co_filename
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
698 self.log.error("%s:%d: Rule '%s' must be defined as a string", file, line, t.__name__)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
699 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
700 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
701 for s in states:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
702 self.funcsym[s].append((f, t))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
703 elif isinstance(t, StringTypes):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
704 if tokname == 'ignore':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
705 for s in states:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
706 self.ignore[s] = t
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
707 if '\\' in t:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
708 self.log.warning("%s contains a literal backslash '\\'", f)
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 tokname == 'error':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
711 self.log.error("Rule '%s' must be defined as a function", f)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
712 self.error = True
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 for s in states:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
715 self.strsym[s].append((f, t))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
716 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
717 self.log.error('%s not defined as a function or string', f)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
718 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
719
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
720 # Sort the functions by line number
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
721 for f in self.funcsym.values():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
722 f.sort(key=lambda x: x[1].__code__.co_firstlineno)
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 # Sort the strings by regular expression length
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
725 for s in self.strsym.values():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
726 s.sort(key=lambda x: len(x[1]), reverse=True)
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 # Validate all of the t_rules collected
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
729 def validate_rules(self):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
730 for state in self.stateinfo:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
731 # Validate all rules defined by functions
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 for fname, f in self.funcsym[state]:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
734 line = f.__code__.co_firstlineno
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
735 file = f.__code__.co_filename
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
736 module = inspect.getmodule(f)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
737 self.modules.add(module)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
738
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
739 tokname = self.toknames[fname]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
740 if isinstance(f, types.MethodType):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
741 reqargs = 2
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
742 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
743 reqargs = 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
744 nargs = f.__code__.co_argcount
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
745 if nargs > reqargs:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
746 self.log.error("%s:%d: Rule '%s' has too many arguments", file, line, f.__name__)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
747 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
748 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
749
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
750 if nargs < reqargs:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
751 self.log.error("%s:%d: Rule '%s' requires an argument", file, line, f.__name__)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
752 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
753 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
754
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
755 if not _get_regex(f):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
756 self.log.error("%s:%d: No regular expression defined for rule '%s'", file, line, f.__name__)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
757 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
758 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
759
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
760 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
761 c = re.compile('(?P<%s>%s)' % (fname, _get_regex(f)), re.VERBOSE | self.reflags)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
762 if c.match(''):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
763 self.log.error("%s:%d: Regular expression for rule '%s' matches empty string", file, line, f.__name__)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
764 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
765 except re.error as e:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
766 self.log.error("%s:%d: Invalid regular expression for rule '%s'. %s", file, line, f.__name__, e)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
767 if '#' in _get_regex(f):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
768 self.log.error("%s:%d. Make sure '#' in rule '%s' is escaped with '\\#'", file, line, f.__name__)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
769 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
770
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
771 # Validate all rules defined by strings
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
772 for name, r in self.strsym[state]:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
773 tokname = self.toknames[name]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
774 if tokname == 'error':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
775 self.log.error("Rule '%s' must be defined as a function", name)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
776 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
777 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
778
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
779 if tokname not in self.tokens and tokname.find('ignore_') < 0:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
780 self.log.error("Rule '%s' defined for an unspecified token %s", name, tokname)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
781 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
782 continue
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
783
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
784 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
785 c = re.compile('(?P<%s>%s)' % (name, r), re.VERBOSE | self.reflags)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
786 if (c.match('')):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
787 self.log.error("Regular expression for rule '%s' matches empty string", name)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
788 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
789 except re.error as e:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
790 self.log.error("Invalid regular expression for rule '%s'. %s", name, e)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
791 if '#' in r:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
792 self.log.error("Make sure '#' in rule '%s' is escaped with '\\#'", name)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
793 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
794
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
795 if not self.funcsym[state] and not self.strsym[state]:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
796 self.log.error("No rules defined for state '%s'", state)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
797 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
798
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
799 # Validate the error function
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
800 efunc = self.errorf.get(state, None)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
801 if efunc:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
802 f = efunc
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
803 line = f.__code__.co_firstlineno
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
804 file = f.__code__.co_filename
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
805 module = inspect.getmodule(f)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
806 self.modules.add(module)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
807
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
808 if isinstance(f, types.MethodType):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
809 reqargs = 2
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
810 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
811 reqargs = 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
812 nargs = f.__code__.co_argcount
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
813 if nargs > reqargs:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
814 self.log.error("%s:%d: Rule '%s' has too many arguments", file, line, f.__name__)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
815 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
816
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
817 if nargs < reqargs:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
818 self.log.error("%s:%d: Rule '%s' requires an argument", file, line, f.__name__)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
819 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
820
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
821 for module in self.modules:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
822 self.validate_module(module)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
823
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
824 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
825 # validate_module()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
826 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
827 # This checks to see if there are duplicated t_rulename() functions or strings
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
828 # in the parser input file. This is done using a simple regular expression
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
829 # match on each line in the source code of the given module.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
830 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
831
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
832 def validate_module(self, module):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
833 lines, linen = inspect.getsourcelines(module)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
834
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
835 fre = re.compile(r'\s*def\s+(t_[a-zA-Z_0-9]*)\(')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
836 sre = re.compile(r'\s*(t_[a-zA-Z_0-9]*)\s*=')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
837
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
838 counthash = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
839 linen += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
840 for line in lines:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
841 m = fre.match(line)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
842 if not m:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
843 m = sre.match(line)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
844 if m:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
845 name = m.group(1)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
846 prev = counthash.get(name)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
847 if not prev:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
848 counthash[name] = linen
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
849 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
850 filename = inspect.getsourcefile(module)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
851 self.log.error('%s:%d: Rule %s redefined. Previously defined on line %d', filename, linen, name, prev)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
852 self.error = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
853 linen += 1
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
854
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
855 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
856 # lex(module)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
857 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
858 # Build all of the regular expression rules from definitions in the supplied module
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 def lex(module=None, object=None, debug=False, optimize=False, lextab='lextab',
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
861 reflags=0, nowarn=False, outputdir=None, debuglog=None, errorlog=None):
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 if lextab is None:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
864 lextab = 'lextab'
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
865
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
866 global lexer
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
867
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
868 ldict = None
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
869 stateinfo = {'INITIAL': 'inclusive'}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
870 lexobj = Lexer()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
871 lexobj.lexoptimize = optimize
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
872 global token, input
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 if errorlog is None:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
875 errorlog = PlyLogger(sys.stderr)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
876
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
877 if debug:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
878 if debuglog is None:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
879 debuglog = PlyLogger(sys.stderr)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
880
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
881 # Get the module dictionary used for the lexer
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
882 if object:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
883 module = object
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
884
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
885 # Get the module dictionary used for the parser
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
886 if module:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
887 _items = [(k, getattr(module, k)) for k in dir(module)]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
888 ldict = dict(_items)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
889 # If no __file__ attribute is available, try to obtain it from the __module__ instead
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
890 if '__file__' not in ldict:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
891 ldict['__file__'] = sys.modules[ldict['__module__']].__file__
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
892 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
893 ldict = get_caller_module_dict(2)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
894
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
895 # Determine if the module is package of a package or not.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
896 # If so, fix the tabmodule setting so that tables load correctly
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
897 pkg = ldict.get('__package__')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
898 if pkg and isinstance(lextab, str):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
899 if '.' not in lextab:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
900 lextab = pkg + '.' + lextab
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 # Collect parser information from the dictionary
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
903 linfo = LexerReflect(ldict, log=errorlog, reflags=reflags)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
904 linfo.get_all()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
905 if not optimize:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
906 if linfo.validate_all():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
907 raise SyntaxError("Can't build lexer")
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
908
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
909 if optimize and lextab:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
910 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
911 lexobj.readtab(lextab, ldict)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
912 token = lexobj.token
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
913 input = lexobj.input
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
914 lexer = lexobj
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
915 return lexobj
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
916
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
917 except ImportError:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
918 pass
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
919
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
920 # Dump some basic debugging information
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
921 if debug:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
922 debuglog.info('lex: tokens = %r', linfo.tokens)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
923 debuglog.info('lex: literals = %r', linfo.literals)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
924 debuglog.info('lex: states = %r', linfo.stateinfo)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
925
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
926 # Build a dictionary of valid token names
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
927 lexobj.lextokens = set()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
928 for n in linfo.tokens:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
929 lexobj.lextokens.add(n)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
930
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
931 # Get literals specification
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
932 if isinstance(linfo.literals, (list, tuple)):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
933 lexobj.lexliterals = type(linfo.literals[0])().join(linfo.literals)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
934 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
935 lexobj.lexliterals = linfo.literals
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
936
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
937 lexobj.lextokens_all = lexobj.lextokens | set(lexobj.lexliterals)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
938
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
939 # Get the stateinfo dictionary
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
940 stateinfo = linfo.stateinfo
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
941
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
942 regexs = {}
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
943 # Build the master regular expressions
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
944 for state in stateinfo:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
945 regex_list = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
946
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
947 # Add rules defined by functions first
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
948 for fname, f in linfo.funcsym[state]:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
949 line = f.__code__.co_firstlineno
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
950 file = f.__code__.co_filename
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
951 regex_list.append('(?P<%s>%s)' % (fname, _get_regex(f)))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
952 if debug:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
953 debuglog.info("lex: Adding rule %s -> '%s' (state '%s')", fname, _get_regex(f), state)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
954
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
955 # Now add all of the simple rules
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
956 for name, r in linfo.strsym[state]:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
957 regex_list.append('(?P<%s>%s)' % (name, r))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
958 if debug:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
959 debuglog.info("lex: Adding rule %s -> '%s' (state '%s')", name, r, state)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
960
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
961 regexs[state] = regex_list
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
962
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
963 # Build the master regular expressions
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
964
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
965 if debug:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
966 debuglog.info('lex: ==== MASTER REGEXS FOLLOW ====')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
967
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
968 for state in regexs:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
969 lexre, re_text, re_names = _form_master_re(regexs[state], reflags, ldict, linfo.toknames)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
970 lexobj.lexstatere[state] = lexre
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
971 lexobj.lexstateretext[state] = re_text
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
972 lexobj.lexstaterenames[state] = re_names
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
973 if debug:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
974 for i, text in enumerate(re_text):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
975 debuglog.info("lex: state '%s' : regex[%d] = '%s'", state, i, text)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
976
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
977 # For inclusive states, we need to add the regular expressions from the INITIAL state
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
978 for state, stype in stateinfo.items():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
979 if state != 'INITIAL' and stype == 'inclusive':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
980 lexobj.lexstatere[state].extend(lexobj.lexstatere['INITIAL'])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
981 lexobj.lexstateretext[state].extend(lexobj.lexstateretext['INITIAL'])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
982 lexobj.lexstaterenames[state].extend(lexobj.lexstaterenames['INITIAL'])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
983
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
984 lexobj.lexstateinfo = stateinfo
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
985 lexobj.lexre = lexobj.lexstatere['INITIAL']
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
986 lexobj.lexretext = lexobj.lexstateretext['INITIAL']
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
987 lexobj.lexreflags = reflags
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
988
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
989 # Set up ignore variables
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
990 lexobj.lexstateignore = linfo.ignore
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
991 lexobj.lexignore = lexobj.lexstateignore.get('INITIAL', '')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
992
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
993 # Set up error functions
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
994 lexobj.lexstateerrorf = linfo.errorf
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
995 lexobj.lexerrorf = linfo.errorf.get('INITIAL', None)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
996 if not lexobj.lexerrorf:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
997 errorlog.warning('No t_error rule is defined')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
998
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
999 # Set up eof functions
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1000 lexobj.lexstateeoff = linfo.eoff
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1001 lexobj.lexeoff = linfo.eoff.get('INITIAL', None)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1002
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1003 # Check state information for ignore and error rules
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1004 for s, stype in stateinfo.items():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1005 if stype == 'exclusive':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1006 if s not in linfo.errorf:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1007 errorlog.warning("No error rule is defined for exclusive state '%s'", s)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1008 if s not in linfo.ignore and lexobj.lexignore:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1009 errorlog.warning("No ignore rule is defined for exclusive state '%s'", s)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1010 elif stype == 'inclusive':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1011 if s not in linfo.errorf:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1012 linfo.errorf[s] = linfo.errorf.get('INITIAL', None)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1013 if s not in linfo.ignore:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1014 linfo.ignore[s] = linfo.ignore.get('INITIAL', '')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1015
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1016 # Create global versions of the token() and input() functions
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1017 token = lexobj.token
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1018 input = lexobj.input
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1019 lexer = lexobj
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1020
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1021 # If in optimize mode, we write the lextab
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1022 if lextab and optimize:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1023 if outputdir is None:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1024 # If no output directory is set, the location of the output files
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1025 # is determined according to the following rules:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1026 # - If lextab specifies a package, files go into that package directory
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1027 # - Otherwise, files go in the same directory as the specifying module
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1028 if isinstance(lextab, types.ModuleType):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1029 srcfile = lextab.__file__
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1030 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1031 if '.' not in lextab:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1032 srcfile = ldict['__file__']
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1033 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1034 parts = lextab.split('.')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1035 pkgname = '.'.join(parts[:-1])
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1036 exec('import %s' % pkgname)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1037 srcfile = getattr(sys.modules[pkgname], '__file__', '')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1038 outputdir = os.path.dirname(srcfile)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1039 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1040 lexobj.writetab(lextab, outputdir)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1041 except IOError as e:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1042 errorlog.warning("Couldn't write lextab module %r. %s" % (lextab, e))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1043
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1044 return lexobj
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1045
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1046 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1047 # runmain()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1048 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1049 # This runs the lexer as a main program
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1050 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1051
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1052 def runmain(lexer=None, data=None):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1053 if not data:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1054 try:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1055 filename = sys.argv[1]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1056 f = open(filename)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1057 data = f.read()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1058 f.close()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1059 except IndexError:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1060 sys.stdout.write('Reading from standard input (type EOF to end):\n')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1061 data = sys.stdin.read()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1062
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1063 if lexer:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1064 _input = lexer.input
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1065 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1066 _input = input
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1067 _input(data)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1068 if lexer:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1069 _token = lexer.token
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1070 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1071 _token = token
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1072
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1073 while True:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1074 tok = _token()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1075 if not tok:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1076 break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1077 sys.stdout.write('(%s,%r,%d,%d)\n' % (tok.type, tok.value, tok.lineno, tok.lexpos))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1078
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1079 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1080 # @TOKEN(regex)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1081 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1082 # This decorator function can be used to set the regex expression on a function
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1083 # when its docstring might need to be set in an alternative way
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1084 # -----------------------------------------------------------------------------
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1085
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1086 def TOKEN(r):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1087 def set_regex(f):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1088 if hasattr(r, '__call__'):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1089 f.regex = _get_regex(r)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1090 else:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1091 f.regex = r
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1092 return f
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1093 return set_regex
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1094
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1095 # Alternative spelling of the TOKEN decorator
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1096 Token = TOKEN
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1097