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
c3361b9a
Commit
c3361b9a
authored
Aug 03, 2014
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Plain Diff
merge
parents
bbeac6eb
c566431b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
37 deletions
+38
-37
Doc/library/re.rst
Doc/library/re.rst
+38
-37
No files found.
Doc/library/re.rst
View file @
c3361b9a
...
...
@@ -1333,7 +1333,7 @@ successive matches::
Token = collections.namedtuple('Token', ['typ', 'value', 'line', 'column'])
def tokenize(
s
):
def tokenize(
code
):
keywords = {'IF', 'THEN', 'ENDIF', 'FOR', 'NEXT', 'GOSUB', 'RETURN'}
token_specification = [
('NUMBER', r'\d+(\.\d*)?'), # Integer or decimal number
...
...
@@ -1343,26 +1343,27 @@ successive matches::
('OP', r'[+\-*/]'), # Arithmetic operators
('NEWLINE', r'\n'), # Line endings
('SKIP', r'[ \t]+'), # Skip over spaces and tabs
('MISMATCH',r'.'), # Any other character
]
tok_regex = '|'.join('(?P<%s>%s)' % pair for pair in token_specification)
get_token = re.compile(tok_regex).match
line = 1
pos =
line_start = 0
mo = get_token(s)
while mo is not None:
typ = mo.lastgroup
if
typ
== 'NEWLINE':
line_start =
pos
line += 1
elif
typ !
= 'SKIP':
val = mo.group(typ)
if typ == 'ID' and val in keywords
:
typ = val
yield Token(typ, val, line, mo.start()-line_start)
pos = mo.end()
mo = get_token(s, pos)
if pos != len(s):
raise RuntimeError('Unexpected character %r on line %d' %(s[pos], line)
)
line
_num
= 1
line_start = 0
for mo in re.finditer(tok_regex, code):
kind = mo.lastgroup
value = mo.group(kind)
if
kind
== 'NEWLINE':
line_start =
mo.end()
line
_num
+= 1
elif
kind =
= 'SKIP':
pass
elif kind == 'MISMATCH'
:
raise RuntimeError('%r unexpected on line %d' % (value, line_num))
else:
if kind == 'ID' and value in keywords:
kind = value
column = mo.start() - line_start
yield Token(kind, value, line_num, column
)
statements = '''
IF quantity THEN
...
...
@@ -1376,22 +1377,22 @@ successive matches::
The tokenizer produces the following output::
Token(typ='IF', value='IF', line=2, column=
5
)
Token(typ='ID', value='quantity', line=2, column=
8
)
Token(typ='THEN', value='THEN', line=2, column=1
7
)
Token(typ='ID', value='total', line=3, column=
9
)
Token(typ='ASSIGN', value=':=', line=3, column=1
5
)
Token(typ='ID', value='total', line=3, column=1
8
)
Token(typ='OP', value='+', line=3, column=2
4
)
Token(typ='ID', value='price', line=3, column=2
6
)
Token(typ='OP', value='*', line=3, column=3
2
)
Token(typ='ID', value='quantity', line=3, column=3
4
)
Token(typ='END', value=';', line=3, column=4
2
)
Token(typ='ID', value='tax', line=4, column=
9
)
Token(typ='ASSIGN', value=':=', line=4, column=1
3
)
Token(typ='ID', value='price', line=4, column=1
6
)
Token(typ='OP', value='*', line=4, column=2
2
)
Token(typ='NUMBER', value='0.05', line=4, column=2
4
)
Token(typ='END', value=';', line=4, column=2
8
)
Token(typ='ENDIF', value='ENDIF', line=5, column=
5
)
Token(typ='END', value=';', line=5, column=
10
)
Token(typ='IF', value='IF', line=2, column=
4
)
Token(typ='ID', value='quantity', line=2, column=
7
)
Token(typ='THEN', value='THEN', line=2, column=1
6
)
Token(typ='ID', value='total', line=3, column=
8
)
Token(typ='ASSIGN', value=':=', line=3, column=1
4
)
Token(typ='ID', value='total', line=3, column=1
7
)
Token(typ='OP', value='+', line=3, column=2
3
)
Token(typ='ID', value='price', line=3, column=2
5
)
Token(typ='OP', value='*', line=3, column=3
1
)
Token(typ='ID', value='quantity', line=3, column=3
3
)
Token(typ='END', value=';', line=3, column=4
1
)
Token(typ='ID', value='tax', line=4, column=
8
)
Token(typ='ASSIGN', value=':=', line=4, column=1
2
)
Token(typ='ID', value='price', line=4, column=1
5
)
Token(typ='OP', value='*', line=4, column=2
1
)
Token(typ='NUMBER', value='0.05', line=4, column=2
3
)
Token(typ='END', value=';', line=4, column=2
7
)
Token(typ='ENDIF', value='ENDIF', line=5, column=
4
)
Token(typ='END', value=';', line=5, column=
9
)
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