Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
4d8e859e
Commit
4d8e859e
authored
Jan 01, 1992
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial revision
parent
42d1f63c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
0 deletions
+60
-0
Lib/tokenize.py
Lib/tokenize.py
+60
-0
No files found.
Lib/tokenize.py
0 → 100644
View file @
4d8e859e
# This module compiles a regular expression that recognizes Python tokens.
# It is designed to match the working of the Python tokenizer exactly.
# It takes care of everything except indentation;
# note that un-escaped newlines are tokens, too.
# tokenprog.regs[3] gives the location of the token without whitespace
# It also defines various subexpressions, but doesn't compile them.
# See the function test() below for an example of how to use.
import
regex
# Note: to get a quoted backslash in a regexp, it must be quadrupled.
Ignore
=
'[
\
t
]*
\
(
\
\
\
\
\
n[
\
t
]*
\
)*
\
(#.*
\
)?
'
Name = '
[
a
-
zA
-
Z_
][
a
-
zA
-
Z0
-
9
_
]
*
'
Hexnumber = '
0
[
xX
][
0
-
9
a
-
fA
-
F
]
*
[
lL
]
?
'
Octnumber = '
0
[
0
-
7
]
*
[
lL
]
?
'
Decnumber = '
[
1
-
9
][
0
-
9
]
*
[
lL
]
?
'
Intnumber = Hexnumber + '
\
|
' + Octnumber + '
\
|
' + Decnumber
Exponent = '
[
eE
][
-+
]
?
[
0
-
9
]
+
'
Pointfloat = '
\
([
0
-
9
]
+
\
.[
0
-
9
]
*
\
|
\
.[
0
-
9
]
+
\
)
\
(
' + Exponent + '
\
)
?
'
Expfloat = '
[
0
-
9
]
+
' + Exponent
Floatnumber = Pointfloat + '
\
|
' + Expfloat
Number = Intnumber + '
\
|
' + Floatnumber
String = '
\
'
\
(
\
\
\
\
.
\
|[^
\
\
\
n
\
'
]
\
)*
\
''
Operator = '
~
\
|
\
+
\
|-
\
|
\
*
\
|/
\
|%
\
|
\
^
\
|&
\
||
\
|<<
\
|>>
\
|==
\
|<=
\
|<>
\
|!=
\
|>=
\
|=
\
|<
\
|>
'
Bracket = '
[][(){}]
'
Special = '
[:;.,
`
\
n
]
'
Funny = Operator + '
\
|
' + Bracket + '
\
|
' + Special
PlainToken = Name + '
\
|
' + Number + '
\
|
' + String + '
\
|
' + Funny
Token = Ignore + '
\
(
' + PlainToken + '
\
)
'
try:
save_syntax = regex.set_syntax(0) # Use default syntax
tokenprog = regex.compile(Token)
finally:
dummy = regex.set_syntax(save_syntax) # Restore original syntax
def test(file):
f = open(file, 'r')
while 1:
line = f.readline()
if not line: break
i, n = 0, len(line)
while i < n:
j = tokenprog.match(line, i)
if j < 0:
print '
No
token
at
', `line[i:i+20]` + '
...
'
i = i+1
else:
i = i+j
a, b = tokenprog.regs[3]
if a < b:
print '
Token
:
', `line[a:b]`
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment