Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
c3361b9a
Kaydet (Commit)
c3361b9a
authored
Agu 04, 2014
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Sade Fark
merge
üst
bbeac6eb
c566431b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
37 deletions
+38
-37
re.rst
Doc/library/re.rst
+38
-37
No files found.
Doc/library/re.rst
Dosyayı görüntüle @
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